Skip to content

Commit b10f7c9

Browse files
committed
added error and warn for old haul chunks
1 parent 81ee500 commit b10f7c9

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

cmd/hauler/cli/store/load.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/url"
88
"os"
99
"path/filepath"
10+
"regexp"
1011
"strings"
1112

1213
"hauler.dev/go/hauler/v2/internal/flags"
@@ -20,6 +21,10 @@ import (
2021
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2122
)
2223

24+
// matches the old <base>_NNN.<ext> chunk naming of haul_0.tar.zst
25+
// used only to print a hint when a load fails on a file shaped like a stale chunk
26+
var legacyChunkRe = regexp.MustCompile(`_\d+\.`)
27+
2328
// extracts the contents of an archived oci layout to an existing oci layout
2429
func LoadCmd(ctx context.Context, o *flags.LoadOpts, rso *flags.StoreRootOpts, ro *flags.CliRootOpts) error {
2530
l := log.FromContext(ctx)
@@ -41,7 +46,7 @@ func LoadCmd(ctx context.Context, o *flags.LoadOpts, rso *flags.StoreRootOpts, r
4146
for _, fileName := range o.FileName {
4247
resolved := resolveHaulPath(fileName)
4348
l.Infof("loading haul [%s] to [%s]", resolved, o.StoreDir)
44-
err := unarchiveLayoutTo(ctx, resolved, o.StoreDir, tempDir)
49+
err := unarchiveLayoutTo(ctx, resolved, o.StoreDir, tempDir, ro)
4550
if err != nil {
4651
return err
4752
}
@@ -52,7 +57,7 @@ func LoadCmd(ctx context.Context, o *flags.LoadOpts, rso *flags.StoreRootOpts, r
5257
}
5358

5459
// accepts an archived OCI layout, extracts the contents to an existing OCI layout, and preserves the index
55-
func unarchiveLayoutTo(ctx context.Context, haulPath string, dest string, tempDir string) error {
60+
func unarchiveLayoutTo(ctx context.Context, haulPath string, dest string, tempDir string, ro *flags.CliRootOpts) error {
5661
l := log.FromContext(ctx)
5762

5863
if strings.HasPrefix(haulPath, "http://") || strings.HasPrefix(haulPath, "https://") {
@@ -94,6 +99,19 @@ func unarchiveLayoutTo(ctx context.Context, haulPath string, dest string, tempDi
9499
haulPath = joined
95100

96101
if err := archives.Unarchive(ctx, haulPath, tempDir); err != nil {
102+
if legacyChunkRe.MatchString(filepath.Base(haulPath)) {
103+
ignoreErrors := ro.IgnoreErrors
104+
if !ignoreErrors && os.Getenv(consts.HaulerIgnoreErrors) == "true" {
105+
ignoreErrors = true
106+
}
107+
if ignoreErrors {
108+
l.Warnf("possibly detected an old chunk format for haul: [%s]", haulPath)
109+
l.Warnf("attempt to rename to '<base>.<ext>.NNN' and try loading it again...")
110+
return nil
111+
}
112+
l.Errorf("possibly detected an old chunk format for haul: [%s]", haulPath)
113+
l.Errorf("attempt to rename to '<base>.<ext>.NNN' and try loading it again...")
114+
}
97115
return err
98116
}
99117

cmd/hauler/cli/store/load_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestUnarchiveLayoutTo(t *testing.T) {
7171
destDir := t.TempDir()
7272
tempDir := t.TempDir()
7373

74-
if err := unarchiveLayoutTo(ctx, testHaulArchive, destDir, tempDir); err != nil {
74+
if err := unarchiveLayoutTo(ctx, testHaulArchive, destDir, tempDir, defaultCliOpts()); err != nil {
7575
t.Fatalf("unarchiveLayoutTo: %v", err)
7676
}
7777

@@ -262,7 +262,7 @@ func TestUnarchiveLayoutTo_AnnotationBackfill(t *testing.T) {
262262
// Step 4: Load the stripped archive.
263263
destDir := t.TempDir()
264264
tempDir := t.TempDir()
265-
if err := unarchiveLayoutTo(ctx, strippedArchive, destDir, tempDir); err != nil {
265+
if err := unarchiveLayoutTo(ctx, strippedArchive, destDir, tempDir, defaultCliOpts()); err != nil {
266266
t.Fatalf("unarchiveLayoutTo stripped: %v", err)
267267
}
268268

@@ -354,7 +354,7 @@ func TestUnarchiveLayoutTo_LegacyKindMigration(t *testing.T) {
354354
// Step 4: Load the legacy archive.
355355
destDir := t.TempDir()
356356
tempDir := t.TempDir()
357-
if err := unarchiveLayoutTo(ctx, legacyArchive, destDir, tempDir); err != nil {
357+
if err := unarchiveLayoutTo(ctx, legacyArchive, destDir, tempDir, defaultCliOpts()); err != nil {
358358
t.Fatalf("unarchiveLayoutTo legacy: %v", err)
359359
}
360360

0 commit comments

Comments
 (0)