2016-08-17 14:36:33 -04:00
|
|
|
/*
|
2017-01-17 13:02:58 -05:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-08-17 14:36:33 -04:00
|
|
|
*
|
|
|
|
* 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-08-17 14:36:33 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2017-11-25 14:58:29 -05:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/errors"
|
2016-08-17 14:36:33 -04:00
|
|
|
)
|
|
|
|
|
2016-10-14 22:57:40 -04:00
|
|
|
func listDirHealFactory(isLeaf isLeafFunc, disks ...StorageAPI) listDirFunc {
|
2016-08-17 14:36:33 -04:00
|
|
|
// Returns sorted merged entries from all the disks.
|
2016-10-14 22:57:40 -04:00
|
|
|
listDir := func(bucket, prefixDir, prefixEntry string) (mergedEntries []string, delayIsLeaf bool, err error) {
|
2016-08-17 14:36:33 -04:00
|
|
|
for _, disk := range disks {
|
2016-10-14 22:57:40 -04:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-08-17 14:36:33 -04:00
|
|
|
var entries []string
|
|
|
|
var newEntries []string
|
|
|
|
entries, err = disk.ListDir(bucket, prefixDir)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-10-14 22:57:40 -04:00
|
|
|
|
2017-03-23 11:24:59 -04:00
|
|
|
// isLeaf() check has to happen here so that
|
|
|
|
// trailing "/" for objects can be removed.
|
2016-08-17 14:36:33 -04:00
|
|
|
for i, entry := range entries {
|
2016-10-14 22:57:40 -04:00
|
|
|
if isLeaf(bucket, pathJoin(prefixDir, entry)) {
|
|
|
|
entries[i] = strings.TrimSuffix(entry, slashSeparator)
|
2016-08-17 14:36:33 -04:00
|
|
|
}
|
|
|
|
}
|
2017-03-23 11:24:59 -04:00
|
|
|
|
|
|
|
// Find elements in entries which are not in mergedEntries
|
2016-08-17 14:36:33 -04:00
|
|
|
for _, entry := range entries {
|
2016-10-14 22:57:40 -04:00
|
|
|
idx := sort.SearchStrings(mergedEntries, entry)
|
2017-03-23 11:24:59 -04:00
|
|
|
// if entry is already present in mergedEntries don't add.
|
|
|
|
if idx < len(mergedEntries) && mergedEntries[idx] == entry {
|
2016-08-17 14:36:33 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
newEntries = append(newEntries, entry)
|
|
|
|
}
|
2017-03-23 11:24:59 -04:00
|
|
|
|
2016-08-17 14:36:33 -04:00
|
|
|
if len(newEntries) > 0 {
|
|
|
|
// Merge the entries and sort it.
|
2016-10-14 22:57:40 -04:00
|
|
|
mergedEntries = append(mergedEntries, newEntries...)
|
|
|
|
sort.Strings(mergedEntries)
|
2016-08-17 14:36:33 -04:00
|
|
|
}
|
2018-01-22 17:54:55 -05:00
|
|
|
|
|
|
|
// Filter entries that have the prefix prefixEntry.
|
|
|
|
mergedEntries = filterMatchingPrefix(mergedEntries, prefixEntry)
|
2016-08-17 14:36:33 -04:00
|
|
|
}
|
2016-10-14 22:57:40 -04:00
|
|
|
return mergedEntries, false, nil
|
2016-08-17 14:36:33 -04:00
|
|
|
}
|
|
|
|
return listDir
|
|
|
|
}
|
|
|
|
|
|
|
|
// listObjectsHeal - wrapper function implemented over file tree walk.
|
2017-06-21 22:53:09 -04:00
|
|
|
func (xl xlObjects) listObjectsHeal(bucket, prefix, marker, delimiter string, maxKeys int) (loi ListObjectsInfo, e error) {
|
2016-08-17 14:36:33 -04:00
|
|
|
// Default is recursive, if delimiter is set then list non recursive.
|
|
|
|
recursive := true
|
|
|
|
if delimiter == slashSeparator {
|
|
|
|
recursive = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// "heal" true for listObjectsHeal() and false for listObjects()
|
|
|
|
heal := true
|
|
|
|
walkResultCh, endWalkCh := xl.listPool.Release(listParams{bucket, recursive, marker, prefix, heal})
|
|
|
|
if walkResultCh == nil {
|
|
|
|
endWalkCh = make(chan struct{})
|
2016-10-14 22:57:40 -04:00
|
|
|
isLeaf := xl.isObject
|
|
|
|
listDir := listDirHealFactory(isLeaf, xl.storageDisks...)
|
2016-08-17 14:36:33 -04:00
|
|
|
walkResultCh = startTreeWalk(bucket, prefix, marker, recursive, listDir, nil, endWalkCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
var objInfos []ObjectInfo
|
|
|
|
var eof bool
|
|
|
|
var nextMarker string
|
|
|
|
for i := 0; i < maxKeys; {
|
|
|
|
walkResult, ok := <-walkResultCh
|
|
|
|
if !ok {
|
|
|
|
// Closed channel.
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// For any walk error return right away.
|
|
|
|
if walkResult.err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return loi, toObjectErr(walkResult.err, bucket, prefix)
|
2016-08-17 14:36:33 -04:00
|
|
|
}
|
|
|
|
entry := walkResult.entry
|
|
|
|
var objInfo ObjectInfo
|
2017-02-16 17:52:14 -05:00
|
|
|
if hasSuffix(entry, slashSeparator) {
|
2016-08-17 14:36:33 -04:00
|
|
|
// Object name needs to be full path.
|
|
|
|
objInfo.Bucket = bucket
|
|
|
|
objInfo.Name = entry
|
|
|
|
objInfo.IsDir = true
|
|
|
|
} else {
|
2017-01-17 13:02:58 -05:00
|
|
|
var err error
|
|
|
|
objInfo, err = xl.getObjectInfo(bucket, entry)
|
|
|
|
if err != nil {
|
|
|
|
// Ignore errFileNotFound
|
2017-11-25 14:58:29 -05:00
|
|
|
if errors.Cause(err) == errFileNotFound {
|
2017-01-17 13:02:58 -05:00
|
|
|
continue
|
|
|
|
}
|
2017-06-21 22:53:09 -04:00
|
|
|
return loi, toObjectErr(err, bucket, prefix)
|
2017-01-17 13:02:58 -05:00
|
|
|
}
|
2016-08-17 14:36:33 -04:00
|
|
|
}
|
|
|
|
nextMarker = objInfo.Name
|
|
|
|
objInfos = append(objInfos, objInfo)
|
|
|
|
i++
|
2017-03-08 13:00:47 -05:00
|
|
|
if walkResult.end {
|
2016-08-17 14:36:33 -04:00
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
params := listParams{bucket, recursive, nextMarker, prefix, heal}
|
|
|
|
if !eof {
|
|
|
|
xl.listPool.Set(params, walkResultCh, endWalkCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := ListObjectsInfo{IsTruncated: !eof}
|
|
|
|
for _, objInfo := range objInfos {
|
|
|
|
result.NextMarker = objInfo.Name
|
|
|
|
if objInfo.IsDir {
|
|
|
|
result.Prefixes = append(result.Prefixes, objInfo.Name)
|
|
|
|
continue
|
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// Add each object seen to the result - objects are
|
|
|
|
// checked for healing later.
|
|
|
|
result.Objects = append(result.Objects, ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: objInfo.Name,
|
|
|
|
ModTime: objInfo.ModTime,
|
|
|
|
Size: objInfo.Size,
|
|
|
|
IsDir: false,
|
|
|
|
})
|
2016-08-17 14:36:33 -04:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjects - list all objects at prefix, delimited by '/'.
|
2017-06-21 22:53:09 -04:00
|
|
|
func (xl xlObjects) ListObjectsHeal(bucket, prefix, marker, delimiter string, maxKeys int) (loi ListObjectsInfo, e error) {
|
2016-12-02 02:15:17 -05:00
|
|
|
if err := checkListObjsArgs(bucket, prefix, marker, delimiter, xl); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return loi, err
|
2016-08-17 14:36:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// With max keys of zero we have reached eof, return right here.
|
|
|
|
if maxKeys == 0 {
|
2017-06-21 22:53:09 -04:00
|
|
|
return loi, nil
|
2016-08-17 14:36:33 -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 {
|
2017-06-21 22:53:09 -04:00
|
|
|
return loi, nil
|
2016-08-17 14:36:33 -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.
|
|
|
|
listObjInfo, err := xl.listObjectsHeal(bucket, prefix, marker, delimiter, maxKeys)
|
|
|
|
if err == nil {
|
|
|
|
// We got the entries successfully return.
|
|
|
|
return listObjInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return error at the end.
|
2017-06-21 22:53:09 -04:00
|
|
|
return loi, toObjectErr(err, bucket, prefix)
|
2016-08-17 14:36:33 -04:00
|
|
|
}
|