| 
									
										
										
										
											2021-08-19 09:35:22 +08:00
										 |  |  | //go:build windows
 | 
					
						
							| 
									
										
										
										
											2018-08-10 05:52:29 +08:00
										 |  |  | // +build windows
 | 
					
						
							| 
									
										
										
										
											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 ( | 
					
						
							|  |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2018-08-10 05:52:29 +08:00
										 |  |  | 	"syscall" | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-29 23:07:23 +08:00
										 |  |  | func access(name string) error { | 
					
						
							|  |  |  | 	_, err := os.Lstat(name) | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 23:14:36 +08:00
										 |  |  | func osMkdirAll(dirPath string, perm os.FileMode, _ string) error { | 
					
						
							|  |  |  | 	// baseDir is not honored in windows platform
 | 
					
						
							| 
									
										
										
										
											2022-07-24 15:43:11 +08:00
										 |  |  | 	return os.MkdirAll(dirPath, perm) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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, filter func(name string, typ os.FileMode) error) error { | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 	// Ensure we don't pick up files as directories.
 | 
					
						
							|  |  |  | 	globAll := filepath.Clean(dirPath) + `\*` | 
					
						
							|  |  |  | 	globAllP, err := syscall.UTF16PtrFromString(globAll) | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 		return errInvalidArgument | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 	data := &syscall.Win32finddata{} | 
					
						
							|  |  |  | 	handle, err := syscall.FindFirstFile(globAllP, data) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-04-07 13:28:58 +08:00
										 |  |  | 		if err = syscallErrToFileErr(dirPath, err); err == errFileNotFound { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-30 17:15:57 +08:00
										 |  |  | 	defer syscall.FindClose(handle) | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 	for ; ; err = syscall.FindNextFile(handle, data) { | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			if err == syscall.ERROR_NO_MORE_FILES { | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 				break | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 				if isSysErrPathNotFound(err) { | 
					
						
							| 
									
										
										
										
											2021-02-18 07:34:42 +08:00
										 |  |  | 					return nil | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-02-24 16:14:16 +08:00
										 |  |  | 				err = osErrToFileErr(&os.PathError{ | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 					Op:   "FindNextFile", | 
					
						
							|  |  |  | 					Path: dirPath, | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 					Err:  err, | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2021-02-24 16:14:16 +08:00
										 |  |  | 				if err == errFileNotFound { | 
					
						
							|  |  |  | 					return nil | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return err | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		name := syscall.UTF16ToString(data.FileName[0:]) | 
					
						
							|  |  |  | 		if name == "" || name == "." || name == ".." { // Useless names
 | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-19 02:08:15 +08:00
										 |  |  | 		var typ os.FileMode // regular file
 | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 		switch { | 
					
						
							|  |  |  | 		case data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0: | 
					
						
							|  |  |  | 			// Reparse point is a symlink
 | 
					
						
							| 
									
										
										
										
											2023-10-19 02:08:15 +08:00
										 |  |  | 			fi, err := os.Stat(pathJoin(dirPath, 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 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if fi.IsDir() { | 
					
						
							|  |  |  | 				// Ignore symlinked directories.
 | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			typ = fi.Mode() | 
					
						
							|  |  |  | 		case data.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY != 0: | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 			typ = os.ModeDir | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 		if err = filter(name, typ); err == errDoneForNow { | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | 			// filtering requested to return by caller.
 | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 07:34:42 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2020-04-24 03:26:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-10 07:20:51 +08:00
										 |  |  | // Return N entries at the directory dirPath.
 | 
					
						
							|  |  |  | func readDirWithOpts(dirPath string, opts readDirOpts) (entries []string, err error) { | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 	// Ensure we don't pick up files as directories.
 | 
					
						
							|  |  |  | 	globAll := filepath.Clean(dirPath) + `\*` | 
					
						
							|  |  |  | 	globAllP, err := syscall.UTF16PtrFromString(globAll) | 
					
						
							| 
									
										
										
										
											2018-08-10 05:52:29 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 		return nil, errInvalidArgument | 
					
						
							| 
									
										
										
										
											2018-05-09 10:08:21 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 	data := &syscall.Win32finddata{} | 
					
						
							|  |  |  | 	handle, err := syscall.FindFirstFile(globAllP, data) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-04-07 13:28:58 +08:00
										 |  |  | 		return nil, syscallErrToFileErr(dirPath, err) | 
					
						
							| 
									
										
										
										
											2020-09-02 00:33:16 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-04-07 13:28:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-30 17:15:57 +08:00
										 |  |  | 	defer syscall.FindClose(handle) | 
					
						
							| 
									
										
										
										
											2018-05-09 10:08:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-10 07:20:51 +08:00
										 |  |  | 	count := opts.count | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 	for ; count != 0; err = syscall.FindNextFile(handle, data) { | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			if err == syscall.ERROR_NO_MORE_FILES { | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 				break | 
					
						
							| 
									
										
										
										
											2018-08-10 05:52:29 +08:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 				return nil, osErrToFileErr(&os.PathError{ | 
					
						
							| 
									
										
										
										
											2018-08-10 05:52:29 +08:00
										 |  |  | 					Op:   "FindNextFile", | 
					
						
							|  |  |  | 					Path: dirPath, | 
					
						
							| 
									
										
										
										
											2023-04-06 05:36:49 +08:00
										 |  |  | 					Err:  err, | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-06-10 00:44:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 05:52:29 +08:00
										 |  |  | 		name := syscall.UTF16ToString(data.FileName[0:]) | 
					
						
							| 
									
										
										
										
											2019-08-09 23:54:11 +08:00
										 |  |  | 		if name == "" || name == "." || name == ".." { // Useless names
 | 
					
						
							| 
									
										
										
										
											2018-08-10 05:52:29 +08:00
										 |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2018-05-09 10:08:21 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 05:52:29 +08:00
										 |  |  | 		switch { | 
					
						
							|  |  |  | 		case data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0: | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 			// Reparse point is a symlink
 | 
					
						
							| 
									
										
										
										
											2023-10-19 02:08:15 +08:00
										 |  |  | 			fi, err := os.Stat(pathJoin(dirPath, 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 nil, err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-10 07:20:51 +08:00
										 |  |  | 			if !opts.followDirSymlink && fi.IsDir() { | 
					
						
							| 
									
										
										
										
											2021-02-20 16:30:12 +08:00
										 |  |  | 				// directory symlinks are ignored.
 | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-08-10 05:52:29 +08:00
										 |  |  | 		case data.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY != 0: | 
					
						
							| 
									
										
										
										
											2023-10-19 02:08:15 +08:00
										 |  |  | 			name += SlashSeparator | 
					
						
							| 
									
										
										
										
											2018-08-10 05:52:29 +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, name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 02:46:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-27 10:31:53 +08:00
										 |  |  | 	return entries, nil | 
					
						
							| 
									
										
										
										
											2016-04-05 08:27:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func globalSync() { | 
					
						
							|  |  |  | 	// no-op on windows
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-04-07 13:28:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func syscallErrToFileErr(dirPath string, err error) error { | 
					
						
							|  |  |  | 	switch err { | 
					
						
							|  |  |  | 	case nil: | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	case syscall.ERROR_FILE_NOT_FOUND: | 
					
						
							|  |  |  | 		return errFileNotFound | 
					
						
							|  |  |  | 	case syscall.ERROR_ACCESS_DENIED: | 
					
						
							|  |  |  | 		return errFileAccessDenied | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		// Fails on file not found and when not a directory.
 | 
					
						
							|  |  |  | 		return osErrToFileErr(&os.PathError{ | 
					
						
							|  |  |  | 			Op:   "FindNextFile", | 
					
						
							|  |  |  | 			Path: dirPath, | 
					
						
							|  |  |  | 			Err:  err, | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |