mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* 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>
158 lines
6.0 KiB
Go
158 lines
6.0 KiB
Go
package filer
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// missHookStore runs onMiss, outside the store lock, right after the miss-th
|
|
// lookup of path that finds nothing.
|
|
type missHookStore struct {
|
|
*stubFilerStore
|
|
path util.FullPath
|
|
miss int32
|
|
misses atomic.Int32
|
|
onMiss func()
|
|
}
|
|
|
|
func (s *missHookStore) FindEntry(ctx context.Context, p util.FullPath) (*Entry, error) {
|
|
e, err := s.stubFilerStore.FindEntry(ctx, p)
|
|
if p == s.path && errors.Is(err, filer_pb.ErrNotFound) && s.misses.Add(1) == s.miss {
|
|
s.onMiss()
|
|
}
|
|
return e, err
|
|
}
|
|
|
|
func directoryMarker(p util.FullPath) *Entry {
|
|
return &Entry{
|
|
FullPath: p,
|
|
Attr: Attr{Mode: os.ModeDir | 0o755, Mime: s3_constants.FolderMimeType},
|
|
Extended: map[string][]byte{s3_constants.ExtETagKey: []byte("d41d8cd98f00b204e9800998ecf8427e")},
|
|
}
|
|
}
|
|
|
|
func assertDirectoryMarker(t *testing.T, store *stubFilerStore, p util.FullPath) {
|
|
t.Helper()
|
|
dir, err := store.FindEntry(context.Background(), p)
|
|
require.NoError(t, err)
|
|
assert.True(t, dir.IsDirectory())
|
|
assert.Equal(t, s3_constants.FolderMimeType, dir.Attr.Mime, "the implicit parent replaced the directory created meanwhile")
|
|
assert.Contains(t, dir.Extended, s3_constants.ExtETagKey)
|
|
}
|
|
|
|
// A directory created while a nested create is filling in that directory as
|
|
// a missing parent keeps what it was created with: the parent row is only
|
|
// written where none exists.
|
|
func TestImplicitParentKeepsConcurrentDirectory(t *testing.T) {
|
|
f, stub := newTestFilerWithStubStore(t)
|
|
ctx := context.Background()
|
|
dirPath := util.FullPath("/buckets/bkt/markers/dir")
|
|
|
|
store := &missHookStore{stubFilerStore: stub, path: dirPath, miss: 1}
|
|
f.Store = NewFilerStoreWrapper(store)
|
|
store.onMiss = func() {
|
|
require.NoError(t, f.CreateEntry(ctx, directoryMarker(dirPath), nil, false, false, nil, false, f.MaxFilenameLength))
|
|
}
|
|
|
|
child := &Entry{FullPath: dirPath.Child("child"), Attr: Attr{Mode: 0o644}}
|
|
require.NoError(t, f.CreateEntry(ctx, child, nil, false, false, nil, false, f.MaxFilenameLength))
|
|
|
|
assertDirectoryMarker(t, stub, dirPath)
|
|
}
|
|
|
|
// A directory create arriving after the parent's re-check, before its insert,
|
|
// waits for that insert and then updates the row rather than being replaced.
|
|
func TestDirectoryCreateWaitsForImplicitParentInsert(t *testing.T) {
|
|
f, stub := newTestFilerWithStubStore(t)
|
|
ctx := context.Background()
|
|
dirPath := util.FullPath("/buckets/bkt/markers/dir")
|
|
|
|
markerDone := make(chan error, 1)
|
|
store := &missHookStore{stubFilerStore: stub, path: dirPath, miss: 2}
|
|
f.Store = NewFilerStoreWrapper(store)
|
|
store.onMiss = func() {
|
|
go func() {
|
|
markerDone <- f.CreateEntry(ctx, directoryMarker(dirPath), nil, false, false, nil, false, f.MaxFilenameLength)
|
|
}()
|
|
waitForDirectoryCreateWaiter(t, markerDone)
|
|
}
|
|
|
|
child := &Entry{FullPath: dirPath.Child("child"), Attr: Attr{Mode: 0o644}}
|
|
require.NoError(t, f.CreateEntry(ctx, child, nil, false, false, nil, false, f.MaxFilenameLength))
|
|
require.GreaterOrEqual(t, store.misses.Load(), int32(2), "the parent was inserted without a re-check")
|
|
require.NoError(t, <-markerDone)
|
|
|
|
assertDirectoryMarker(t, stub, dirPath)
|
|
}
|
|
|
|
// waitForDirectoryCreateWaiter returns once a goroutine waits in
|
|
// lockDirectoryCreate, or once the create has finished without waiting.
|
|
func waitForDirectoryCreateWaiter(t *testing.T, done chan error) {
|
|
t.Helper()
|
|
buf := make([]byte, 1<<20)
|
|
for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(time.Millisecond) {
|
|
select {
|
|
case err := <-done:
|
|
done <- err
|
|
return
|
|
default:
|
|
}
|
|
for _, g := range strings.Split(string(buf[:runtime.Stack(buf, true)]), "\n\n") {
|
|
if strings.Contains(g, "sync.(*Cond).Wait") && strings.Contains(g, "lockDirectoryCreate") {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
t.Fatal("the directory create neither waited nor finished")
|
|
}
|
|
|
|
// A missing parent takes the permission bits of the entry created under it,
|
|
// never its type: the parents of a symlink made in a new directory are plain
|
|
// directories, not entries a mount shows as regular files.
|
|
func TestImplicitParentOfSymlinkIsPlainDirectory(t *testing.T) {
|
|
f, stub := newTestFilerWithStubStore(t)
|
|
ctx := context.Background()
|
|
link := &Entry{FullPath: "/buckets/bkt/cold/env/bin/python", Attr: Attr{Mode: os.ModeSymlink | 0o660, SymlinkTarget: "python3.10"}}
|
|
require.NoError(t, f.CreateEntry(ctx, link, nil, false, false, nil, false, f.MaxFilenameLength))
|
|
|
|
for _, p := range []util.FullPath{"/buckets/bkt", "/buckets/bkt/cold", "/buckets/bkt/cold/env", "/buckets/bkt/cold/env/bin"} {
|
|
dir, err := stub.FindEntry(ctx, p)
|
|
require.NoError(t, err, p)
|
|
assert.Equal(t, os.ModeDir|0o771, dir.Mode, "%s", p)
|
|
}
|
|
stored, err := stub.FindEntry(ctx, link.FullPath)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, os.ModeSymlink|0o660, stored.Mode)
|
|
assert.Equal(t, "python3.10", stored.SymlinkTarget)
|
|
}
|
|
|
|
// A symlink that gets a key nested under it is promoted to a directory. It
|
|
// keeps its permission bits but not the symlink type, or a mount shows the
|
|
// directory as a regular file and cannot reach the nested key.
|
|
func TestPromotedSymlinkIsPlainDirectory(t *testing.T) {
|
|
f, stub := newTestFilerWithStubStore(t)
|
|
ctx := context.Background()
|
|
link := &Entry{FullPath: "/buckets/bkt/cold/link", Attr: Attr{Mode: os.ModeSymlink | 0o660, SymlinkTarget: "elsewhere"}}
|
|
require.NoError(t, f.CreateEntry(ctx, link, nil, false, false, nil, false, f.MaxFilenameLength))
|
|
child := &Entry{FullPath: "/buckets/bkt/cold/link/child", Attr: Attr{Mode: 0o660}}
|
|
require.NoError(t, f.CreateEntry(ctx, child, nil, false, false, nil, false, f.MaxFilenameLength))
|
|
|
|
promoted, err := stub.FindEntry(ctx, link.FullPath)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, os.ModeDir|0o771, promoted.Mode)
|
|
assert.Empty(t, promoted.SymlinkTarget)
|
|
assert.Equal(t, []byte("true"), promoted.Extended[s3_constants.SeaweedFSPrefixObject])
|
|
}
|