From 5386533bb912918ed4a045154072bda15cdf8b65 Mon Sep 17 00:00:00 2001 From: Shubhendu Date: Sat, 30 Dec 2023 01:45:29 +0530 Subject: [PATCH] Use correct target path during mirroring objects (#4807) Currently if we don't use absolute paths in mirror command, absolute path of source gets appended to the target path and so in destination object gets mirrored to wrong prefix / path. For example say source object path is `/tmp/source/prefix/a.txt` and mirror command executed from `/tmp` dir as `mc mirror --json --watch source xyz`, the object in target gets mirrored to location `xyz/tmp/source/prefix/a.txt`. Also any new file created gets copied properly though. ``` mc mirror --json --watch source/ xyz/ { "status": "success", "source": "/tmp/source/prefix/a.txt", "target": "xyz/tmp/source/prefix/a.txt", "size": 0, "totalCount": 1, "totalSize": 0 } { "status": "success", "source": "/tmp/source/b.txt", "target": "xyz/b.txt", "size": 0, "totalCount": 2, "totalSize": 0 } ``` With the fix now after running the command `mc mirror --json --watch source xyz` the object would correctly get copied to location `/tmp/xyz/prefix/a.txt` as below ``` { "status": "success", "source": "/tmp/source/prefix/a.txt", "target": "xyz/prefix/a.txt", "size": 0, "totalCount": 2, "totalSize": 0 } { "status": "success", "source": "/tmp/source/b.txt", "target": "xyz/b.txt", "size": 0, "totalCount": 1, "totalSize": 0 } { "status": "success", "source": "/tmp/source/c.txt", "target": "xyz/c.txt", "size": 0, "totalCount": 3, "totalSize": 0 } ``` Signed-off-by: Shubhendu Ram Tripathi --- cmd/mirror-url.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/mirror-url.go b/cmd/mirror-url.go index db4348fe..d1f02b13 100644 --- a/cmd/mirror-url.go +++ b/cmd/mirror-url.go @@ -127,6 +127,15 @@ func deltaSourceTarget(ctx context.Context, sourceURL, targetURL string, opts mi return } + // If the passed source URL points to fs, fetch the absolute src path + // to correctly calculate targetPath + if sourceAlias == "" { + tmpSrcURL, e := filepath.Abs(sourceURL) + if e == nil { + sourceURL = tmpSrcURL + } + } + // List both source and target, compare and return values through channel. for diffMsg := range objectDifference(ctx, sourceClnt, targetClnt, opts.isMetadata) { if diffMsg.Error != nil {