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 crash when creating archive to stdout - fixes #9910

Vladimir Babin3342e34f583d
diff --git a/cmd/archive/archive_test.go b/cmd/archive/archive_test.go
index edef3b880..ba3166c8c 100644
--- a/cmd/archive/archive_test.go
+++ b/cmd/archive/archive_test.go
@@ -4,6 +4,8 @@ import (
"archive/zip"
"bytes"
"context"
+ "os"
+ "path/filepath"
"strings"
"testing"
@@ -134,6 +136,47 @@ func testArchiveRemote(t *testing.T, fromLocal bool, subDir string, extension st
fstest.CheckListingWithPrecision(t, src, items, nil, fs.ModTimeNotSupported)
}
+// test archiving to stdout when no destination is given
+func TestArchiveStdout(t *testing.T) {
+ ctx := context.Background()
+ r := fstest.NewRun(t)
+
+ f1 := r.WriteObject(ctx, "file1.txt", "content 1", t1)
+ f2 := r.WriteObject(ctx, "dir1/sub1.txt", "sub content 1", t1)
+ fstest.CheckItems(t, r.Fremote, f1, f2)
+
+ // redirect stdout into a file on the local fs
+ archiveName := "stdout.zip"
+ require.NoError(t, r.Flocal.Mkdir(ctx, ""))
+ out, err := os.Create(filepath.Join(r.LocalName, archiveName))
+ require.NoError(t, err)
+ oldStdout := os.Stdout
+ os.Stdout = out
+ err = create.ArchiveCreate(ctx, nil, "", r.Fremote, "zip", "")
+ os.Stdout = oldStdout
+ require.NoError(t, out.Close())
+ require.NoError(t, err)
+
+ // check the archive written to stdout is readable
+ expected := map[string]bool{
+ "file1.txt": true,
+ "dir1/": true,
+ "dir1/sub1.txt": true,
+ }
+ listFile := func(ctx context.Context, f archives.FileInfo) error {
+ name := f.NameInArchive
+ if f.IsDir() && !strings.HasSuffix(name, "/") {
+ name += "/"
+ }
+ assert.True(t, expected[name], name)
+ delete(expected, name)
+ return nil
+ }
+ err = list.ArchiveList(ctx, r.Flocal, archiveName, listFile)
+ require.NoError(t, err)
+ assert.Equal(t, 0, len(expected), expected)
+}
+
func testArchive(t *testing.T) {
var extensions = []string{
"zip",
diff --git a/cmd/archive/create/create.go b/cmd/archive/create/create.go
index c982951d3..51a096193 100644
--- a/cmd/archive/create/create.go
+++ b/cmd/archive/create/create.go
@@ -118,7 +118,6 @@ will make an archive with the contents:
cmd.CheckArgs(1, 2, command, args)
}
cmd.Run(false, false, command, func() error {
- fmt.Printf("dst=%v, dstFile=%q, src=%v, format=%q, prefix=%q\n", dst, dstFile, src, format, prefix)
if prefix != "" {
return ArchiveCreate(context.Background(), dst, dstFile, src, format, prefix)
} else if fullPath {
@@ -301,10 +300,12 @@ func ArchiveCreate(ctx context.Context, dst fs.Fs, dstFile string, src fs.Fs, fo
var compArchive archives.CompressedArchive
var totalLength int64
- // check id dst is valid
- err = CheckValidDestination(ctx, dst, dstFile)
- if err != nil {
- return err
+ // check if dst is valid, nil dst means write to stdout
+ if dst != nil {
+ err = CheckValidDestination(ctx, dst, dstFile)
+ if err != nil {
+ return err
+ }
}
ci := fs.GetConfig(ctx)