@cryptotaxi247 / kubo / commits / b66030514

extract initCheckDir to dir.Writable

Brian Tiger Chow committed Jan 14, 2015 at 08:10 UTC b6603051428df8e1b886005608c57335495a7011
2 files changed +27 -18
repo/fsrepo/dir/dir.go new
+24
@@ -0,0 +1,24 @@
1 +package dir
2 +
3 +// TODO move somewhere generic
4 +
5 +import (
6 + "errors"
7 + "os"
8 + "path/filepath"
9 +)
10 +
11 +// Writable ensures the directory exists and is writable
12 +func Writable(path string) error {
13 + // Construct the path if missing
14 + if err := os.MkdirAll(path, os.ModePerm); err != nil {
15 + return err
16 + }
17 + // Check the directory is writeable
18 + if f, err := os.Create(filepath.Join(path, "._check_writeable")); err == nil {
19 + os.Remove(f.Name())
20 + } else {
21 + return errors.New("'" + path + "' is not writeable")
22 + }
23 + return nil
24 +}
repo/fsrepo/fsrepo.go
+3 -18
@@ -6,12 +6,12 @@ import (
6 "io"
7 "os"
8 "path"
9 - "path/filepath"
9 "sync"
10
11 repo "github.com/jbenet/go-ipfs/repo"
12 config "github.com/jbenet/go-ipfs/repo/config"
13 component "github.com/jbenet/go-ipfs/repo/fsrepo/component"
14 + dir "github.com/jbenet/go-ipfs/repo/fsrepo/dir"
15 lockfile "github.com/jbenet/go-ipfs/repo/fsrepo/lock"
16 opener "github.com/jbenet/go-ipfs/repo/fsrepo/opener"
17 serialize "github.com/jbenet/go-ipfs/repo/fsrepo/serialize"
@@ -148,7 +148,7 @@ func (r *FSRepo) Open() error {
148 // check repo path, then check all constituent parts.
149 // TODO acquire repo lock
150 // TODO if err := initCheckDir(logpath); err != nil { // }
151 - if err := initCheckDir(r.path); err != nil {
151 + if err := dir.Writable(r.path); err != nil {
152 return err
153 }
154
@@ -162,7 +162,7 @@ func (r *FSRepo) Open() error {
162 if err != nil {
163 return debugerror.Wrap(err)
164 }
165 - if err := initCheckDir(logpath); err != nil {
165 + if err := dir.Writable(logpath); err != nil {
166 return debugerror.Errorf("logs: %s", err)
167 }
168
@@ -264,21 +264,6 @@ func isInitializedUnsynced(path string) bool {
264 return true
265 }
266
267 -// initCheckDir ensures the directory exists and is writable
268 -func initCheckDir(path string) error {
269 - // Construct the path if missing
270 - if err := os.MkdirAll(path, os.ModePerm); err != nil {
271 - return err
272 - }
273 - // Check the directory is writeable
274 - if f, err := os.Create(filepath.Join(path, "._check_writeable")); err == nil {
275 - os.Remove(f.Name())
276 - } else {
277 - return debugerror.New("'" + path + "' is not writeable")
278 - }
279 - return nil
280 -}
281 -
267 // transitionToOpened manages the state transition to |opened|. Caller must hold
268 // the package mutex.
269 func (r *FSRepo) transitionToOpened() error {