2019-08-09 17:54:11 +02:00
|
|
|
// +build linux,!appengine darwin freebsd netbsd openbsd
|
2016-04-05 02:27:55 +02:00
|
|
|
|
|
|
|
/*
|
2020-04-23 21:26:13 +02:00
|
|
|
* MinIO Cloud Storage, (C) 2016-2020 MinIO, Inc.
|
2016-04-05 02:27:55 +02: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-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-04-05 02:27:55 +02:00
|
|
|
|
|
|
|
import (
|
2020-09-15 02:19:54 +02:00
|
|
|
"bytes"
|
2019-08-09 17:54:11 +02:00
|
|
|
"fmt"
|
2016-04-05 02:27:55 +02:00
|
|
|
"os"
|
2020-06-09 18:44:50 +02:00
|
|
|
"sync"
|
2016-04-05 02:27:55 +02:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2021-03-29 17:07:23 +02:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2016-04-05 02:27:55 +02:00
|
|
|
)
|
|
|
|
|
2021-03-29 17:07:23 +02:00
|
|
|
func access(name string) error {
|
|
|
|
if err := unix.Access(name, unix.R_OK|unix.W_OK); err != nil {
|
|
|
|
return &os.PathError{Op: "lstat", Path: name, Err: err}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:54:11 +02:00
|
|
|
// The buffer must be at least a block long.
|
|
|
|
// refer https://github.com/golang/go/issues/24015
|
2020-06-09 18:44:50 +02:00
|
|
|
const blockSize = 8 << 10 // 8192
|
|
|
|
|
2020-11-18 18:21:02 +01:00
|
|
|
// By default atleast 128 entries in single getdents call (1MiB buffer)
|
2020-06-09 18:44:50 +02:00
|
|
|
var direntPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
2020-11-18 18:21:02 +01:00
|
|
|
buf := make([]byte, blockSize*128)
|
2020-06-09 18:44:50 +02:00
|
|
|
return &buf
|
|
|
|
},
|
|
|
|
}
|
2016-04-05 02:27:55 +02:00
|
|
|
|
2019-08-09 17:54:11 +02:00
|
|
|
// unexpectedFileMode is a sentinel (and bogus) os.FileMode
|
|
|
|
// value used to represent a syscall.DT_UNKNOWN Dirent.Type.
|
|
|
|
const unexpectedFileMode os.FileMode = os.ModeNamedPipe | os.ModeSocket | os.ModeDevice
|
2016-04-08 20:46:03 +02:00
|
|
|
|
2020-09-15 02:19:54 +02:00
|
|
|
func parseDirEnt(buf []byte) (consumed int, name []byte, typ os.FileMode, err error) {
|
2019-08-09 17:54:11 +02:00
|
|
|
// golang.org/issue/15653
|
|
|
|
dirent := (*syscall.Dirent)(unsafe.Pointer(&buf[0]))
|
|
|
|
if v := unsafe.Offsetof(dirent.Reclen) + unsafe.Sizeof(dirent.Reclen); uintptr(len(buf)) < v {
|
2020-09-15 02:19:54 +02:00
|
|
|
return consumed, nil, typ, fmt.Errorf("buf size of %d smaller than dirent header size %d", len(buf), v)
|
2019-08-09 17:54:11 +02:00
|
|
|
}
|
|
|
|
if len(buf) < int(dirent.Reclen) {
|
2020-09-15 02:19:54 +02:00
|
|
|
return consumed, nil, typ, fmt.Errorf("buf size %d < record length %d", len(buf), dirent.Reclen)
|
2019-08-09 17:54:11 +02:00
|
|
|
}
|
|
|
|
consumed = int(dirent.Reclen)
|
|
|
|
if direntInode(dirent) == 0 { // File absent in directory.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch dirent.Type {
|
|
|
|
case syscall.DT_REG:
|
|
|
|
typ = 0
|
|
|
|
case syscall.DT_DIR:
|
|
|
|
typ = os.ModeDir
|
|
|
|
case syscall.DT_LNK:
|
|
|
|
typ = os.ModeSymlink
|
|
|
|
default:
|
|
|
|
// Skip all other file types. Revisit if/when this code needs
|
|
|
|
// to handle such files, MinIO is only interested in
|
|
|
|
// files and directories.
|
|
|
|
typ = unexpectedFileMode
|
|
|
|
}
|
2016-05-13 20:39:48 +02:00
|
|
|
|
2019-08-09 17:54:11 +02:00
|
|
|
nameBuf := (*[unsafe.Sizeof(dirent.Name)]byte)(unsafe.Pointer(&dirent.Name[0]))
|
|
|
|
nameLen, err := direntNamlen(dirent)
|
|
|
|
if err != nil {
|
2020-09-15 02:19:54 +02:00
|
|
|
return consumed, nil, typ, err
|
2016-04-05 02:27:55 +02:00
|
|
|
}
|
|
|
|
|
2020-09-15 02:19:54 +02:00
|
|
|
return consumed, nameBuf[:nameLen], typ, nil
|
2016-10-27 02:14:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-05 21:51:56 +02:00
|
|
|
// Return all the entries at the directory dirPath.
|
|
|
|
func readDir(dirPath string) (entries []string, err error) {
|
2018-05-09 04:08:21 +02:00
|
|
|
return readDirN(dirPath, -1)
|
|
|
|
}
|
|
|
|
|
2021-02-18 00:34:42 +01:00
|
|
|
// 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
|
|
|
|
// an error.
|
|
|
|
func readDirFn(dirPath string, fn func(name string, typ os.FileMode) error) error {
|
2020-06-13 05:04:01 +02:00
|
|
|
f, err := os.Open(dirPath)
|
2020-04-23 21:26:13 +02:00
|
|
|
if err != nil {
|
2021-02-18 00:34:42 +01:00
|
|
|
if osErrToFileErr(err) == errFileNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-13 05:04:01 +02:00
|
|
|
return osErrToFileErr(err)
|
2020-04-23 21:26:13 +02:00
|
|
|
}
|
2020-06-13 05:04:01 +02:00
|
|
|
defer f.Close()
|
2020-04-23 21:26:13 +02:00
|
|
|
|
2020-06-09 18:44:50 +02:00
|
|
|
buf := make([]byte, blockSize)
|
|
|
|
boff := 0 // starting read position in buf
|
|
|
|
nbuf := 0 // end valid data in buf
|
2020-04-23 21:26:13 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
if boff >= nbuf {
|
|
|
|
boff = 0
|
2020-06-13 05:04:01 +02:00
|
|
|
nbuf, err = syscall.ReadDirent(int(f.Fd()), buf)
|
2020-04-23 21:26:13 +02:00
|
|
|
if err != nil {
|
|
|
|
if isSysErrNotDir(err) {
|
2021-02-18 00:34:42 +01:00
|
|
|
return nil
|
2020-04-23 21:26:13 +02:00
|
|
|
}
|
2021-02-24 09:14:16 +01:00
|
|
|
err = osErrToFileErr(err)
|
|
|
|
if err == errFileNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-23 21:26:13 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if nbuf <= 0 {
|
2020-06-09 18:44:50 +02:00
|
|
|
break // EOF
|
2020-04-23 21:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
consumed, name, typ, err := parseDirEnt(buf[boff:nbuf])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
boff += consumed
|
2020-09-15 02:19:54 +02:00
|
|
|
if len(name) == 0 || bytes.Equal(name, []byte{'.'}) || bytes.Equal(name, []byte{'.', '.'}) {
|
2020-04-23 21:26:13 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-02-20 09:30:12 +01:00
|
|
|
|
|
|
|
// Fallback for filesystems (like old XFS) that don't
|
|
|
|
// support Dirent.Type and have DT_UNKNOWN (0) there
|
|
|
|
// instead.
|
|
|
|
if typ == unexpectedFileMode || typ&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
fi, err := os.Stat(pathJoin(dirPath, string(name)))
|
|
|
|
if err != nil {
|
|
|
|
// It got deleted in the meantime, not found
|
|
|
|
// or returns too many symlinks ignore this
|
|
|
|
// file/directory.
|
|
|
|
if osIsNotExist(err) || isSysErrPathNotFound(err) ||
|
|
|
|
isSysErrTooManySymlinks(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore symlinked directories.
|
|
|
|
if typ&os.ModeSymlink == os.ModeSymlink && fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
typ = fi.Mode() & os.ModeType
|
2020-04-23 21:26:13 +02:00
|
|
|
}
|
2021-02-18 00:34:42 +01:00
|
|
|
if err = fn(string(name), typ); err == errDoneForNow {
|
|
|
|
// fn() requested to return by caller.
|
2020-04-23 21:26:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-09 04:08:21 +02:00
|
|
|
// Return count entries at the directory dirPath and all entries
|
|
|
|
// if count is set to -1
|
|
|
|
func readDirN(dirPath string, count int) (entries []string, err error) {
|
2020-06-13 05:04:01 +02:00
|
|
|
f, err := os.Open(dirPath)
|
2016-04-08 20:46:03 +02:00
|
|
|
if err != nil {
|
2020-06-13 05:04:01 +02:00
|
|
|
return nil, osErrToFileErr(err)
|
2016-04-08 20:46:03 +02:00
|
|
|
}
|
2020-06-13 05:04:01 +02:00
|
|
|
defer f.Close()
|
2018-05-09 04:08:21 +02:00
|
|
|
|
2020-06-09 18:44:50 +02:00
|
|
|
bufp := direntPool.Get().(*[]byte)
|
|
|
|
defer direntPool.Put(bufp)
|
|
|
|
|
2020-09-15 02:19:54 +02:00
|
|
|
nameTmp := direntPool.Get().(*[]byte)
|
|
|
|
defer direntPool.Put(nameTmp)
|
|
|
|
tmp := *nameTmp
|
|
|
|
|
2020-06-09 18:44:50 +02:00
|
|
|
boff := 0 // starting read position in buf
|
|
|
|
nbuf := 0 // end valid data in buf
|
2018-05-09 04:08:21 +02:00
|
|
|
|
2019-08-09 17:54:11 +02:00
|
|
|
for count != 0 {
|
|
|
|
if boff >= nbuf {
|
|
|
|
boff = 0
|
2020-06-13 05:04:01 +02:00
|
|
|
nbuf, err = syscall.ReadDirent(int(f.Fd()), *bufp)
|
2019-08-09 17:54:11 +02:00
|
|
|
if err != nil {
|
|
|
|
if isSysErrNotDir(err) {
|
|
|
|
return nil, errFileNotFound
|
|
|
|
}
|
2021-02-24 09:14:16 +01:00
|
|
|
return nil, osErrToFileErr(err)
|
2019-08-09 17:54:11 +02:00
|
|
|
}
|
|
|
|
if nbuf <= 0 {
|
|
|
|
break
|
2018-07-14 08:41:48 +02:00
|
|
|
}
|
2016-04-08 20:46:03 +02:00
|
|
|
}
|
2020-06-09 18:44:50 +02:00
|
|
|
consumed, name, typ, err := parseDirEnt((*bufp)[boff:nbuf])
|
2019-08-09 17:54:11 +02:00
|
|
|
if err != nil {
|
2016-05-05 21:51:56 +02:00
|
|
|
return nil, err
|
2016-04-08 20:46:03 +02:00
|
|
|
}
|
2019-08-09 17:54:11 +02:00
|
|
|
boff += consumed
|
2020-09-15 02:19:54 +02:00
|
|
|
if len(name) == 0 || bytes.Equal(name, []byte{'.'}) || bytes.Equal(name, []byte{'.', '.'}) {
|
2019-08-09 17:54:11 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-02-20 09:30:12 +01:00
|
|
|
|
2019-08-09 17:54:11 +02:00
|
|
|
// Fallback for filesystems (like old XFS) that don't
|
|
|
|
// support Dirent.Type and have DT_UNKNOWN (0) there
|
|
|
|
// instead.
|
2021-02-20 09:30:12 +01:00
|
|
|
if typ == unexpectedFileMode || typ&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
fi, err := os.Stat(pathJoin(dirPath, string(name)))
|
2019-08-09 17:54:11 +02:00
|
|
|
if err != nil {
|
2020-01-21 23:07:49 +01:00
|
|
|
// It got deleted in the meantime, not found
|
|
|
|
// or returns too many symlinks ignore this
|
|
|
|
// file/directory.
|
2020-11-23 17:36:49 +01:00
|
|
|
if osIsNotExist(err) || isSysErrPathNotFound(err) ||
|
2020-01-21 23:07:49 +01:00
|
|
|
isSysErrTooManySymlinks(err) {
|
2019-08-09 17:54:11 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
2018-05-09 04:08:21 +02:00
|
|
|
}
|
2021-02-20 09:30:12 +01:00
|
|
|
|
|
|
|
// Ignore symlinked directories.
|
|
|
|
if typ&os.ModeSymlink == os.ModeSymlink && fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:54:11 +02:00
|
|
|
typ = fi.Mode() & os.ModeType
|
|
|
|
}
|
2021-02-20 09:30:12 +01:00
|
|
|
|
|
|
|
var nameStr string
|
2019-08-09 17:54:11 +02:00
|
|
|
if typ.IsRegular() {
|
2021-02-20 09:30:12 +01:00
|
|
|
nameStr = string(name)
|
2019-08-09 17:54:11 +02:00
|
|
|
} else if typ.IsDir() {
|
2020-09-15 02:19:54 +02:00
|
|
|
// Use temp buffer to append a slash to avoid string concat.
|
|
|
|
tmp = tmp[:len(name)+1]
|
|
|
|
copy(tmp, name)
|
|
|
|
tmp[len(tmp)-1] = '/' // SlashSeparator
|
2021-02-20 09:30:12 +01:00
|
|
|
nameStr = string(tmp)
|
2018-05-09 04:08:21 +02:00
|
|
|
}
|
2021-02-20 09:30:12 +01:00
|
|
|
|
2019-08-09 17:54:11 +02:00
|
|
|
count--
|
2021-02-20 09:30:12 +01:00
|
|
|
entries = append(entries, nameStr)
|
2016-04-08 20:46:03 +02:00
|
|
|
}
|
2021-02-20 09:30:12 +01:00
|
|
|
|
2016-05-05 21:51:56 +02:00
|
|
|
return
|
2016-04-05 02:27:55 +02:00
|
|
|
}
|
2020-06-13 05:04:01 +02:00
|
|
|
|
|
|
|
func globalSync() {
|
|
|
|
syscall.Sync()
|
|
|
|
}
|