| 
									
										
										
										
											2017-02-09 14:27:35 +08:00
										 |  |  | // +build linux darwin freebsd netbsd openbsd
 | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * Minio Cloud Storage, (C) 2016 Minio, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2016-04-27 01:35:39 +08:00
										 |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2016-10-27 08:14:05 +08:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 	"syscall" | 
					
						
							|  |  |  | 	"unsafe" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 	// readDirentBufSize for syscall.ReadDirent() to hold multiple
 | 
					
						
							|  |  |  | 	// directory entries in one buffer. golang source uses 4096 as
 | 
					
						
							| 
									
										
										
										
											2016-10-27 08:14:05 +08:00
										 |  |  | 	// buffer size whereas we want 64 times larger to save lots of
 | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 	// entries to avoid multiple syscall.ReadDirent() call.
 | 
					
						
							| 
									
										
										
										
											2016-10-27 08:14:05 +08:00
										 |  |  | 	readDirentBufSize = 4096 * 64 | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:55:07 +08:00
										 |  |  | // parseDirents - inspired from
 | 
					
						
							|  |  |  | // https://golang.org/src/syscall/syscall_<os>.go
 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | func parseDirents(dirPath string, buf []byte) (entries []string, err error) { | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 	bufidx := 0 | 
					
						
							|  |  |  | 	for bufidx < len(buf) { | 
					
						
							|  |  |  | 		dirent := (*syscall.Dirent)(unsafe.Pointer(&buf[bufidx])) | 
					
						
							| 
									
										
										
										
											2016-04-05 10:55:07 +08:00
										 |  |  | 		// 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 | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 		bufidx += int(dirent.Reclen) | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 		// Skip if they are absent in directory.
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:55:07 +08:00
										 |  |  | 		if isEmptyDirent(dirent) { | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0])) | 
					
						
							|  |  |  | 		var name = string(bytes[0:clen(bytes[:])]) | 
					
						
							| 
									
										
										
										
											2016-04-05 10:55:07 +08:00
										 |  |  | 		// Reserved names skip them.
 | 
					
						
							|  |  |  | 		if name == "." || name == ".." { | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		switch dirent.Type { | 
					
						
							|  |  |  | 		case syscall.DT_DIR: | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 			entries = append(entries, name+slashSeparator) | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 		case syscall.DT_REG: | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 			entries = append(entries, name) | 
					
						
							| 
									
										
										
										
											2016-05-14 02:39:48 +08:00
										 |  |  | 		case syscall.DT_LNK, syscall.DT_UNKNOWN: | 
					
						
							| 
									
										
										
										
											2017-10-13 18:01:15 +08:00
										 |  |  | 			// If its symbolic link, follow the link using os.Stat()
 | 
					
						
							| 
									
										
										
										
											2016-05-14 02:39:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-17 03:48:41 +08:00
										 |  |  | 			// On Linux XFS does not implement d_type for on disk
 | 
					
						
							| 
									
										
										
										
											2017-05-02 17:35:27 +08:00
										 |  |  | 			// format << v5. Fall back to OsStat().
 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 			var fi os.FileInfo | 
					
						
							| 
									
										
										
										
											2017-10-13 18:01:15 +08:00
										 |  |  | 			fi, err = os.Stat(path.Join(dirPath, name)) | 
					
						
							| 
									
										
										
										
											2016-05-12 03:54:21 +08:00
										 |  |  | 			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 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-05-12 03:54:21 +08:00
										 |  |  | 				return nil, err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if fi.IsDir() { | 
					
						
							|  |  |  | 				entries = append(entries, fi.Name()+slashSeparator) | 
					
						
							|  |  |  | 			} else if fi.Mode().IsRegular() { | 
					
						
							|  |  |  | 				entries = append(entries, fi.Name()) | 
					
						
							| 
									
										
										
										
											2016-04-17 03:48:41 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 		default: | 
					
						
							|  |  |  | 			// Skip entries which are not file or directory.
 | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-13 19:03:38 +08:00
										 |  |  | 	return entries, nil | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 08:14:05 +08:00
										 |  |  | var readDirBufPool = sync.Pool{ | 
					
						
							|  |  |  | 	New: func() interface{} { | 
					
						
							|  |  |  | 		b := make([]byte, readDirentBufSize) | 
					
						
							|  |  |  | 		return &b | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | // Return all the entries at the directory dirPath.
 | 
					
						
							|  |  |  | func readDir(dirPath string) (entries []string, err error) { | 
					
						
							| 
									
										
										
										
											2016-10-27 08:14:05 +08:00
										 |  |  | 	bufp := readDirBufPool.Get().(*[]byte) | 
					
						
							|  |  |  | 	buf := *bufp | 
					
						
							|  |  |  | 	defer readDirBufPool.Put(bufp) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 	d, err := os.Open(dirPath) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 		// File is really not found.
 | 
					
						
							|  |  |  | 		if os.IsNotExist(err) { | 
					
						
							|  |  |  | 			return nil, errFileNotFound | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-21 07:09:55 +08:00
										 |  |  | 		if os.IsPermission(err) { | 
					
						
							|  |  |  | 			return nil, errFileAccessDenied | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// File path cannot be verified since one of the parents is a file.
 | 
					
						
							|  |  |  | 		if strings.Contains(err.Error(), "not a directory") { | 
					
						
							|  |  |  | 			return nil, errFileNotFound | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer d.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fd := int(d.Fd()) | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		nbuf, err := syscall.ReadDirent(fd, buf) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if nbuf <= 0 { | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 		var tmpEntries []string | 
					
						
							|  |  |  | 		if tmpEntries, err = parseDirents(dirPath, buf[:nbuf]); err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 		entries = append(entries, tmpEntries...) | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 	return | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | } |