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

compress: fix corrupted objects being created when the source ends early

Nick Craig-Wood18fa445ffcad
diff --git a/backend/compress/compress.go b/backend/compress/compress.go
index 961d3898f..904954ca5 100644
--- a/backend/compress/compress.go
+++ b/backend/compress/compress.go
@@ -698,6 +698,16 @@ func (f *Fs) putWithCustomFunctions(ctx context.Context, in io.Reader, src fs.Ob
return nil, err
}
+ // Check the source supplied the number of bytes it declared -
+ // compressed data is stored under a name containing the
+ // original size so a mismatch makes the object unreadable.
+ if size := src.Size(); size >= 0 && meta.Size != size {
+ if removeErr := dataObject.Remove(ctx); removeErr != nil {
+ fs.Errorf(dataObject, "Failed to remove partially transferred object: %v", removeErr)
+ }
+ return nil, fmt.Errorf("expected %d bytes in input, but got %d: %w", size, meta.Size, io.ErrUnexpectedEOF)
+ }
+
mo, err := f.putMetadata(ctx, meta, src, options, putMeta)
// meta data upload may fail. in this case we try to remove the original object