From 3391f39521d9432fc9a26eb3ebe74103d7525faf Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Wed, 29 Dec 2021 02:49:45 +0100 Subject: [PATCH] mirror: Sort bucket names with slash for site wide scenarios (#3894) mc diff/mirror command fails to compare source & target when doing an S3 site-wide listing. The reason is that client-s3.List(), when doing a site-wide listing, lists buckets first and list objects inside each bucket with the order returned by S3 ListBuckets API. However this can produce a non properly sorted listing output, e.g.: ``` $ mc ls -r myminio/ [2021-12-14 15:58:27 CET] 227KiB bucketname/object1 [2021-12-14 15:58:24 CET] 227KiB bucketname-0/object2 ``` Though it should be ``` $ mc ls -r myminio/ [2021-12-14 15:58:24 CET] 227KiB bucketname-0/object2 [2021-12-14 15:58:27 CET] 227KiB bucketname/object1 ``` because '/' has a higher ASCII value than '-' This commit fixes the behavior by always sorting bucket names with a fake trailing slash when doing a site-wide recursive listing. --- cmd/client-s3.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 0dd48472..b7457749 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -33,6 +33,7 @@ import ( "path" "path/filepath" "regexp" + "sort" "strings" "sync" "time" @@ -1758,7 +1759,9 @@ func (c *S3Client) versionedList(ctx context.Context, contentCh chan *ClientCont } return } - + if opts.Recursive { + sortBucketsNameWithSlash(buckets) + } for _, bucket := range buckets { if opts.ShowDir != DirLast { contentCh <- c.bucketInfo2ClientContent(bucket) @@ -1905,6 +1908,7 @@ func (c *S3Client) listIncompleteRecursiveInRoutine(ctx context.Context, content } return } + sortBucketsNameWithSlash(buckets) isRecursive := true for _, bucket := range buckets { if opts.ShowDir != DirLast { @@ -2099,6 +2103,15 @@ const ( s3StorageClassGlacier = "GLACIER" ) +// Sorting buckets name with an additional '/' to make sure that a +// site-wide listing returns sorted output. This is crucial for +// correct diff/mirror calculation. +func sortBucketsNameWithSlash(bucketsInfo []minio.BucketInfo) { + sort.Slice(bucketsInfo, func(i, j int) bool { + return bucketsInfo[i].Name+"/" < bucketsInfo[j].Name+"/" + }) +} + func (c *S3Client) listRecursiveInRoutine(ctx context.Context, contentCh chan *ClientContent, opts ListOptions) { // get bucket and object from URL. b, o := c.url2BucketAndObject() @@ -2111,6 +2124,7 @@ func (c *S3Client) listRecursiveInRoutine(ctx context.Context, contentCh chan *C } return } + sortBucketsNameWithSlash(buckets) for _, bucket := range buckets { if opts.ShowDir == DirFirst { contentCh <- c.bucketInfo2ClientContent(bucket)