aboutsummaryrefslogtreecommitdiff
path: root/internal/nametransform/dirivcache/dirivcache.go
blob: 890ebac718422d2cdcf2e2e42f3acee9a46c6c06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package dirivcache

import (
	"sync"
	"time"
)

// Single-entry DirIV cache. Stores the directory IV and the encrypted
// path.
type DirIVCache struct {
	// Directory the DirIV belongs to
	dir string
	// 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

	// The DirIV
	iv []byte
	// Ecrypted version of "dir"
	cDir string

	// Invalidated?
	cleared bool
	sync.RWMutex
}

// lookup - fetch entry for "dir" from the cache
func (c *DirIVCache) Lookup(dir string) ([]byte, string) {
	c.RLock()
	defer c.RUnlock()
	if c.cleared || c.dir != dir {
		return nil, ""
	}
	if time.Since(c.expiry) > 0 {
		c.cleared = true
		return nil, ""
	}
	return c.iv, c.cDir
}

// store - write entry for "dir" into the cache
func (c *DirIVCache) Store(dir string, iv []byte, cDir string) {
	c.Lock()
	defer c.Unlock()
	c.cleared = false
	c.iv = iv
	c.dir = dir
	c.cDir = cDir
	// Set expiry time one second into the future
	c.expiry = time.Now().Add(1 * time.Second)
}

// Clear ... clear the cache.
// Exported because it is called from fusefrontend when directories are
// renamed or deleted.
func (c *DirIVCache) Clear() {
	c.Lock()
	defer c.Unlock()
	c.cleared = true
}