diff --git a/weed/command/fix.go b/weed/command/fix.go index 1516220279..cbb1e0aef1 100644 --- a/weed/command/fix.go +++ b/weed/command/fix.go @@ -33,6 +33,8 @@ var cmdFix = &Command{ Long: `Fix runs the SeaweedFS fix command on local dat files ( or remote files) or whole folders to re-create the index .idx file. If fixing remote files, you need to synchronize master.toml to the same directory on the current node as on the master node. You Need to stop the volume server when running this command. Use -ecx to rebuild a lost EC index (.ecx) — and the .vif when missing — from the local .ec## shards. + On a damaged .dat, fix indexes only the needles before the first unreadable header; repair from a replica, or run fix on a copy, before compacting or loading the volume. + With -ignoreError, fix keeps going and writes what it could index, but still exits non-zero if any volume failed. `, } @@ -240,6 +242,7 @@ func doFixOneVolume(basepath string, baseFileName string, collection string, vol err := fmt.Errorf("volume ID out of range: %d", volumeId) if *fixIgnoreError { glog.Error(err) + SetCommandExitStatus(1) return } else { glog.Fatal(err) @@ -261,6 +264,11 @@ func doFixOneVolume(basepath string, baseFileName string, collection string, vol err := fmt.Errorf("scan .dat File: %w", err) if *fixIgnoreError { glog.Error(err) + SetCommandExitStatus(1) + // Without a super block nothing was scanned; keep the existing .idx. + if scanner.version == 0 { + return + } } else { glog.Fatal(err) } @@ -270,6 +278,7 @@ func doFixOneVolume(basepath string, baseFileName string, collection string, vol err := fmt.Errorf("save to .idx File: %w", err) if *fixIgnoreError { glog.Error(err) + SetCommandExitStatus(1) } else { if err := os.Remove(indexFileName); err != nil { glog.Errorf("failed to cleanup file %s:%v", indexFileName, err) @@ -294,6 +303,7 @@ func doFixEcxFromShards(basePath, baseFileName, collection string, volumeId int6 fail := func(err error) { if *fixIgnoreError { glog.Error(err) + SetCommandExitStatus(1) } else { glog.Fatal(err) } diff --git a/weed/command/fix_test.go b/weed/command/fix_test.go index e554dda630..274846c926 100644 --- a/weed/command/fix_test.go +++ b/weed/command/fix_test.go @@ -163,3 +163,40 @@ func TestFixSkipsRecordPastEndOfDat(t *testing.T) { }) } } + +func TestFixIgnoreErrorReportsIncompleteScan(t *testing.T) { + savedIgnore, savedStatus := *fixIgnoreError, commandExitStatus + *fixIgnoreError = true + defer func() { *fixIgnoreError, commandExitStatus = savedIgnore, savedStatus }() + + t.Run("bad header", func(t *testing.T) { + dir := t.TempDir() + base, offsets := writeFixTestVolume(t, dir, 5) + overwriteAt(t, base+".dat", offsets[2], bytes.Repeat([]byte{0xa5}, 16)) + commandExitStatus = 0 + doFixOneVolume(dir, "fix_7", "fix", 7, false) + if CommandExitStatus() == 0 { + t.Error("expected a non-zero exit status for a scan that stopped before the end of the .dat") + } + if got := indexedKeys(t, base+".idx"); !slices.Equal(got, []uint64{1, 2}) { + t.Errorf("indexed keys %v, want [1 2]", got) + } + }) + + t.Run("bad super block keeps the existing index", func(t *testing.T) { + dir := t.TempDir() + base, _ := writeFixTestVolume(t, dir, 3) + overwriteAt(t, base+".dat", 0, bytes.Repeat([]byte{0xa5}, 8)) + if err := os.WriteFile(base+".idx", []byte("previous index"), 0644); err != nil { + t.Fatal(err) + } + commandExitStatus = 0 + doFixOneVolume(dir, "fix_7", "fix", 7, true) + if CommandExitStatus() == 0 { + t.Error("expected a non-zero exit status for an unreadable super block") + } + if b, err := os.ReadFile(base + ".idx"); err != nil || string(b) != "previous index" { + t.Errorf("existing .idx changed: %q, %v", b, err) + } + }) +}