db72fcea41
Using dirfd-relative operations allows safe lockless handling of the ".name" files.
103 lines
2.9 KiB
Go
103 lines
2.9 KiB
Go
package nametransform
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/toggledlog"
|
|
)
|
|
|
|
const (
|
|
// Files with long names are stored in two files:
|
|
// gocryptfs.longname.[sha256] <--- File content, prefix = gocryptfs.longname.
|
|
// gocryptfs.longname.[sha256].name <--- File name, suffix = .name
|
|
LongNameSuffix = ".name"
|
|
longNamePrefix = "gocryptfs.longname."
|
|
)
|
|
|
|
// HashLongName - take the hash of a long string "name" and return
|
|
// "gocryptfs.longname.[sha256]"
|
|
func HashLongName(name string) string {
|
|
hashBin := sha256.Sum256([]byte(name))
|
|
hashBase64 := base64.URLEncoding.EncodeToString(hashBin[:])
|
|
return longNamePrefix + hashBase64
|
|
}
|
|
|
|
// Values returned by IsLongName
|
|
const (
|
|
LongNameContent = iota
|
|
LongNameFilename = iota
|
|
LongNameNone = iota
|
|
)
|
|
|
|
// NameType - detect if cName is
|
|
// 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 NameType(cName string) int {
|
|
if !strings.HasPrefix(cName, longNamePrefix) {
|
|
return LongNameNone
|
|
}
|
|
if strings.HasSuffix(cName, LongNameSuffix) {
|
|
return LongNameFilename
|
|
}
|
|
return LongNameContent
|
|
}
|
|
|
|
// IsLongContent returns true if "cName" is the content store of a long name file.
|
|
func IsLongContent(cName string) bool {
|
|
return NameType(cName) == LongNameContent
|
|
}
|
|
|
|
// ReadLongName - read path.name
|
|
func ReadLongName(path string) (string, error) {
|
|
content, err := ioutil.ReadFile(path + LongNameSuffix)
|
|
if err != nil {
|
|
toggledlog.Warn.Printf("ReadLongName: %v", err)
|
|
}
|
|
return string(content), err
|
|
}
|
|
|
|
// DeleteLongName deletes "hashName.name".
|
|
func DeleteLongName(dirfd *os.File, hashName string) error {
|
|
err := syscall.Unlinkat(int(dirfd.Fd()), hashName+LongNameSuffix)
|
|
if err != nil {
|
|
toggledlog.Warn.Printf("DeleteLongName: %v", err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// WriteLongName encrypts plainName and writes it into "hashName.name".
|
|
// For the convenience of the caller, plainName may also be a path and will be
|
|
// converted internally.
|
|
func (n *NameTransform) WriteLongName(dirfd *os.File, hashName string, plainName string) (err error) {
|
|
plainName = filepath.Base(plainName)
|
|
|
|
// Encrypt the basename
|
|
dirIV, err := ReadDirIVAt(dirfd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cName := n.EncryptName(plainName, dirIV)
|
|
|
|
// Write the encrypted name into hashName.name
|
|
fdRaw, err := syscall.Openat(int(dirfd.Fd()), hashName+LongNameSuffix,
|
|
syscall.O_WRONLY|syscall.O_CREAT|syscall.O_EXCL, 0600)
|
|
if err != nil {
|
|
toggledlog.Warn.Printf("WriteLongName: Openat: %v", err)
|
|
return err
|
|
}
|
|
fd := os.NewFile(uintptr(fdRaw), hashName+LongNameSuffix)
|
|
defer fd.Close()
|
|
_, err = fd.Write([]byte(cName))
|
|
if err != nil {
|
|
toggledlog.Warn.Printf("WriteLongName: Write: %v", err)
|
|
}
|
|
return err
|
|
}
|