From deec7b2b63ef68d5890c3414f1185711cc605c34 Mon Sep 17 00:00:00 2001 From: Kohei Tokunaga Date: Sat, 5 Mar 2022 11:49:07 +0900 Subject: [PATCH] fs: return correct file size of symlink Signed-off-by: Kohei Tokunaga --- fs/layer/node.go | 3 +++ fs/layer/testutil.go | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/fs/layer/node.go b/fs/layer/node.go index 3185930dc..dbe1c9450 100644 --- a/fs/layer/node.go +++ b/fs/layer/node.go @@ -619,6 +619,9 @@ func (sf *statFile) updateStatUnlocked() ([]byte, error) { func entryToAttr(ino uint64, e metadata.Attr, out *fuse.Attr) fusefs.StableAttr { out.Ino = ino out.Size = uint64(e.Size) + if e.Mode&os.ModeSymlink != 0 { + out.Size = uint64(len(e.LinkName)) + } out.Blksize = blockSize out.Blocks = out.Size / uint64(out.Blksize) if out.Size%uint64(out.Blksize) > 0 { diff --git a/fs/layer/testutil.go b/fs/layer/testutil.go index c6995cb67..b849bb70f 100644 --- a/fs/layer/testutil.go +++ b/fs/layer/testutil.go @@ -501,6 +501,15 @@ func testExistence(t *testing.T, factory metadata.Store) { hasExtraMode("test", os.ModeSticky), }, }, + { + name: "symlink_size", + in: []testutil.TarEntry{ + testutil.Symlink("test", "target"), + }, + want: []check{ + hasSize("test", len("target")), + }, + }, } for _, tt := range tests { @@ -598,6 +607,22 @@ func hasFileDigest(filename string, digest string) check { } } +func hasSize(name string, size int) check { + return func(t *testing.T, root *node) { + _, n, err := getDirentAndNode(t, root, name) + if err != nil { + t.Fatalf("failed to get node %q: %v", name, err) + } + var ao fuse.AttrOut + if errno := n.Operations().(fusefs.NodeGetattrer).Getattr(context.Background(), nil, &ao); errno != 0 { + t.Fatalf("failed to get attributes of node %q: %v", name, errno) + } + if ao.Attr.Size != uint64(size) { + t.Fatalf("got size = %d, want %d", ao.Attr.Size, size) + } + } +} + func hasExtraMode(name string, mode os.FileMode) check { return func(t *testing.T, root *node) { _, n, err := getDirentAndNode(t, root, name)