2016-04-29 20:52:17 -04:00
|
|
|
/*
|
2019-04-17 12:52:08 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2016-2019 MinIO, Inc.
|
2016-04-29 20:52:17 -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-04-29 20:52:17 -04:00
|
|
|
|
2016-05-25 04:33:39 -04:00
|
|
|
import (
|
2018-03-15 16:27:16 -04:00
|
|
|
"context"
|
2016-05-25 04:33:39 -04:00
|
|
|
"sync"
|
2016-11-22 21:18:22 -05:00
|
|
|
|
2019-07-05 17:06:12 -04:00
|
|
|
"strings"
|
|
|
|
|
2016-11-22 21:18:22 -05:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2016-05-25 04:33:39 -04:00
|
|
|
)
|
2016-04-29 20:52:17 -04:00
|
|
|
|
2016-05-29 18:38:14 -04:00
|
|
|
const (
|
|
|
|
// Block size used for all internal operations version 1.
|
2016-11-22 21:18:22 -05:00
|
|
|
blockSizeV1 = 10 * humanize.MiByte
|
2016-06-25 06:03:27 -04:00
|
|
|
|
|
|
|
// Staging buffer read size for all internal operations version 1.
|
2016-11-22 21:18:22 -05:00
|
|
|
readSizeV1 = 1 * humanize.MiByte
|
2016-07-21 20:31:14 -04:00
|
|
|
|
|
|
|
// Buckets meta prefix.
|
|
|
|
bucketMetaPrefix = "buckets"
|
2017-04-10 22:51:23 -04:00
|
|
|
|
2017-05-14 15:05:51 -04:00
|
|
|
// ETag (hex encoded md5sum) of empty string.
|
|
|
|
emptyETag = "d41d8cd98f00b204e9800998ecf8427e"
|
2016-05-29 18:38:14 -04:00
|
|
|
)
|
|
|
|
|
2016-10-10 02:03:10 -04:00
|
|
|
// Global object layer mutex, used for safely updating object layer.
|
2020-05-04 01:35:40 -04:00
|
|
|
var globalObjLayerMutex sync.RWMutex
|
2016-10-10 02:03:10 -04:00
|
|
|
|
2019-10-31 02:39:09 -04:00
|
|
|
// Global object layer, only accessed by globalObjectAPI.
|
2016-10-10 02:03:10 -04:00
|
|
|
var globalObjectAPI ObjectLayer
|
|
|
|
|
2018-03-28 17:14:06 -04:00
|
|
|
//Global cacheObjects, only accessed by newCacheObjectsFn().
|
|
|
|
var globalCacheObjectAPI CacheObjectLayer
|
|
|
|
|
2017-01-20 19:33:01 -05:00
|
|
|
// Checks if the object is a directory, this logic uses
|
2019-08-06 15:08:58 -04:00
|
|
|
// if size == 0 and object ends with SlashSeparator then
|
2017-01-20 19:33:01 -05:00
|
|
|
// returns true.
|
|
|
|
func isObjectDir(object string, size int64) bool {
|
2019-12-06 02:16:06 -05:00
|
|
|
return HasSuffix(object, SlashSeparator) && size == 0
|
2017-01-20 19:33:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Converts just bucket, object metadata into ObjectInfo datatype.
|
|
|
|
func dirObjectInfo(bucket, object string, size int64, metadata map[string]string) ObjectInfo {
|
|
|
|
// This is a special case with size as '0' and object ends with
|
|
|
|
// a slash separator, we treat it like a valid operation and
|
|
|
|
// return success.
|
2017-05-14 15:05:51 -04:00
|
|
|
etag := metadata["etag"]
|
|
|
|
delete(metadata, "etag")
|
|
|
|
if etag == "" {
|
|
|
|
etag = emptyETag
|
2017-04-10 22:51:23 -04:00
|
|
|
}
|
|
|
|
|
2017-01-20 19:33:01 -05:00
|
|
|
return ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: object,
|
2017-03-18 14:28:41 -04:00
|
|
|
ModTime: UTCNow(),
|
2017-01-20 19:33:01 -05:00
|
|
|
ContentType: "application/octet-stream",
|
|
|
|
IsDir: true,
|
|
|
|
Size: size,
|
2017-05-14 15:05:51 -04:00
|
|
|
ETag: etag,
|
2017-01-20 19:33:01 -05:00
|
|
|
UserDefined: metadata,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 04:49:46 -04:00
|
|
|
// Depending on the disk type network or local, initialize storage API.
|
2017-04-11 18:44:27 -04:00
|
|
|
func newStorageAPI(endpoint Endpoint) (storage StorageAPI, err error) {
|
|
|
|
if endpoint.IsLocal {
|
2020-08-25 13:55:15 -04:00
|
|
|
storage, err := newXLStorage(endpoint)
|
2019-10-25 13:37:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-12 23:04:01 -04:00
|
|
|
return &xlStorageDiskIDCheck{storage: storage}, nil
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
2017-04-11 18:44:27 -04:00
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
return newStorageRESTClient(endpoint), nil
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
|
2016-05-08 13:15:34 -04:00
|
|
|
// Cleanup a directory recursively.
|
2018-04-05 18:04:40 -04:00
|
|
|
func cleanupDir(ctx context.Context, storage StorageAPI, volume, dirPath string) error {
|
2016-05-08 13:15:34 -04:00
|
|
|
var delFunc func(string) error
|
|
|
|
// Function to delete entries recursively.
|
|
|
|
delFunc = func(entryPath string) error {
|
2019-12-06 02:16:06 -05:00
|
|
|
if !HasSuffix(entryPath, SlashSeparator) {
|
2016-07-12 04:01:47 -04:00
|
|
|
// Delete the file entry.
|
2020-09-04 12:45:06 -04:00
|
|
|
err := storage.DeleteFile(ctx, volume, entryPath)
|
2020-07-24 16:16:11 -04:00
|
|
|
if err != errDiskNotFound && err != errUnformattedDisk {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return err
|
2016-05-08 13:15:34 -04:00
|
|
|
}
|
2016-07-12 04:01:47 -04:00
|
|
|
|
2016-05-08 13:15:34 -04:00
|
|
|
// If it's a directory, list and call delFunc() for each entry.
|
2020-09-04 12:45:06 -04:00
|
|
|
entries, err := storage.ListDir(ctx, volume, entryPath, -1)
|
2016-07-12 04:01:47 -04:00
|
|
|
// If entryPath prefix never existed, safe to ignore.
|
|
|
|
if err == errFileNotFound {
|
|
|
|
return nil
|
|
|
|
} else if err != nil { // For any other errors fail.
|
2020-07-24 16:16:11 -04:00
|
|
|
if err != errDiskNotFound && err != errUnformattedDisk {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return err
|
2016-07-12 04:01:47 -04:00
|
|
|
} // else on success..
|
2016-06-14 04:39:40 -04:00
|
|
|
|
2018-01-29 21:43:13 -05:00
|
|
|
// Entry path is empty, just delete it.
|
|
|
|
if len(entries) == 0 {
|
2020-09-04 12:45:06 -04:00
|
|
|
err = storage.DeleteFile(ctx, volume, entryPath)
|
2020-07-24 16:16:11 -04:00
|
|
|
if err != errDiskNotFound && err != errUnformattedDisk {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return err
|
2018-01-29 21:43:13 -05:00
|
|
|
}
|
|
|
|
|
2016-07-12 04:01:47 -04:00
|
|
|
// Recurse and delete all other entries.
|
|
|
|
for _, entry := range entries {
|
|
|
|
if err = delFunc(pathJoin(entryPath, entry)); err != nil {
|
2016-05-08 13:15:34 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-02 19:34:15 -04:00
|
|
|
err := delFunc(retainSlash(pathJoin(dirPath)))
|
|
|
|
return err
|
2016-05-08 13:15:34 -04:00
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2020-08-25 15:26:48 -04:00
|
|
|
func listObjectsNonSlash(ctx context.Context, bucket, prefix, marker, delimiter string, maxKeys int, tpool *TreeWalkPool, listDir ListDirFunc, isLeaf IsLeafFunc, isLeafDir IsLeafDirFunc, getObjInfo func(context.Context, string, string) (ObjectInfo, error), getObjectInfoDirs ...func(context.Context, string, string) (ObjectInfo, error)) (loi ListObjectsInfo, err error) {
|
2019-07-05 17:06:12 -04:00
|
|
|
endWalkCh := make(chan struct{})
|
|
|
|
defer close(endWalkCh)
|
|
|
|
recursive := true
|
2020-08-25 15:26:48 -04:00
|
|
|
walkResultCh := startTreeWalk(ctx, bucket, prefix, "", recursive, listDir, isLeaf, isLeafDir, endWalkCh)
|
2019-07-05 17:06:12 -04:00
|
|
|
|
|
|
|
var objInfos []ObjectInfo
|
|
|
|
var eof bool
|
|
|
|
var prevPrefix string
|
|
|
|
|
|
|
|
for {
|
|
|
|
if len(objInfos) == maxKeys {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
result, ok := <-walkResultCh
|
|
|
|
if !ok {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var objInfo ObjectInfo
|
|
|
|
var err error
|
|
|
|
|
|
|
|
index := strings.Index(strings.TrimPrefix(result.entry, prefix), delimiter)
|
|
|
|
if index == -1 {
|
|
|
|
objInfo, err = getObjInfo(ctx, bucket, result.entry)
|
|
|
|
if err != nil {
|
|
|
|
// 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.
|
|
|
|
if IsErrIgnored(err, []error{
|
|
|
|
errFileNotFound,
|
2020-06-12 23:04:01 -04:00
|
|
|
errErasureReadQuorum,
|
2019-07-05 17:06:12 -04:00
|
|
|
}...) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return loi, toObjectErr(err, bucket, prefix)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
index = len(prefix) + index + len(delimiter)
|
|
|
|
currPrefix := result.entry[:index]
|
|
|
|
if currPrefix == prevPrefix {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
prevPrefix = currPrefix
|
|
|
|
|
|
|
|
objInfo = ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: currPrefix,
|
|
|
|
IsDir: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if objInfo.Name <= marker {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
objInfos = append(objInfos, objInfo)
|
|
|
|
if result.end {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := ListObjectsInfo{}
|
|
|
|
for _, objInfo := range objInfos {
|
|
|
|
if objInfo.IsDir {
|
|
|
|
result.Prefixes = append(result.Prefixes, objInfo.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result.Objects = append(result.Objects, objInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !eof {
|
|
|
|
result.IsTruncated = true
|
|
|
|
if len(objInfos) > 0 {
|
|
|
|
result.NextMarker = objInfos[len(objInfos)-1].Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-02-25 10:52:28 -05:00
|
|
|
// Walk a bucket, optionally prefix recursively, until we have returned
|
|
|
|
// all the content to objectInfo channel, it is callers responsibility
|
|
|
|
// to allocate a receive channel for ObjectInfo, upon any unhandled
|
|
|
|
// error walker returns error. Optionally if context.Done() is received
|
|
|
|
// then Walk() stops the walker.
|
2020-08-25 15:26:48 -04:00
|
|
|
func fsWalk(ctx context.Context, obj ObjectLayer, bucket, prefix string, listDir ListDirFunc, isLeaf IsLeafFunc, isLeafDir IsLeafDirFunc, results chan<- ObjectInfo, getObjInfo func(context.Context, string, string) (ObjectInfo, error), getObjectInfoDirs ...func(context.Context, string, string) (ObjectInfo, error)) error {
|
2020-02-25 10:52:28 -05:00
|
|
|
if err := checkListObjsArgs(ctx, bucket, prefix, "", obj); err != nil {
|
2020-02-25 22:58:58 -05:00
|
|
|
// Upon error close the channel.
|
|
|
|
close(results)
|
2020-02-25 10:52:28 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:26:48 -04:00
|
|
|
walkResultCh := startTreeWalk(ctx, bucket, prefix, "", true, listDir, isLeaf, isLeafDir, ctx.Done())
|
2020-02-25 10:52:28 -05:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(results)
|
|
|
|
|
|
|
|
for {
|
|
|
|
walkResult, ok := <-walkResultCh
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var objInfo ObjectInfo
|
|
|
|
var err error
|
|
|
|
if HasSuffix(walkResult.entry, SlashSeparator) {
|
|
|
|
for _, getObjectInfoDir := range getObjectInfoDirs {
|
|
|
|
objInfo, err = getObjectInfoDir(ctx, bucket, walkResult.entry)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err == errFileNotFound {
|
|
|
|
err = nil
|
|
|
|
objInfo = ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: walkResult.entry,
|
|
|
|
IsDir: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
objInfo, err = getObjInfo(ctx, bucket, walkResult.entry)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
results <- objInfo
|
|
|
|
if walkResult.end {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:26:48 -04:00
|
|
|
func listObjects(ctx context.Context, obj ObjectLayer, bucket, prefix, marker, delimiter string, maxKeys int, tpool *TreeWalkPool, listDir ListDirFunc, isLeaf IsLeafFunc, isLeafDir IsLeafDirFunc, getObjInfo func(context.Context, string, string) (ObjectInfo, error), getObjectInfoDirs ...func(context.Context, string, string) (ObjectInfo, error)) (loi ListObjectsInfo, err error) {
|
2019-08-06 15:08:58 -04:00
|
|
|
if delimiter != SlashSeparator && delimiter != "" {
|
2020-08-25 15:26:48 -04:00
|
|
|
return listObjectsNonSlash(ctx, bucket, prefix, marker, delimiter, maxKeys, tpool, listDir, isLeaf, isLeafDir, getObjInfo, getObjectInfoDirs...)
|
2019-07-05 17:06:12 -04:00
|
|
|
}
|
|
|
|
|
2020-02-25 10:52:28 -05:00
|
|
|
if err := checkListObjsArgs(ctx, bucket, prefix, marker, obj); err != nil {
|
2019-04-17 12:52:08 -04:00
|
|
|
return loi, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marker is set validate pre-condition.
|
|
|
|
if marker != "" {
|
|
|
|
// Marker not common with prefix is not implemented. Send an empty response
|
2019-12-06 02:16:06 -05:00
|
|
|
if !HasPrefix(marker, prefix) {
|
2019-04-17 12:52:08 -04:00
|
|
|
return loi, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// With max keys of zero we have reached eof, return right here.
|
|
|
|
if maxKeys == 0 {
|
|
|
|
return loi, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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/...'
|
2019-08-06 15:08:58 -04:00
|
|
|
if delimiter == SlashSeparator && prefix == SlashSeparator {
|
2019-04-17 12:52:08 -04:00
|
|
|
return loi, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Over flowing count - reset to maxObjectList.
|
|
|
|
if maxKeys < 0 || maxKeys > maxObjectList {
|
|
|
|
maxKeys = maxObjectList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default is recursive, if delimiter is set then list non recursive.
|
|
|
|
recursive := true
|
2019-08-06 15:08:58 -04:00
|
|
|
if delimiter == SlashSeparator {
|
2019-04-17 12:52:08 -04:00
|
|
|
recursive = false
|
|
|
|
}
|
|
|
|
|
2020-01-30 06:50:07 -05:00
|
|
|
walkResultCh, endWalkCh := tpool.Release(listParams{bucket, recursive, marker, prefix})
|
2019-04-17 12:52:08 -04:00
|
|
|
if walkResultCh == nil {
|
|
|
|
endWalkCh = make(chan struct{})
|
2020-08-25 15:26:48 -04:00
|
|
|
walkResultCh = startTreeWalk(ctx, bucket, prefix, marker, recursive, listDir, isLeaf, isLeafDir, endWalkCh)
|
2019-04-17 12:52:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var objInfos []ObjectInfo
|
|
|
|
var eof bool
|
|
|
|
var nextMarker string
|
|
|
|
|
|
|
|
// List until maxKeys requested.
|
|
|
|
for i := 0; i < maxKeys; {
|
|
|
|
walkResult, ok := <-walkResultCh
|
|
|
|
if !ok {
|
|
|
|
// Closed channel.
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var objInfo ObjectInfo
|
|
|
|
var err error
|
2019-12-06 02:16:06 -05:00
|
|
|
if HasSuffix(walkResult.entry, SlashSeparator) {
|
2019-04-17 12:52:08 -04:00
|
|
|
for _, getObjectInfoDir := range getObjectInfoDirs {
|
|
|
|
objInfo, err = getObjectInfoDir(ctx, bucket, walkResult.entry)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2019-04-23 17:54:28 -04:00
|
|
|
if err == errFileNotFound {
|
|
|
|
err = nil
|
|
|
|
objInfo = ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: walkResult.entry,
|
|
|
|
IsDir: true,
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 12:52:08 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
objInfo, err = getObjInfo(ctx, bucket, walkResult.entry)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// 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.
|
|
|
|
if IsErrIgnored(err, []error{
|
|
|
|
errFileNotFound,
|
2020-06-12 23:04:01 -04:00
|
|
|
errErasureReadQuorum,
|
2019-04-17 12:52:08 -04:00
|
|
|
}...) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return loi, toObjectErr(err, bucket, prefix)
|
|
|
|
}
|
|
|
|
nextMarker = objInfo.Name
|
|
|
|
objInfos = append(objInfos, objInfo)
|
|
|
|
if walkResult.end {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save list routine for the next marker if we haven't reached EOF.
|
2020-01-30 06:50:07 -05:00
|
|
|
params := listParams{bucket, recursive, nextMarker, prefix}
|
2019-04-17 12:52:08 -04:00
|
|
|
if !eof {
|
|
|
|
tpool.Set(params, walkResultCh, endWalkCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := ListObjectsInfo{}
|
|
|
|
for _, objInfo := range objInfos {
|
2019-08-06 15:08:58 -04:00
|
|
|
if objInfo.IsDir && delimiter == SlashSeparator {
|
2019-04-17 12:52:08 -04:00
|
|
|
result.Prefixes = append(result.Prefixes, objInfo.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result.Objects = append(result.Objects, objInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !eof {
|
|
|
|
result.IsTruncated = true
|
|
|
|
if len(objInfos) > 0 {
|
|
|
|
result.NextMarker = objInfos[len(objInfos)-1].Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success.
|
|
|
|
return result, nil
|
|
|
|
}
|