Fixes normalization of inmemory file paths

A normalized path always begins with "/" and never has a trailing slash
unless it is the root directory.
This commit is contained in:
Brian Bland 2014-12-09 17:20:10 -08:00
parent cacf33ab62
commit 94052ea213
2 changed files with 14 additions and 20 deletions

View File

@ -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
}

View File

@ -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, "/")
}