nametransform: add Dir() function

Dir is like filepath.Dir but returns "" instead of ".".
This was already implemented in fusefrontend_reverse as saneDir().

We will need it in nametransform for the improved diriv caching.
This commit is contained in:
Jakob Unterwurzacher 2017-08-06 23:12:27 +02:00
parent 5190cc09bb
commit 75ec94a87a
4 changed files with 14 additions and 13 deletions

View File

@ -86,7 +86,7 @@ func (rfs *ReverseFS) newNameFile(relPath string) (nodefs.File, fuse.Status) {
dotName := filepath.Base(relPath) // gocryptfs.longname.XYZ.name dotName := filepath.Base(relPath) // gocryptfs.longname.XYZ.name
longname := dotName[:len(dotName)-len(nametransform.LongNameSuffix)] // gocryptfs.longname.XYZ longname := dotName[:len(dotName)-len(nametransform.LongNameSuffix)] // gocryptfs.longname.XYZ
// cipher directory // cipher directory
cDir := saneDir(relPath) cDir := nametransform.Dir(relPath)
// plain directory // plain directory
pDir, err := rfs.decryptPath(cDir) pDir, err := rfs.decryptPath(cDir)
if err != nil { if err != nil {

View File

@ -11,15 +11,6 @@ import (
"github.com/rfjakob/gocryptfs/internal/tlog" "github.com/rfjakob/gocryptfs/internal/tlog"
) )
// saneDir is like filepath.Dir but returns "" instead of "."
func saneDir(path string) string {
d := filepath.Dir(path)
if d == "." {
return ""
}
return d
}
// abs basically returns storage dir + "/" + relPath. // abs basically returns storage dir + "/" + relPath.
// It takes an error parameter so it can directly wrap decryptPath like this: // It takes an error parameter so it can directly wrap decryptPath like this:
// a, err := rfs.abs(rfs.decryptPath(relPath)) // a, err := rfs.abs(rfs.decryptPath(relPath))
@ -71,7 +62,7 @@ func (rfs *ReverseFS) decryptPath(relPath string) (string, error) {
return relPath, nil return relPath, nil
} }
// Check if the parent dir is in the cache // Check if the parent dir is in the cache
cDir := saneDir(relPath) cDir := nametransform.Dir(relPath)
dirIV, pDir := rPathCache.lookup(cDir) dirIV, pDir := rPathCache.lookup(cDir)
if dirIV != nil { if dirIV != nil {
cName := filepath.Base(relPath) cName := filepath.Base(relPath)
@ -95,6 +86,6 @@ func (rfs *ReverseFS) decryptPath(relPath string) (string, error) {
transformedParts = append(transformedParts, transformedPart) transformedParts = append(transformedParts, transformedPart)
} }
pRelPath := filepath.Join(transformedParts...) pRelPath := filepath.Join(transformedParts...)
rPathCache.store(cDir, dirIV, saneDir(pRelPath)) rPathCache.store(cDir, dirIV, nametransform.Dir(pRelPath))
return pRelPath, nil return pRelPath, nil
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs" "github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/rfjakob/gocryptfs/internal/nametransform"
"github.com/rfjakob/gocryptfs/internal/pathiv" "github.com/rfjakob/gocryptfs/internal/pathiv"
"github.com/rfjakob/gocryptfs/internal/tlog" "github.com/rfjakob/gocryptfs/internal/tlog"
) )
@ -31,7 +32,7 @@ const (
) )
func (rfs *ReverseFS) newDirIVFile(cRelPath string) (nodefs.File, fuse.Status) { func (rfs *ReverseFS) newDirIVFile(cRelPath string) (nodefs.File, fuse.Status) {
cDir := saneDir(cRelPath) cDir := nametransform.Dir(cRelPath)
absDir, err := rfs.abs(rfs.decryptPath(cDir)) absDir, err := rfs.abs(rfs.decryptPath(cDir))
if err != nil { if err != nil {
return nil, fuse.ToStatus(err) return nil, fuse.ToStatus(err)

View File

@ -162,3 +162,12 @@ func (be *NameTransform) EncryptPathDirIV(plainPath string, rootDir string) (cip
be.DirIVCache.Store(parentDir, iv, cParentDir) be.DirIVCache.Store(parentDir, iv, cParentDir)
return cipherPath, nil return cipherPath, nil
} }
// Dir is like filepath.Dir but returns "" instead of ".".
func Dir(path string) string {
d := filepath.Dir(path)
if d == "." {
return ""
}
return d
}