nametransform: move diriv cache into it's own package

Needs some space to grow.

renamed:    internal/nametransform/diriv_cache.go -> internal/nametransform/dirivcache/dirivcache.go
This commit is contained in:
Jakob Unterwurzacher 2017-08-06 21:59:15 +02:00
parent 32611ff97a
commit 5190cc09bb
3 changed files with 10 additions and 9 deletions

View File

@ -125,7 +125,7 @@ func (be *NameTransform) EncryptPathDirIV(plainPath string, rootDir string) (cip
// Check if the DirIV is cached. This catches the case of the user iterating // Check if the DirIV is cached. This catches the case of the user iterating
// over files in a directory pretty well. // over files in a directory pretty well.
parentDir := filepath.Dir(plainPath) parentDir := filepath.Dir(plainPath)
iv, cParentDir := be.DirIVCache.lookup(parentDir) iv, cParentDir := be.DirIVCache.Lookup(parentDir)
if iv != nil { if iv != nil {
cBaseName := be.encryptAndHashName(baseName, iv) cBaseName := be.encryptAndHashName(baseName, iv)
return filepath.Join(cParentDir, cBaseName), nil return filepath.Join(cParentDir, cBaseName), nil
@ -138,7 +138,7 @@ func (be *NameTransform) EncryptPathDirIV(plainPath string, rootDir string) (cip
// cached. Then we can skip a few items in the directory walk. // cached. Then we can skip a few items in the directory walk.
// This catches the case of walking directories recursively. // This catches the case of walking directories recursively.
parentDir2 := filepath.Dir(parentDir) parentDir2 := filepath.Dir(parentDir)
iv, cParentDir = be.DirIVCache.lookup(parentDir2) iv, cParentDir = be.DirIVCache.Lookup(parentDir2)
if iv != nil { if iv != nil {
parentDirBase := filepath.Base(parentDir) parentDirBase := filepath.Base(parentDir)
cBaseName := be.encryptAndHashName(parentDirBase, iv) cBaseName := be.encryptAndHashName(parentDirBase, iv)
@ -159,6 +159,6 @@ func (be *NameTransform) EncryptPathDirIV(plainPath string, rootDir string) (cip
} }
// Cache the final DirIV // Cache the final DirIV
cParentDir = filepath.Dir(cipherPath) cParentDir = filepath.Dir(cipherPath)
be.DirIVCache.store(parentDir, iv, cParentDir) be.DirIVCache.Store(parentDir, iv, cParentDir)
return cipherPath, nil return cipherPath, nil
} }

View File

@ -1,4 +1,4 @@
package nametransform package dirivcache
import ( import (
"sync" "sync"
@ -7,7 +7,7 @@ import (
// Single-entry DirIV cache. Stores the directory IV and the encrypted // Single-entry DirIV cache. Stores the directory IV and the encrypted
// path. // path.
type dirIVCache struct { type DirIVCache struct {
// Directory the DirIV belongs to // Directory the DirIV belongs to
dir string dir string
// Time the entry expires. // Time the entry expires.
@ -28,7 +28,7 @@ type dirIVCache struct {
} }
// lookup - fetch entry for "dir" from the cache // lookup - fetch entry for "dir" from the cache
func (c *dirIVCache) lookup(dir string) ([]byte, string) { func (c *DirIVCache) Lookup(dir string) ([]byte, string) {
c.RLock() c.RLock()
defer c.RUnlock() defer c.RUnlock()
if c.cleared || c.dir != dir { if c.cleared || c.dir != dir {
@ -42,7 +42,7 @@ func (c *dirIVCache) lookup(dir string) ([]byte, string) {
} }
// store - write entry for "dir" into the cache // store - write entry for "dir" into the cache
func (c *dirIVCache) store(dir string, iv []byte, cDir string) { func (c *DirIVCache) Store(dir string, iv []byte, cDir string) {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
c.cleared = false c.cleared = false
@ -56,7 +56,7 @@ func (c *dirIVCache) store(dir string, iv []byte, cDir string) {
// Clear ... clear the cache. // Clear ... clear the cache.
// Exported because it is called from fusefrontend when directories are // Exported because it is called from fusefrontend when directories are
// renamed or deleted. // renamed or deleted.
func (c *dirIVCache) Clear() { func (c *DirIVCache) Clear() {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
c.cleared = true c.cleared = true

View File

@ -9,6 +9,7 @@ import (
"github.com/rfjakob/eme" "github.com/rfjakob/eme"
"github.com/rfjakob/gocryptfs/internal/nametransform/dirivcache"
"github.com/rfjakob/gocryptfs/internal/tlog" "github.com/rfjakob/gocryptfs/internal/tlog"
) )
@ -16,7 +17,7 @@ import (
type NameTransform struct { type NameTransform struct {
emeCipher *eme.EMECipher emeCipher *eme.EMECipher
longNames bool longNames bool
DirIVCache dirIVCache DirIVCache dirivcache.DirIVCache
// B64 = either base64.URLEncoding or base64.RawURLEncoding, depeding // B64 = either base64.URLEncoding or base64.RawURLEncoding, depeding
// on the Raw64 feature flag // on the Raw64 feature flag
B64 *base64.Encoding B64 *base64.Encoding