From 94052ea2130292f47a9f34131d7d5ef8d4ebb7d7 Mon Sep 17 00:00:00 2001 From: Brian Bland Date: Tue, 9 Dec 2014 17:20:10 -0800 Subject: [PATCH] Fixes normalization of inmemory file paths A normalized path always begins with "/" and never has a trailing slash unless it is the root directory. --- storagedriver/inmemory/driver.go | 20 ++++++-------------- storagedriver/inmemory/mfs.go | 14 ++++++++------ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/storagedriver/inmemory/driver.go b/storagedriver/inmemory/driver.go index 0b68e021..841ce56c 100644 --- a/storagedriver/inmemory/driver.go +++ b/storagedriver/inmemory/driver.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "io/ioutil" - "strings" "sync" "time" @@ -87,7 +86,7 @@ func (d *Driver) ReadStream(path string, offset int64) (io.ReadCloser, error) { return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset} } - path = d.normalize(path) + path = normalize(path) found := d.root.find(path) if found.path() != path { @@ -111,7 +110,7 @@ func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (nn in return 0, storagedriver.InvalidOffsetError{Path: path, Offset: offset} } - normalized := d.normalize(path) + normalized := normalize(path) f, err := d.root.mkfile(normalized) if err != nil { @@ -139,7 +138,7 @@ func (d *Driver) Stat(path string) (storagedriver.FileInfo, error) { d.mutex.RLock() defer d.mutex.RUnlock() - normalized := d.normalize(path) + normalized := normalize(path) found := d.root.find(path) if found.path() != normalized { @@ -162,7 +161,7 @@ func (d *Driver) Stat(path string) (storagedriver.FileInfo, error) { // List returns a list of the objects that are direct descendants of the given // path. func (d *Driver) List(path string) ([]string, error) { - normalized := d.normalize(path) + normalized := normalize(path) found := d.root.find(normalized) @@ -192,7 +191,7 @@ func (d *Driver) Move(sourcePath string, destPath string) error { d.mutex.Lock() defer d.mutex.Unlock() - normalizedSrc, normalizedDst := d.normalize(sourcePath), d.normalize(destPath) + normalizedSrc, normalizedDst := normalize(sourcePath), normalize(destPath) err := d.root.move(normalizedSrc, normalizedDst) switch err { @@ -208,7 +207,7 @@ func (d *Driver) Delete(path string) error { d.mutex.Lock() defer d.mutex.Unlock() - normalized := d.normalize(path) + normalized := normalize(path) err := d.root.delete(normalized) switch err { @@ -218,10 +217,3 @@ func (d *Driver) Delete(path string) error { return err } } - -func (d *Driver) normalize(p string) string { - if !strings.HasPrefix(p, "/") { - p = "/" + p // Ghetto path absolution. - } - return p -} diff --git a/storagedriver/inmemory/mfs.go b/storagedriver/inmemory/mfs.go index 9eeac0da..c5797238 100644 --- a/storagedriver/inmemory/mfs.go +++ b/storagedriver/inmemory/mfs.go @@ -11,7 +11,7 @@ import ( var ( errExists = fmt.Errorf("exists") - errNotExists = fmt.Errorf("exists") + errNotExists = fmt.Errorf("notexists") errIsNotDir = fmt.Errorf("notdir") errIsDir = fmt.Errorf("isdir") ) @@ -139,9 +139,7 @@ func (d *dir) mkfile(p string) (*file, error) { // mkdirs creates any missing directory entries in p and returns the result. func (d *dir) mkdirs(p string) (*dir, error) { - if p == "" { - p = "/" - } + p = normalize(p) n := d.find(p) @@ -210,7 +208,7 @@ func (d *dir) move(src, dst string) error { srcDirname, srcFilename := path.Split(src) sp := d.find(srcDirname) - if srcDirname != strings.TrimSuffix(sp.path(), "/")+"/" { + if normalize(srcDirname) != normalize(sp.path()) { return errNotExists } @@ -237,7 +235,7 @@ func (d *dir) delete(p string) error { dirname, filename := path.Split(p) parent := d.find(dirname) - if dirname != strings.TrimSuffix(parent.path(), "/")+"/" { + if normalize(dirname) != normalize(parent.path()) { return errNotExists } @@ -327,3 +325,7 @@ func (c *common) path() string { func (c *common) modtime() time.Time { return c.mod } + +func normalize(p string) string { + return "/" + strings.Trim(p, "/") +}