From 43b03a8532ec48e09deee8fa533db4caabd903b1 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 10 Aug 2021 18:29:22 -0700 Subject: [PATCH] support 'mc rm --non-current' to delete non-current objects (#3768) --- cmd/rm-main.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/cmd/rm-main.go b/cmd/rm-main.go index bf0a5308..9ed1ac81 100644 --- a/cmd/rm-main.go +++ b/cmd/rm-main.go @@ -42,6 +42,10 @@ var ( Name: "versions", Usage: "remove object(s) and all its versions", }, + cli.BoolFlag{ + Name: "non-current", + Usage: "remove object(s) versions that are non current (with top-level delete marker)", + }, cli.StringFlag{ Name: "rewind", Usage: "roll back object(s) to current version at specified time", @@ -331,7 +335,7 @@ func removeSingle(url, versionID string, isIncomplete, isFake, isForce, isBypass // Use cases: // * Remove objects recursively // * Remove all versions of a single object -func listAndRemove(url string, timeRef time.Time, withVersions, isRecursive, isIncomplete, isFake, isBypass bool, olderThan, newerThan string, encKeyDB map[string][]prefixSSEPair) error { +func listAndRemove(url string, timeRef time.Time, withVersions, nonCurrentVersion, isRecursive, isIncomplete, isFake, isBypass bool, olderThan, newerThan string, encKeyDB map[string][]prefixSSEPair) error { ctx, cancelRemove := context.WithCancel(globalContext) defer cancelRemove() @@ -353,8 +357,19 @@ func listAndRemove(url string, timeRef time.Time, withVersions, isRecursive, isI listOpts.TimeRef = timeRef } + isNonCurrent := func(contents []*ClientContent) bool { + for i, content := range contents { + if content.IsDeleteMarker && i == 0 { + return true + } + } + return false + } + atLeastOneObjectFound := false + var lastPath string + var perObjectVersions []*ClientContent for content := range clnt.List(ctx, listOpts) { if content.Err != nil { errorIf(content.Err.Trace(url), "Failed to remove `"+url+"` recursively.") @@ -382,6 +397,41 @@ func listAndRemove(url string, timeRef time.Time, withVersions, isRecursive, isI } } + if nonCurrentVersion && isRecursive && withVersions { + if lastPath != content.URL.Path { + lastPath = content.URL.Path + if isNonCurrent(perObjectVersions) { + if isFake { + continue + } + for _, content := range perObjectVersions { + printMsg(rmMessage{ + Key: targetAlias + content.URL.Path, + Size: content.Size, + VersionID: content.VersionID, + ModTime: content.Time, + }) + select { + case contentCh <- content: + case pErr := <-errorCh: + errorIf(pErr.Trace(content.URL.Path), "Failed to remove `"+content.URL.Path+"`.") + switch pErr.ToGoError().(type) { + case PathInsufficientPermission: + // Ignore Permission error. + continue + } + close(contentCh) + return exitStatus(globalErrorExitStatus) + } + } + } + perObjectVersions = []*ClientContent{} + } + atLeastOneObjectFound = true + perObjectVersions = append(perObjectVersions, content) + continue + } + // This will mark that we found at least one target object // even that it could be ineligible for deletion. So we can // inform the user that he was searching in an empty area @@ -429,6 +479,34 @@ func listAndRemove(url string, timeRef time.Time, withVersions, isRecursive, isI } } + if nonCurrentVersion && isRecursive && withVersions { + if isNonCurrent(perObjectVersions) { + if isFake { + return nil + } + for _, content := range perObjectVersions { + printMsg(rmMessage{ + Key: targetAlias + content.URL.Path, + Size: content.Size, + VersionID: content.VersionID, + ModTime: content.Time, + }) + select { + case contentCh <- content: + case pErr := <-errorCh: + errorIf(pErr.Trace(content.URL.Path), "Failed to remove `"+content.URL.Path+"`.") + switch pErr.ToGoError().(type) { + case PathInsufficientPermission: + // Ignore Permission error. + continue + } + close(contentCh) + return exitStatus(globalErrorExitStatus) + } + } + } + } + close(contentCh) for pErr := range errorCh { errorIf(pErr.Trace(url), "Failed to remove `"+url+"` recursively.") @@ -469,6 +547,7 @@ func mainRm(cliCtx *cli.Context) error { olderThan := cliCtx.String("older-than") newerThan := cliCtx.String("newer-than") isForce := cliCtx.Bool("force") + withNoncurrentVersion := cliCtx.Bool("non-current") withVersions := cliCtx.Bool("versions") versionID := cliCtx.String("version-id") rewind := parseRewindFlag(cliCtx.String("rewind")) @@ -485,7 +564,7 @@ func mainRm(cliCtx *cli.Context) error { // Support multiple targets. for _, url := range cliCtx.Args() { if isRecursive || withVersions { - e = listAndRemove(url, rewind, withVersions, isRecursive, isIncomplete, isFake, isBypass, olderThan, newerThan, encKeyDB) + e = listAndRemove(url, rewind, withVersions, withNoncurrentVersion, isRecursive, isIncomplete, isFake, isBypass, olderThan, newerThan, encKeyDB) } else { e = removeSingle(url, versionID, isIncomplete, isFake, isForce, isBypass, olderThan, newerThan, encKeyDB) } @@ -502,7 +581,7 @@ func mainRm(cliCtx *cli.Context) error { for scanner.Scan() { url := scanner.Text() if isRecursive || withVersions { - e = listAndRemove(url, rewind, withVersions, isRecursive, isIncomplete, isFake, isBypass, olderThan, newerThan, encKeyDB) + e = listAndRemove(url, rewind, withVersions, withNoncurrentVersion, isRecursive, isIncomplete, isFake, isBypass, olderThan, newerThan, encKeyDB) } else { e = removeSingle(url, versionID, isIncomplete, isFake, isForce, isBypass, olderThan, newerThan, encKeyDB) }