Fix invalid -longnamemax for reverse mode

This commit is contained in:
NekoGirlSAIKOU 2022-04-25 01:35:30 +08:00 committed by Jakob Unterwurzacher
parent bf29c9f99d
commit 1bff80b46c
4 changed files with 17 additions and 5 deletions

View File

@ -26,7 +26,7 @@ func (rn *RootNode) EncryptPath(plainPath string) (string, error) {
if err != nil {
return "", err
}
if rn.args.LongNames && len(encryptedPart) > unix.NAME_MAX {
if rn.args.LongNames && (len(encryptedPart) > unix.NAME_MAX || len(encryptedPart) > rn.nameTransform.GetLongNameMax()) {
encryptedPart = rn.nameTransform.HashLongName(encryptedPart)
}
cipherPath = filepath.Join(cipherPath, encryptedPart)

View File

@ -73,7 +73,7 @@ func (n *Node) Readdir(ctx context.Context) (stream fs.DirStream, errno syscall.
entries[i].Name = "___GOCRYPTFS_INVALID_NAME___"
continue
}
if len(cName) > unix.NAME_MAX {
if len(cName) > unix.NAME_MAX || len(cName) > rn.nameTransform.GetLongNameMax() {
cName = rn.nameTransform.HashLongName(cName)
dotNameFile := fuse.DirEntry{
Mode: virtualFileMode,

View File

@ -42,6 +42,9 @@ type RootNode struct {
// rootDev stores the device number of the backing directory. Used for
// --one-file-system.
rootDev uint64
// If a file name length is shorter than shortNameMax, there is no need to
// hash it.
shortNameMax int
}
// NewRootNode returns an encrypted FUSE overlay filesystem.
@ -50,6 +53,7 @@ type RootNode struct {
func NewRootNode(args fusefrontend.Args, c *contentenc.ContentEnc, n *nametransform.NameTransform) *RootNode {
var rootDev uint64
var st syscall.Stat_t
var shortNameMax int
if err := syscall.Stat(args.Cipherdir, &st); err != nil {
tlog.Warn.Printf("Could not stat backing directory %q: %v", args.Cipherdir, err)
if args.OneFileSystem {
@ -60,12 +64,16 @@ func NewRootNode(args fusefrontend.Args, c *contentenc.ContentEnc, n *nametransf
rootDev = uint64(st.Dev)
}
shortNameMax = n.GetLongNameMax() * 3 / 4
shortNameMax = shortNameMax - shortNameMax % 16 - 1
rn := &RootNode{
args: args,
nameTransform: n,
contentEnc: c,
inoMap: inomap.New(rootDev),
rootDev: rootDev,
shortNameMax: shortNameMax,
}
if len(args.Exclude) > 0 || len(args.ExcludeWildcard) > 0 || len(args.ExcludeFrom) > 0 {
rn.excluder = prepareExcluder(args)
@ -87,16 +95,16 @@ func (rn *RootNode) findLongnameParent(fd int, diriv []byte, longname string) (p
return
}
for _, entry := range entries {
if len(entry.Name) <= shortNameMax {
if len(entry.Name) <= rn.shortNameMax {
continue
}
cFullName, err = rn.nameTransform.EncryptName(entry.Name, diriv)
if err != nil {
continue
}
if len(cFullName) <= unix.NAME_MAX {
if len(cFullName) <= unix.NAME_MAX && len(cFullName) <= rn.nameTransform.GetLongNameMax() {
// Entry should have been skipped by the shortNameMax check above
log.Panic("logic error or wrong shortNameMax constant?")
log.Panic("logic error or wrong shortNameMax?")
}
hName := rn.nameTransform.HashLongName(cFullName)
if longname == hName {

View File

@ -167,3 +167,7 @@ func Dir(path string) string {
}
return d
}
func (n *NameTransform) GetLongNameMax() int {
return n.longNameMax
}