fix: linter errors in Windows specific code (#18276)

This commit is contained in:
Klaus Post 2023-10-18 11:08:15 -07:00 committed by GitHub
parent b46a717425
commit e37508fb8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 21 deletions

View File

@ -80,11 +80,11 @@ func readDirFn(dirPath string, filter func(name string, typ os.FileMode) error)
continue continue
} }
var typ os.FileMode = 0 // regular file var typ os.FileMode // regular file
switch { switch {
case data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0: case data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0:
// Reparse point is a symlink // Reparse point is a symlink
fi, err := os.Stat(pathJoin(dirPath, string(name))) fi, err := os.Stat(pathJoin(dirPath, name))
if err != nil { if err != nil {
// It got deleted in the meantime, not found // It got deleted in the meantime, not found
// or returns too many symlinks ignore this // or returns too many symlinks ignore this
@ -153,7 +153,7 @@ func readDirWithOpts(dirPath string, opts readDirOpts) (entries []string, err er
switch { switch {
case data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0: case data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0:
// Reparse point is a symlink // Reparse point is a symlink
fi, err := os.Stat(pathJoin(dirPath, string(name))) fi, err := os.Stat(pathJoin(dirPath, name))
if err != nil { if err != nil {
// It got deleted in the meantime, not found // It got deleted in the meantime, not found
// or returns too many symlinks ignore this // or returns too many symlinks ignore this
@ -170,7 +170,7 @@ func readDirWithOpts(dirPath string, opts readDirOpts) (entries []string, err er
continue continue
} }
case data.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY != 0: case data.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY != 0:
name = name + SlashSeparator name += SlashSeparator
} }
count-- count--

View File

@ -63,6 +63,6 @@ func DisableDirectIO(f *os.File) error {
// AlignedBlock simply returns an unaligned buffer // AlignedBlock simply returns an unaligned buffer
// for systems that do not support DirectIO. // for systems that do not support DirectIO.
func AlignedBlock(BlockSize int) []byte { func AlignedBlock(blockSize int) []byte {
return make([]byte, BlockSize) return make([]byte, blockSize)
} }

View File

@ -81,11 +81,10 @@ func lockedOpenFile(path string, flag int, perm os.FileMode, lockType uint32) (*
// acquire a write lock. // acquire a write lock.
func TryLockedOpenFile(path string, flag int, perm os.FileMode) (*LockedFile, error) { func TryLockedOpenFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
var lockType uint32 = lockFileFailImmediately | lockFileExclusiveLock var lockType uint32 = lockFileFailImmediately | lockFileExclusiveLock
switch flag { if flag == syscall.O_RDONLY {
case syscall.O_RDONLY:
// https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-lockfileex // https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-lockfileex
//lint:ignore SA4016 Reasons //lint:ignore SA4016 Reasons
lockType = lockFileFailImmediately | 0 // Set this to enable shared lock and fail immediately. lockType = lockFileFailImmediately // Set this to enable shared lock and fail immediately.
} }
return lockedOpenFile(path, flag, perm, lockType) return lockedOpenFile(path, flag, perm, lockType)
} }
@ -94,8 +93,7 @@ func TryLockedOpenFile(path string, flag int, perm os.FileMode) (*LockedFile, er
// the file from concurrent access. // the file from concurrent access.
func LockedOpenFile(path string, flag int, perm os.FileMode) (*LockedFile, error) { func LockedOpenFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
var lockType uint32 = lockFileExclusiveLock var lockType uint32 = lockFileExclusiveLock
switch flag { if flag == syscall.O_RDONLY {
case syscall.O_RDONLY:
// https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-lockfileex // https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-lockfileex
lockType = 0 // Set this to enable shared lock. lockType = 0 // Set this to enable shared lock.
} }
@ -239,15 +237,13 @@ func lockFile(fd syscall.Handle, flags uint32) error {
} }
err := lockFileEx(fd, flags, 1, 0, &syscall.Overlapped{}) err := lockFileEx(fd, flags, 1, 0, &syscall.Overlapped{})
if err == nil { if err == nil || err == errLockViolation {
return nil return nil
} else if err.Error() == "The process cannot access the file because another process has locked a portion of the file." {
return ErrAlreadyLocked
} else if err != errLockViolation {
return err
} }
if err.Error() == "The process cannot access the file because another process has locked a portion of the file." {
return nil return ErrAlreadyLocked
}
return err
} }
func lockFileEx(h syscall.Handle, flags, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) { func lockFileEx(h syscall.Handle, flags, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {

View File

@ -51,10 +51,10 @@ func TestFixLongPath(t *testing.T) {
{`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`}, {`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
{`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`}, {`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
} { } {
in := strings.Replace(test.in, "long", veryLong, -1) in := strings.ReplaceAll(test.in, "long", veryLong)
want := strings.Replace(test.want, "long", veryLong, -1) want := strings.ReplaceAll(test.want, "long", veryLong)
if got := fixLongPath(in); got != want { if got := fixLongPath(in); got != want {
got = strings.Replace(got, veryLong, "long", -1) got = strings.ReplaceAll(got, veryLong, "long")
t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want) t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
} }
} }