From 276567eb13f8fcbea73aa6d0b40082983301ac80 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 3 Sep 2017 13:56:11 +0200 Subject: [PATCH] fusefrontend: use DirIVCache in OpenDir() Previously, OpenDir() did not use the cache at all, missing an opportunity to speed up repeated directory reads. --- internal/fusefrontend/fs_dir.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/internal/fusefrontend/fs_dir.go b/internal/fusefrontend/fs_dir.go index 44a4ad2..211eeea 100644 --- a/internal/fusefrontend/fs_dir.go +++ b/internal/fusefrontend/fs_dir.go @@ -260,18 +260,24 @@ func (fs *FS) OpenDir(dirName string, context *fuse.Context) ([]fuse.DirEntry, f // Get DirIV (stays nil if PlaintextNames is used) var cachedIV []byte if !fs.args.PlaintextNames { - // Read the DirIV once and use it for all later name decryptions - cachedIV, err = nametransform.ReadDirIV(cDirAbsPath) - if err != nil { - // This can happen during normal operation when the directory has - // been deleted concurrently. But it can also mean that the - // gocryptfs.diriv is missing due to an error, so log the event - // at "info" level. - tlog.Info.Printf("OpenDir: %v", err) - return nil, fuse.ToStatus(err) + cachedIV, _ = fs.nameTransform.DirIVCache.Lookup(dirName) + if cachedIV == nil { + // Read the DirIV from disk and store it in the cache + fs.dirIVLock.RLock() + cachedIV, err = nametransform.ReadDirIV(cDirAbsPath) + if err != nil { + fs.dirIVLock.RUnlock() + // This can happen during normal operation when the directory has + // been deleted concurrently. But it can also mean that the + // gocryptfs.diriv is missing due to an error, so log the event + // at "info" level. + tlog.Info.Printf("OpenDir: %v", err) + return nil, fuse.ToStatus(err) + } + fs.nameTransform.DirIVCache.Store(dirName, cachedIV, cDirName) + fs.dirIVLock.RUnlock() } } - // Decrypted directory entries var plain []fuse.DirEntry var errorCount int