longnames: use symbolic constants instead of naked ints

This commit is contained in:
Jakob Unterwurzacher 2016-04-10 12:36:43 +02:00
parent e42e46c97c
commit 63d3e51734
3 changed files with 22 additions and 14 deletions

View File

@ -207,21 +207,22 @@ func (fs *FS) OpenDir(dirName string, context *fuse.Context) ([]fuse.DirEntry, f
if fs.args.LongNames { if fs.args.LongNames {
isLong := nametransform.IsLongName(cName) isLong := nametransform.IsLongName(cName)
if isLong == 1 { if isLong == nametransform.LongNameContent {
cNameLong, err := nametransform.ReadLongName(filepath.Join(cDirAbsPath, cName)) cNameLong, err := nametransform.ReadLongName(filepath.Join(cDirAbsPath, cName))
if err != nil { if err != nil {
toggledlog.Warn.Printf("Could not read long name for file %s, skipping file", cName) toggledlog.Warn.Printf("Could not read long name for file %s, skipping file", cName)
continue continue
} }
cName = cNameLong cName = cNameLong
} else if isLong == 2 { } else if isLong == nametransform.LongNameFilename {
// ignore "gocryptfs.longname.*.name" // ignore "gocryptfs.longname.*.name"
continue continue
} }
} }
name, err := fs.nameTransform.DecryptName(cName, cachedIV) name, err := fs.nameTransform.DecryptName(cName, cachedIV)
if err != nil { 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 continue
} }

View File

@ -25,18 +25,25 @@ func HashLongName(name string) string {
return longNamePrefix + hashBase64 return longNamePrefix + hashBase64
} }
// Values returned by IsLongName
const (
LongNameContent = iota
LongNameFilename = iota
LongNameNone = iota
)
// IsLongName - detect if cName is // IsLongName - detect if cName is
// gocryptfs.longname.* ........ 1 // gocryptfs.longname.[sha256] ........ LongNameContent (content of a long name file)
// gocryptfs.longname.*.name ... 2 // gocryptfs.longname.[sha256].name .... LongNameFilename (full file name of a long name file)
// else ........................ 0 // else ................................ LongNameNone (normal file)
func IsLongName(cName string) int { func IsLongName(cName string) int {
if !strings.HasPrefix(cName, longNamePrefix) { if !strings.HasPrefix(cName, longNamePrefix) {
return 0 return LongNameNone
} }
if strings.HasSuffix(cName, longNameSuffix) { if strings.HasSuffix(cName, longNameSuffix) {
return 2 return LongNameFilename
} }
return 1 return LongNameContent
} }
// ReadLongName - read path.name // ReadLongName - read path.name
@ -51,7 +58,7 @@ func ReadLongName(path string) (string, error) {
// DeleteLongName - if cPath ends in "gocryptfs.longname.[sha256]", // DeleteLongName - if cPath ends in "gocryptfs.longname.[sha256]",
// delete the "gocryptfs.longname.[sha256].name" file // delete the "gocryptfs.longname.[sha256].name" file
func DeleteLongName(cPath string) error { func DeleteLongName(cPath string) error {
if IsLongName(filepath.Base(cPath)) == 1 { if IsLongName(filepath.Base(cPath)) == LongNameContent {
err := syscall.Unlink(cPath + longNameSuffix) err := syscall.Unlink(cPath + longNameSuffix)
if err != nil { if err != nil {
toggledlog.Warn.Printf("DeleteLongName: %v", err) toggledlog.Warn.Printf("DeleteLongName: %v", err)
@ -65,7 +72,7 @@ func DeleteLongName(cPath string) error {
// "gocryptfs.longname.[sha256].name" file // "gocryptfs.longname.[sha256].name" file
func (n *NameTransform) WriteLongName(cPath string, plainPath string) (err error) { func (n *NameTransform) WriteLongName(cPath string, plainPath string) (err error) {
cHashedName := filepath.Base(cPath) cHashedName := filepath.Base(cPath)
if IsLongName(cHashedName) != 1 { if IsLongName(cHashedName) != LongNameContent {
// This is not a hashed file name, nothing to do // This is not a hashed file name, nothing to do
return nil return nil
} }

View File

@ -6,17 +6,17 @@ import (
func TestIsLongName(t *testing.T) { func TestIsLongName(t *testing.T) {
n := "gocryptfs.longname.LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU=.name" n := "gocryptfs.longname.LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU=.name"
if IsLongName(n) != 2 { if IsLongName(n) != LongNameFilename {
t.Errorf("False negative") t.Errorf("False negative")
} }
n = "gocryptfs.longname.LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU=" n = "gocryptfs.longname.LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU="
if IsLongName(n) != 1 { if IsLongName(n) != LongNameContent {
t.Errorf("False negative") t.Errorf("False negative")
} }
n = "LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU=" n = "LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU="
if IsLongName(n) != 0 { if IsLongName(n) != LongNameNone {
t.Errorf("False positive") t.Errorf("False positive")
} }
} }