Add support of empty directories in ls/rm/find commands (#2452)

This commit is contained in:
Anis Elleuch
2018-06-04 17:20:07 -07:00
committed by Dee Koder
parent 4f1f864f64
commit c0b89a7aec
3 changed files with 45 additions and 42 deletions
+15 -18
View File
@@ -719,14 +719,6 @@ func (c *s3Client) Remove(isIncomplete bool, contentCh <-chan *clientContent) <-
for content := range contentCh {
// Convert content.URL.Path to objectName for objectsCh.
bucket, objectName := c.splitPath(content.URL.Path)
// Currently only supported hosts with virtual style
// are Amazon S3 and Google Cloud Storage.
// which also support objects with "/" as delimiter.
// Skip trimming "/" and let the server reply error
// if any.
if !c.virtualStyle {
objectName = strings.TrimSuffix(objectName, string(c.targetURL.Separator))
}
// Init cContentCh channel and objectsCh the first time.
if prevBucket == "" {
cContentCh = make(chan *clientContent)
@@ -931,16 +923,23 @@ func (c *s3Client) Stat(isIncomplete, isFetchMeta bool, sseKey string) (*clientC
return &bucketMetadata, nil
}
// Remove trailing slashes needed for the following ListObjects call.
// In addition, Stat() will be as smart as the client fs version and will
// facilitate the work of the upper layers
object = strings.TrimRight(object, string(c.targetURL.Separator))
// The following code tries to calculate if a given prefix/object does really exist
// using minio-go listing API. The following inputs are supported:
// - /path/to/existing/object
// - /path/to/existing_directory
// - /path/to/existing_directory/
// - /path/to/empty_directory
// - /path/to/empty_directory/
nonRecursive := false
objectMetadata := &clientContent{}
// Prefix to pass to minio-go listing in order to fetch a given object/directory
prefix := strings.TrimRight(object, string(c.targetURL.Separator))
// If the request is for incomplete upload stat, handle it here.
if isIncomplete {
for objectMultipartInfo := range c.api.ListIncompleteUploads(bucket, object, nonRecursive, nil) {
for objectMultipartInfo := range c.api.ListIncompleteUploads(bucket, prefix, nonRecursive, nil) {
if objectMultipartInfo.Err != nil {
return nil, probe.NewError(objectMultipartInfo.Err)
}
@@ -966,12 +965,14 @@ func (c *s3Client) Stat(isIncomplete, isFetchMeta bool, sseKey string) (*clientC
}
return nil, probe.NewError(ObjectMissing{})
}
opts := minio.StatObjectOptions{}
if sseKey != "" {
key, _ := encrypt.NewSSEC([]byte(sseKey))
opts.ServerSideEncryption = key
}
for objectStat := range c.listObjectWrapper(bucket, object, nonRecursive, nil) {
for objectStat := range c.listObjectWrapper(bucket, prefix, nonRecursive, nil) {
if objectStat.Err != nil {
return nil, probe.NewError(objectStat.Err)
}
@@ -1721,10 +1722,6 @@ func (c *s3Client) listRecursiveInRoutine(contentCh chan *clientContent) {
}
continue
}
// Ignore S3 empty directories
if object.Size == 0 && strings.HasSuffix(object.Key, "/") {
continue
}
content := &clientContent{}
url := *c.targetURL
// Join bucket and incoming object key.
+23 -11
View File
@@ -223,13 +223,6 @@ func getAliasedPath(ctx *findContext, path string) string {
}
func find(ctx *findContext, fileContent contentMessage) {
// Maxdepth can modify the filepath to end as a directory prefix
// to be consistent with the find behavior, we wont list directories
// so any paths which end with a separator are ignored.
if strings.HasSuffix(fileContent.Key, string(ctx.clnt.GetURL().Separator)) {
return
}
// Match the incoming content, didn't match return.
if !matchFind(ctx, fileContent) {
return
@@ -254,6 +247,8 @@ func doFind(ctx *findContext) error {
// following defer is a no-op.
defer watchFind(ctx)
var prevKeyName string
// iterate over all content which is within the given directory
for content := range ctx.clnt.List(true, false, DirNone) {
if content.Err != nil {
@@ -279,13 +274,30 @@ func doFind(ctx *findContext) error {
continue
}
// Executes all the find functionalities.
find(ctx, contentMessage{
Key: getAliasedPath(ctx, content.URL.String()),
fileKeyName := getAliasedPath(ctx, content.URL.String())
fileContent := contentMessage{
Key: fileKeyName,
Time: content.Time.Local(),
Size: content.Size,
})
}
// Match the incoming content, didn't match return.
if !matchFind(ctx, fileContent) || prevKeyName == fileKeyName {
continue
} // For all matching content
prevKeyName = fileKeyName
// proceed to either exec, format the output string.
if ctx.execCmd != "" {
execFind(stringsReplace(ctx.execCmd, fileContent))
continue
}
if ctx.printFmt != "" {
fileContent.Key = stringsReplace(ctx.printFmt, fileContent)
}
printMsg(findMessage{fileContent})
}
// Success, notice watch will execute in defer only if enabled and this call
+7 -13
View File
@@ -90,22 +90,16 @@ func parseContent(c *clientContent) contentMessage {
// get content key
func getKey(c *clientContent) string {
switch {
sep := "/"
// for windows make sure to print in 'windows' specific style.
case runtime.GOOS == "windows":
if runtime.GOOS == "windows" {
c.URL.Path = strings.Replace(c.URL.Path, "/", "\\", -1)
c.URL.Path = strings.TrimSuffix(c.URL.Path, "\\")
default:
c.URL.Path = strings.TrimSuffix(c.URL.Path, "/")
sep = "\\"
}
if c.Type.IsDir() {
switch {
// for windows make sure to print in 'windows' specific style.
case runtime.GOOS == "windows":
return fmt.Sprintf("%s\\", c.URL.Path)
default:
return fmt.Sprintf("%s/", c.URL.Path)
}
if c.Type.IsDir() && !strings.HasSuffix(c.URL.Path, sep) {
return fmt.Sprintf("%s%s", c.URL.Path, sep)
}
return c.URL.Path
}