2016-09-22 23:28:11 +02:00
|
|
|
package fusefrontend_reverse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-09-24 22:40:00 +02:00
|
|
|
"sync"
|
2016-09-22 23:28:11 +02:00
|
|
|
"syscall"
|
2016-09-24 22:40:00 +02:00
|
|
|
"time"
|
2016-09-22 23:28:11 +02:00
|
|
|
|
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
|
|
|
"github.com/hanwen/go-fuse/fuse/nodefs"
|
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
shortNameMax = 176
|
|
|
|
)
|
|
|
|
|
2016-09-24 22:40:00 +02:00
|
|
|
var longnameParentCache map[string]string
|
|
|
|
var longnameCacheLock sync.Mutex
|
|
|
|
|
|
|
|
// Very simple cache cleaner: Nuke it every hour
|
|
|
|
func longnameCacheCleaner() {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Hour)
|
|
|
|
longnameCacheLock.Lock()
|
|
|
|
longnameParentCache = map[string]string{}
|
|
|
|
longnameCacheLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
longnameParentCache = map[string]string{}
|
|
|
|
go longnameCacheCleaner()
|
|
|
|
}
|
|
|
|
|
|
|
|
// findLongnameParent converts "gocryptfs.longname.XYZ" to the plaintext name
|
|
|
|
func (rfs *reverseFS) findLongnameParent(dir string, dirIV []byte, longname string) (plaintextName string, err error) {
|
|
|
|
longnameCacheLock.Lock()
|
|
|
|
hit := longnameParentCache[longname]
|
|
|
|
longnameCacheLock.Unlock()
|
|
|
|
if hit != "" {
|
|
|
|
return hit, nil
|
|
|
|
}
|
2016-09-22 23:28:11 +02:00
|
|
|
absDir := filepath.Join(rfs.args.Cipherdir, dir)
|
|
|
|
dirfd, err := os.Open(absDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
dirEntries, err := dirfd.Readdirnames(-1)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-09-24 22:40:00 +02:00
|
|
|
longnameCacheLock.Lock()
|
|
|
|
defer longnameCacheLock.Unlock()
|
|
|
|
for _, plaintextName = range dirEntries {
|
|
|
|
if len(plaintextName) <= shortNameMax {
|
2016-09-22 23:28:11 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-09-24 22:40:00 +02:00
|
|
|
cName := rfs.nameTransform.EncryptName(plaintextName, dirIV)
|
2016-09-22 23:28:11 +02:00
|
|
|
if len(cName) <= syscall.NAME_MAX {
|
|
|
|
panic("logic error or wrong shortNameMax constant?")
|
|
|
|
}
|
|
|
|
hName := nametransform.HashLongName(cName)
|
2016-09-24 22:40:00 +02:00
|
|
|
longnameParentCache[hName] = plaintextName
|
2016-09-22 23:28:11 +02:00
|
|
|
if longname == hName {
|
2016-09-24 22:40:00 +02:00
|
|
|
hit = plaintextName
|
2016-09-22 23:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-24 22:40:00 +02:00
|
|
|
if hit == "" {
|
|
|
|
return "", syscall.ENOENT
|
|
|
|
} else {
|
|
|
|
return hit, nil
|
|
|
|
}
|
2016-09-22 23:28:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rfs *reverseFS) newNameFile(relPath string) (nodefs.File, fuse.Status) {
|
|
|
|
dotName := filepath.Base(relPath) // gocryptfs.longname.XYZ.name
|
|
|
|
longname := dotName[:len(dotName)-len(nametransform.LongNameSuffix)] // gocryptfs.longname.XYZ
|
|
|
|
|
|
|
|
cDir := saneDir(relPath)
|
|
|
|
pDir, err := rfs.decryptPath(cDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2016-09-29 21:29:45 +02:00
|
|
|
dirIV := derivePathIV(cDir, ivPurposeDirIV)
|
2016-09-22 23:28:11 +02:00
|
|
|
e, err := rfs.findLongnameParent(pDir, dirIV, longname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
content := []byte(rfs.nameTransform.EncryptName(e, dirIV))
|
|
|
|
parentFile := filepath.Join(rfs.args.Cipherdir, pDir)
|
|
|
|
return rfs.NewVirtualFile(content, parentFile)
|
|
|
|
}
|