From b8d652cc10bb7b1ac15b87d4b475bcf07b0c52f6 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 20 Nov 2026 05:23:16 -0800 Subject: [PATCH] filer: normalize the stored entry too when matching IF_ENTRY_EQUAL A store that keeps the entry as given (the JSON-backed ones) returns FileSize zero beside chunks, while the expected entry was normalized to the chunk extent, so an unchanged entry failed the match. Co-Authored-By: Claude Opus 5.5 --- weed/server/filer_grpc_server_condition.go | 10 +++++---- .../filer_grpc_server_condition_test.go | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/weed/server/filer_grpc_server_condition.go b/weed/server/filer_grpc_server_condition.go index 31442b18fb..093c1c957f 100644 --- a/weed/server/filer_grpc_server_condition.go +++ b/weed/server/filer_grpc_server_condition.go @@ -90,14 +90,16 @@ func clauseSatisfied(c *filer_pb.WriteCondition_Clause, current *filer.Entry) bo if !exists || c.ExpectedEntry == nil { return !exists && c.ExpectedEntry == nil } - // Compare both sides in serialized form on clones: chunks are matched - // by their fid only — the stored entry may carry the restored file_id - // while an expected one built from a metadata event does not. + // Compare both sides in serialized, normalized form on clones: chunks + // are matched by their fid only — the stored entry may carry the + // restored file_id while an expected one built from a metadata event + // does not — and a store that does not decode through FromPbEntry + // keeps the FileSize it was given. expected := proto.Clone(c.ExpectedEntry).(*filer_pb.Entry) filer_pb.BeforeEntrySerialization(expected.Chunks) actual := proto.Clone(current.ToProtoEntry()).(*filer_pb.Entry) filer_pb.BeforeEntrySerialization(actual.Chunks) - return proto.Equal(actual, filer.FromPbEntry("", expected).ToProtoEntry()) + return proto.Equal(filer.FromPbEntry("", actual).ToProtoEntry(), filer.FromPbEntry("", expected).ToProtoEntry()) default: // An unrecognized clause kind (e.g. from a newer client) must not be // treated as satisfied, which would silently bypass the guard. Fail diff --git a/weed/server/filer_grpc_server_condition_test.go b/weed/server/filer_grpc_server_condition_test.go index 548f153e93..5b775f73cd 100644 --- a/weed/server/filer_grpc_server_condition_test.go +++ b/weed/server/filer_grpc_server_condition_test.go @@ -230,6 +230,28 @@ func TestIfEntryEqualNormalizesExpected(t *testing.T) { } } +// A store that keeps the entry as given (JSON-backed stores) returns FileSize +// zero beside chunks; an expected entry read from it must still match it. +func TestIfEntryEqualUnnormalizedStored(t *testing.T) { + stored := &filer.Entry{ + FullPath: "/d/f", + Attr: filer.Attr{Mtime: time.Unix(42, 0)}, + Chunks: []*filer_pb.FileChunk{ + {Fid: &filer_pb.FileId{VolumeId: 3, FileKey: 1, Cookie: 2}, Size: 4}, + }, + } + cond := one(&filer_pb.WriteCondition_Clause{ + Kind: filer_pb.WriteCondition_IF_ENTRY_EQUAL, + ExpectedEntry: stored.ToProtoEntry(), + }) + if !writeConditionSatisfied(cond, stored) { + t.Error("an entry read from the store must equal the stored entry") + } + if stored.FileSize != 0 { + t.Fatalf("the check changed the stored entry's FileSize to %d", stored.FileSize) + } +} + // A stamp built from the metadata-log event carries chunks in serialized // form (file_id moved into fid), while the stored entry came through // FindEntry which restores file_id. The comparison must still match.