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.
This commit is contained in:
Anis Elleuch
2021-12-28 17:49:45 -08:00
committed by GitHub
parent e08ded6d28
commit 3391f39521
+15 -1
View File
@@ -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)