2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-10-28 12:18:35 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-06-01 17:59:40 -04:00
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
|
|
|
xioutil "github.com/minio/minio/internal/ioutil"
|
|
|
|
"github.com/minio/minio/internal/logger"
|
2020-10-28 12:18:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// WalkDirOptions provides options for WalkDir operations.
|
|
|
|
type WalkDirOptions struct {
|
2021-02-26 18:11:42 -05:00
|
|
|
// Bucket to scanner
|
2020-10-28 12:18:35 -04:00
|
|
|
Bucket string
|
|
|
|
|
|
|
|
// Directory inside the bucket.
|
|
|
|
BaseDir string
|
|
|
|
|
|
|
|
// Do a full recursive scan.
|
|
|
|
Recursive bool
|
2020-11-18 13:44:18 -05:00
|
|
|
|
2020-12-01 15:07:39 -05:00
|
|
|
// ReportNotFound will return errFileNotFound if all disks reports the BaseDir cannot be found.
|
|
|
|
ReportNotFound bool
|
|
|
|
|
2020-11-18 13:44:18 -05:00
|
|
|
// FilterPrefix will only return results with given prefix within folder.
|
|
|
|
// Should never contain a slash.
|
|
|
|
FilterPrefix string
|
2021-02-18 14:06:54 -05:00
|
|
|
|
|
|
|
// ForwardTo will forward to the given object path.
|
|
|
|
ForwardTo string
|
2020-10-28 12:18:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WalkDir will traverse a directory and return all entries found.
|
|
|
|
// On success a sorted meta cache stream will be returned.
|
2021-03-29 20:00:55 -04:00
|
|
|
// Metadata has data stripped, if any.
|
2021-06-15 17:34:26 -04:00
|
|
|
func (s *xlStorage) WalkDir(ctx context.Context, opts WalkDirOptions, wr io.Writer) (err error) {
|
2020-10-28 12:18:35 -04:00
|
|
|
// Verify if volume is valid and it exists.
|
|
|
|
volumeDir, err := s.getVolDir(opts.Bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat a volume entry.
|
2021-07-25 01:03:38 -04:00
|
|
|
if err = Access(volumeDir); err != nil {
|
2020-11-23 11:36:49 -05:00
|
|
|
if osIsNotExist(err) {
|
2020-10-28 12:18:35 -04:00
|
|
|
return errVolumeNotFound
|
|
|
|
} else if isSysErrIO(err) {
|
|
|
|
return errFaultyDisk
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use a small block size to start sending quickly
|
|
|
|
w := newMetacacheWriter(wr, 16<<10)
|
2021-08-12 17:27:22 -04:00
|
|
|
w.reuseBlocks = true // We are not sharing results, so reuse buffers.
|
2020-10-28 12:18:35 -04:00
|
|
|
defer w.Close()
|
|
|
|
out, err := w.stream()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer close(out)
|
|
|
|
|
2021-02-05 13:12:25 -05:00
|
|
|
// Fast exit track to check if we are listing an object with
|
|
|
|
// a trailing slash, this will avoid to list the object content.
|
|
|
|
if HasSuffix(opts.BaseDir, SlashSeparator) {
|
2021-09-17 17:11:01 -04:00
|
|
|
metadata, err := s.readMetadata(ctx, pathJoin(volumeDir,
|
2021-02-05 13:12:25 -05:00
|
|
|
opts.BaseDir[:len(opts.BaseDir)-1]+globalDirSuffix,
|
|
|
|
xlStorageFormatFile))
|
|
|
|
if err == nil {
|
|
|
|
// if baseDir is already a directory object, consider it
|
|
|
|
// as part of the list call, this is a AWS S3 specific
|
|
|
|
// behavior.
|
|
|
|
out <- metaCacheEntry{
|
|
|
|
name: opts.BaseDir,
|
2021-05-21 14:41:25 -04:00
|
|
|
metadata: metadata,
|
2021-02-05 13:12:25 -05:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-25 01:03:38 -04:00
|
|
|
st, sterr := Lstat(pathJoin(volumeDir, opts.BaseDir, xlStorageFormatFile))
|
|
|
|
if sterr == nil && st.Mode().IsRegular() {
|
2021-02-05 13:12:25 -05:00
|
|
|
return errFileNotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 13:44:18 -05:00
|
|
|
prefix := opts.FilterPrefix
|
2020-10-28 12:18:35 -04:00
|
|
|
var scanDir func(path string) error
|
2021-07-05 18:34:41 -04:00
|
|
|
|
2020-10-28 12:18:35 -04:00
|
|
|
scanDir = func(current string) error {
|
2021-07-05 18:34:41 -04:00
|
|
|
// Skip forward, if requested...
|
|
|
|
forward := ""
|
|
|
|
if len(opts.ForwardTo) > 0 && strings.HasPrefix(opts.ForwardTo, current) {
|
|
|
|
forward = strings.TrimPrefix(opts.ForwardTo, current)
|
|
|
|
if idx := strings.IndexByte(forward, '/'); idx > 0 {
|
|
|
|
forward = forward[:idx]
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 14:18:30 -04:00
|
|
|
if contextCanceled(ctx) {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2021-09-17 15:14:12 -04:00
|
|
|
|
2021-08-18 21:10:36 -04:00
|
|
|
s.walkMu.Lock()
|
2020-10-28 12:18:35 -04:00
|
|
|
entries, err := s.ListDir(ctx, opts.Bucket, current, -1)
|
2021-08-18 21:10:36 -04:00
|
|
|
s.walkMu.Unlock()
|
2020-10-28 12:18:35 -04:00
|
|
|
if err != nil {
|
|
|
|
// Folder could have gone away in-between
|
|
|
|
if err != errVolumeNotFound && err != errFileNotFound {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2020-12-01 15:07:39 -05:00
|
|
|
if opts.ReportNotFound && err == errFileNotFound && current == opts.BaseDir {
|
|
|
|
return errFileNotFound
|
|
|
|
}
|
2020-10-28 12:18:35 -04:00
|
|
|
// Forward some errors?
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-25 01:03:38 -04:00
|
|
|
if len(entries) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-12 16:09:34 -05:00
|
|
|
dirObjects := make(map[string]struct{})
|
2020-10-28 12:18:35 -04:00
|
|
|
for i, entry := range entries {
|
2021-08-18 10:40:53 -04:00
|
|
|
if len(prefix) > 0 && !strings.HasPrefix(entry, prefix) {
|
|
|
|
// Do do not retain the file, since it doesn't
|
|
|
|
// match the prefix.
|
|
|
|
entries[i] = ""
|
|
|
|
continue
|
|
|
|
}
|
2021-02-18 14:06:54 -05:00
|
|
|
if len(forward) > 0 && entry < forward {
|
2021-08-18 10:40:53 -04:00
|
|
|
// Do do not retain the file, since its
|
|
|
|
// lexially smaller than 'forward'
|
|
|
|
entries[i] = ""
|
2021-02-18 14:06:54 -05:00
|
|
|
continue
|
|
|
|
}
|
2020-10-28 12:18:35 -04:00
|
|
|
if strings.HasSuffix(entry, slashSeparator) {
|
2020-11-12 16:09:34 -05:00
|
|
|
if strings.HasSuffix(entry, globalDirSuffixWithSlash) {
|
|
|
|
// Add without extension so it is sorted correctly.
|
|
|
|
entry = strings.TrimSuffix(entry, globalDirSuffixWithSlash) + slashSeparator
|
|
|
|
dirObjects[entry] = struct{}{}
|
|
|
|
entries[i] = entry
|
|
|
|
continue
|
|
|
|
}
|
2020-10-28 12:18:35 -04:00
|
|
|
// Trim slash, maybe compiler is clever?
|
|
|
|
entries[i] = entries[i][:len(entry)-1]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Do do not retain the file.
|
|
|
|
entries[i] = ""
|
|
|
|
|
2021-03-26 14:18:30 -04:00
|
|
|
if contextCanceled(ctx) {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2020-10-28 12:18:35 -04:00
|
|
|
// If root was an object return it as such.
|
|
|
|
if HasSuffix(entry, xlStorageFormatFile) {
|
|
|
|
var meta metaCacheEntry
|
2021-09-17 15:14:12 -04:00
|
|
|
s.walkReadMu.Lock()
|
2021-09-17 17:11:01 -04:00
|
|
|
meta.metadata, err = s.readMetadata(ctx, pathJoin(volumeDir, current, entry))
|
2021-09-17 15:14:12 -04:00
|
|
|
s.walkReadMu.Unlock()
|
2021-05-21 12:10:54 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-11-13 19:58:20 -05:00
|
|
|
meta.name = strings.TrimSuffix(entry, xlStorageFormatFile)
|
2020-10-28 12:18:35 -04:00
|
|
|
meta.name = strings.TrimSuffix(meta.name, SlashSeparator)
|
2020-11-13 19:58:20 -05:00
|
|
|
meta.name = pathJoin(current, meta.name)
|
2020-11-12 16:09:34 -05:00
|
|
|
meta.name = decodeDirObject(meta.name)
|
2020-10-28 12:18:35 -04:00
|
|
|
out <- meta
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Check legacy.
|
|
|
|
if HasSuffix(entry, xlStorageFormatFileV1) {
|
|
|
|
var meta metaCacheEntry
|
2021-09-17 15:14:12 -04:00
|
|
|
s.walkReadMu.Lock()
|
2021-02-24 03:14:16 -05:00
|
|
|
meta.metadata, err = xioutil.ReadFile(pathJoin(volumeDir, current, entry))
|
2021-09-17 15:14:12 -04:00
|
|
|
s.walkReadMu.Unlock()
|
2020-10-28 12:18:35 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-11-13 19:58:20 -05:00
|
|
|
meta.name = strings.TrimSuffix(entry, xlStorageFormatFileV1)
|
2020-10-28 12:18:35 -04:00
|
|
|
meta.name = strings.TrimSuffix(meta.name, SlashSeparator)
|
2020-11-13 19:58:20 -05:00
|
|
|
meta.name = pathJoin(current, meta.name)
|
2020-10-28 12:18:35 -04:00
|
|
|
out <- meta
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Skip all other files.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process in sort order.
|
|
|
|
sort.Strings(entries)
|
|
|
|
dirStack := make([]string, 0, 5)
|
2021-08-18 10:40:53 -04:00
|
|
|
prefix = "" // Remove prefix after first level as we have already filtered the list.
|
2021-07-05 18:34:41 -04:00
|
|
|
if len(forward) > 0 {
|
|
|
|
idx := sort.SearchStrings(entries, forward)
|
|
|
|
if idx > 0 {
|
|
|
|
entries = entries[idx:]
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 14:06:54 -05:00
|
|
|
|
2020-10-28 12:18:35 -04:00
|
|
|
for _, entry := range entries {
|
|
|
|
if entry == "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-26 14:18:30 -04:00
|
|
|
if contextCanceled(ctx) {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2020-10-28 12:18:35 -04:00
|
|
|
meta := metaCacheEntry{name: PathJoin(current, entry)}
|
|
|
|
|
|
|
|
// If directory entry on stack before this, pop it now.
|
|
|
|
for len(dirStack) > 0 && dirStack[len(dirStack)-1] < meta.name {
|
|
|
|
pop := dirStack[len(dirStack)-1]
|
|
|
|
out <- metaCacheEntry{name: pop}
|
|
|
|
if opts.Recursive {
|
|
|
|
// Scan folder we found. Should be in correct sort order where we are.
|
2021-02-18 14:06:54 -05:00
|
|
|
forward = ""
|
|
|
|
if len(opts.ForwardTo) > 0 && strings.HasPrefix(opts.ForwardTo, pop) {
|
|
|
|
forward = strings.TrimPrefix(opts.ForwardTo, pop)
|
|
|
|
}
|
|
|
|
logger.LogIf(ctx, scanDir(pop))
|
2020-10-28 12:18:35 -04:00
|
|
|
}
|
|
|
|
dirStack = dirStack[:len(dirStack)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// All objects will be returned as directories, there has been no object check yet.
|
|
|
|
// Check it by attempting to read metadata.
|
2020-11-12 16:09:34 -05:00
|
|
|
_, isDirObj := dirObjects[entry]
|
|
|
|
if isDirObj {
|
|
|
|
meta.name = meta.name[:len(meta.name)-1] + globalDirSuffixWithSlash
|
|
|
|
}
|
|
|
|
|
2021-09-17 15:14:12 -04:00
|
|
|
s.walkReadMu.Lock()
|
2021-09-17 17:11:01 -04:00
|
|
|
meta.metadata, err = s.readMetadata(ctx, pathJoin(volumeDir, meta.name, xlStorageFormatFile))
|
2021-09-17 15:14:12 -04:00
|
|
|
s.walkReadMu.Unlock()
|
2020-10-28 12:18:35 -04:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
// It was an object
|
2020-11-12 16:09:34 -05:00
|
|
|
if isDirObj {
|
|
|
|
meta.name = strings.TrimSuffix(meta.name, globalDirSuffixWithSlash) + slashSeparator
|
|
|
|
}
|
2020-10-28 12:18:35 -04:00
|
|
|
out <- meta
|
2021-08-21 03:12:29 -04:00
|
|
|
case osIsNotExist(err), isSysErrIsDir(err):
|
2021-02-24 03:14:16 -05:00
|
|
|
meta.metadata, err = xioutil.ReadFile(pathJoin(volumeDir, meta.name, xlStorageFormatFileV1))
|
2020-10-28 12:18:35 -04:00
|
|
|
if err == nil {
|
|
|
|
// It was an object
|
|
|
|
out <- meta
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOT an object, append to stack (with slash)
|
2020-11-12 16:09:34 -05:00
|
|
|
// If dirObject, but no metadata (which is unexpected) we skip it.
|
|
|
|
if !isDirObj {
|
2021-02-03 17:06:54 -05:00
|
|
|
if !isDirEmpty(pathJoin(volumeDir, meta.name+slashSeparator)) {
|
|
|
|
dirStack = append(dirStack, meta.name+slashSeparator)
|
|
|
|
}
|
2020-11-12 16:09:34 -05:00
|
|
|
}
|
2020-11-19 12:15:09 -05:00
|
|
|
case isSysErrNotDir(err):
|
|
|
|
// skip
|
2020-10-28 12:18:35 -04:00
|
|
|
default:
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
|
|
|
}
|
2021-07-25 01:03:38 -04:00
|
|
|
|
2020-10-28 12:18:35 -04:00
|
|
|
// If directory entry left on stack, pop it now.
|
|
|
|
for len(dirStack) > 0 {
|
|
|
|
pop := dirStack[len(dirStack)-1]
|
|
|
|
out <- metaCacheEntry{name: pop}
|
|
|
|
if opts.Recursive {
|
|
|
|
// Scan folder we found. Should be in correct sort order where we are.
|
2021-02-18 14:06:54 -05:00
|
|
|
logger.LogIf(ctx, scanDir(pop))
|
2020-10-28 12:18:35 -04:00
|
|
|
}
|
|
|
|
dirStack = dirStack[:len(dirStack)-1]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream output.
|
|
|
|
return scanDir(opts.BaseDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *xlStorageDiskIDCheck) WalkDir(ctx context.Context, opts WalkDirOptions, wr io.Writer) error {
|
2021-06-15 17:34:26 -04:00
|
|
|
defer p.updateStorageMetrics(storageMetricWalkDir, opts.Bucket, opts.BaseDir)()
|
2021-09-17 17:11:01 -04:00
|
|
|
|
|
|
|
if contextCanceled(ctx) {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2020-10-28 12:18:35 -04:00
|
|
|
if err := p.checkDiskStale(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-17 17:11:01 -04:00
|
|
|
|
2020-10-28 12:18:35 -04:00
|
|
|
return p.storage.WalkDir(ctx, opts, wr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WalkDir will traverse a directory and return all entries found.
|
|
|
|
// On success a meta cache stream will be returned, that should be closed when done.
|
|
|
|
func (client *storageRESTClient) WalkDir(ctx context.Context, opts WalkDirOptions, wr io.Writer) error {
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, opts.Bucket)
|
|
|
|
values.Set(storageRESTDirPath, opts.BaseDir)
|
|
|
|
values.Set(storageRESTRecursive, strconv.FormatBool(opts.Recursive))
|
2020-12-28 13:31:00 -05:00
|
|
|
values.Set(storageRESTReportNotFound, strconv.FormatBool(opts.ReportNotFound))
|
2020-11-18 13:44:18 -05:00
|
|
|
values.Set(storageRESTPrefixFilter, opts.FilterPrefix)
|
2021-02-18 14:06:54 -05:00
|
|
|
values.Set(storageRESTForwardFilter, opts.ForwardTo)
|
2020-10-28 12:18:35 -04:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodWalkDir, values, nil, -1)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
|
|
|
}
|
2021-04-26 11:59:54 -04:00
|
|
|
defer xhttp.DrainBody(respBody)
|
2020-10-28 12:18:35 -04:00
|
|
|
return waitForHTTPStream(respBody, wr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WalkDirHandler - remote caller to list files and folders in a requested directory path.
|
|
|
|
func (s *storageRESTServer) WalkDirHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !s.IsValid(w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
volume := vars[storageRESTVolume]
|
|
|
|
dirPath := vars[storageRESTDirPath]
|
|
|
|
recursive, err := strconv.ParseBool(vars[storageRESTRecursive])
|
|
|
|
if err != nil {
|
|
|
|
s.writeErrorResponse(w, err)
|
|
|
|
return
|
|
|
|
}
|
2020-12-28 13:31:00 -05:00
|
|
|
|
|
|
|
var reportNotFound bool
|
2021-08-18 21:05:05 -04:00
|
|
|
if v := r.Form.Get(storageRESTReportNotFound); v != "" {
|
2020-12-28 13:31:00 -05:00
|
|
|
reportNotFound, err = strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
|
|
|
s.writeErrorResponse(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-08 01:43:01 -04:00
|
|
|
prefix := r.Form.Get(storageRESTPrefixFilter)
|
|
|
|
forward := r.Form.Get(storageRESTForwardFilter)
|
2020-10-28 12:18:35 -04:00
|
|
|
writer := streamHTTPResponse(w)
|
2020-11-18 13:44:18 -05:00
|
|
|
writer.CloseWithError(s.storage.WalkDir(r.Context(), WalkDirOptions{
|
2020-12-28 13:31:00 -05:00
|
|
|
Bucket: volume,
|
|
|
|
BaseDir: dirPath,
|
|
|
|
Recursive: recursive,
|
|
|
|
ReportNotFound: reportNotFound,
|
|
|
|
FilterPrefix: prefix,
|
2021-02-18 14:06:54 -05:00
|
|
|
ForwardTo: forward,
|
2020-11-18 13:44:18 -05:00
|
|
|
}, writer))
|
2020-10-28 12:18:35 -04:00
|
|
|
}
|