summaryrefslogtreecommitdiff
path: root/internal/nametransform/longnames.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/nametransform/longnames.go')
-rw-r--r--internal/nametransform/longnames.go39
1 files changed, 29 insertions, 10 deletions
diff --git a/internal/nametransform/longnames.go b/internal/nametransform/longnames.go
index e442b64..dad269b 100644
--- a/internal/nametransform/longnames.go
+++ b/internal/nametransform/longnames.go
@@ -1,12 +1,12 @@
package nametransform
import (
- "syscall"
- "path/filepath"
- "io/ioutil"
"crypto/sha256"
"encoding/base64"
+ "io/ioutil"
+ "path/filepath"
"strings"
+ "syscall"
"github.com/rfjakob/gocryptfs/internal/toggledlog"
)
@@ -39,28 +39,47 @@ func IsLongName(cName string) int {
return 1
}
-// ReadLongName - read "path".name
+// ReadLongName - read path.name
func ReadLongName(path string) (string, error) {
- content, err := ioutil.ReadFile(path+longNameSuffix)
+ content, err := ioutil.ReadFile(path + longNameSuffix)
if err != nil {
toggledlog.Warn.Printf("ReadLongName: %v", err)
}
return string(content), err
}
-// WriteLongName -
-func (n *NameTransform) WriteLongName(cDir string, hashedName string, plainName string) (err error) {
- if len(plainName) > syscall.NAME_MAX {
- return syscall.ENAMETOOLONG
+// 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 {
+ err := syscall.Unlink(cPath + longNameSuffix)
+ if err != nil {
+ toggledlog.Warn.Printf("DeleteLongName: %v", err)
+ }
+ return err
}
+ return nil
+}
+// WriteLongName - if cPath ends in "gocryptfs.longname.[sha256]", write the
+// "gocryptfs.longname.[sha256].name" file
+func (n *NameTransform) WriteLongName(cPath string, plainPath string) (err error) {
+ cHashedName := filepath.Base(cPath)
+ if IsLongName(cHashedName) != 1 {
+ // This is not a hashed file name, nothing to do
+ return nil
+ }
+ // Encrypt (but do not hash) the plaintext name
+ cDir := filepath.Dir(cPath)
dirIV, err := ReadDirIV(cDir)
if err != nil {
toggledlog.Warn.Printf("WriteLongName: %v", err)
return err
}
+ plainName := filepath.Base(plainPath)
cName := n.EncryptName(plainName, dirIV)
- err = ioutil.WriteFile(filepath.Join(cDir, hashedName + longNameSuffix), []byte(cName), 0600)
+ // Write the encrypted name into gocryptfs.longname.[sha256].name
+ err = ioutil.WriteFile(filepath.Join(cDir, cHashedName+longNameSuffix), []byte(cName), 0600)
if err != nil {
toggledlog.Warn.Printf("WriteLongName: %v", err)
}