2016-02-06 20:27:28 +01:00
|
|
|
package nametransform
|
|
|
|
|
2016-11-01 10:57:39 +01:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
2016-02-06 20:27:28 +01:00
|
|
|
|
2016-11-01 10:34:41 +01:00
|
|
|
// Single-entry DirIV cache. Stores the directory IV and the encrypted
|
|
|
|
// path.
|
2016-02-06 20:27:28 +01:00
|
|
|
type dirIVCache struct {
|
|
|
|
// Directory the DirIV belongs to
|
|
|
|
dir string
|
2016-11-01 10:57:39 +01:00
|
|
|
// Time the entry expires.
|
|
|
|
// The cached entry my become out-of-date if the ciphertext directory is
|
|
|
|
// modifed behind the back of gocryptfs. Having an expiry time limits the
|
|
|
|
// inconstency to one second, like attr_timeout does for the kernel
|
|
|
|
// getattr cache.
|
|
|
|
expiry time.Time
|
2016-11-01 10:34:41 +01:00
|
|
|
|
|
|
|
// The DirIV
|
|
|
|
iv []byte
|
2016-02-06 20:27:28 +01:00
|
|
|
// Ecrypted version of "dir"
|
2016-11-01 10:34:41 +01:00
|
|
|
cDir string
|
|
|
|
|
|
|
|
// Invalidated?
|
|
|
|
cleared bool
|
|
|
|
sync.RWMutex
|
2016-02-06 20:27:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// lookup - fetch entry for "dir" from the cache
|
2016-11-01 10:34:41 +01:00
|
|
|
func (c *dirIVCache) lookup(dir string) ([]byte, string) {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
if c.cleared || c.dir != dir {
|
|
|
|
return nil, ""
|
2016-02-06 20:27:28 +01:00
|
|
|
}
|
2016-11-01 10:57:39 +01:00
|
|
|
if time.Since(c.expiry) > 0 {
|
|
|
|
c.cleared = true
|
|
|
|
return nil, ""
|
|
|
|
}
|
2016-11-01 10:34:41 +01:00
|
|
|
return c.iv, c.cDir
|
2016-02-06 20:27:28 +01:00
|
|
|
}
|
|
|
|
|
2016-11-01 10:34:41 +01:00
|
|
|
// store - write entry for "dir" into the cache
|
|
|
|
func (c *dirIVCache) store(dir string, iv []byte, cDir string) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2016-02-06 20:27:28 +01:00
|
|
|
c.cleared = false
|
|
|
|
c.iv = iv
|
|
|
|
c.dir = dir
|
2016-11-01 10:34:41 +01:00
|
|
|
c.cDir = cDir
|
2016-11-01 10:57:39 +01:00
|
|
|
// Set expiry time one second into the future
|
|
|
|
c.expiry = time.Now().Add(1 * time.Second)
|
2016-02-06 20:27:28 +01:00
|
|
|
}
|
|
|
|
|
2016-11-01 10:34:41 +01:00
|
|
|
// Clear ... clear the cache.
|
|
|
|
// Exported because it is called from fusefrontend when directories are
|
|
|
|
// renamed or deleted.
|
2016-02-06 20:27:28 +01:00
|
|
|
func (c *dirIVCache) Clear() {
|
2016-11-01 10:34:41 +01:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2016-02-06 20:27:28 +01:00
|
|
|
c.cleared = true
|
|
|
|
}
|