2016-05-05 15:51:56 -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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// listParams - list object params used for list object map
|
|
|
|
type listParams struct {
|
|
|
|
bucket string
|
|
|
|
recursive bool
|
|
|
|
marker string
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tree walk result carries results of tree walking.
|
|
|
|
type treeWalkResult struct {
|
2016-05-25 12:22:39 -04:00
|
|
|
entry string
|
|
|
|
err error
|
|
|
|
end bool
|
2016-05-05 15:51:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tree walk notify carries a channel which notifies tree walk
|
|
|
|
// results, additionally it also carries information if treeWalk
|
|
|
|
// should be timedOut.
|
|
|
|
type treeWalker struct {
|
|
|
|
ch <-chan treeWalkResult
|
|
|
|
timedOut bool
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// listDir - listDir.
|
2016-05-25 12:22:39 -04:00
|
|
|
func (xl xlObjects) listDir(bucket, prefixDir string, filter func(entry string) bool, isLeaf func(string, string) bool) (entries []string, err error) {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Count for list errors encountered.
|
|
|
|
var listErrCount = 0
|
|
|
|
|
2016-05-24 20:48:58 -04:00
|
|
|
// Return the first success entry based on the selected random disk.
|
2016-05-20 23:48:47 -04:00
|
|
|
for listErrCount < len(xl.storageDisks) {
|
2016-05-26 06:15:01 -04:00
|
|
|
disk := xl.getRandomDisk() // Choose a random disk on each attempt.
|
2016-05-20 23:48:47 -04:00
|
|
|
if entries, err = disk.ListDir(bucket, prefixDir); err == nil {
|
|
|
|
// Skip the entries which do not match the filter.
|
|
|
|
for i, entry := range entries {
|
|
|
|
if filter(entry) {
|
|
|
|
entries[i] = ""
|
|
|
|
continue
|
|
|
|
}
|
2016-05-25 12:22:39 -04:00
|
|
|
if strings.HasSuffix(entry, slashSeparator) && isLeaf(bucket, pathJoin(prefixDir, entry)) {
|
2016-05-20 23:48:47 -04:00
|
|
|
entries[i] = strings.TrimSuffix(entry, slashSeparator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(entries)
|
|
|
|
// Skip the empty strings
|
|
|
|
for len(entries) > 0 && entries[0] == "" {
|
|
|
|
entries = entries[1:]
|
|
|
|
}
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
listErrCount++ // Update list error count.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return error at the end.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// treeWalk walks directory tree recursively pushing fileInfo into the channel as and when it encounters files.
|
|
|
|
func (xl xlObjects) treeWalk(bucket, prefixDir, entryPrefixMatch, marker string, recursive bool, send func(treeWalkResult) bool, count *int, isLeaf func(string, string) bool) bool {
|
2016-05-05 15:51:56 -04:00
|
|
|
// Example:
|
|
|
|
// if prefixDir="one/two/three/" and marker="four/five.txt" treeWalk is recursively
|
|
|
|
// called with prefixDir="one/two/three/four/" and marker="five.txt"
|
|
|
|
|
|
|
|
var markerBase, markerDir string
|
|
|
|
if marker != "" {
|
|
|
|
// Ex: if marker="four/five.txt", markerDir="four/" markerBase="five.txt"
|
|
|
|
markerSplit := strings.SplitN(marker, slashSeparator, 2)
|
|
|
|
markerDir = markerSplit[0]
|
|
|
|
if len(markerSplit) == 2 {
|
|
|
|
markerDir += slashSeparator
|
|
|
|
markerBase = markerSplit[1]
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
entries, err := xl.listDir(bucket, prefixDir, func(entry string) bool {
|
|
|
|
return !strings.HasPrefix(entry, entryPrefixMatch)
|
2016-05-25 12:22:39 -04:00
|
|
|
}, isLeaf)
|
2016-05-05 15:51:56 -04:00
|
|
|
if err != nil {
|
|
|
|
send(treeWalkResult{err: err})
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(entries) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-05-05 15:51:56 -04:00
|
|
|
// example:
|
|
|
|
// If markerDir="four/" Search() returns the index of "four/" in the sorted
|
|
|
|
// entries list so we skip all the entries till "four/"
|
2016-05-07 05:08:03 -04:00
|
|
|
idx := sort.Search(len(entries), func(i int) bool {
|
2016-05-20 23:48:47 -04:00
|
|
|
return entries[i] >= markerDir
|
2016-05-07 05:08:03 -04:00
|
|
|
})
|
2016-05-05 15:51:56 -04:00
|
|
|
entries = entries[idx:]
|
|
|
|
*count += len(entries)
|
|
|
|
for i, entry := range entries {
|
|
|
|
if i == 0 && markerDir == entry {
|
|
|
|
if !recursive {
|
|
|
|
// Skip as the marker would already be listed in the previous listing.
|
|
|
|
*count--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if recursive && !strings.HasSuffix(entry, slashSeparator) {
|
|
|
|
// We should not skip for recursive listing and if markerDir is a directory
|
|
|
|
// for ex. if marker is "four/five.txt" markerDir will be "four/" which
|
2016-05-30 19:51:59 -04:00
|
|
|
// should not be skipped, instead it will need to be treeWalk()'ed into.
|
2016-05-05 15:51:56 -04:00
|
|
|
|
|
|
|
// Skip if it is a file though as it would be listed in previous listing.
|
|
|
|
*count--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if recursive && strings.HasSuffix(entry, slashSeparator) {
|
|
|
|
// If the entry is a directory, we will need recurse into it.
|
|
|
|
markerArg := ""
|
|
|
|
if entry == markerDir {
|
|
|
|
// We need to pass "five.txt" as marker only if we are
|
|
|
|
// recursing into "four/"
|
|
|
|
markerArg = markerBase
|
|
|
|
}
|
|
|
|
*count--
|
|
|
|
prefixMatch := "" // Valid only for first level treeWalk and empty for subdirectories.
|
2016-05-30 19:51:59 -04:00
|
|
|
if !xl.treeWalk(bucket, pathJoin(prefixDir, entry), prefixMatch, markerArg, recursive, send, count, isLeaf) {
|
2016-05-05 15:51:56 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
*count--
|
2016-05-25 12:22:39 -04:00
|
|
|
if !send(treeWalkResult{entry: pathJoin(prefixDir, entry)}) {
|
2016-05-05 15:51:56 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initiate a new treeWalk in a goroutine.
|
2016-05-30 19:51:59 -04:00
|
|
|
func (xl xlObjects) startTreeWalk(bucket, prefix, marker string, recursive bool, isLeaf func(string, string) bool) *treeWalker {
|
2016-05-05 15:51:56 -04:00
|
|
|
// Example 1
|
|
|
|
// If prefix is "one/two/three/" and marker is "one/two/three/four/five.txt"
|
|
|
|
// treeWalk is called with prefixDir="one/two/three/" and marker="four/five.txt"
|
|
|
|
// and entryPrefixMatch=""
|
|
|
|
|
|
|
|
// Example 2
|
|
|
|
// if prefix is "one/two/th" and marker is "one/two/three/four/five.txt"
|
|
|
|
// treeWalk is called with prefixDir="one/two/" and marker="three/four/five.txt"
|
|
|
|
// and entryPrefixMatch="th"
|
|
|
|
|
|
|
|
ch := make(chan treeWalkResult, maxObjectList)
|
|
|
|
walkNotify := treeWalker{ch: ch}
|
|
|
|
entryPrefixMatch := prefix
|
|
|
|
prefixDir := ""
|
|
|
|
lastIndex := strings.LastIndex(prefix, slashSeparator)
|
|
|
|
if lastIndex != -1 {
|
|
|
|
entryPrefixMatch = prefix[lastIndex+1:]
|
|
|
|
prefixDir = prefix[:lastIndex+1]
|
|
|
|
}
|
|
|
|
count := 0
|
|
|
|
marker = strings.TrimPrefix(marker, prefixDir)
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
send := func(walkResult treeWalkResult) bool {
|
|
|
|
if count == 0 {
|
|
|
|
walkResult.end = true
|
|
|
|
}
|
|
|
|
timer := time.After(time.Second * 60)
|
|
|
|
select {
|
|
|
|
case ch <- walkResult:
|
|
|
|
return true
|
|
|
|
case <-timer:
|
|
|
|
walkNotify.timedOut = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2016-05-30 19:51:59 -04:00
|
|
|
xl.treeWalk(bucket, prefixDir, entryPrefixMatch, marker, recursive, send, &count, isLeaf)
|
2016-05-05 15:51:56 -04:00
|
|
|
}()
|
|
|
|
return &walkNotify
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the goroutine reference in the map
|
2016-05-30 19:51:59 -04:00
|
|
|
func (xl xlObjects) saveTreeWalk(params listParams, walker *treeWalker) {
|
2016-05-20 23:48:47 -04:00
|
|
|
xl.listObjectMapMutex.Lock()
|
|
|
|
defer xl.listObjectMapMutex.Unlock()
|
2016-05-05 15:51:56 -04:00
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
walkers, _ := xl.listObjectMap[params]
|
2016-05-05 15:51:56 -04:00
|
|
|
walkers = append(walkers, walker)
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
xl.listObjectMap[params] = walkers
|
2016-05-05 15:51:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the goroutine reference from map
|
2016-05-30 19:51:59 -04:00
|
|
|
func (xl xlObjects) lookupTreeWalk(params listParams) *treeWalker {
|
2016-05-20 23:48:47 -04:00
|
|
|
xl.listObjectMapMutex.Lock()
|
|
|
|
defer xl.listObjectMapMutex.Unlock()
|
2016-05-05 15:51:56 -04:00
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
if walkChs, ok := xl.listObjectMap[params]; ok {
|
2016-05-05 15:51:56 -04:00
|
|
|
for i, walkCh := range walkChs {
|
|
|
|
if !walkCh.timedOut {
|
|
|
|
newWalkChs := walkChs[i+1:]
|
|
|
|
if len(newWalkChs) > 0 {
|
2016-05-20 23:48:47 -04:00
|
|
|
xl.listObjectMap[params] = newWalkChs
|
2016-05-05 15:51:56 -04:00
|
|
|
} else {
|
2016-05-20 23:48:47 -04:00
|
|
|
delete(xl.listObjectMap, params)
|
2016-05-05 15:51:56 -04:00
|
|
|
}
|
|
|
|
return walkCh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// As all channels are timed out, delete the map entry
|
2016-05-20 23:48:47 -04:00
|
|
|
delete(xl.listObjectMap, params)
|
2016-05-05 15:51:56 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|