提交 d28c9cc7 作者: Brian Tiger Chow

refactor(init) extract initCheckDir function

License: MIT
Signed-off-by: 's avatarBrian Tiger Chow <brian@perfmode.com>

# TYPES
# feat
# fix
# docs
# style (formatting, missing semi colons, etc; no code change):
# refactor
# test (adding missing tests, refactoring tests; no production code change)
# chore (updating grunt tasks etc; no production code change)
上级 2b72d9f1
...@@ -131,16 +131,9 @@ func datastoreConfig(dspath string) (config.Datastore, error) { ...@@ -131,16 +131,9 @@ func datastoreConfig(dspath string) (config.Datastore, error) {
ds.Path = dspath ds.Path = dspath
ds.Type = "leveldb" ds.Type = "leveldb"
// Construct the data store if missing err := initCheckDir(dspath)
if err := os.MkdirAll(dspath, os.ModePerm); err != nil { if err != nil {
return ds, err return ds, errors.Errorf("datastore: %s", err)
}
// Check the directory is writeable
if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
os.Remove(f.Name())
} else {
return ds, errors.New("Datastore '" + dspath + "' is not writeable")
} }
return ds, nil return ds, nil
...@@ -224,3 +217,19 @@ func identityConfig(nbits int) (config.Identity, error) { ...@@ -224,3 +217,19 @@ func identityConfig(nbits int) (config.Identity, error) {
return ident, nil return ident, nil
} }
// initCheckDir ensures the directory exists and is writable
func initCheckDir(path string) error {
// Construct the path if missing
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return err
}
// Check the directory is writeable
if f, err := os.Create(filepath.Join(path, "._check_writeable")); err == nil {
os.Remove(f.Name())
} else {
return errors.New("'" + path + "' is not writeable")
}
return nil
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论