本文整理汇总了Golang中os.FileMode函数的典型用法代码示例。如果您正苦于以下问题:Golang FileMode函数的具体用法?Golang FileMode怎么用?Golang FileMode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FileMode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SetupDataDir
func (p linux) SetupDataDir() error {
dataDir := p.dirProvider.DataDir()
sysDir := filepath.Join(dataDir, "sys")
logDir := filepath.Join(sysDir, "log")
err := p.fs.MkdirAll(logDir, os.FileMode(0750))
if err != nil {
return bosherr.WrapError(err, "Making %s dir", logDir)
}
_, _, _, err = p.cmdRunner.RunCommand("chown", "root:vcap", sysDir)
if err != nil {
return bosherr.WrapError(err, "chown %s", sysDir)
}
_, _, _, err = p.cmdRunner.RunCommand("chown", "root:vcap", logDir)
if err != nil {
return bosherr.WrapError(err, "chown %s", logDir)
}
runDir := filepath.Join(sysDir, "run")
err = p.fs.MkdirAll(runDir, os.FileMode(0750))
if err != nil {
return bosherr.WrapError(err, "Making %s dir", runDir)
}
_, _, _, err = p.cmdRunner.RunCommand("chown", "root:vcap", runDir)
if err != nil {
return bosherr.WrapError(err, "chown %s", runDir)
}
return nil
}
示例2: TestExecutor_MakeExecutable
func TestExecutor_MakeExecutable(t *testing.T) {
// Create a temp file
f, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer f.Close()
defer os.Remove(f.Name())
// Set its permissions to be non-executable
f.Chmod(os.FileMode(0610))
// Make a fake exececutor
ctx := testExecutorContext(t)
defer ctx.AllocDir.Destroy()
executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
err = executor.(*UniversalExecutor).makeExecutable(f.Name())
if err != nil {
t.Fatalf("makeExecutable() failed: %v", err)
}
// Check the permissions
stat, err := f.Stat()
if err != nil {
t.Fatalf("Stat() failed: %v", err)
}
act := stat.Mode().Perm()
exp := os.FileMode(0755)
if act != exp {
t.Fatalf("expected permissions %v; got %v", err)
}
}
示例3: sameConfig
// sameConfig reports whether src and dest config files are equal.
// Two config files are equal when they have the same file contents and
// Unix permissions. The owner, group, and mode must match.
// It return false in other cases.
func sameConfig(src, dest string) (bool, error) {
if !isFileExist(dest) {
return false, nil
}
d, err := fileStat(dest)
if err != nil {
return false, err
}
s, err := fileStat(src)
if err != nil {
return false, err
}
if d.Uid != s.Uid {
log.Info(fmt.Sprintf("%s has UID %d should be %d", dest, d.Uid, s.Uid))
}
if d.Gid != s.Gid {
log.Info(fmt.Sprintf("%s has GID %d should be %d", dest, d.Gid, s.Gid))
}
if d.Mode != s.Mode {
log.Info(fmt.Sprintf("%s has mode %s should be %s", dest, os.FileMode(d.Mode), os.FileMode(s.Mode)))
}
if d.Md5 != s.Md5 {
log.Info(fmt.Sprintf("%s has md5sum %s should be %s", dest, d.Md5, s.Md5))
}
if d.Uid != s.Uid || d.Gid != s.Gid || d.Mode != s.Mode || d.Md5 != s.Md5 {
return false, nil
}
return true, nil
}
示例4: createTestFiles
func (r *RestoreSuite) createTestFiles(c *gc.C) {
tarDirE := path.Join(r.cwd, "TarDirectoryEmpty")
err := os.Mkdir(tarDirE, os.FileMode(0755))
c.Check(err, jc.ErrorIsNil)
tarDirP := path.Join(r.cwd, "TarDirectoryPopulated")
err = os.Mkdir(tarDirP, os.FileMode(0755))
c.Check(err, jc.ErrorIsNil)
tarSubFile1 := path.Join(tarDirP, "TarSubFile1")
tarSubFile1Handle, err := os.Create(tarSubFile1)
c.Check(err, jc.ErrorIsNil)
tarSubFile1Handle.WriteString("TarSubFile1")
tarSubFile1Handle.Close()
tarSubDir := path.Join(tarDirP, "TarDirectoryPopulatedSubDirectory")
err = os.Mkdir(tarSubDir, os.FileMode(0755))
c.Check(err, jc.ErrorIsNil)
tarFile1 := path.Join(r.cwd, "TarFile1")
tarFile1Handle, err := os.Create(tarFile1)
c.Check(err, jc.ErrorIsNil)
tarFile1Handle.WriteString("TarFile1")
tarFile1Handle.Close()
tarFile2 := path.Join(r.cwd, "TarFile2")
tarFile2Handle, err := os.Create(tarFile2)
c.Check(err, jc.ErrorIsNil)
tarFile2Handle.WriteString("TarFile2")
tarFile2Handle.Close()
r.testFiles = []string{tarDirE, tarDirP, tarFile1, tarFile2}
}
示例5: createDevices
func createDevices() error {
err := os.MkdirAll(pathPrefix, 644)
if err != nil {
log.Warnf("Failed to ensure presence of tether device directory: %s", err)
}
// create serial devices
for i := 0; i < 3; i++ {
path := fmt.Sprintf("%s/ttyS%d", pathPrefix, i)
minor := 64 + i
err = syscall.Mknod(path, syscall.S_IFCHR|uint32(os.FileMode(0660)), tether.Mkdev(4, minor))
if err != nil {
return fmt.Errorf("failed to create %s for com%d: %s", path, i+1, err)
}
}
// make an access to urandom
path := fmt.Sprintf("%s/urandom", pathPrefix)
err = syscall.Mknod(path, syscall.S_IFCHR|uint32(os.FileMode(0444)), tether.Mkdev(1, 9))
if err != nil {
return fmt.Errorf("failed to create urandom access %s: %s", path, err)
}
return nil
}
示例6: Write
// Write the pidfile based on the flag. It is an error if the pidfile hasn't
// been configured.
func Write() error {
if *pidfile == "" {
return errNotConfigured
}
if err := os.MkdirAll(filepath.Dir(*pidfile), os.FileMode(0755)); err != nil {
return err
}
file, err := atomicfile.New(*pidfile, os.FileMode(0644))
if err != nil {
return fmt.Errorf("error opening pidfile %s: %s", *pidfile, err)
}
defer file.Close() // in case we fail before the explicit close
_, err = fmt.Fprintf(file, "%d", os.Getpid())
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
return nil
}
示例7: Close
// Close writes symlinks and dirs of the container, then closes
// the zip writer.
func (zwp *ZipWriterPool) Close() error {
for _, symlink := range zwp.container.Symlinks {
fh := zip.FileHeader{
Name: symlink.Path,
}
fh.SetMode(os.FileMode(symlink.Mode))
entryWriter, eErr := zwp.zw.CreateHeader(&fh)
if eErr != nil {
return errors.Wrap(eErr, 1)
}
entryWriter.Write([]byte(symlink.Dest))
}
for _, dir := range zwp.container.Dirs {
fh := zip.FileHeader{
Name: dir.Path + "/",
}
fh.SetMode(os.FileMode(dir.Mode))
fh.SetModTime(time.Now())
_, hErr := zwp.zw.CreateHeader(&fh)
if hErr != nil {
return errors.Wrap(hErr, 1)
}
}
err := zwp.zw.Close()
if err != nil {
return errors.Wrap(err, 1)
}
return nil
}
示例8: Defaults
func (t *FlagsTest) Defaults() {
f := parseArgs([]string{})
// File system
ExpectNe(nil, f.MountOptions)
ExpectEq(0, len(f.MountOptions), "Options: %v", f.MountOptions)
ExpectEq(os.FileMode(0755), f.DirMode)
ExpectEq(os.FileMode(0644), f.FileMode)
ExpectEq(-1, f.Uid)
ExpectEq(-1, f.Gid)
ExpectFalse(f.ImplicitDirs)
// GCS
ExpectEq("", f.KeyFile)
ExpectEq(-1, f.EgressBandwidthLimitBytesPerSecond)
ExpectEq(5, f.OpRateLimitHz)
// Tuning
ExpectEq(time.Minute, f.StatCacheTTL)
ExpectEq(time.Minute, f.TypeCacheTTL)
ExpectEq(1<<24, f.GCSChunkSize)
ExpectEq("", f.TempDir)
ExpectEq(1<<31, f.TempDirLimit)
// Debugging
ExpectFalse(f.DebugCPUProfile)
ExpectFalse(f.DebugFuse)
ExpectFalse(f.DebugGCS)
ExpectFalse(f.DebugHTTP)
ExpectFalse(f.DebugInvariants)
ExpectFalse(f.DebugMemProfile)
}
示例9: TestSetPermissions
func TestSetPermissions(t *testing.T) {
testPerm := 0755
resp, err := remote.Tell("setPermissions", struct {
Path string
Mode os.FileMode
Recursive bool
}{
Path: testfile1,
Mode: os.FileMode(testPerm),
})
if err != nil {
t.Fatal(err)
}
if !resp.MustBool() {
t.Fatal("setPermissions should return true")
}
f, err := os.Open(testfile1)
if err != nil {
t.Fatal(err)
}
fi, err := f.Stat()
if err != nil {
t.Fatal(err)
}
if fi.Mode() != os.FileMode(testPerm) {
t.Errorf("got %v expecting %v", fi.Mode(), testPerm)
}
}
示例10: ProcessWriteFile
func ProcessWriteFile(base string, wf *WriteFile) error {
fullPath := path.Join(base, wf.Path)
if err := os.MkdirAll(path.Dir(fullPath), os.FileMode(0744)); err != nil {
return err
}
// Parse string representation of file mode as octal
perm, err := strconv.ParseInt(wf.Permissions, 8, 32)
if err != nil {
return errors.New("Unable to parse file permissions as octal integer")
}
if err := ioutil.WriteFile(fullPath, []byte(wf.Content), os.FileMode(perm)); err != nil {
return err
}
if wf.Owner != "" {
// We shell out since we don't have a way to look up unix groups natively
cmd := exec.Command("chown", wf.Owner, fullPath)
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
示例11: populateFlags
// Add the flags accepted by run to the supplied flag set, returning the
// variables into which the flags will parse.
func populateFlags(c *cli.Context) (flags *flagStorage) {
flags = &flagStorage{
// File system
MountOptions: make(map[string]string),
DirMode: os.FileMode(c.Int("dir-mode")),
FileMode: os.FileMode(c.Int("file-mode")),
Uid: int64(c.Int("uid")),
Gid: int64(c.Int("gid")),
// GCS,
KeyFile: c.String("key-file"),
EgressBandwidthLimitBytesPerSecond: c.Float64("limit-bytes-per-sec"),
OpRateLimitHz: c.Float64("limit-ops-per-sec"),
// Tuning,
StatCacheTTL: c.Duration("stat-cache-ttl"),
TypeCacheTTL: c.Duration("type-cache-ttl"),
TempDir: c.String("temp-dir"),
ImplicitDirs: c.Bool("implicit-dirs"),
// Debugging,
DebugFuse: c.Bool("debug_fuse"),
DebugGCS: c.Bool("debug_gcs"),
DebugHTTP: c.Bool("debug_http"),
DebugInvariants: c.Bool("debug_invariants"),
}
// Handle the repeated "-o" flag.
for _, o := range c.StringSlice("o") {
mountpkg.ParseOptions(flags.MountOptions, o)
}
return
}
示例12: TestUbuntuSetupSsh
func TestUbuntuSetupSsh(t *testing.T) {
fakeFs, fakeCmdRunner, fakePartitioner, fakeFormatter := getUbuntuDependencies()
fakeFs.HomeDirHomeDir = "/some/home/dir"
ubuntu := newUbuntuPlatform(fakeFs, fakeCmdRunner, fakePartitioner, fakeFormatter)
ubuntu.SetupSsh("some public key", "vcap")
sshDirPath := "/some/home/dir/.ssh"
sshDirStat := fakeFs.GetFileTestStat(sshDirPath)
assert.Equal(t, fakeFs.HomeDirUsername, "vcap")
assert.NotNil(t, sshDirStat)
assert.Equal(t, sshDirStat.CreatedWith, "MkdirAll")
assert.Equal(t, sshDirStat.FileMode, os.FileMode(0700))
assert.Equal(t, sshDirStat.Username, "vcap")
authKeysStat := fakeFs.GetFileTestStat(filepath.Join(sshDirPath, "authorized_keys"))
assert.NotNil(t, authKeysStat)
assert.Equal(t, authKeysStat.CreatedWith, "WriteToFile")
assert.Equal(t, authKeysStat.FileMode, os.FileMode(0600))
assert.Equal(t, authKeysStat.Username, "vcap")
assert.Equal(t, authKeysStat.Content, "some public key")
}
示例13: createDevices
func createDevices(rootDir string, uid, gid int) error {
nullDir := fp.Join(rootDir, os.DevNull)
if err := osutil.Mknod(nullDir, syscall.S_IFCHR|uint32(os.FileMode(0666)), 1*256+3); err != nil {
return err
}
if err := os.Lchown(nullDir, uid, gid); err != nil {
return errwrap.Wrapff(err, "Failed to lchown %s: {{err}}", nullDir)
}
zeroDir := fp.Join(rootDir, "/dev/zero")
if err := osutil.Mknod(zeroDir, syscall.S_IFCHR|uint32(os.FileMode(0666)), 1*256+3); err != nil {
return err
}
if err := os.Lchown(zeroDir, uid, gid); err != nil {
return errwrap.Wrapff(err, "Failed to lchown %s:", zeroDir)
}
for _, f := range []string{"/dev/random", "/dev/urandom"} {
randomDir := fp.Join(rootDir, f)
if err := osutil.Mknod(randomDir, syscall.S_IFCHR|uint32(os.FileMode(0666)), 1*256+9); err != nil {
return err
}
if err := os.Lchown(randomDir, uid, gid); err != nil {
return errwrap.Wrapff(err, "Failed to lchown %s: {{err}}", randomDir)
}
}
return nil
}
示例14: WritePidfile
func WritePidfile(pidfile string, pid int) error {
if pidfile == "" {
return errors.New("pidfile not configured")
}
if err := os.MkdirAll(filepath.Dir(pidfile), os.FileMode(0755)); err != nil {
return err
}
file, err := atomicfile.New(pidfile, os.FileMode(0644))
if err != nil {
return fmt.Errorf("error opening pidfile %s: %s", pidfile, err)
}
defer file.Close()
_, err = fmt.Fprintf(file, "%d", pid)
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
return nil
}
示例15: TestWriterReset
func TestWriterReset(t *testing.T) {
b := new(bytes.Buffer)
w := NewWriter(b)
for i := 0; i < 2; i++ {
debian := &fileInfo{
name: "debian-binary",
mtime: time.Unix(1385068169, 0),
mode: os.FileMode(0100644) & os.ModePerm,
size: 4,
}
if _, err := w.WriteFile(debian, strings.NewReader("2.0\n")); err != nil {
t.Errorf("%d: %q", i, err)
return
}
control := &fileInfo{
name: "control.tar.gz",
mtime: time.Unix(1385068169, 0),
mode: os.FileMode(0100644) & os.ModePerm,
size: 0,
}
if _, err := w.WriteFile(control, strings.NewReader("")); err != nil {
t.Errorf("%d: %q", i, err)
return
}
if archive := b.String(); archive != testCommon {
t.Errorf("%d: got\n%q\nwant\n%q", i, archive, testCommon)
}
b.Reset()
w.Reset(b)
}
}