From 9ff6c146b42c6f0d526aaee4053549e2c73656a2 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Mon, 21 Sep 2020 19:42:31 +0100 Subject: [PATCH] rm: Show deleted error msg when delete marker is found (#3407) HEAD object returns 404 with a delete-marker header when the object is a delete-marker in a versioning bucket. This PR will show a better error message in that case. Use url2Stat() and ignore 400 bad request error. --- cmd/client-errors.go | 8 ++++++++ cmd/client-s3.go | 5 ++++- cmd/rm-main.go | 46 ++++++++++++++++++++++++++++++-------------- go.mod | 2 +- go.sum | 2 ++ 5 files changed, 47 insertions(+), 16 deletions(-) diff --git a/cmd/client-errors.go b/cmd/client-errors.go index 24e10c44..47defedc 100644 --- a/cmd/client-errors.go +++ b/cmd/client-errors.go @@ -168,6 +168,14 @@ func (e ObjectMissing) Error() string { return "Object does not exist" } +// ObjectIsDeleteMarker - object is a delete marker as latest +type ObjectIsDeleteMarker struct { +} + +func (e ObjectIsDeleteMarker) Error() string { + return "Object is marked as deleted" +} + // UnexpectedShortWrite - write wrote less bytes than expected. type UnexpectedShortWrite struct { InputSize int diff --git a/cmd/client-s3.go b/cmd/client-s3.go index c12f75c5..2dd00251 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -1442,6 +1442,7 @@ func (c *S3Client) Stat(ctx context.Context, opts StatOptions) (*ClientContent, // getObjectStat returns the metadata of an object from a HEAD call. func (c *S3Client) getObjectStat(ctx context.Context, bucket, object string, opts minio.StatObjectOptions) (*ClientContent, *probe.Error) { objectStat, e := c.api.StatObject(ctx, bucket, object, opts) + objectMetadata := c.objectInfo2ClientContent(bucket, objectStat) if e != nil { errResponse := minio.ToErrorResponse(e) if errResponse.Code == "AccessDenied" { @@ -1458,11 +1459,13 @@ func (c *S3Client) getObjectStat(ctx context.Context, bucket, object string, opt }) } if errResponse.Code == "NoSuchKey" { + if objectMetadata.IsDeleteMarker { + return nil, probe.NewError(ObjectIsDeleteMarker{}) + } return nil, probe.NewError(ObjectMissing{}) } return nil, probe.NewError(e) } - objectMetadata := c.objectInfo2ClientContent(bucket, objectStat) return objectMetadata, nil } diff --git a/cmd/rm-main.go b/cmd/rm-main.go index 703cbec2..4df0d688 100644 --- a/cmd/rm-main.go +++ b/cmd/rm-main.go @@ -20,6 +20,7 @@ import ( "bufio" "context" "fmt" + "net/http" "os" "path/filepath" "strings" @@ -29,6 +30,7 @@ import ( "github.com/minio/cli" json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/probe" + "github.com/minio/minio-go/v7" "github.com/minio/minio/pkg/console" ) @@ -241,34 +243,49 @@ func remove(url, versionID string, isIncomplete, isFake, isForce, isBypass bool, ctx, cancelRemoveSingle := context.WithCancel(globalContext) defer cancelRemoveSingle() - isRecursive := false - contents, pErr := statURL(ctx, url, versionID, time.Time{}, false, isIncomplete, isRecursive, encKeyDB) + var ( + // A remove request can fail with 400 Bad Request when we STAT an + // object which is SSE-C encrypted, but we still want to continue + // object deletion. + failedWith400 bool + + isDir bool + size int64 + modTime time.Time + ) + + _, content, pErr := url2Stat(ctx, url, versionID, false, encKeyDB, time.Time{}) if pErr != nil { + errResp := minio.ToErrorResponse(pErr.ToGoError()) + if errResp.StatusCode != http.StatusBadRequest { + errorIf(pErr.Trace(url), "Failed to remove `"+url+"`.") + return exitStatus(globalErrorExitStatus) + } + failedWith400 = true + } else { + isDir = content.Type.IsDir() + size = content.Size + modTime = content.Time + } + + if failedWith400 && olderThan != "" || newerThan != "" { errorIf(pErr.Trace(url), "Failed to remove `"+url+"`.") return exitStatus(globalErrorExitStatus) } - if len(contents) == 0 { - if !isForce { - errorIf(errDummy().Trace(url), "Failed to remove `"+url+"`. Target object is not found") - return exitStatus(globalErrorExitStatus) - } - return nil - } - content := contents[0] // Skip objects older than older--than parameter if specified - if olderThan != "" && isOlder(content.Time, olderThan) { + if olderThan != "" && isOlder(modTime, olderThan) { return nil } // Skip objects older than older--than parameter if specified - if newerThan != "" && isNewer(content.Time, newerThan) { + if newerThan != "" && isNewer(modTime, newerThan) { return nil } printMsg(rmMessage{ Key: url, - Size: content.Size, + Size: size, VersionID: versionID, }) @@ -279,7 +296,8 @@ func remove(url, versionID string, isIncomplete, isFake, isForce, isBypass bool, errorIf(pErr.Trace(url), "Invalid argument `"+url+"`.") return exitStatus(globalErrorExitStatus) // End of journey. } - if !strings.HasSuffix(targetURL, string(clnt.GetURL().Separator)) && content.Type.IsDir() { + + if !strings.HasSuffix(targetURL, string(clnt.GetURL().Separator)) && isDir { targetURL = targetURL + string(clnt.GetURL().Separator) } diff --git a/go.mod b/go.mod index ee4044f0..92d3e663 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/mattn/go-runewidth v0.0.9 // indirect github.com/minio/cli v1.22.0 github.com/minio/minio v0.0.0-20200917180950-00555c747ec5 - github.com/minio/minio-go/v7 v7.0.6-0.20200901014009-5f8d15bbc5fd + github.com/minio/minio-go/v7 v7.0.6-0.20200918223035-9cefc0929b69 github.com/minio/sha256-simd v0.1.1 github.com/mitchellh/go-homedir v1.1.0 github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect diff --git a/go.sum b/go.sum index 4f36061c..a8ea1bbb 100644 --- a/go.sum +++ b/go.sum @@ -296,6 +296,8 @@ github.com/minio/minio v0.0.0-20200917180950-00555c747ec5/go.mod h1:dRmX9jR9niWr github.com/minio/minio-go/v7 v7.0.5-0.20200811211821-14ed05478889/go.mod h1:CSt2ETZNs+bIIhWTse0mcZKZWMGrFU7Er7RR0TmkDYk= github.com/minio/minio-go/v7 v7.0.6-0.20200901014009-5f8d15bbc5fd h1:38hrGmWnv+C+yBcKgAWipiwRDu7Exr0kKAfy1QArW0g= github.com/minio/minio-go/v7 v7.0.6-0.20200901014009-5f8d15bbc5fd/go.mod h1:CSt2ETZNs+bIIhWTse0mcZKZWMGrFU7Er7RR0TmkDYk= +github.com/minio/minio-go/v7 v7.0.6-0.20200918223035-9cefc0929b69 h1:/FOBaOo5vVZrw0QRq1mYrIoQ1tiIi6PLCtYmdb68B0Q= +github.com/minio/minio-go/v7 v7.0.6-0.20200918223035-9cefc0929b69/go.mod h1:CSt2ETZNs+bIIhWTse0mcZKZWMGrFU7Er7RR0TmkDYk= github.com/minio/selfupdate v0.3.1 h1:BWEFSNnrZVMUWXbXIgLDNDjbejkmpAmZvy/nCz1HlEs= github.com/minio/selfupdate v0.3.1/go.mod h1:b8ThJzzH7u2MkF6PcIra7KaXO9Khf6alWPvMSyTDCFM= github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU=