fusefrontend: refactor dirIVCache

Simplify the code a bit.
This commit is contained in:
Jakob Unterwurzacher 2016-11-01 10:34:41 +01:00
parent fd88dbd687
commit a9c7565b80
2 changed files with 28 additions and 23 deletions

View File

@ -92,8 +92,8 @@ func (be *NameTransform) EncryptPathDirIV(plainPath string, rootDir string) (cip
} }
// Check if the DirIV is cached // Check if the DirIV is cached
parentDir := filepath.Dir(plainPath) parentDir := filepath.Dir(plainPath)
found, iv, cParentDir := be.DirIVCache.lookup(parentDir) iv, cParentDir := be.DirIVCache.lookup(parentDir)
if found { if iv != nil {
cBaseName := be.EncryptName(baseName, iv) cBaseName := be.EncryptName(baseName, iv)
if be.longNames && len(cBaseName) > syscall.NAME_MAX { if be.longNames && len(cBaseName) > syscall.NAME_MAX {
cBaseName = HashLongName(cBaseName) cBaseName = HashLongName(cBaseName)

View File

@ -2,42 +2,47 @@ package nametransform
import "sync" import "sync"
// A simple one-entry DirIV cache // Single-entry DirIV cache. Stores the directory IV and the encrypted
// path.
type dirIVCache struct { type dirIVCache struct {
// Invalidated?
cleared bool
// The DirIV
iv []byte
// Directory the DirIV belongs to // Directory the DirIV belongs to
dir string dir string
// The DirIV
iv []byte
// Ecrypted version of "dir" // Ecrypted version of "dir"
translatedDir string cDir string
// Synchronisation
lock sync.RWMutex // Invalidated?
cleared bool
sync.RWMutex
} }
// lookup - fetch entry for "dir" from the cache // lookup - fetch entry for "dir" from the cache
func (c *dirIVCache) lookup(dir string) (bool, []byte, string) { func (c *dirIVCache) lookup(dir string) ([]byte, string) {
c.lock.RLock() c.RLock()
defer c.lock.RUnlock() defer c.RUnlock()
if !c.cleared && c.dir == dir { if c.cleared || c.dir != dir {
return true, c.iv, c.translatedDir return nil, ""
} }
return false, nil, "" return c.iv, c.cDir
} }
// store - write entry for "dir" into the caches // store - write entry for "dir" into the cache
func (c *dirIVCache) store(dir string, iv []byte, translatedDir string) { func (c *dirIVCache) store(dir string, iv []byte, cDir string) {
c.lock.Lock() c.Lock()
defer c.lock.Unlock() defer c.Unlock()
c.cleared = false c.cleared = false
c.iv = iv c.iv = iv
c.dir = dir c.dir = dir
c.translatedDir = translatedDir c.cDir = cDir
} }
// Clear ... clear the cache.
// Exported because it is called from fusefrontend when directories are
// renamed or deleted.
func (c *dirIVCache) Clear() { func (c *dirIVCache) Clear() {
c.lock.Lock() c.Lock()
defer c.lock.Unlock() defer c.Unlock()
c.cleared = true c.cleared = true
} }