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

serve s3: fix CopyObject metadata including the source's and an mtime

Nick Craig-Wood85a0617d215e
diff --git a/cmd/serve/s3/backend.go b/cmd/serve/s3/backend.go
index cdd44d98e..73b37836e 100644
--- a/cmd/serve/s3/backend.go
+++ b/cmd/serve/s3/backend.go
@@ -166,28 +166,23 @@ func (t *tenant) getMeta(ctx context.Context, fp string, node vfs.Node) (meta ma
// storeMeta stores meta as the metadata of the object at fp in _vfs.
//
-// If meta has a modification time in "X-Amz-Meta-Mtime" or "mtime" the
-// object's modification time is set to it, returning any error, and
-// both are set to it in meta.
+// If meta has a modification time in the "X-Amz-Meta-Mtime" user
+// metadata, or failing that in "mtime", the object's modification time
+// is set to it, returning any error. "mtime" isn't user metadata so it
+// is removed from meta.
//
// Any "Last-Modified" in meta is dropped: gofakes3 sets it to the time
// of the request, and caching it would make HeadObject and GetObject
// report that instead of the object's modification time.
func (t *tenant) storeMeta(_vfs *vfs.VFS, fp string, meta map[string]string) (err error) {
delete(meta, "Last-Modified")
- for _, key := range []string{"X-Amz-Meta-Mtime", "mtime"} {
- val, ok := meta[key]
- if !ok {
- continue
- }
- ti, parseErr := swift.FloatStringToTime(val)
- if parseErr != nil {
- continue
- }
- meta["X-Amz-Meta-Mtime"] = val
- meta["mtime"] = val
+ mtime, ok := meta["X-Amz-Meta-Mtime"]
+ if !ok {
+ mtime = meta["mtime"]
+ }
+ delete(meta, "mtime")
+ if ti, parseErr := swift.FloatStringToTime(mtime); mtime != "" && parseErr == nil {
err = _vfs.Chtimes(fp, ti, ti)
- break
}
// Stat after setting the modification time as getMeta compares with it
node, statErr := _vfs.Stat(fp)
@@ -678,12 +673,9 @@ func (b *s3Backend) CopyObject(ctx context.Context, srcBucket, srcKey, dstBucket
_ = c.Contents.Close()
}()
- for k, v := range c.Metadata {
- if _, found := meta[k]; !found && k != "X-Amz-Acl" {
- meta[k] = v
- }
- }
- if _, ok := meta["mtime"]; !ok {
+ // meta is the complete metadata of the copy. It keeps the source's
+ // modification time unless the metadata sets one.
+ if _, ok := meta["X-Amz-Meta-Mtime"]; !ok {
meta["mtime"] = swift.TimeToFloatString(cStat.ModTime())
}
diff --git a/cmd/serve/s3/backend_test.go b/cmd/serve/s3/backend_test.go
index 54bef0f52..a81724bdd 100644
--- a/cmd/serve/s3/backend_test.go
+++ b/cmd/serve/s3/backend_test.go
@@ -307,3 +307,32 @@ func TestMetadataStale(t *testing.T) {
require.NoError(t, os.Chtimes(objPath, later, later))
assert.Equal(t, "", colour(ctxA), "metadata kept after the object was changed on the backend")
}
+
+// TestCopyObjectMetadata checks a copy gets exactly the metadata given,
+// as worked out by gofakes3 from the metadata directive, and keeps the
+// source's modification time without adding it to the metadata.
+func TestCopyObjectMetadata(t *testing.T) {
+ b, root := newTestBackend(t)
+ ctx := context.Background()
+ srcTime := time.Date(2011, 12, 25, 12, 59, 59, 0, time.UTC)
+ require.NoError(t, os.Chtimes(filepath.Join(root, "bucket", "object.txt"), srcTime, srcTime))
+
+ _, err := b.CopyObject(ctx, "bucket", "object.txt", "bucket", "copy.txt", map[string]string{"X-Amz-Meta-Colour": "red"})
+ require.NoError(t, err)
+ obj, err := b.HeadObject(ctx, "bucket", "copy.txt")
+ require.NoError(t, err)
+ assert.Equal(t, "red", obj.Metadata["X-Amz-Meta-Colour"])
+ assert.NotContains(t, obj.Metadata, "X-Amz-Meta-Mtime")
+ assert.NotContains(t, obj.Metadata, "mtime")
+ info, err := os.Stat(filepath.Join(root, "bucket", "copy.txt"))
+ require.NoError(t, err)
+ assert.True(t, srcTime.Equal(info.ModTime()), "modification time %v", info.ModTime())
+
+ // Copying again with other metadata replaces it all
+ _, err = b.CopyObject(ctx, "bucket", "copy.txt", "bucket", "copy2.txt", map[string]string{"X-Amz-Meta-Size": "big"})
+ require.NoError(t, err)
+ obj, err = b.HeadObject(ctx, "bucket", "copy2.txt")
+ require.NoError(t, err)
+ assert.Equal(t, "big", obj.Metadata["X-Amz-Meta-Size"])
+ assert.NotContains(t, obj.Metadata, "X-Amz-Meta-Colour")
+}