| 
									
										
										
										
											2016-05-31 07:51:59 +08: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-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-02 02:47:49 +08:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-03-15 03:01:47 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2018-02-02 02:47:49 +08:00
										 |  |  | 	"sort" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/minio/minio/pkg/errors" | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2017-11-26 03:58:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-27 07:39:22 +08: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.
 | 
					
						
							| 
									
										
										
										
											2018-02-02 02:47:49 +08:00
										 |  |  | // disks - used for doing disk.ListDir()
 | 
					
						
							| 
									
										
										
										
											2017-01-27 07:39:22 +08:00
										 |  |  | func listDirFactory(isLeaf isLeafFunc, treeWalkIgnoredErrs []error, disks ...StorageAPI) listDirFunc { | 
					
						
							| 
									
										
										
										
											2018-02-02 02:47:49 +08:00
										 |  |  | 	// Returns sorted merged entries from all the disks.
 | 
					
						
							|  |  |  | 	listDir := func(bucket, prefixDir, prefixEntry string) (mergedEntries []string, delayIsLeaf bool, err error) { | 
					
						
							| 
									
										
										
										
											2017-01-27 07:39:22 +08:00
										 |  |  | 		for _, disk := range disks { | 
					
						
							|  |  |  | 			if disk == nil { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-02 02:47:49 +08:00
										 |  |  | 			var entries []string | 
					
						
							|  |  |  | 			var newEntries []string | 
					
						
							| 
									
										
										
										
											2017-01-27 07:39:22 +08:00
										 |  |  | 			entries, err = disk.ListDir(bucket, prefixDir) | 
					
						
							| 
									
										
										
										
											2017-03-21 02:09:05 +08:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				// For any reason disk was deleted or goes offline, continue
 | 
					
						
							|  |  |  | 				// and list from other disks if possible.
 | 
					
						
							| 
									
										
										
										
											2017-11-26 03:58:29 +08:00
										 |  |  | 				if errors.IsErrIgnored(err, treeWalkIgnoredErrs...) { | 
					
						
							| 
									
										
										
										
											2017-03-21 02:09:05 +08:00
										 |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-11-26 03:58:29 +08:00
										 |  |  | 				return nil, false, errors.Trace(err) | 
					
						
							| 
									
										
										
										
											2017-01-27 07:39:22 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-03-21 02:09:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-02 02:47:49 +08:00
										 |  |  | 			// Find elements in entries which are not in mergedEntries
 | 
					
						
							|  |  |  | 			for _, entry := range entries { | 
					
						
							|  |  |  | 				idx := sort.SearchStrings(mergedEntries, entry) | 
					
						
							|  |  |  | 				// if entry is already present in mergedEntries don't add.
 | 
					
						
							|  |  |  | 				if idx < len(mergedEntries) && mergedEntries[idx] == entry { | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				newEntries = append(newEntries, entry) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if len(newEntries) > 0 { | 
					
						
							|  |  |  | 				// Merge the entries and sort it.
 | 
					
						
							|  |  |  | 				mergedEntries = append(mergedEntries, newEntries...) | 
					
						
							|  |  |  | 				sort.Strings(mergedEntries) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-01-27 07:39:22 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-02 02:47:49 +08:00
										 |  |  | 		mergedEntries, delayIsLeaf = filterListEntries(bucket, prefixDir, mergedEntries, prefixEntry, isLeaf) | 
					
						
							|  |  |  | 		return mergedEntries, delayIsLeaf, nil | 
					
						
							| 
									
										
										
										
											2017-01-27 07:39:22 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return listDir | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-31 07:51:59 +08:00
										 |  |  | // listObjects - wrapper function implemented over file tree walk.
 | 
					
						
							| 
									
										
										
										
											2017-06-22 10:53:09 +08:00
										 |  |  | func (xl xlObjects) listObjects(bucket, prefix, marker, delimiter string, maxKeys int) (loi ListObjectsInfo, e error) { | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 	// Default is recursive, if delimiter is set then list non recursive.
 | 
					
						
							|  |  |  | 	recursive := true | 
					
						
							|  |  |  | 	if delimiter == slashSeparator { | 
					
						
							|  |  |  | 		recursive = false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-18 02:36:33 +08:00
										 |  |  | 	heal := false // true only for xl.ListObjectsHeal
 | 
					
						
							|  |  |  | 	walkResultCh, endWalkCh := xl.listPool.Release(listParams{bucket, recursive, marker, prefix, heal}) | 
					
						
							| 
									
										
										
										
											2016-06-06 02:55:45 +08:00
										 |  |  | 	if walkResultCh == nil { | 
					
						
							|  |  |  | 		endWalkCh = make(chan struct{}) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 		isLeaf := xl.isObject | 
					
						
							| 
									
										
										
										
											2016-09-16 04:43:40 +08:00
										 |  |  | 		listDir := listDirFactory(isLeaf, xlTreeWalkIgnoredErrs, xl.getLoadBalancedDisks()...) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 		walkResultCh = startTreeWalk(bucket, prefix, marker, recursive, listDir, isLeaf, endWalkCh) | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-30 12:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 	var objInfos []ObjectInfo | 
					
						
							|  |  |  | 	var eof bool | 
					
						
							|  |  |  | 	var nextMarker string | 
					
						
							|  |  |  | 	for i := 0; i < maxKeys; { | 
					
						
							| 
									
										
										
										
											2018-02-10 07:19:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-06 02:55:45 +08:00
										 |  |  | 		walkResult, ok := <-walkResultCh | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 		if !ok { | 
					
						
							|  |  |  | 			// Closed channel.
 | 
					
						
							|  |  |  | 			eof = true | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// For any walk error return right away.
 | 
					
						
							|  |  |  | 		if walkResult.err != nil { | 
					
						
							| 
									
										
										
										
											2017-06-22 10:53:09 +08:00
										 |  |  | 			return loi, toObjectErr(walkResult.err, bucket, prefix) | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-05-26 00:22:39 +08:00
										 |  |  | 		entry := walkResult.entry | 
					
						
							|  |  |  | 		var objInfo ObjectInfo | 
					
						
							| 
									
										
										
										
											2017-02-17 06:52:14 +08:00
										 |  |  | 		if hasSuffix(entry, slashSeparator) { | 
					
						
							| 
									
										
										
										
											2016-05-26 00:22:39 +08:00
										 |  |  | 			// 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 { | 
					
						
							| 
									
										
										
										
											2018-02-16 09:45:57 +08:00
										 |  |  | 				// Ignore errFileNotFound as the object might have got
 | 
					
						
							|  |  |  | 				// deleted in the interim period of listing and getObjectInfo(),
 | 
					
						
							|  |  |  | 				// ignore quorum error as it might be an entry from an outdated disk.
 | 
					
						
							|  |  |  | 				switch errors.Cause(err) { | 
					
						
							|  |  |  | 				case errFileNotFound, errXLReadQuorum: | 
					
						
							| 
									
										
										
										
											2016-06-27 13:10:22 +08:00
										 |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-06-22 10:53:09 +08:00
										 |  |  | 				return loi, toObjectErr(err, bucket, prefix) | 
					
						
							| 
									
										
										
										
											2016-05-26 00:22:39 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 		nextMarker = objInfo.Name | 
					
						
							|  |  |  | 		objInfos = append(objInfos, objInfo) | 
					
						
							| 
									
										
										
										
											2016-05-30 12:05:00 +08:00
										 |  |  | 		i++ | 
					
						
							| 
									
										
										
										
											2016-08-16 22:57:14 +08:00
										 |  |  | 		if walkResult.end { | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 			eof = true | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-30 12:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-18 02:36:33 +08:00
										 |  |  | 	params := listParams{bucket, recursive, nextMarker, prefix, heal} | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 	if !eof { | 
					
						
							| 
									
										
										
										
											2016-06-06 02:55:45 +08:00
										 |  |  | 		xl.listPool.Set(params, walkResultCh, endWalkCh) | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	result := ListObjectsInfo{IsTruncated: !eof} | 
					
						
							|  |  |  | 	for _, objInfo := range objInfos { | 
					
						
							| 
									
										
										
										
											2016-06-01 13:10:55 +08:00
										 |  |  | 		result.NextMarker = objInfo.Name | 
					
						
							|  |  |  | 		if objInfo.IsDir { | 
					
						
							|  |  |  | 			result.Prefixes = append(result.Prefixes, objInfo.Name) | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-11-14 03:48:02 +08:00
										 |  |  | 		result.Objects = append(result.Objects, objInfo) | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return result, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ListObjects - list all objects at prefix, delimited by '/'.
 | 
					
						
							| 
									
										
										
										
											2018-03-15 03:01:47 +08:00
										 |  |  | func (xl xlObjects) ListObjects(ctx context.Context, bucket, prefix, marker, delimiter string, maxKeys int) (loi ListObjectsInfo, e error) { | 
					
						
							| 
									
										
										
										
											2016-12-02 15:15:17 +08:00
										 |  |  | 	if err := checkListObjsArgs(bucket, prefix, marker, delimiter, xl); err != nil { | 
					
						
							| 
									
										
										
										
											2017-06-22 10:53:09 +08:00
										 |  |  | 		return loi, err | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// With max keys of zero we have reached eof, return right here.
 | 
					
						
							|  |  |  | 	if maxKeys == 0 { | 
					
						
							| 
									
										
										
										
											2017-06-22 10:53:09 +08:00
										 |  |  | 		return loi, nil | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 07:19:30 +08:00
										 |  |  | 	// Marker is set validate pre-condition.
 | 
					
						
							|  |  |  | 	if marker != "" { | 
					
						
							|  |  |  | 		// Marker not common with prefix is not implemented.Send an empty response
 | 
					
						
							|  |  |  | 		if !hasPrefix(marker, prefix) { | 
					
						
							|  |  |  | 			return ListObjectsInfo{}, e | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-28 06:43:51 +08: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-22 10:53:09 +08:00
										 |  |  | 		return loi, nil | 
					
						
							| 
									
										
										
										
											2016-05-28 06:43:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08: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-31 07:51:59 +08:00
										 |  |  | 	listObjInfo, err := xl.listObjects(bucket, prefix, marker, delimiter, maxKeys) | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		// We got the entries successfully return.
 | 
					
						
							|  |  |  | 		return listObjInfo, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Return error at the end.
 | 
					
						
							| 
									
										
										
										
											2017-06-22 10:53:09 +08:00
										 |  |  | 	return loi, toObjectErr(err, bucket, prefix) | 
					
						
							| 
									
										
										
										
											2016-05-21 11:48:47 +08:00
										 |  |  | } |