Skip to content

Commit

Permalink
Small optimizations for os.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Limero committed Nov 3, 2024
1 parent 1b1628c commit 3c747d3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
22 changes: 10 additions & 12 deletions os.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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(uint64(stat.Nlink), 10)
}
return ""
}
Expand Down
15 changes: 7 additions & 8 deletions os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3c747d3

Please sign in to comment.