From 260a63ca73b09cf029a872554aceed809ae47231 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Thu, 6 Apr 2023 22:28:58 -0700 Subject: [PATCH] os/windows: do not return errFileNotFound in readDir's (#16988) --- cmd/os_windows.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/cmd/os_windows.go b/cmd/os_windows.go index ce9d1c2ee..d06a65151 100644 --- a/cmd/os_windows.go +++ b/cmd/os_windows.go @@ -48,8 +48,10 @@ func readDirFn(dirPath string, filter func(name string, typ os.FileMode) error) data := &syscall.Win32finddata{} handle, err := syscall.FindFirstFile(globAllP, data) if err != nil { - // Fails on file not found and when not a directory. - return errFileNotFound + if err = syscallErrToFileErr(dirPath, err); err == errFileNotFound { + return nil + } + return err } defer syscall.CloseHandle(handle) @@ -123,9 +125,9 @@ func readDirWithOpts(dirPath string, opts readDirOpts) (entries []string, err er data := &syscall.Win32finddata{} handle, err := syscall.FindFirstFile(globAllP, data) if err != nil { - // Fails on file not found and when not a directory. - return nil, errFileNotFound + return nil, syscallErrToFileErr(dirPath, err) } + defer syscall.CloseHandle(handle) count := opts.count @@ -181,3 +183,21 @@ func readDirWithOpts(dirPath string, opts readDirOpts) (entries []string, err er func globalSync() { // no-op on windows } + +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, + }) + } +}