From 63d3e517349a6c4774a3f75f2fa039c780eaf5f9 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 10 Apr 2016 12:36:43 +0200 Subject: longnames: use symbolic constants instead of naked ints --- internal/fusefrontend/fs_dir.go | 7 ++++--- internal/nametransform/longnames.go | 23 +++++++++++++++-------- internal/nametransform/longnames_test.go | 6 +++--- 3 files changed, 22 insertions(+), 14 deletions(-) (limited to 'internal') diff --git a/internal/fusefrontend/fs_dir.go b/internal/fusefrontend/fs_dir.go index ebf7015..bcb93af 100644 --- a/internal/fusefrontend/fs_dir.go +++ b/internal/fusefrontend/fs_dir.go @@ -207,21 +207,22 @@ func (fs *FS) OpenDir(dirName string, context *fuse.Context) ([]fuse.DirEntry, f if fs.args.LongNames { isLong := nametransform.IsLongName(cName) - if isLong == 1 { + if isLong == nametransform.LongNameContent { cNameLong, err := nametransform.ReadLongName(filepath.Join(cDirAbsPath, cName)) if err != nil { toggledlog.Warn.Printf("Could not read long name for file %s, skipping file", cName) continue } cName = cNameLong - } else if isLong == 2 { + } else if isLong == nametransform.LongNameFilename { // ignore "gocryptfs.longname.*.name" continue } } name, err := fs.nameTransform.DecryptName(cName, cachedIV) if err != nil { - toggledlog.Warn.Printf("Skipping invalid name '%s' in dir '%s': %s", cName, cDirName, err) + toggledlog.Warn.Printf("Skipping invalid name '%s' in dir '%s': %s", + cName, cDirName, err) continue } diff --git a/internal/nametransform/longnames.go b/internal/nametransform/longnames.go index dad269b..d048f95 100644 --- a/internal/nametransform/longnames.go +++ b/internal/nametransform/longnames.go @@ -25,18 +25,25 @@ func HashLongName(name string) string { return longNamePrefix + hashBase64 } +// Values returned by IsLongName +const ( + LongNameContent = iota + LongNameFilename = iota + LongNameNone = iota +) + // IsLongName - detect if cName is -// gocryptfs.longname.* ........ 1 -// gocryptfs.longname.*.name ... 2 -// else ........................ 0 +// gocryptfs.longname.[sha256] ........ LongNameContent (content of a long name file) +// gocryptfs.longname.[sha256].name .... LongNameFilename (full file name of a long name file) +// else ................................ LongNameNone (normal file) func IsLongName(cName string) int { if !strings.HasPrefix(cName, longNamePrefix) { - return 0 + return LongNameNone } if strings.HasSuffix(cName, longNameSuffix) { - return 2 + return LongNameFilename } - return 1 + return LongNameContent } // ReadLongName - read path.name @@ -51,7 +58,7 @@ func ReadLongName(path string) (string, error) { // DeleteLongName - if cPath ends in "gocryptfs.longname.[sha256]", // delete the "gocryptfs.longname.[sha256].name" file func DeleteLongName(cPath string) error { - if IsLongName(filepath.Base(cPath)) == 1 { + if IsLongName(filepath.Base(cPath)) == LongNameContent { err := syscall.Unlink(cPath + longNameSuffix) if err != nil { toggledlog.Warn.Printf("DeleteLongName: %v", err) @@ -65,7 +72,7 @@ func DeleteLongName(cPath string) error { // "gocryptfs.longname.[sha256].name" file func (n *NameTransform) WriteLongName(cPath string, plainPath string) (err error) { cHashedName := filepath.Base(cPath) - if IsLongName(cHashedName) != 1 { + if IsLongName(cHashedName) != LongNameContent { // This is not a hashed file name, nothing to do return nil } diff --git a/internal/nametransform/longnames_test.go b/internal/nametransform/longnames_test.go index dc4098c..62073ec 100644 --- a/internal/nametransform/longnames_test.go +++ b/internal/nametransform/longnames_test.go @@ -6,17 +6,17 @@ import ( func TestIsLongName(t *testing.T) { n := "gocryptfs.longname.LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU=.name" - if IsLongName(n) != 2 { + if IsLongName(n) != LongNameFilename { t.Errorf("False negative") } n = "gocryptfs.longname.LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU=" - if IsLongName(n) != 1 { + if IsLongName(n) != LongNameContent { t.Errorf("False negative") } n = "LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU=" - if IsLongName(n) != 0 { + if IsLongName(n) != LongNameNone { t.Errorf("False positive") } } -- cgit v1.2.3