You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.2 KiB
117 lines
3.2 KiB
9 years ago
|
package main
|
||
|
|
||
|
import "strings"
|
||
|
|
||
|
func (xl xlObjects) listObjectsXL(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, error) {
|
||
|
// Default is recursive, if delimiter is set then list non recursive.
|
||
|
recursive := true
|
||
|
if delimiter == slashSeparator {
|
||
|
recursive = false
|
||
|
}
|
||
|
|
||
|
walker := xl.lookupTreeWalkXL(listParams{bucket, recursive, marker, prefix})
|
||
|
if walker == nil {
|
||
|
walker = xl.startTreeWalkXL(bucket, prefix, marker, recursive)
|
||
|
}
|
||
|
var objInfos []ObjectInfo
|
||
|
var eof bool
|
||
|
var nextMarker string
|
||
|
for i := 0; i < maxKeys; {
|
||
|
walkResult, ok := <-walker.ch
|
||
|
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.
|
||
|
if walkResult.err == errFileNotFound {
|
||
|
return ListObjectsInfo{}, nil
|
||
|
}
|
||
|
return ListObjectsInfo{}, toObjectErr(walkResult.err, bucket, prefix)
|
||
|
}
|
||
|
objInfo := walkResult.objInfo
|
||
|
nextMarker = objInfo.Name
|
||
|
objInfos = append(objInfos, objInfo)
|
||
|
if walkResult.end {
|
||
|
eof = true
|
||
|
break
|
||
|
}
|
||
|
i++
|
||
|
}
|
||
|
params := listParams{bucket, recursive, nextMarker, prefix}
|
||
|
if !eof {
|
||
|
xl.saveTreeWalkXL(params, walker)
|
||
|
}
|
||
|
|
||
|
result := ListObjectsInfo{IsTruncated: !eof}
|
||
|
for _, objInfo := range objInfos {
|
||
|
// With delimiter set we fill in NextMarker and Prefixes.
|
||
|
if delimiter == slashSeparator {
|
||
|
result.NextMarker = objInfo.Name
|
||
|
if objInfo.IsDir {
|
||
|
result.Prefixes = append(result.Prefixes, objInfo.Name)
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
result.Objects = append(result.Objects, ObjectInfo{
|
||
|
Name: objInfo.Name,
|
||
|
ModTime: objInfo.ModTime,
|
||
|
Size: objInfo.Size,
|
||
|
IsDir: false,
|
||
|
})
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
// ListObjects - list all objects at prefix, delimited by '/'.
|
||
|
func (xl xlObjects) ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, error) {
|
||
|
// Verify if bucket is valid.
|
||
|
if !IsValidBucketName(bucket) {
|
||
|
return ListObjectsInfo{}, BucketNameInvalid{Bucket: bucket}
|
||
|
}
|
||
|
// Verify if bucket exists.
|
||
|
if !xl.isBucketExist(bucket) {
|
||
|
return ListObjectsInfo{}, BucketNotFound{Bucket: bucket}
|
||
|
}
|
||
|
if !IsValidObjectPrefix(prefix) {
|
||
|
return ListObjectsInfo{}, ObjectNameInvalid{Bucket: bucket, Object: prefix}
|
||
|
}
|
||
|
// Verify if delimiter is anything other than '/', which we do not support.
|
||
|
if delimiter != "" && delimiter != slashSeparator {
|
||
|
return ListObjectsInfo{}, UnsupportedDelimiter{
|
||
|
Delimiter: delimiter,
|
||
|
}
|
||
|
}
|
||
|
// Verify if marker has prefix.
|
||
|
if marker != "" {
|
||
|
if !strings.HasPrefix(marker, prefix) {
|
||
|
return ListObjectsInfo{}, InvalidMarkerPrefixCombination{
|
||
|
Marker: marker,
|
||
|
Prefix: prefix,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// With max keys of zero we have reached eof, return right here.
|
||
|
if maxKeys == 0 {
|
||
|
return ListObjectsInfo{}, nil
|
||
|
}
|
||
|
|
||
|
// 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.listObjectsXL(bucket, prefix, marker, delimiter, maxKeys)
|
||
|
if err == nil {
|
||
|
// We got the entries successfully return.
|
||
|
return listObjInfo, nil
|
||
|
}
|
||
|
|
||
|
// Return error at the end.
|
||
|
return ListObjectsInfo{}, toObjectErr(err, bucket, prefix)
|
||
|
}
|