Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small optimizations for os.go #1829

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, "!")
Limero marked this conversation as resolved.
Show resolved Hide resolved
}
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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