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.
167 lines
4.1 KiB
167 lines
4.1 KiB
6 years ago
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
|
||
9 years ago
|
|
||
|
/*
|
||
6 years ago
|
* Minio Cloud Storage, (C) 2016, 2017, 2018 Minio, Inc.
|
||
9 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
|
"os"
|
||
9 years ago
|
"path"
|
||
9 years ago
|
"runtime"
|
||
8 years ago
|
"sync"
|
||
9 years ago
|
"syscall"
|
||
|
"unsafe"
|
||
|
)
|
||
|
|
||
|
const (
|
||
9 years ago
|
// readDirentBufSize for syscall.ReadDirent() to hold multiple
|
||
|
// directory entries in one buffer. golang source uses 4096 as
|
||
8 years ago
|
// buffer size whereas we want 64 times larger to save lots of
|
||
9 years ago
|
// entries to avoid multiple syscall.ReadDirent() call.
|
||
8 years ago
|
readDirentBufSize = 4096 * 64
|
||
9 years ago
|
)
|
||
|
|
||
|
// actual length of the byte array from the c - world.
|
||
|
func clen(n []byte) int {
|
||
|
for i := 0; i < len(n); i++ {
|
||
|
if n[i] == 0 {
|
||
|
return i
|
||
|
}
|
||
|
}
|
||
|
return len(n)
|
||
|
}
|
||
|
|
||
9 years ago
|
// parseDirents - inspired from
|
||
|
// https://golang.org/src/syscall/syscall_<os>.go
|
||
9 years ago
|
func parseDirents(dirPath string, buf []byte) (entries []string, err error) {
|
||
9 years ago
|
bufidx := 0
|
||
|
for bufidx < len(buf) {
|
||
|
dirent := (*syscall.Dirent)(unsafe.Pointer(&buf[bufidx]))
|
||
9 years ago
|
// On non-Linux operating systems for rec length of zero means
|
||
|
// we have reached EOF break out.
|
||
|
if runtime.GOOS != "linux" && dirent.Reclen == 0 {
|
||
|
break
|
||
|
}
|
||
9 years ago
|
bufidx += int(dirent.Reclen)
|
||
9 years ago
|
// Skip if they are absent in directory.
|
||
9 years ago
|
if isEmptyDirent(dirent) {
|
||
9 years ago
|
continue
|
||
|
}
|
||
|
bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0]))
|
||
|
var name = string(bytes[0:clen(bytes[:])])
|
||
9 years ago
|
// Reserved names skip them.
|
||
|
if name == "." || name == ".." {
|
||
9 years ago
|
continue
|
||
|
}
|
||
9 years ago
|
|
||
|
switch dirent.Type {
|
||
|
case syscall.DT_DIR:
|
||
9 years ago
|
entries = append(entries, name+slashSeparator)
|
||
9 years ago
|
case syscall.DT_REG:
|
||
9 years ago
|
entries = append(entries, name)
|
||
9 years ago
|
case syscall.DT_LNK, syscall.DT_UNKNOWN:
|
||
7 years ago
|
// If its symbolic link, follow the link using os.Stat()
|
||
9 years ago
|
|
||
9 years ago
|
// On Linux XFS does not implement d_type for on disk
|
||
8 years ago
|
// format << v5. Fall back to OsStat().
|
||
9 years ago
|
var fi os.FileInfo
|
||
7 years ago
|
fi, err = os.Stat(path.Join(dirPath, name))
|
||
9 years ago
|
if err != nil {
|
||
|
// If file does not exist, we continue and skip it.
|
||
|
// Could happen if it was deleted in the middle while
|
||
|
// this list was being performed.
|
||
|
if os.IsNotExist(err) {
|
||
|
continue
|
||
9 years ago
|
}
|
||
9 years ago
|
return nil, err
|
||
|
}
|
||
|
if fi.IsDir() {
|
||
6 years ago
|
entries = append(entries, name+slashSeparator)
|
||
9 years ago
|
} else if fi.Mode().IsRegular() {
|
||
6 years ago
|
entries = append(entries, name)
|
||
9 years ago
|
}
|
||
9 years ago
|
default:
|
||
|
// Skip entries which are not file or directory.
|
||
|
continue
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
return entries, nil
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
var readDirBufPool = sync.Pool{
|
||
|
New: func() interface{} {
|
||
|
b := make([]byte, readDirentBufSize)
|
||
|
return &b
|
||
|
},
|
||
|
}
|
||
|
|
||
9 years ago
|
// Return all the entries at the directory dirPath.
|
||
|
func readDir(dirPath string) (entries []string, err error) {
|
||
7 years ago
|
return readDirN(dirPath, -1)
|
||
|
}
|
||
|
|
||
|
// 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) {
|
||
8 years ago
|
bufp := readDirBufPool.Get().(*[]byte)
|
||
|
buf := *bufp
|
||
|
defer readDirBufPool.Put(bufp)
|
||
|
|
||
9 years ago
|
d, err := os.Open(dirPath)
|
||
|
if err != nil {
|
||
7 years ago
|
if os.IsNotExist(err) || isSysErrNotDir(err) {
|
||
9 years ago
|
return nil, errFileNotFound
|
||
|
}
|
||
8 years ago
|
if os.IsPermission(err) {
|
||
|
return nil, errFileAccessDenied
|
||
|
}
|
||
9 years ago
|
return nil, err
|
||
|
}
|
||
|
defer d.Close()
|
||
|
|
||
|
fd := int(d.Fd())
|
||
7 years ago
|
|
||
|
remaining := count
|
||
|
done := false
|
||
|
|
||
|
for !done {
|
||
9 years ago
|
nbuf, err := syscall.ReadDirent(fd, buf)
|
||
|
if err != nil {
|
||
7 years ago
|
if isSysErrNotDir(err) {
|
||
|
return nil, errFileNotFound
|
||
|
}
|
||
9 years ago
|
return nil, err
|
||
|
}
|
||
|
if nbuf <= 0 {
|
||
|
break
|
||
|
}
|
||
9 years ago
|
var tmpEntries []string
|
||
|
if tmpEntries, err = parseDirents(dirPath, buf[:nbuf]); err != nil {
|
||
|
return nil, err
|
||
9 years ago
|
}
|
||
7 years ago
|
if count > 0 {
|
||
|
if remaining <= len(tmpEntries) {
|
||
|
tmpEntries = tmpEntries[:remaining]
|
||
|
done = true
|
||
|
}
|
||
|
remaining -= len(tmpEntries)
|
||
|
}
|
||
9 years ago
|
entries = append(entries, tmpEntries...)
|
||
9 years ago
|
}
|
||
9 years ago
|
return
|
||
9 years ago
|
}
|