| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2016 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08: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-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 	"sort" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 00:52:08 +08:00
										 |  |  | // TreeWalkResult - Tree walk result carries results of tree walking.
 | 
					
						
							|  |  |  | type TreeWalkResult struct { | 
					
						
							| 
									
										
										
										
											2016-05-26 00:22:39 +08:00
										 |  |  | 	entry string | 
					
						
							|  |  |  | 	end   bool | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | // Return entries that have prefix prefixEntry.
 | 
					
						
							|  |  |  | // Note: input entries are expected to be sorted.
 | 
					
						
							|  |  |  | func filterMatchingPrefix(entries []string, prefixEntry string) []string { | 
					
						
							|  |  |  | 	start := 0 | 
					
						
							|  |  |  | 	end := len(entries) | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		if start == end { | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-04 15:27:50 +08:00
										 |  |  | 		if hasPrefix(entries[start], prefixEntry) { | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		start++ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		if start == end { | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-04 15:27:50 +08:00
										 |  |  | 		if hasPrefix(entries[end-1], prefixEntry) { | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		end-- | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-05-02 13:06:57 +08:00
										 |  |  | 	sort.Strings(entries[start:end]) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	return entries[start:end] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 00:52:08 +08:00
										 |  |  | // ListDirFunc - "listDir" function of type listDirFunc returned by listDirFactory() - explained below.
 | 
					
						
							| 
									
										
										
										
											2019-05-02 13:06:57 +08:00
										 |  |  | type ListDirFunc func(bucket, prefixDir, prefixEntry string) (entries []string) | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 00:52:08 +08:00
										 |  |  | // treeWalk walks directory tree recursively pushing TreeWalkResult into the channel as and when it encounters files.
 | 
					
						
							| 
									
										
										
										
											2019-05-06 22:52:42 +08:00
										 |  |  | func doTreeWalk(ctx context.Context, bucket, prefixDir, entryPrefixMatch, marker string, recursive bool, listDir ListDirFunc, resultCh chan TreeWalkResult, endWalkCh chan struct{}, isEnd bool) (totalNum int, treeErr error) { | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08: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] | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-11 07:53:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 13:06:57 +08:00
										 |  |  | 	entries := listDir(bucket, prefixDir, entryPrefixMatch) | 
					
						
							| 
									
										
										
										
											2016-06-04 02:33:50 +08:00
										 |  |  | 	// For an empty list return right here.
 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 	if len(entries) == 0 { | 
					
						
							| 
									
										
										
										
											2019-05-06 22:52:42 +08:00
										 |  |  | 		return 0, nil | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08: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 17:08:03 +08:00
										 |  |  | 	idx := sort.Search(len(entries), func(i int) bool { | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 		return entries[i] >= markerDir | 
					
						
							| 
									
										
										
										
											2016-05-07 17:08:03 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 	entries = entries[idx:] | 
					
						
							| 
									
										
										
										
											2016-06-04 02:33:50 +08:00
										 |  |  | 	// For an empty list after search through the entries, return right here.
 | 
					
						
							| 
									
										
										
										
											2016-05-30 12:05:00 +08:00
										 |  |  | 	if len(entries) == 0 { | 
					
						
							| 
									
										
										
										
											2019-05-06 22:52:42 +08:00
										 |  |  | 		return 0, nil | 
					
						
							| 
									
										
										
										
											2016-05-30 12:05:00 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-11 07:53:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 	for i, entry := range entries { | 
					
						
							| 
									
										
										
										
											2019-04-24 05:54:28 +08:00
										 |  |  | 		pentry := pathJoin(prefixDir, entry) | 
					
						
							| 
									
										
										
										
											2019-05-06 22:52:42 +08:00
										 |  |  | 		isDir := hasSuffix(pentry, slashSeparator) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 		if i == 0 && markerDir == entry { | 
					
						
							|  |  |  | 			if !recursive { | 
					
						
							|  |  |  | 				// Skip as the marker would already be listed in the previous listing.
 | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-05-09 10:08:21 +08:00
										 |  |  | 			if recursive && !isDir { | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 				// 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-31 07:51:59 +08:00
										 |  |  | 				// should not be skipped, instead it will need to be treeWalk()'ed into.
 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// Skip if it is a file though as it would be listed in previous listing.
 | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-05-09 10:08:21 +08:00
										 |  |  | 		if recursive && isDir { | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 			// 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 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			prefixMatch := "" // Valid only for first level treeWalk and empty for subdirectories.
 | 
					
						
							| 
									
										
										
										
											2016-06-04 02:33:50 +08:00
										 |  |  | 			// markIsEnd is passed to this entry's treeWalk() so that treeWalker.end can be marked
 | 
					
						
							|  |  |  | 			// true at the end of the treeWalk stream.
 | 
					
						
							|  |  |  | 			markIsEnd := i == len(entries)-1 && isEnd | 
					
						
							| 
									
										
										
										
											2019-05-06 22:52:42 +08:00
										 |  |  | 			totalFound, err := doTreeWalk(ctx, bucket, pentry, prefixMatch, markerArg, recursive, | 
					
						
							|  |  |  | 				listDir, resultCh, endWalkCh, markIsEnd) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return 0, err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// A nil totalFound means this is an empty directory that
 | 
					
						
							|  |  |  | 			// needs to be sent to the result channel, otherwise continue
 | 
					
						
							|  |  |  | 			// to the next entry.
 | 
					
						
							|  |  |  | 			if totalFound > 0 { | 
					
						
							|  |  |  | 				continue | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-04-24 05:54:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-04 02:33:50 +08:00
										 |  |  | 		// EOF is set if we are at last entry and the caller indicated we at the end.
 | 
					
						
							|  |  |  | 		isEOF := ((i == len(entries)-1) && isEnd) | 
					
						
							| 
									
										
										
										
											2016-05-30 12:05:00 +08:00
										 |  |  | 		select { | 
					
						
							| 
									
										
										
										
											2016-06-06 02:55:45 +08:00
										 |  |  | 		case <-endWalkCh: | 
					
						
							| 
									
										
										
										
											2019-05-06 22:52:42 +08:00
										 |  |  | 			return 0, errWalkAbort | 
					
						
							| 
									
										
										
										
											2019-04-24 05:54:28 +08:00
										 |  |  | 		case resultCh <- TreeWalkResult{entry: pentry, end: isEOF}: | 
					
						
							| 
									
										
										
										
											2016-05-30 12:05:00 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-04 02:33:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Everything is listed.
 | 
					
						
							| 
									
										
										
										
											2019-05-06 22:52:42 +08:00
										 |  |  | 	return len(entries), nil | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Initiate a new treeWalk in a goroutine.
 | 
					
						
							| 
									
										
										
										
											2019-05-02 13:06:57 +08:00
										 |  |  | func startTreeWalk(ctx context.Context, bucket, prefix, marker string, recursive bool, listDir ListDirFunc, endWalkCh chan struct{}) chan TreeWalkResult { | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08: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"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 00:52:08 +08:00
										 |  |  | 	resultCh := make(chan TreeWalkResult, maxObjectList) | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 	entryPrefixMatch := prefix | 
					
						
							|  |  |  | 	prefixDir := "" | 
					
						
							|  |  |  | 	lastIndex := strings.LastIndex(prefix, slashSeparator) | 
					
						
							|  |  |  | 	if lastIndex != -1 { | 
					
						
							|  |  |  | 		entryPrefixMatch = prefix[lastIndex+1:] | 
					
						
							|  |  |  | 		prefixDir = prefix[:lastIndex+1] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	marker = strings.TrimPrefix(marker, prefixDir) | 
					
						
							| 
									
										
										
										
											2016-06-04 02:33:50 +08:00
										 |  |  | 	go func() { | 
					
						
							|  |  |  | 		isEnd := true // Indication to start walking the tree with end as true.
 | 
					
						
							| 
									
										
										
										
											2019-05-02 13:06:57 +08:00
										 |  |  | 		doTreeWalk(ctx, bucket, prefixDir, entryPrefixMatch, marker, recursive, listDir, resultCh, endWalkCh, isEnd) | 
					
						
							| 
									
										
										
										
											2016-06-06 02:55:45 +08:00
										 |  |  | 		close(resultCh) | 
					
						
							| 
									
										
										
										
											2016-06-04 02:33:50 +08:00
										 |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2016-06-06 02:55:45 +08:00
										 |  |  | 	return resultCh | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | } |