From 6c356b3d978c7fcc72990d2c18d4d83b57bb8049 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 3 Nov 2024 13:58:36 +0700 Subject: [PATCH] Small optimizations for os.go --- os.go | 22 ++++++++++------------ os_windows.go | 15 +++++++-------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/os.go b/os.go index ac2d2f60..9b5dc5d6 100644 --- a/os.go +++ b/os.go @@ -10,6 +10,7 @@ import ( "os/user" "path/filepath" "runtime" + "strconv" "strings" "syscall" @@ -155,7 +156,7 @@ func shellKill(cmd *exec.Cmd) error { pgid, err := unix.Getpgid(cmd.Process.Pid) if err == nil && cmd.Process.Pid == pgid { // kill the process group - err = unix.Kill(-pgid, 15) + err = unix.Kill(-pgid, syscall.SIGTERM) if err == nil { return nil } @@ -189,42 +190,39 @@ func isHidden(f os.FileInfo, path string, hiddenfiles []string) bool { hidden := false for _, pattern := range hiddenfiles { matched := matchPattern(strings.TrimPrefix(pattern, "!"), f.Name(), path) - if strings.HasPrefix(pattern, "!") && matched { - hidden = false - } else if matched { - hidden = true + if !matched { + continue } + hidden = !strings.HasPrefix(pattern, "!") } return hidden } func userName(f os.FileInfo) string { if stat, ok := f.Sys().(*syscall.Stat_t); ok { - uid := fmt.Sprint(stat.Uid) + uid := strconv.FormatUint(uint64(stat.Uid), 10) if u, err := user.LookupId(uid); err == nil { return u.Username - } else { - return uid } + return uid } return "" } func groupName(f os.FileInfo) string { if stat, ok := f.Sys().(*syscall.Stat_t); ok { - gid := fmt.Sprint(stat.Gid) + gid := strconv.FormatUint(uint64(stat.Gid), 10) if g, err := user.LookupGroupId(gid); err == nil { return g.Name - } else { - return gid } + return gid } return "" } func linkCount(f os.FileInfo) string { if stat, ok := f.Sys().(*syscall.Stat_t); ok { - return fmt.Sprint(stat.Nlink) + return strconv.FormatUint(stat.Nlink, 10) } return "" } diff --git a/os_windows.go b/os_windows.go index db9ef562..e92b7f39 100644 --- a/os_windows.go +++ b/os_windows.go @@ -190,30 +190,29 @@ func isHidden(f os.FileInfo, path string, hiddenfiles []string) bool { hidden := false for _, pattern := range hiddenfiles { matched := matchPattern(strings.TrimPrefix(pattern, "!"), f.Name(), path) - if strings.HasPrefix(pattern, "!") && matched { - hidden = false - } else if matched { - hidden = true + if !matched { + continue } + hidden = !strings.HasPrefix(pattern, "!") } return hidden } -func userName(f os.FileInfo) string { +func userName(_ os.FileInfo) string { return "" } -func groupName(f os.FileInfo) string { +func groupName(_ os.FileInfo) string { return "" } -func linkCount(f os.FileInfo) string { +func linkCount(_ os.FileInfo) string { return "" } func errCrossDevice(err error) bool { - return err.(*os.LinkError).Err.(windows.Errno) == 17 + return err.(*os.LinkError).Err.(windows.Errno) == windows.ERROR_NOT_SAME_DEVICE } func quoteString(s string) string {