Skip to content

Commit

Permalink
fusefrontent: report correct size on hard link creation
Browse files Browse the repository at this point in the history
And add a test for it.

Fixes #724
  • Loading branch information
rfjakob committed Mar 29, 2023
1 parent b370325 commit 24b3978
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/fusefrontend/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ func (n *Node) Link(ctx context.Context, target fs.InodeEmbedder, name string, o
return
}
inode = n.newChild(ctx, st, out)
n.translateSize(dirfd, cName, &out.Attr)
return inode, 0
}

Expand Down
1 change: 1 addition & 0 deletions internal/fusefrontend/node_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (n *Node) translateSize(dirfd int, cName string, out *fuse.Attr) {
rn := n.rootNode()
out.Size = rn.contentEnc.CipherSizeToPlainSize(out.Size)
} else if out.IsSymlink() {
// read and decrypt target
target, _ := n.readlink(dirfd, cName)
out.Size = uint64(len(target))
}
Expand Down
39 changes: 39 additions & 0 deletions tests/matrix/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,45 @@ func TestSymlinkSize(t *testing.T) {
}
}

// gocryptfs 2.0+ reported the ciphertext size on hard link creation
// https://github.com/rfjakob/gocryptfs/issues/724
func TestLinkSize(t *testing.T) {
p := filepath.Join(test_helpers.DefaultPlainDir, t.Name()) + ".regular"
f, err := os.Create(p)
if err != nil {
t.Fatal(err)
}
_, err = f.WriteString("x")
f.Close()
if err != nil {
t.Fatal(err)
}
doTestLinkSize(t, p)

p = filepath.Join(test_helpers.DefaultPlainDir, t.Name()) + ".symlink"
err = syscall.Symlink("x", p)
if err != nil {
t.Fatal(err)
}
doTestLinkSize(t, p)
}

func doTestLinkSize(t *testing.T, p string) {
p2 := p + ".link"
err := syscall.Link(p, p2)
if err != nil {
t.Fatal(err)
}
var st syscall.Stat_t
err = syscall.Lstat(p2, &st)
if err != nil {
t.Fatal(filepath.Base(p2), err)
}
if st.Size != 1 {
t.Errorf("wrong %s size: want=1 have=%d", filepath.Base(p), st.Size)
}
}

// TestPwd check that /usr/bin/pwd works inside gocryptfs.
//
// This was broken in gocryptfs v2.0 with -sharedstorage:
Expand Down

0 comments on commit 24b3978

Please sign in to comment.