mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
* export: fail the tar export on an unreadable live needle * export: honor -newer after needle body read errors Skip a damaged record when its decoded modification time excludes it from the tar. Publish the date flag only after decoding the timestamp so incomplete metadata still fails the export. * needle: keep reading a record whose fields end before its timestamp The last-modified field was decoded without the length check the other optional fields keep, so a record whose bytes end right after its flags, name or mime now failed as corrupted. Older reads, and the Rust volume server, read it without a timestamp. Keep the check; the flag stays off until the timestamp is decoded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
3.9 KiB
Go
118 lines
3.9 KiB
Go
package command
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
)
|
|
|
|
func TestExportUnreadableNeedleBody(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
indexLast bool
|
|
limit int
|
|
wantStatus int
|
|
}{
|
|
{"live record", true, 0, 1},
|
|
{"past the requested limit", true, 1, 0},
|
|
{"unindexed record", false, 0, 0},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
base, offsets := writeFixTestVolume(t, dir, 2, needle.GetCurrentVersion())
|
|
doFixOneVolume(dir, "fix_7", "fix", 7, true)
|
|
if !tc.indexLast {
|
|
if err := os.Truncate(base+".idx", int64(types.NeedleMapEntrySize)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// The last record still has its complete header and declared size.
|
|
if err := os.Truncate(base+".dat", offsets[1]+int64(types.NeedleHeaderSize)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
checkTestExport(t, dir, tc.limit, "", tc.wantStatus, bytes.Repeat([]byte{1}, 1000))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExportNewerWithDamagedRecord(t *testing.T) {
|
|
const cutoff = 3600
|
|
since := time.Unix(cutoff, 0).In(localLocation).Format(timeFormat)
|
|
later := needle.Needle{Id: 2, Data: []byte("later"), LastModified: cutoff + 1, Flags: needle.FlagHasLastModifiedDate}
|
|
for _, tc := range []struct {
|
|
name string
|
|
modified uint64
|
|
damageDate bool
|
|
wantStatus int
|
|
wantMembers [][]byte
|
|
}{
|
|
{"before cutoff", 0, false, 0, [][]byte{later.Data}},
|
|
{"at cutoff", cutoff, false, 1, nil},
|
|
{"unreadable date", 0, true, 1, nil},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
first := needle.Needle{Id: 1, Data: []byte("first"), LastModified: tc.modified, Flags: needle.FlagHasLastModifiedDate}
|
|
base, offsets := writeTestVolume(t, dir, needle.GetCurrentVersion(), first, later)
|
|
doFixOneVolume(dir, "fix_7", "fix", 7, true)
|
|
payload := offsets[0] + types.NeedleHeaderSize + types.DataSizeSize
|
|
overwriteAt(t, base+".dat", payload, []byte{'X'})
|
|
if tc.damageDate {
|
|
// The extra name field leaves too few bytes for the declared timestamp.
|
|
overwriteAt(t, base+".dat", payload+int64(len(first.Data)), []byte{needle.FlagHasName | needle.FlagHasLastModifiedDate})
|
|
}
|
|
checkTestExport(t, dir, 0, since, tc.wantStatus, tc.wantMembers...)
|
|
})
|
|
}
|
|
}
|
|
|
|
func checkTestExport(t *testing.T, dir string, maxEntries int, since string, wantStatus int, wantMembers ...[]byte) {
|
|
t.Helper()
|
|
oldDir, oldCollection, oldID := *export.dir, *export.collection, *export.volumeId
|
|
oldOutput, oldLimit, oldStatus := *output, *limit, commandExitStatus
|
|
oldTar, oldHeader, oldTemplate := tarOutputFile, tarHeader, fileNameTemplate
|
|
oldNewer, oldTime, oldUnix := *newer, newerThan, newerThanUnix
|
|
t.Cleanup(func() {
|
|
*export.dir, *export.collection, *export.volumeId = oldDir, oldCollection, oldID
|
|
*output, *limit, commandExitStatus = oldOutput, oldLimit, oldStatus
|
|
tarOutputFile, tarHeader, fileNameTemplate = oldTar, oldHeader, oldTemplate
|
|
*newer, newerThan, newerThanUnix = oldNewer, oldTime, oldUnix
|
|
})
|
|
|
|
*export.dir, *export.collection, *export.volumeId = dir, "fix", 7
|
|
*output, *limit, commandExitStatus = filepath.Join(dir, "export.tar"), maxEntries, 0
|
|
*newer, newerThanUnix = since, -1
|
|
if !runExport(cmdExport, nil) {
|
|
t.Fatal("export rejected test arguments")
|
|
}
|
|
if got := CommandExitStatus(); got != wantStatus {
|
|
t.Errorf("exit status = %d, want %d", got, wantStatus)
|
|
}
|
|
f, err := os.Open(*output)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer f.Close()
|
|
r := tar.NewReader(f)
|
|
for i, want := range wantMembers {
|
|
if _, err := r.Next(); err != nil {
|
|
t.Fatalf("member %d: %v", i, err)
|
|
}
|
|
got, err := io.ReadAll(r)
|
|
if err != nil || !bytes.Equal(got, want) {
|
|
t.Fatalf("member %d: got %d bytes, want %d bytes, error %v", i, len(got), len(want), err)
|
|
}
|
|
}
|
|
if h, err := r.Next(); err != io.EOF {
|
|
t.Fatalf("unexpected tar member: %+v, error %v", h, err)
|
|
}
|
|
}
|