fileutil.go (etcd-3.5.5) | : | fileutil.go (etcd-3.5.6) | ||
---|---|---|---|---|
skipping to change at line 47 | skipping to change at line 47 | |||
return err | return err | |||
} | } | |||
if err := ioutil.WriteFile(f, []byte(""), PrivateFileMode); err != nil { | if err := ioutil.WriteFile(f, []byte(""), PrivateFileMode); err != nil { | |||
return err | return err | |||
} | } | |||
return os.Remove(f) | return os.Remove(f) | |||
} | } | |||
// TouchDirAll is similar to os.MkdirAll. It creates directories with 0700 permi ssion if any directory | // TouchDirAll is similar to os.MkdirAll. It creates directories with 0700 permi ssion if any directory | |||
// does not exists. TouchDirAll also ensures the given directory is writable. | // does not exists. TouchDirAll also ensures the given directory is writable. | |||
func TouchDirAll(dir string) error { | func TouchDirAll(lg *zap.Logger, dir string) error { | |||
// If path is already a directory, MkdirAll does nothing and returns nil, so, | // If path is already a directory, MkdirAll does nothing and returns nil, so, | |||
// first check if dir exist with an expected permission mode. | // first check if dir exist with an expected permission mode. | |||
if Exist(dir) { | if Exist(dir) { | |||
err := CheckDirPermission(dir, PrivateDirMode) | err := CheckDirPermission(dir, PrivateDirMode) | |||
if err != nil { | if err != nil { | |||
lg, _ := zap.NewProduction() | ||||
if lg == nil { | ||||
lg = zap.NewExample() | ||||
} | ||||
lg.Warn("check file permission", zap.Error(err)) | lg.Warn("check file permission", zap.Error(err)) | |||
} | } | |||
} else { | } else { | |||
err := os.MkdirAll(dir, PrivateDirMode) | err := os.MkdirAll(dir, PrivateDirMode) | |||
if err != nil { | if err != nil { | |||
// if mkdirAll("a/text") and "text" is not | // if mkdirAll("a/text") and "text" is not | |||
// a directory, this will return syscall.ENOTDIR | // a directory, this will return syscall.ENOTDIR | |||
return err | return err | |||
} | } | |||
} | } | |||
return IsDirWriteable(dir) | return IsDirWriteable(dir) | |||
} | } | |||
// CreateDirAll is similar to TouchDirAll but returns error | // CreateDirAll is similar to TouchDirAll but returns error | |||
// if the deepest directory was not empty. | // if the deepest directory was not empty. | |||
func CreateDirAll(dir string) error { | func CreateDirAll(lg *zap.Logger, dir string) error { | |||
err := TouchDirAll(dir) | err := TouchDirAll(lg, dir) | |||
if err == nil { | if err == nil { | |||
var ns []string | var ns []string | |||
ns, err = ReadDir(dir) | ns, err = ReadDir(dir) | |||
if err != nil { | if err != nil { | |||
return err | return err | |||
} | } | |||
if len(ns) != 0 { | if len(ns) != 0 { | |||
err = fmt.Errorf("expected %q to be empty, got %q", dir, ns) | err = fmt.Errorf("expected %q to be empty, got %q", dir, ns) | |||
} | } | |||
} | } | |||
End of changes. 3 change blocks. | ||||
7 lines changed or deleted | 3 lines changed or added |