diff --git a/cmd/ipfs2/init.go b/cmd/ipfs2/init.go index b0b945716..84b229da0 100644 --- a/cmd/ipfs2/init.go +++ b/cmd/ipfs2/init.go @@ -131,16 +131,9 @@ func datastoreConfig(dspath string) (config.Datastore, error) { ds.Path = dspath ds.Type = "leveldb" - // Construct the data store if missing - if err := os.MkdirAll(dspath, os.ModePerm); err != nil { - return ds, 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") + err := initCheckDir(dspath) + if err != nil { + return ds, errors.Errorf("datastore: %s", err) } return ds, nil @@ -224,3 +217,19 @@ func identityConfig(nbits int) (config.Identity, error) { 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 +}