2016-05-30 19:51:59 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
2017-01-26 18:39:22 -05:00
|
|
|
// Returns function "listDir" of the type listDirFunc.
|
|
|
|
// isLeaf - is used by listDir function to check if an entry is a leaf or non-leaf entry.
|
|
|
|
// disks - used for doing disk.ListDir(). FS passes single disk argument, XL passes a list of disks.
|
|
|
|
func listDirFactory(isLeaf isLeafFunc, treeWalkIgnoredErrs []error, disks ...StorageAPI) listDirFunc {
|
|
|
|
// listDir - lists all the entries at a given prefix and given entry in the prefix.
|
|
|
|
listDir := func(bucket, prefixDir, prefixEntry string) (entries []string, delayIsLeaf bool, err error) {
|
|
|
|
for _, disk := range disks {
|
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
entries, err = disk.ListDir(bucket, prefixDir)
|
|
|
|
if err == nil {
|
|
|
|
entries, delayIsLeaf = filterListEntries(bucket, prefixDir, entries, prefixEntry, isLeaf)
|
|
|
|
return entries, delayIsLeaf, nil
|
|
|
|
}
|
|
|
|
// For any reason disk was deleted or goes offline, continue
|
|
|
|
// and list from other disks if possible.
|
|
|
|
if isErrIgnored(err, treeWalkIgnoredErrs...) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Return error at the end.
|
|
|
|
return nil, false, traceError(err)
|
|
|
|
}
|
|
|
|
return listDir
|
|
|
|
}
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// listObjects - wrapper function implemented over file tree walk.
|
|
|
|
func (xl xlObjects) listObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, error) {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Default is recursive, if delimiter is set then list non recursive.
|
|
|
|
recursive := true
|
|
|
|
if delimiter == slashSeparator {
|
|
|
|
recursive = false
|
|
|
|
}
|
|
|
|
|
2016-08-17 14:36:33 -04:00
|
|
|
heal := false // true only for xl.ListObjectsHeal
|
|
|
|
walkResultCh, endWalkCh := xl.listPool.Release(listParams{bucket, recursive, marker, prefix, heal})
|
2016-06-05 14:55:45 -04:00
|
|
|
if walkResultCh == nil {
|
|
|
|
endWalkCh = make(chan struct{})
|
2016-07-17 18:16:52 -04:00
|
|
|
isLeaf := xl.isObject
|
2016-09-15 16:43:40 -04:00
|
|
|
listDir := listDirFactory(isLeaf, xlTreeWalkIgnoredErrs, xl.getLoadBalancedDisks()...)
|
2016-07-17 18:16:52 -04:00
|
|
|
walkResultCh = startTreeWalk(bucket, prefix, marker, recursive, listDir, isLeaf, endWalkCh)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-30 00:05:00 -04:00
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
var objInfos []ObjectInfo
|
|
|
|
var eof bool
|
|
|
|
var nextMarker string
|
|
|
|
for i := 0; i < maxKeys; {
|
2016-06-05 14:55:45 -04:00
|
|
|
walkResult, ok := <-walkResultCh
|
2016-05-20 23:48:47 -04:00
|
|
|
if !ok {
|
|
|
|
// Closed channel.
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// For any walk error return right away.
|
|
|
|
if walkResult.err != nil {
|
|
|
|
// File not found is a valid case.
|
2016-08-25 12:39:01 -04:00
|
|
|
if errorCause(walkResult.err) == errFileNotFound {
|
2016-05-20 23:48:47 -04:00
|
|
|
return ListObjectsInfo{}, nil
|
|
|
|
}
|
|
|
|
return ListObjectsInfo{}, toObjectErr(walkResult.err, bucket, prefix)
|
|
|
|
}
|
2016-05-25 12:22:39 -04:00
|
|
|
entry := walkResult.entry
|
|
|
|
var objInfo ObjectInfo
|
|
|
|
if strings.HasSuffix(entry, slashSeparator) {
|
|
|
|
// Object name needs to be full path.
|
|
|
|
objInfo.Bucket = bucket
|
|
|
|
objInfo.Name = entry
|
|
|
|
objInfo.IsDir = true
|
|
|
|
} else {
|
|
|
|
// Set the Mode to a "regular" file.
|
|
|
|
var err error
|
|
|
|
objInfo, err = xl.getObjectInfo(bucket, entry)
|
|
|
|
if err != nil {
|
2016-06-27 01:10:22 -04:00
|
|
|
// Ignore errFileNotFound
|
2016-08-25 12:39:01 -04:00
|
|
|
if errorCause(err) == errFileNotFound {
|
2016-06-27 01:10:22 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-05-25 12:22:39 -04:00
|
|
|
return ListObjectsInfo{}, toObjectErr(err, bucket, prefix)
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
nextMarker = objInfo.Name
|
|
|
|
objInfos = append(objInfos, objInfo)
|
2016-05-30 00:05:00 -04:00
|
|
|
i++
|
2016-08-16 10:57:14 -04:00
|
|
|
if walkResult.end {
|
2016-05-20 23:48:47 -04:00
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-05-30 00:05:00 -04:00
|
|
|
|
2016-08-17 14:36:33 -04:00
|
|
|
params := listParams{bucket, recursive, nextMarker, prefix, heal}
|
2016-05-20 23:48:47 -04:00
|
|
|
if !eof {
|
2016-06-05 14:55:45 -04:00
|
|
|
xl.listPool.Set(params, walkResultCh, endWalkCh)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
result := ListObjectsInfo{IsTruncated: !eof}
|
|
|
|
for _, objInfo := range objInfos {
|
2016-06-01 01:10:55 -04:00
|
|
|
result.NextMarker = objInfo.Name
|
|
|
|
if objInfo.IsDir {
|
|
|
|
result.Prefixes = append(result.Prefixes, objInfo.Name)
|
|
|
|
continue
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-11-13 14:48:02 -05:00
|
|
|
result.Objects = append(result.Objects, objInfo)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjects - list all objects at prefix, delimited by '/'.
|
|
|
|
func (xl xlObjects) ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, error) {
|
2016-12-02 02:15:17 -05:00
|
|
|
if err := checkListObjsArgs(bucket, prefix, marker, delimiter, xl); err != nil {
|
|
|
|
return ListObjectsInfo{}, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// With max keys of zero we have reached eof, return right here.
|
|
|
|
if maxKeys == 0 {
|
|
|
|
return ListObjectsInfo{}, nil
|
|
|
|
}
|
|
|
|
|
2016-05-27 18:43:51 -04:00
|
|
|
// For delimiter and prefix as '/' we do not list anything at all
|
|
|
|
// since according to s3 spec we stop at the 'delimiter' along
|
|
|
|
// with the prefix. On a flat namespace with 'prefix' as '/'
|
|
|
|
// we don't have any entries, since all the keys are of form 'keyName/...'
|
|
|
|
if delimiter == slashSeparator && prefix == slashSeparator {
|
|
|
|
return ListObjectsInfo{}, nil
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Over flowing count - reset to maxObjectList.
|
|
|
|
if maxKeys < 0 || maxKeys > maxObjectList {
|
|
|
|
maxKeys = maxObjectList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initiate a list operation, if successful filter and return quickly.
|
2016-05-30 19:51:59 -04:00
|
|
|
listObjInfo, err := xl.listObjects(bucket, prefix, marker, delimiter, maxKeys)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err == nil {
|
|
|
|
// We got the entries successfully return.
|
|
|
|
return listObjInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return error at the end.
|
|
|
|
return ListObjectsInfo{}, toObjectErr(err, bucket, prefix)
|
|
|
|
}
|