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