mirror: Fix removal of FS target without a trailing slash (#4560)

This commit is contained in:
Anis Eleuch
2023-05-04 11:10:16 -07:00
committed by GitHub
parent b005b9a94d
commit 6c8aedfb71
6 changed files with 152 additions and 24 deletions
+12 -5
View File
@@ -115,39 +115,46 @@ type GenericFileError struct {
Path string
}
// PathNotADirectory - this path does not correspond to a directory
type PathNotADirectory GenericFileError
func (e PathNotADirectory) Error() string {
return "Requested path `" + e.Path + "` not a directory"
}
// PathNotFound (ENOENT) - file not found.
type PathNotFound GenericFileError
func (e PathNotFound) Error() string {
return "Requested file `" + e.Path + "` not found"
return "Requested path `" + e.Path + "` not found"
}
// PathIsNotRegular (ENOTREG) - file is not a regular file.
type PathIsNotRegular GenericFileError
func (e PathIsNotRegular) Error() string {
return "Requested file `" + e.Path + "` is not a regular file."
return "Requested path `" + e.Path + "` is not a regular file."
}
// PathInsufficientPermission (EPERM) - permission denied.
type PathInsufficientPermission GenericFileError
func (e PathInsufficientPermission) Error() string {
return "Insufficient permissions to access this file `" + e.Path + "`"
return "Insufficient permissions to access this path `" + e.Path + "`"
}
// BrokenSymlink (ENOTENT) - file has broken symlink.
type BrokenSymlink GenericFileError
func (e BrokenSymlink) Error() string {
return "Requested file `" + e.Path + "` has broken symlink"
return "Requested path `" + e.Path + "` has broken symlink"
}
// TooManyLevelsSymlink (ELOOP) - file has too many levels of symlinks.
type TooManyLevelsSymlink GenericFileError
func (e TooManyLevelsSymlink) Error() string {
return "Requested file `" + e.Path + "` has too many levels of symlinks"
return "Requested path `" + e.Path + "` has too many levels of symlinks"
}
// EmptyPath (EINVAL) - invalid argument.
+59
View File
@@ -661,6 +661,65 @@ func (f *fsClient) Remove(_ context.Context, isIncomplete, _, _, _ bool, content
return resultCh
}
// ListBuckets returns the list of directories inside a base path
func (f *fsClient) ListBuckets(_ context.Context) ([]*ClientContent, *probe.Error) {
// save pathURL and file path for further usage.
pathURL := *f.PathURL
path := pathURL.Path
st, e := os.Stat(path)
if e != nil {
if os.IsNotExist(e) {
return nil, probe.NewError(PathNotFound{Path: path})
}
return nil, probe.NewError(e)
}
if !st.Mode().IsDir() {
return nil, probe.NewError(PathNotADirectory{Path: path})
}
// List directories (buckets) inside path in a sorted way
files, e := readDir(path)
if e != nil {
return nil, probe.NewError(e)
}
bucketsList := make([]*ClientContent, 0, len(files))
for _, file := range files {
fi := file
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
fp := filepath.Join(path, fi.Name())
fi, e = os.Stat(fp)
if e != nil {
// Ignore all errors on symlinks
continue
}
}
if isIgnoredFile(fi.Name()) {
continue
}
if !fi.Mode().IsDir() {
continue
}
pathURL = *f.PathURL
pathURL.Path = filepath.Join(pathURL.Path, fi.Name())
bucketsList = append(bucketsList, &ClientContent{
URL: pathURL,
Time: fi.ModTime(),
Type: fi.Mode(),
Err: nil,
})
}
return bucketsList, nil
}
// List - list files and folders.
func (f *fsClient) List(_ context.Context, opts ListOptions) <-chan *ClientContent {
contentCh := make(chan *ClientContent, 1)
+13
View File
@@ -1812,6 +1812,19 @@ func (c *S3Client) listVersionsRoutine(ctx context.Context, b, o string, opts Li
}
}
// ListBuckets - list buckets
func (c *S3Client) ListBuckets(ctx context.Context) ([]*ClientContent, *probe.Error) {
buckets, err := c.api.ListBuckets(ctx)
if err != nil {
return nil, probe.NewError(err)
}
bucketsList := make([]*ClientContent, 0, len(buckets))
for _, b := range buckets {
bucketsList = append(bucketsList, c.bucketInfo2ClientContent(b))
}
return bucketsList, nil
}
// List - list at delimited path, if not recursive.
func (c *S3Client) List(ctx context.Context, opts ListOptions) <-chan *ClientContent {
c.Lock()
+1
View File
@@ -106,6 +106,7 @@ type Client interface {
// Bucket operations
MakeBucket(ctx context.Context, region string, ignoreExisting, withLock bool) *probe.Error
RemoveBucket(ctx context.Context, forceRemove bool) *probe.Error
ListBuckets(ctx context.Context) ([]*ClientContent, *probe.Error)
// Object lock config
SetObjectLockConfig(ctx context.Context, mode minio.RetentionMode, validity uint64, unit minio.ValidityUnit) *probe.Error
+66 -18
View File
@@ -162,18 +162,66 @@ func metadataEqual(m1, m2 map[string]string) bool {
}
func objectDifference(ctx context.Context, sourceClnt, targetClnt Client, isMetadata bool) (diffCh chan diffMessage) {
return difference(ctx, sourceClnt, targetClnt, isMetadata, true, false, DirNone)
sourceURL := sourceClnt.GetURL().String()
sourceCh := sourceClnt.List(ctx, ListOptions{Recursive: true, WithMetadata: isMetadata, ShowDir: DirNone})
targetURL := targetClnt.GetURL().String()
targetCh := targetClnt.List(ctx, ListOptions{Recursive: true, WithMetadata: isMetadata, ShowDir: DirNone})
return difference(sourceURL, sourceCh, targetURL, targetCh, isMetadata, false)
}
func dirDifference(ctx context.Context, sourceClnt, targetClnt Client) (diffCh chan diffMessage) {
return difference(ctx, sourceClnt, targetClnt, false, false, true, DirFirst)
func bucketDifference(ctx context.Context, sourceClnt, targetClnt Client) (diffCh chan diffMessage) {
sourceURL := sourceClnt.GetURL().String()
sourceCh := make(chan *ClientContent)
go func() {
defer close(sourceCh)
buckets, err := sourceClnt.ListBuckets(ctx)
if err != nil {
select {
case <-ctx.Done():
case sourceCh <- &ClientContent{Err: err}:
}
return
}
for _, b := range buckets {
select {
case <-ctx.Done():
return
case sourceCh <- b:
}
}
}()
targetURL := targetClnt.GetURL().String()
targetCh := make(chan *ClientContent)
go func() {
defer close(targetCh)
buckets, err := targetClnt.ListBuckets(ctx)
if err != nil {
select {
case <-ctx.Done():
case targetCh <- &ClientContent{Err: err}:
}
return
}
for _, b := range buckets {
select {
case <-ctx.Done():
return
case targetCh <- b:
}
}
}()
return difference(sourceURL, sourceCh, targetURL, targetCh, false, false)
}
func differenceInternal(ctx context.Context, sourceClnt, targetClnt Client, isMetadata bool, isRecursive, returnSimilar bool, dirOpt DirOpt, diffCh chan<- diffMessage) *probe.Error {
// Set default values for listing.
srcCh := sourceClnt.List(ctx, ListOptions{Recursive: isRecursive, WithMetadata: isMetadata, ShowDir: dirOpt})
tgtCh := targetClnt.List(ctx, ListOptions{Recursive: isRecursive, WithMetadata: isMetadata, ShowDir: dirOpt})
func differenceInternal(sourceURL string, srcCh <-chan *ClientContent, targetURL string, tgtCh <-chan *ClientContent,
cmpMetadata, returnSimilar bool, diffCh chan<- diffMessage,
) *probe.Error {
// Pop first entries from the source and targets
srcCtnt, srcOk := <-srcCh
tgtCtnt, tgtOk := <-tgtCh
@@ -189,11 +237,11 @@ func differenceInternal(ctx context.Context, sourceClnt, targetClnt Client, isMe
}
if !srcEOF && srcCtnt.Err != nil {
return srcCtnt.Err.Trace(sourceClnt.GetURL().String(), targetClnt.GetURL().String())
return srcCtnt.Err.Trace(sourceURL, targetURL)
}
if !tgtEOF && tgtCtnt.Err != nil {
return tgtCtnt.Err.Trace(sourceClnt.GetURL().String(), targetClnt.GetURL().String())
return tgtCtnt.Err.Trace(sourceURL, targetURL)
}
// If source doesn't have objects anymore, comparison becomes obvious
@@ -218,11 +266,11 @@ func differenceInternal(ctx context.Context, sourceClnt, targetClnt Client, isMe
continue
}
srcSuffix := strings.TrimPrefix(srcCtnt.URL.String(), sourceClnt.GetURL().String())
tgtSuffix := strings.TrimPrefix(tgtCtnt.URL.String(), targetClnt.GetURL().String())
srcSuffix := strings.TrimPrefix(srcCtnt.URL.String(), sourceURL)
tgtSuffix := strings.TrimPrefix(tgtCtnt.URL.String(), targetURL)
current := urlJoinPath(targetClnt.GetURL().String(), srcSuffix)
expected := urlJoinPath(targetClnt.GetURL().String(), tgtSuffix)
current := urlJoinPath(targetURL, srcSuffix)
expected := urlJoinPath(targetURL, tgtSuffix)
if !utf8.ValidString(srcSuffix) {
// Error. Keys must be valid UTF-8.
@@ -284,7 +332,7 @@ func differenceInternal(ctx context.Context, sourceClnt, targetClnt Client, isMe
firstContent: srcCtnt,
secondContent: tgtCtnt,
}
} else if isMetadata &&
} else if cmpMetadata &&
!metadataEqual(srcCtnt.UserMetadata, tgtCtnt.UserMetadata) &&
!metadataEqual(srcCtnt.Metadata, tgtCtnt.Metadata) {
@@ -327,17 +375,17 @@ func differenceInternal(ctx context.Context, sourceClnt, targetClnt Client, isMe
// objectDifference function finds the difference between all objects
// recursively in sorted order from source and target.
func difference(ctx context.Context, sourceClnt, targetClnt Client, isMetadata bool, isRecursive, returnSimilar bool, dirOpt DirOpt) (diffCh chan diffMessage) {
func difference(sourceURL string, sourceCh <-chan *ClientContent, targetURL string, targetCh <-chan *ClientContent, cmpMetadata, returnSimilar bool) (diffCh chan diffMessage) {
diffCh = make(chan diffMessage, 10000)
go func() {
defer close(diffCh)
err := differenceInternal(ctx, sourceClnt, targetClnt, isMetadata, isRecursive, returnSimilar, dirOpt, diffCh)
err := differenceInternal(sourceURL, sourceCh, targetURL, targetCh, cmpMetadata, returnSimilar, diffCh)
if err != nil {
// handle this specifically for filesystem related errors.
switch v := err.ToGoError().(type) {
case PathNotFound, PathInsufficientPermission:
case PathNotFound, PathInsufficientPermission, PathNotADirectory:
diffCh <- diffMessage{
Error: err,
}
+1 -1
View File
@@ -917,7 +917,7 @@ func runMirror(ctx context.Context, srcURL, dstURL string, cli *cli.Context, enc
if mirrorSrcBuckets || createDstBuckets {
// Synchronize buckets using dirDifference function
for d := range dirDifference(ctx, srcClt, dstClt) {
for d := range bucketDifference(ctx, srcClt, dstClt) {
if d.Error != nil {
if mj.opts.activeActive {
errorIf(d.Error, "Failed to start mirroring.. retrying")