mirror of
https://github.com/minio/minio.git
synced 2025-01-11 15:03:22 -05:00
Add option in readDir to enable symlink following of dirs (#12668)
This commit is contained in:
parent
da0fd5f056
commit
9be040dd14
@ -534,7 +534,7 @@ func (fs *FSObjects) ListBuckets(ctx context.Context) ([]BucketInfo, error) {
|
|||||||
atomic.AddInt64(&fs.activeIOCount, -1)
|
atomic.AddInt64(&fs.activeIOCount, -1)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
entries, err := readDir(fs.fsPath)
|
entries, err := readDirWithOpts(fs.fsPath, readDirOpts{count: -1, followDirSymlink: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LogIf(ctx, errDiskNotFound)
|
logger.LogIf(ctx, errDiskNotFound)
|
||||||
return nil, toObjectErr(errDiskNotFound)
|
return nil, toObjectErr(errDiskNotFound)
|
||||||
|
36
cmd/os-readdir-common.go
Normal file
36
cmd/os-readdir-common.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
// 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/>.
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
// Options for readDir function call
|
||||||
|
type readDirOpts struct {
|
||||||
|
// The maximum number of entries to return
|
||||||
|
count int
|
||||||
|
// Follow directory symlink
|
||||||
|
followDirSymlink bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return all the entries at the directory dirPath.
|
||||||
|
func readDir(dirPath string) (entries []string, err error) {
|
||||||
|
return readDirWithOpts(dirPath, readDirOpts{count: -1})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return up to count entries at the directory dirPath.
|
||||||
|
func readDirN(dirPath string, count int) (entries []string, err error) {
|
||||||
|
return readDirWithOpts(dirPath, readDirOpts{count: count})
|
||||||
|
}
|
@ -30,11 +30,6 @@ func access(name string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return all the entries at the directory dirPath.
|
|
||||||
func readDir(dirPath string) (entries []string, err error) {
|
|
||||||
return readDirN(dirPath, -1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// readDirFn applies the fn() function on each entries at dirPath, doesn't recurse into
|
// readDirFn applies the fn() function on each entries at dirPath, doesn't recurse into
|
||||||
// the directory itself, if the dirPath doesn't exist this function doesn't return
|
// the directory itself, if the dirPath doesn't exist this function doesn't return
|
||||||
// an error.
|
// an error.
|
||||||
@ -90,8 +85,8 @@ func readDirFn(dirPath string, filter func(name string, typ os.FileMode) error)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return N entries at the directory dirPath. If count is -1, return all entries
|
// Return entries at the directory dirPath.
|
||||||
func readDirN(dirPath string, count int) (entries []string, err error) {
|
func readDirWithOpts(dirPath string, opts readDirOpts) (entries []string, err error) {
|
||||||
d, err := Open(dirPath)
|
d, err := Open(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, osErrToFileErr(err)
|
return nil, osErrToFileErr(err)
|
||||||
@ -99,12 +94,12 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
|
|||||||
defer d.Close()
|
defer d.Close()
|
||||||
|
|
||||||
maxEntries := 1000
|
maxEntries := 1000
|
||||||
if count > 0 && count < maxEntries {
|
if opts.count > 0 && opts.count < maxEntries {
|
||||||
maxEntries = count
|
maxEntries = count
|
||||||
}
|
}
|
||||||
|
|
||||||
done := false
|
done := false
|
||||||
remaining := count
|
remaining := opts.count
|
||||||
|
|
||||||
for !done {
|
for !done {
|
||||||
// Read up to max number of entries.
|
// Read up to max number of entries.
|
||||||
@ -115,7 +110,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
|
|||||||
}
|
}
|
||||||
return nil, osErrToFileErr(err)
|
return nil, osErrToFileErr(err)
|
||||||
}
|
}
|
||||||
if count > -1 {
|
if opts.count > -1 {
|
||||||
if remaining <= len(fis) {
|
if remaining <= len(fis) {
|
||||||
fis = fis[:remaining]
|
fis = fis[:remaining]
|
||||||
done = true
|
done = true
|
||||||
@ -136,7 +131,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ignore symlinked directories.
|
// Ignore symlinked directories.
|
||||||
if fi.IsDir() {
|
if !opts.followDirSymlink && fi.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -147,7 +142,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
|
|||||||
} else if fi.Mode().IsRegular() {
|
} else if fi.Mode().IsRegular() {
|
||||||
entries = append(entries, fi.Name())
|
entries = append(entries, fi.Name())
|
||||||
}
|
}
|
||||||
if count > 0 {
|
if opts.count > 0 {
|
||||||
remaining--
|
remaining--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,11 +98,6 @@ func parseDirEnt(buf []byte) (consumed int, name []byte, typ os.FileMode, err er
|
|||||||
return consumed, nameBuf[:nameLen], typ, nil
|
return consumed, nameBuf[:nameLen], typ, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return all the entries at the directory dirPath.
|
|
||||||
func readDir(dirPath string) (entries []string, err error) {
|
|
||||||
return readDirN(dirPath, -1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// readDirFn applies the fn() function on each entries at dirPath, doesn't recurse into
|
// readDirFn applies the fn() function on each entries at dirPath, doesn't recurse into
|
||||||
// the directory itself, if the dirPath doesn't exist this function doesn't return
|
// the directory itself, if the dirPath doesn't exist this function doesn't return
|
||||||
// an error.
|
// an error.
|
||||||
@ -184,7 +179,7 @@ func readDirFn(dirPath string, fn func(name string, typ os.FileMode) error) erro
|
|||||||
|
|
||||||
// Return count entries at the directory dirPath and all entries
|
// Return count entries at the directory dirPath and all entries
|
||||||
// if count is set to -1
|
// if count is set to -1
|
||||||
func readDirN(dirPath string, count int) (entries []string, err error) {
|
func readDirWithOpts(dirPath string, opts readDirOpts) (entries []string, err error) {
|
||||||
f, err := Open(dirPath)
|
f, err := Open(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, osErrToFileErr(err)
|
return nil, osErrToFileErr(err)
|
||||||
@ -202,6 +197,8 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
|
|||||||
boff := 0 // starting read position in buf
|
boff := 0 // starting read position in buf
|
||||||
nbuf := 0 // end valid data in buf
|
nbuf := 0 // end valid data in buf
|
||||||
|
|
||||||
|
count := opts.count
|
||||||
|
|
||||||
for count != 0 {
|
for count != 0 {
|
||||||
if boff >= nbuf {
|
if boff >= nbuf {
|
||||||
boff = 0
|
boff = 0
|
||||||
@ -242,7 +239,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ignore symlinked directories.
|
// Ignore symlinked directories.
|
||||||
if typ&os.ModeSymlink == os.ModeSymlink && fi.IsDir() {
|
if !opts.followDirSymlink && typ&os.ModeSymlink == os.ModeSymlink && fi.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,11 +30,6 @@ func access(name string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return all the entries at the directory dirPath.
|
|
||||||
func readDir(dirPath string) (entries []string, err error) {
|
|
||||||
return readDirN(dirPath, -1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// readDirFn applies the fn() function on each entries at dirPath, doesn't recurse into
|
// readDirFn applies the fn() function on each entries at dirPath, doesn't recurse into
|
||||||
// the directory itself, if the dirPath doesn't exist this function doesn't return
|
// the directory itself, if the dirPath doesn't exist this function doesn't return
|
||||||
// an error.
|
// an error.
|
||||||
@ -118,8 +113,8 @@ func readDirFn(dirPath string, filter func(name string, typ os.FileMode) error)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return N entries at the directory dirPath. If count is -1, return all entries
|
// Return N entries at the directory dirPath.
|
||||||
func readDirN(dirPath string, count int) (entries []string, err error) {
|
func readDirWithOpts(dirPath string, opts readDirOpts) (entries []string, err error) {
|
||||||
f, err := os.Open(dirPath)
|
f, err := os.Open(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, osErrToFileErr(err)
|
return nil, osErrToFileErr(err)
|
||||||
@ -138,6 +133,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
|
|||||||
data := &syscall.Win32finddata{}
|
data := &syscall.Win32finddata{}
|
||||||
handle := syscall.Handle(f.Fd())
|
handle := syscall.Handle(f.Fd())
|
||||||
|
|
||||||
|
count := opts.count
|
||||||
for count != 0 {
|
for count != 0 {
|
||||||
e := syscall.FindNextFile(handle, data)
|
e := syscall.FindNextFile(handle, data)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
@ -172,7 +168,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if fi.IsDir() {
|
if !opts.followDirSymlink && fi.IsDir() {
|
||||||
// directory symlinks are ignored.
|
// directory symlinks are ignored.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user