Files
seaweedfs/weed/s3api/s3api_object_handlers_copy_symlink_test.go
T
a9d4714aed s3api, filer: keep a symlink a symlink when CopyObject copies it (#13298)
* s3api, filer: keep a symlink a symlink when CopyObject copies it

A symlink made through weed mount is an entry with the symlink mode bit,
a SymlinkTarget and no data. CopyObject builds the destination's
attributes afresh, with the permission bits from the request, so a copy
to another key dropped both and landed as an empty regular file. A
metadata-only self-copy (REPLACE) kept the target but reset the mode,
leaving the link an empty regular file in place.

Keep the symlink mode bit and the target on both paths. The permission
bits still come from the request, as for any other copy. The filer's own
copy (copyEntry) already carries the source's whole attribute set.

A copy into a new prefix creates the symlink in a missing directory, and
the filer gave each parent it created the entry's whole mode plus
ModeDir. With the symlink bit as well, a mount shows those directories
as regular files, and nothing under them can be reached. Parents now
take the entry's permission bits only.

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

* filer: drop the symlink type when promoting an entry to a directory

A key nested under a symlink promotes the symlink's entry to a
directory. The promotion added ModeDir and kept the symlink bit, so a
mount showed the directory as a regular file and could not reach the
nested key. Keep only the permission bits there too, with the same
restorableModeBits a missing parent now takes from its entry.

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

* s3api: keep a self-copied prefix object a directory when it was a symlink

A prefix object promoted from a symlink before the filer change still has
the symlink bit. A routed self-copy of it restored ModeDir and then
keepSymlink added the symlink bit back, leaving a directory a mount shows
as a regular file. keepSymlink now leaves directory destinations alone.

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

* filer_pb: a prefix object keeps no type bit but the directory's

MarkPrefixObject added ModeDir and the execute bits to whatever mode the
object had. A symlink copied onto a key that other keys are nested
under, or a self-copy outside the owner route of a prefix object
promoted from a symlink before the filer change, kept the symlink bit
next to ModeDir, which a mount shows as a regular file. Clear the other
type bits first.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-12-02 12:04:04 -08:00

140 lines
5.9 KiB
Go

package s3api
import (
"context"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/gorilla/mux"
"github.com/seaweedfs/seaweedfs/weed/cluster"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/stretchr/testify/require"
)
// dirAwareFiler answers a file create over a directory the way the filer does,
// so the gateway writes the object onto the directory as a prefix object.
type dirAwareFiler struct{ *copyACLFiler }
func (f *dirAwareFiler) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (*filer_pb.CreateEntryResponse, error) {
f.mu.Lock()
existing := f.entries[req.Directory+"/"+req.Entry.Name]
f.mu.Unlock()
if existing != nil && existing.IsDirectory && !req.Entry.IsDirectory {
return &filer_pb.CreateEntryResponse{Error: "existing entry is a directory", ErrorCode: filer_pb.FilerError_EXISTING_IS_DIRECTORY}, nil
}
return f.copyACLFiler.CreateEntry(ctx, req)
}
// A symlink made through a mount is an entry with a target and no data. Its
// copy must be a symlink to the same target, not an empty regular file, both
// when it goes to another key and when a self-copy rewrites its metadata. The
// permission bits still come from the request, as for any other copy.
func TestCopyObjectKeepsSymlink(t *testing.T) {
const bucket, source, target = "copy-symlink", "env/bin/python", "python3.10"
for _, tt := range []struct {
name string
self bool
routed bool
replace bool
regular bool
prefix bool
ontoDir bool
}{
{name: "to another key"},
{name: "to another key replacing metadata", replace: true},
{name: "onto itself replacing metadata", self: true, replace: true},
{name: "onto itself through the owner filer", self: true, replace: true, routed: true},
{name: "regular file stays regular", regular: true},
{name: "prefix object promoted from a symlink stays a directory", self: true, replace: true, routed: true, prefix: true},
{name: "prefix object promoted from a symlink stays a directory under the bucket lock", self: true, replace: true, prefix: true},
{name: "onto a key other keys are nested under", ontoDir: true},
} {
t.Run(tt.name, func(t *testing.T) {
filer := &dirAwareFiler{&copyACLFiler{ambiguousPutFiler: &ambiguousPutFiler{apply: true, entries: map[string]*filer_pb.Entry{}}}}
address := startFakeFiler(t, filer)
s3a := newPutTestServer(t, address)
s3a.iam = &IdentityAccessManagement{}
s3a.bucketConfigCache = NewBucketConfigCache(time.Minute)
s3a.bucketConfigCache.Set(bucket, &BucketConfig{Name: bucket, Ownership: s3_constants.OwnershipObjectWriter})
filer.entries["/buckets/"+bucket] = &filer_pb.Entry{Name: bucket, IsDirectory: true, Attributes: &filer_pb.FuseAttributes{}}
if tt.routed {
s3a.objectWriteLockClient = cluster.NewLockClient(s3a.option.GrpcDialOption, address)
s3a.objectWriteLockClient.SetRing([]pb.ServerAddress{address}, 1)
}
link := &filer_pb.Entry{Name: "python", Attributes: &filer_pb.FuseAttributes{
FileMode: uint32(os.ModeSymlink | 0777), SymlinkTarget: target, Mtime: 1, Crtime: 1}}
if tt.regular {
link = &filer_pb.Entry{Name: "python", Content: []byte("data"),
Attributes: &filer_pb.FuseAttributes{FileMode: 0755, FileSize: 4, Mtime: 1, Crtime: 1}}
}
if tt.prefix {
link.IsDirectory = true
link.Attributes.FileMode |= uint32(os.ModeDir)
link.Extended = map[string][]byte{s3_constants.SeaweedFSPrefixObject: []byte("true")}
filer.entries["/buckets/"+bucket+"/"+source+"/child"] = &filer_pb.Entry{Name: "child", Content: []byte("child data")}
}
filer.entries["/buckets/"+bucket+"/"+source] = link
key := "copied/python"
if tt.self {
key = source
}
if tt.ontoDir {
filer.entries["/buckets/"+bucket+"/"+key] = &filer_pb.Entry{Name: "python", IsDirectory: true,
Attributes: &filer_pb.FuseAttributes{FileMode: uint32(os.ModeDir | 0755)}}
filer.entries["/buckets/"+bucket+"/"+key+"/child"] = &filer_pb.Entry{Name: "child", Content: []byte("child data")}
}
req := httptest.NewRequest(http.MethodPut, "/"+bucket+"/"+key, nil)
req = mux.SetURLVars(req, map[string]string{"bucket": bucket, "object": key})
req.Header.Set("X-Amz-Copy-Source", "/"+bucket+"/"+source)
if tt.replace {
req.Header.Set(s3_constants.AmzUserMetaDirective, DirectiveReplace)
req.Header.Set("X-Amz-Meta-Note", "new")
}
rr := httptest.NewRecorder()
s3a.CopyObjectHandler(rr, req)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
filer.mu.Lock()
defer filer.mu.Unlock()
stored := filer.entries["/buckets/"+bucket+"/"+key]
require.NotNil(t, stored)
if tt.regular {
require.Equal(t, defaultFileMode, stored.Attributes.FileMode)
require.Empty(t, stored.Attributes.SymlinkTarget)
require.Equal(t, []byte("data"), stored.Content)
return
}
if tt.prefix || tt.ontoDir {
// The object lands on the directory, which must keep no type bit
// but its own. Written as a prefix object (all but the routed
// self-copy), it is also made traversable.
want := uint32(os.ModeDir) | defaultFileMode
if !tt.routed {
want |= 0111
}
require.True(t, stored.IsDirectory)
require.Equal(t, want, stored.Attributes.FileMode,
"copy onto a prefix object stored mode %v", os.FileMode(stored.Attributes.FileMode))
require.True(t, stored.IsPrefixObject())
require.NotNil(t, filer.entries["/buckets/"+bucket+"/"+key+"/child"])
return
}
require.Equal(t, uint32(os.ModeSymlink)|defaultFileMode, stored.Attributes.FileMode,
"copy of a symlink stored mode %v", os.FileMode(stored.Attributes.FileMode))
require.Equal(t, target, stored.Attributes.SymlinkTarget)
if tt.replace {
require.Equal(t, []byte("new"), stored.Extended["X-Amz-Meta-Note"])
}
if tt.routed {
require.Len(t, filer.updates, 1, "the self-copy must commit through the owner")
}
})
}
}