Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

archive: fix corrupt listings when listing a zip directory more than once

Nick Craig-Wood68eab60564a9
diff --git a/backend/archive/zip/zip.go b/backend/archive/zip/zip.go
index 58e285b8e..6349807b1 100644
--- a/backend/archive/zip/zip.go
+++ b/backend/archive/zip/zip.go
@@ -9,6 +9,7 @@ import (
"io"
"os"
"path"
+ "slices"
"strings"
"time"
@@ -217,7 +218,8 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e
return nil, fs.ErrorDirNotFound
}
fs.Debugf(f, "dir=%q, entries=%v", dir, entries)
- return entries, nil
+ // Return a copy as callers may filter the listing in place
+ return slices.Clone(entries), nil
}
// NewObject finds the Object at remote.
diff --git a/backend/archive/zip/zip_internal_test.go b/backend/archive/zip/zip_internal_test.go
index 27ef80cd4..d13bb9140 100644
--- a/backend/archive/zip/zip_internal_test.go
+++ b/backend/archive/zip/zip_internal_test.go
@@ -128,3 +128,30 @@ func TestReadZipRootNamedEntry(t *testing.T) {
remotes := allRemotes(t, f)
assert.Equal(t, []string{"good.txt"}, remotes)
}
+
+// Listings are served from a cache which must survive callers
+// filtering the returned slice in place, as fs/list does.
+func TestListCacheNotAliased(t *testing.T) {
+ ctx := context.Background()
+ dir := t.TempDir()
+ name := writeZip(t, dir, "test.zip", "a.txt", "b.txt", "c.txt")
+
+ localFs, err := cache.Get(ctx, dir)
+ require.NoError(t, err)
+ f, err := New(ctx, localFs, name, "", "")
+ require.NoError(t, err)
+
+ entries, err := f.List(ctx, "")
+ require.NoError(t, err)
+ require.Len(t, entries, 3)
+ // Compact in place, dropping the first entry
+ copy(entries, entries[1:])
+
+ entries, err = f.List(ctx, "")
+ require.NoError(t, err)
+ var remotes []string
+ for _, entry := range entries {
+ remotes = append(remotes, entry.Remote())
+ }
+ assert.Equal(t, []string{"a.txt", "b.txt", "c.txt"}, remotes)
+}