summaryrefslogtreecommitdiff
path: root/internal/nametransform/dirivcache
diff options
context:
space:
mode:
authorJakob Unterwurzacher2017-09-03 13:53:50 +0200
committerJakob Unterwurzacher2017-09-03 13:53:50 +0200
commit7da0e97c8bffef7a688525f5bb6256c88743b4e2 (patch)
tree6d2117ba09cee3117ce03f0224141daa5059500f /internal/nametransform/dirivcache
parent8f92dd15e13552c3968838e9525118fbcef5fb5f (diff)
dirivcache: add better function comments + a sanity check on Store()
The comments were unclear on whether relative or absolute paths have to be passed.
Diffstat (limited to 'internal/nametransform/dirivcache')
-rw-r--r--internal/nametransform/dirivcache/dirivcache.go20
1 files changed, 17 insertions, 3 deletions
diff --git a/internal/nametransform/dirivcache/dirivcache.go b/internal/nametransform/dirivcache/dirivcache.go
index 2708d89..c0eca6d 100644
--- a/internal/nametransform/dirivcache/dirivcache.go
+++ b/internal/nametransform/dirivcache/dirivcache.go
@@ -1,6 +1,8 @@
package dirivcache
import (
+ "log"
+ "strings"
"sync"
"time"
)
@@ -38,8 +40,10 @@ type DirIVCache struct {
sync.RWMutex
}
-// Lookup - fetch entry for "dir" from the cache
-func (c *DirIVCache) Lookup(dir string) ([]byte, string) {
+// Lookup - fetch entry for "dir" (relative plaintext path) from the cache.
+// Returns the directory IV and the relative encrypted path, or (nil, "")
+// if the entry was not found.
+func (c *DirIVCache) Lookup(dir string) (iv []byte, cDir string) {
c.RLock()
defer c.RUnlock()
if dir == "" {
@@ -56,13 +60,23 @@ func (c *DirIVCache) Lookup(dir string) ([]byte, string) {
return v.iv, v.cDir
}
-// Store - write entry for "dir" into the cache
+// Store - write an entry for directory "dir" into the cache.
+// Arguments:
+// dir ... relative plaintext path
+// iv .... directory IV
+// cDir .. relative ciphertext path
func (c *DirIVCache) Store(dir string, iv []byte, cDir string) {
c.Lock()
defer c.Unlock()
if dir == "" {
c.rootDirIV = iv
}
+ // Sanity check: plaintext and chiphertext paths must have the same number
+ // of segments
+ if strings.Count(dir, "/") != strings.Count(cDir, "/") {
+ log.Panicf("inconsistent number of path segments: dir=%q cDir=%q", dir, cDir)
+ }
+ // Clear() may have cleared c.data: re-initialize
if c.data == nil {
c.data = make(map[string]cacheEntry, maxEntries)
// Set expiry time one second into the future