| 
									
										
										
										
											2021-08-19 09:35:22 +08:00
										 |  |  | //go:build (linux && !appengine) || darwin || freebsd || netbsd || openbsd
 | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | // +build linux,!appengine darwin freebsd netbsd openbsd
 | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 03:41:13 +08: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/>.
 | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-09-15 08:19:54 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2023-09-13 23:14:36 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2020-06-10 00:44:50 +08:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 	"syscall" | 
					
						
							|  |  |  | 	"unsafe" | 
					
						
							| 
									
										
										
										
											2021-03-29 23:07:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"golang.org/x/sys/unix" | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-29 23:07:23 +08:00
										 |  |  | func access(name string) error { | 
					
						
							| 
									
										
										
										
											2023-11-22 07:08:41 +08:00
										 |  |  | 	if err := unix.Access(name, unix.F_OK); err != nil { | 
					
						
							| 
									
										
										
										
											2021-03-29 23:07:23 +08:00
										 |  |  | 		return &os.PathError{Op: "lstat", Path: name, Err: err} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-11 00:48:50 +08:00
										 |  |  | // openFileWithFD return 'fd' based file descriptor
 | 
					
						
							|  |  |  | func openFileWithFD(name string, flag int, perm os.FileMode) (fd int, err error) { | 
					
						
							|  |  |  | 	switch flag & writeMode { | 
					
						
							|  |  |  | 	case writeMode: | 
					
						
							|  |  |  | 		defer updateOSMetrics(osMetricOpenFileWFd, name)(err) | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		defer updateOSMetrics(osMetricOpenFileRFd, name)(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var e error | 
					
						
							|  |  |  | 	fd, e = syscall.Open(name, flag|syscall.O_CLOEXEC, uint32(perm)) | 
					
						
							|  |  |  | 	if e != nil { | 
					
						
							|  |  |  | 		return -1, &os.PathError{Op: "open", Path: name, Err: e} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return fd, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-29 06:16:41 +08:00
										 |  |  | // Forked from Golang but chooses to avoid performing lookup
 | 
					
						
							| 
									
										
										
										
											2022-07-24 15:43:11 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // osMkdirAll creates a directory named path,
 | 
					
						
							|  |  |  | // along with any necessary parents, and returns nil,
 | 
					
						
							|  |  |  | // or else returns an error.
 | 
					
						
							|  |  |  | // The permission bits perm (before umask) are used for all
 | 
					
						
							|  |  |  | // directories that MkdirAll creates.
 | 
					
						
							|  |  |  | // If path is already a directory, MkdirAll does nothing
 | 
					
						
							|  |  |  | // and returns nil.
 | 
					
						
							| 
									
										
										
										
											2023-09-13 23:14:36 +08:00
										 |  |  | func osMkdirAll(dirPath string, perm os.FileMode, baseDir string) error { | 
					
						
							|  |  |  | 	if baseDir != "" { | 
					
						
							|  |  |  | 		if strings.HasPrefix(baseDir, dirPath) { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-24 15:43:11 +08:00
										 |  |  | 	// Slow path: make sure parent exists and then call Mkdir for path.
 | 
					
						
							|  |  |  | 	i := len(dirPath) | 
					
						
							|  |  |  | 	for i > 0 && os.IsPathSeparator(dirPath[i-1]) { // Skip trailing path separator.
 | 
					
						
							|  |  |  | 		i-- | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	j := i | 
					
						
							|  |  |  | 	for j > 0 && !os.IsPathSeparator(dirPath[j-1]) { // Scan backward over element.
 | 
					
						
							|  |  |  | 		j-- | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if j > 1 { | 
					
						
							|  |  |  | 		// Create parent.
 | 
					
						
							| 
									
										
										
										
											2023-09-13 23:14:36 +08:00
										 |  |  | 		if err := osMkdirAll(dirPath[:j-1], perm, baseDir); err != nil { | 
					
						
							| 
									
										
										
										
											2022-07-24 15:43:11 +08:00
										 |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Parent now exists; invoke Mkdir and use its result.
 | 
					
						
							| 
									
										
										
										
											2023-08-29 06:16:41 +08:00
										 |  |  | 	if err := Mkdir(dirPath, perm); err != nil { | 
					
						
							| 
									
										
										
										
											2022-07-24 15:43:11 +08:00
										 |  |  | 		if osIsExist(err) { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | // The buffer must be at least a block long.
 | 
					
						
							|  |  |  | // refer https://github.com/golang/go/issues/24015
 | 
					
						
							| 
									
										
										
										
											2020-06-10 00:44:50 +08:00
										 |  |  | const blockSize = 8 << 10 // 8192
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 01:21:02 +08:00
										 |  |  | // By default atleast 128 entries in single getdents call (1MiB buffer)
 | 
					
						
							| 
									
										
										
										
											2021-05-19 01:29:50 +08:00
										 |  |  | var ( | 
					
						
							|  |  |  | 	direntPool = sync.Pool{ | 
					
						
							|  |  |  | 		New: func() interface{} { | 
					
						
							|  |  |  | 			buf := make([]byte, blockSize*128) | 
					
						
							|  |  |  | 			return &buf | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	direntNamePool = sync.Pool{ | 
					
						
							|  |  |  | 		New: func() interface{} { | 
					
						
							|  |  |  | 			buf := make([]byte, blockSize) | 
					
						
							|  |  |  | 			return &buf | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08: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-09 02:46:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 08:19:54 +08:00
										 |  |  | func parseDirEnt(buf []byte) (consumed int, name []byte, typ os.FileMode, err error) { | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08: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 08:19:54 +08:00
										 |  |  | 		return consumed, nil, typ, fmt.Errorf("buf size of %d smaller than dirent header size %d", len(buf), v) | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if len(buf) < int(dirent.Reclen) { | 
					
						
							| 
									
										
										
										
											2020-09-15 08:19:54 +08:00
										 |  |  | 		return consumed, nil, typ, fmt.Errorf("buf size %d < record length %d", len(buf), dirent.Reclen) | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08: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-14 02:39:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 	nameBuf := (*[unsafe.Sizeof(dirent.Name)]byte)(unsafe.Pointer(&dirent.Name[0])) | 
					
						
							|  |  |  | 	nameLen, err := direntNamlen(dirent) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-09-15 08:19:54 +08:00
										 |  |  | 		return consumed, nil, typ, err | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 08:19:54 +08:00
										 |  |  | 	return consumed, nameBuf[:nameLen], typ, nil | 
					
						
							| 
									
										
										
										
											2016-10-27 08:14:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 07:34:42 +08: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 { | 
					
						
							| 
									
										
										
										
											2024-01-11 00:48:50 +08:00
										 |  |  | 	fd, err := openFileWithFD(dirPath, readMode, 0o666) | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-02-18 07:34:42 +08:00
										 |  |  | 		if osErrToFileErr(err) == errFileNotFound { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-08-04 22:23:05 +08:00
										 |  |  | 		if !osIsPermission(err) { | 
					
						
							|  |  |  | 			return osErrToFileErr(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// There may be permission error when dirPath
 | 
					
						
							|  |  |  | 		// is at the root of the disk mount that may
 | 
					
						
							|  |  |  | 		// not have the permissions to avoid 'noatime'
 | 
					
						
							| 
									
										
										
										
											2024-01-11 00:48:50 +08:00
										 |  |  | 		fd, err = openFileWithFD(dirPath, os.O_RDONLY, 0o666) | 
					
						
							| 
									
										
										
										
											2022-08-04 22:23:05 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			if osErrToFileErr(err) == errFileNotFound { | 
					
						
							|  |  |  | 				return nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return osErrToFileErr(err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-11 00:48:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-11 00:48:50 +08:00
										 |  |  | 	defer syscall.Close(fd) | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-19 01:29:50 +08:00
										 |  |  | 	bufp := direntPool.Get().(*[]byte) | 
					
						
							|  |  |  | 	defer direntPool.Put(bufp) | 
					
						
							|  |  |  | 	buf := *bufp | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 00:44:50 +08:00
										 |  |  | 	boff := 0 // starting read position in buf
 | 
					
						
							|  |  |  | 	nbuf := 0 // end valid data in buf
 | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		if boff >= nbuf { | 
					
						
							|  |  |  | 			boff = 0 | 
					
						
							| 
									
										
										
										
											2022-07-06 05:45:49 +08:00
										 |  |  | 			stop := globalOSMetrics.time(osMetricReadDirent) | 
					
						
							| 
									
										
										
										
											2024-01-11 00:48:50 +08:00
										 |  |  | 			nbuf, err = syscall.ReadDirent(fd, buf) | 
					
						
							| 
									
										
										
										
											2022-07-06 05:45:49 +08:00
										 |  |  | 			stop() | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				if isSysErrNotDir(err) { | 
					
						
							| 
									
										
										
										
											2021-02-18 07:34:42 +08:00
										 |  |  | 					return nil | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-02-24 16:14:16 +08:00
										 |  |  | 				err = osErrToFileErr(err) | 
					
						
							|  |  |  | 				if err == errFileNotFound { | 
					
						
							|  |  |  | 					return nil | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if nbuf <= 0 { | 
					
						
							| 
									
										
										
										
											2020-06-10 00:44:50 +08:00
										 |  |  | 				break // EOF
 | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		consumed, name, typ, err := parseDirEnt(buf[boff:nbuf]) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		boff += consumed | 
					
						
							| 
									
										
										
										
											2020-09-15 08:19:54 +08:00
										 |  |  | 		if len(name) == 0 || bytes.Equal(name, []byte{'.'}) || bytes.Equal(name, []byte{'.', '.'}) { | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08: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 { | 
					
						
							| 
									
										
										
										
											2022-07-06 05:45:49 +08:00
										 |  |  | 			fi, err := Stat(pathJoin(dirPath, string(name))) | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 			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-24 03:26:13 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-18 07:34:42 +08:00
										 |  |  | 		if err = fn(string(name), typ); err == errDoneForNow { | 
					
						
							|  |  |  | 			// fn() requested to return by caller.
 | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 10:08:21 +08:00
										 |  |  | // Return count entries at the directory dirPath and all entries
 | 
					
						
							|  |  |  | // if count is set to -1
 | 
					
						
							| 
									
										
										
										
											2021-07-10 07:20:51 +08:00
										 |  |  | func readDirWithOpts(dirPath string, opts readDirOpts) (entries []string, err error) { | 
					
						
							| 
									
										
										
										
											2024-01-11 00:48:50 +08:00
										 |  |  | 	fd, err := openFileWithFD(dirPath, readMode, 0o666) | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-08-04 22:23:05 +08:00
										 |  |  | 		if !osIsPermission(err) { | 
					
						
							|  |  |  | 			return nil, osErrToFileErr(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// There may be permission error when dirPath
 | 
					
						
							|  |  |  | 		// is at the root of the disk mount that may
 | 
					
						
							|  |  |  | 		// not have the permissions to avoid 'noatime'
 | 
					
						
							| 
									
										
										
										
											2024-01-11 00:48:50 +08:00
										 |  |  | 		fd, err = openFileWithFD(dirPath, os.O_RDONLY, 0o666) | 
					
						
							| 
									
										
										
										
											2022-08-04 22:23:05 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2022-08-03 05:57:36 +08:00
										 |  |  | 			return nil, osErrToFileErr(err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-11 00:48:50 +08:00
										 |  |  | 	defer syscall.Close(fd) | 
					
						
							| 
									
										
										
										
											2018-05-09 10:08:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 00:44:50 +08:00
										 |  |  | 	bufp := direntPool.Get().(*[]byte) | 
					
						
							|  |  |  | 	defer direntPool.Put(bufp) | 
					
						
							| 
									
										
										
										
											2021-05-19 01:29:50 +08:00
										 |  |  | 	buf := *bufp | 
					
						
							| 
									
										
										
										
											2020-06-10 00:44:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-19 01:29:50 +08:00
										 |  |  | 	nameTmp := direntNamePool.Get().(*[]byte) | 
					
						
							|  |  |  | 	defer direntNamePool.Put(nameTmp) | 
					
						
							| 
									
										
										
										
											2020-09-15 08:19:54 +08:00
										 |  |  | 	tmp := *nameTmp | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 00:44:50 +08:00
										 |  |  | 	boff := 0 // starting read position in buf
 | 
					
						
							|  |  |  | 	nbuf := 0 // end valid data in buf
 | 
					
						
							| 
									
										
										
										
											2018-05-09 10:08:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-10 07:20:51 +08:00
										 |  |  | 	count := opts.count | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 	for count != 0 { | 
					
						
							|  |  |  | 		if boff >= nbuf { | 
					
						
							|  |  |  | 			boff = 0 | 
					
						
							| 
									
										
										
										
											2022-07-06 05:45:49 +08:00
										 |  |  | 			stop := globalOSMetrics.time(osMetricReadDirent) | 
					
						
							| 
									
										
										
										
											2024-01-11 00:48:50 +08:00
										 |  |  | 			nbuf, err = syscall.ReadDirent(fd, buf) | 
					
						
							| 
									
										
										
										
											2022-07-06 05:45:49 +08:00
										 |  |  | 			stop() | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				if isSysErrNotDir(err) { | 
					
						
							|  |  |  | 					return nil, errFileNotFound | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-02-24 16:14:16 +08:00
										 |  |  | 				return nil, osErrToFileErr(err) | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			if nbuf <= 0 { | 
					
						
							|  |  |  | 				break | 
					
						
							| 
									
										
										
										
											2018-07-14 14:41:48 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-19 01:29:50 +08:00
										 |  |  | 		consumed, name, typ, err := parseDirEnt(buf[boff:nbuf]) | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 			return nil, err | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 		boff += consumed | 
					
						
							| 
									
										
										
										
											2020-09-15 08:19:54 +08:00
										 |  |  | 		if len(name) == 0 || bytes.Equal(name, []byte{'.'}) || bytes.Equal(name, []byte{'.', '.'}) { | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 		// Fallback for filesystems (like old XFS) that don't
 | 
					
						
							|  |  |  | 		// support Dirent.Type and have DT_UNKNOWN (0) there
 | 
					
						
							|  |  |  | 		// instead.
 | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 		if typ == unexpectedFileMode || typ&os.ModeSymlink == os.ModeSymlink { | 
					
						
							| 
									
										
										
										
											2021-06-16 05:34:26 +08:00
										 |  |  | 			fi, err := Stat(pathJoin(dirPath, string(name))) | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 				// It got deleted in the meantime, not found
 | 
					
						
							|  |  |  | 				// or returns too many symlinks ignore this
 | 
					
						
							|  |  |  | 				// file/directory.
 | 
					
						
							| 
									
										
										
										
											2020-11-24 00:36:49 +08:00
										 |  |  | 				if osIsNotExist(err) || isSysErrPathNotFound(err) || | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 					isSysErrTooManySymlinks(err) { | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return nil, err | 
					
						
							| 
									
										
										
										
											2018-05-09 10:08:21 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// Ignore symlinked directories.
 | 
					
						
							| 
									
										
										
										
											2021-07-10 07:20:51 +08:00
										 |  |  | 			if !opts.followDirSymlink && typ&os.ModeSymlink == os.ModeSymlink && fi.IsDir() { | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 			typ = fi.Mode() & os.ModeType | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		var nameStr string | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 		if typ.IsRegular() { | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 			nameStr = string(name) | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 		} else if typ.IsDir() { | 
					
						
							| 
									
										
										
										
											2020-09-15 08:19:54 +08: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 16:30:12 +08:00
										 |  |  | 			nameStr = string(tmp) | 
					
						
							| 
									
										
										
										
											2018-05-09 10:08:21 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 		count-- | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 		entries = append(entries, nameStr) | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 	return | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func globalSync() { | 
					
						
							| 
									
										
										
										
											2023-06-01 00:40:23 +08:00
										 |  |  | 	defer globalOSMetrics.time(osMetricSync)() | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	syscall.Sync() | 
					
						
							|  |  |  | } |