dirivcache: add better function comments + a sanity check on Store()

The comments were unclear on whether relative or absolute paths
have to be passed.
This commit is contained in:
Jakob Unterwurzacher 2017-09-03 13:53:50 +02:00
parent 8f92dd15e1
commit 7da0e97c8b
1 changed files with 17 additions and 3 deletions

View File

@ -1,6 +1,8 @@
package dirivcache package dirivcache
import ( import (
"log"
"strings"
"sync" "sync"
"time" "time"
) )
@ -38,8 +40,10 @@ type DirIVCache struct {
sync.RWMutex sync.RWMutex
} }
// Lookup - fetch entry for "dir" from the cache // Lookup - fetch entry for "dir" (relative plaintext path) from the cache.
func (c *DirIVCache) Lookup(dir string) ([]byte, string) { // 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() c.RLock()
defer c.RUnlock() defer c.RUnlock()
if dir == "" { if dir == "" {
@ -56,13 +60,23 @@ func (c *DirIVCache) Lookup(dir string) ([]byte, string) {
return v.iv, v.cDir 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) { func (c *DirIVCache) Store(dir string, iv []byte, cDir string) {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
if dir == "" { if dir == "" {
c.rootDirIV = iv 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 { if c.data == nil {
c.data = make(map[string]cacheEntry, maxEntries) c.data = make(map[string]cacheEntry, maxEntries)
// Set expiry time one second into the future // Set expiry time one second into the future