Files
seaweedfs/weed/mount/weedfs_content_md5_test.go
T
Chris LuandClaude Opus 5.5 e84c3f9ada mount, filer: a content change clears the stored content MD5 (#13238)
* mount: a content change clears the stored content MD5

Attributes.Md5 is the whole content's MD5: the S3 and WebDAV ETags and
WebDAV's oc:checksums report it. A write, a size change, a grown
fallocate, an O_TRUNC open or a copy_file_range into the file kept it, so
those reported the old content's MD5 and an ETag that did not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: AppendToEntry clears the stored content MD5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: copy_file_range clears the MD5 once it buffers bytes

A cancelled copy or a later read error returned before the clear, leaving
the bytes already buffered under the old MD5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: an empty AppendToEntry keeps the content MD5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 13:22:56 -08:00

113 lines
4.0 KiB
Go

//go:build !freebsd
package mount
import (
"context"
"testing"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
var storedContentMd5 = []byte("0123456789abcdef")
func openHandleWithMd5(t *testing.T, fileSize uint64) (*WFS, *FileHandle) {
t.Helper()
wfs, fh := newOpenFileHandle(t, fileSize)
t.Cleanup(fh.dirtyPages.Destroy)
fh.GetEntry().GetEntry().Attributes.Md5 = storedContentMd5
return wfs, fh
}
// The filer reports Attributes.Md5 as the content's MD5 (S3 and WebDAV
// ETags, oc:checksums): a change of the bytes through the mount drops it.
func TestContentChangeClearsMd5(t *testing.T) {
t.Run("write", func(t *testing.T) {
wfs, fh := openHandleWithMd5(t, 10)
in := &fuse.WriteIn{Fh: uint64(fh.fh), Offset: 2, Size: 3}
_, status := wfs.Write(nil, in, []byte("abc"))
require.Equal(t, fuse.OK, status)
require.Nil(t, fh.GetEntry().GetEntry().Attributes.Md5)
})
t.Run("fallocate past the end", func(t *testing.T) {
wfs, fh := openHandleWithMd5(t, 10)
require.Equal(t, fuse.OK, wfs.Fallocate(nil, &fuse.FallocateIn{Fh: uint64(fh.fh), Offset: 4096, Length: 4096}))
require.Nil(t, fh.GetEntry().GetEntry().Attributes.Md5)
})
t.Run("fallocate inside the file", func(t *testing.T) {
wfs, fh := openHandleWithMd5(t, 10)
require.Equal(t, fuse.OK, wfs.Fallocate(nil, &fuse.FallocateIn{Fh: uint64(fh.fh), Offset: 0, Length: 1}))
require.Equal(t, storedContentMd5, fh.GetEntry().GetEntry().Attributes.Md5)
})
t.Run("copy_file_range into the file", func(t *testing.T) {
wfs, dst := openHandleWithMd5(t, 10)
srcInode := wfs.inodeToPath.Lookup(util.FullPath("/src.txt"), 1, false, false, 0, true)
src, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, srcInode, &filer_pb.Entry{
Name: "src.txt",
Attributes: &filer_pb.FuseAttributes{FileMode: 0100644, FileSize: 5, Inode: srcInode},
Content: []byte("hello"),
}, 0, 0)
_, status := wfs.CopyFileRange(make(chan struct{}), &fuse.CopyFileRangeIn{
FhIn: uint64(src.fh), FhOut: uint64(dst.fh), OffIn: 1, OffOut: 0, Len: 4,
})
require.Equal(t, fuse.OK, status)
require.Nil(t, dst.GetEntry().GetEntry().Attributes.Md5)
})
}
func TestTruncateClearsMd5(t *testing.T) {
for _, tc := range []struct {
name string
size uint64
keepMd5 bool
oTrunc bool
}{
{name: "shorter", size: 300},
{name: "longer", size: 4096},
{name: "same size", size: 768, keepMd5: true},
{name: "open with O_TRUNC", size: 0, oTrunc: true},
} {
t.Run(tc.name, func(t *testing.T) {
wfs := newInvalidateTestWFS(t)
current := attributeAppendEntry(3)
current.Attributes.Md5 = storedContentMd5
fake := &attributeCASFiler{entry: proto.Clone(current).(*filer_pb.Entry)}
startFakeFiler(t, wfs, fake)
path := util.FullPath("/dir/file")
wfs.inodeToPath.Lookup(util.FullPath("/dir"), 1, true, false, 0, false)
wfs.inodeToPath.Lookup(path, 1, false, false, 42, true)
wfs.inodeToPath.MarkChildrenCached(util.FullPath("/dir"))
require.NoError(t, wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry("/dir", current), 1))
local := proto.Clone(current).(*filer_pb.Entry)
wfs.mapPbIdFromFilerToLocal(local)
fh, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, 42, local, 0, 0)
fh.baseEntry.Store(proto.Clone(local).(*filer_pb.Entry))
fh.RememberPath(path)
t.Cleanup(fh.dirtyPages.Destroy)
if tc.oTrunc {
require.Equal(t, fuse.OK, wfs.truncateEntry(context.Background(), path, proto.Clone(current).(*filer_pb.Entry)))
} else {
in := &fuse.SetAttrIn{}
in.NodeId, in.Valid, in.Size = 42, fuse.FATTR_SIZE, tc.size
require.Equal(t, fuse.OK, wfs.SetAttr(nil, in, &fuse.AttrOut{}))
}
fake.mu.Lock()
defer fake.mu.Unlock()
require.Equal(t, tc.size, fake.entry.Attributes.FileSize)
if tc.keepMd5 {
require.Equal(t, storedContentMd5, fake.entry.Attributes.Md5)
} else {
require.Nil(t, fake.entry.Attributes.Md5)
}
})
}
}