From 8bf383d1c4917870f46b0b802b452924e663edcc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 9 Feb 2025 15:01:05 +0530 Subject: [PATCH] DRYer: Use the existing unix package stat() --- kittens/ssh/config.go | 39 ++++++++++++++-------------------- tools/stat/api.go | 49 ------------------------------------------- tools/stat/get1.go | 22 ------------------- tools/stat/get2.go | 22 ------------------- 4 files changed, 16 insertions(+), 116 deletions(-) delete mode 100644 tools/stat/api.go delete mode 100644 tools/stat/get1.go delete mode 100644 tools/stat/get2.go diff --git a/kittens/ssh/config.go b/kittens/ssh/config.go index ca07bde74..51ac6f9ff 100644 --- a/kittens/ssh/config.go +++ b/kittens/ssh/config.go @@ -7,14 +7,13 @@ import ( "encoding/json" "errors" "fmt" - "io/fs" "os" "path" "path/filepath" "strings" + "time" "kitty/tools/config" - "kitty/tools/stat" "kitty/tools/utils" "kitty/tools/utils/paths" "kitty/tools/utils/shlex" @@ -241,31 +240,27 @@ func excluded(pattern, path string) bool { } func get_file_data(callback func(h *tar.Header, data []byte) error, seen map[file_unique_id]string, local_path, arcname string, exclude_patterns []string) error { - s_, err := os.Lstat(local_path) - if err != nil { + var s unix.Stat_t + if err := unix.Lstat(local_path, &s); err != nil { return err } - s := stat.Get(s_) cb := func(h *tar.Header, data []byte, arcname string) error { h.Name = arcname if h.Typeflag == tar.TypeDir { h.Name = strings.TrimRight(h.Name, "/") + "/" } h.Size = int64(len(data)) - h.Mode = int64(s.Mode.Perm()) - h.ModTime = s.Mtime + h.Mode = int64(s.Mode & 0777) // discard the setuid, setgid and sticky bits + h.ModTime = time.Unix(s.Mtim.Unix()) + h.AccessTime = time.Unix(s.Atim.Unix()) + h.ChangeTime = time.Unix(s.Ctim.Unix()) h.Format = tar.FormatPAX - if s.Has_atime { - h.AccessTime = s.Atime - } - if s.Has_ctime { - h.ChangeTime = s.Ctime - } return callback(h, data) } // we only copy regular files, directories and symlinks - switch s.Mode.Type() { - case fs.ModeSymlink: + switch s.Mode & unix.S_IFMT { + case unix.S_IFBLK, unix.S_IFIFO, unix.S_IFCHR, unix.S_IFSOCK: // ignored + case unix.S_IFLNK: // symlink target, err := os.Readlink(local_path) if err != nil { return err @@ -277,7 +272,7 @@ func get_file_data(callback func(h *tar.Header, data []byte) error, seen map[fil if err != nil { return err } - case fs.ModeDir: + case unix.S_IFDIR: // directory local_path = filepath.Clean(local_path) type entry struct { path, arcname string @@ -320,14 +315,12 @@ func get_file_data(callback func(h *tar.Header, data []byte) error, seen map[fil } } } - case 0: // Regular file - if s.Has_ino && s.Has_dev { - fid := file_unique_id{dev: s.Dev, inode: s.Ino} - if prev, ok := seen[fid]; ok { // Hard link - return cb(&tar.Header{Typeflag: tar.TypeLink, Linkname: prev}, nil, arcname) - } - seen[fid] = arcname + case unix.S_IFREG: // Regular file + fid := file_unique_id{dev: s.Dev, inode: s.Ino} + if prev, ok := seen[fid]; ok { // Hard link + return cb(&tar.Header{Typeflag: tar.TypeLink, Linkname: prev}, nil, arcname) } + seen[fid] = arcname data, err := os.ReadFile(local_path) if err != nil { return err diff --git a/tools/stat/api.go b/tools/stat/api.go deleted file mode 100644 index 084271d68..000000000 --- a/tools/stat/api.go +++ /dev/null @@ -1,49 +0,0 @@ -package stat - -import ( - "fmt" - "io/fs" - "syscall" - "time" -) - -var _ = fmt.Print - -type StatResult struct { - Name string - Size int64 - Mode fs.FileMode - Ctime, Mtime, Atime time.Time - Has_ctime, Has_atime bool - Dev, Ino uint64 - Has_dev, Has_ino bool - Number_links uint64 - Has_number_links bool - Uid, Gid uint64 - Has_uid, Has_gid bool -} - -func (s *StatResult) setup_common(f fs.FileInfo) (u *syscall.Stat_t) { - s.Name = f.Name() - s.Size = f.Size() - s.Mode = f.Mode() - s.Mtime = f.ModTime() - ok := false - u, ok = f.Sys().(*syscall.Stat_t) - if !ok { - u = nil - } - if u != nil { - s.Dev = uint64(u.Dev) - s.Ino = uint64(u.Ino) - s.Has_dev = true - s.Has_ino = true - s.Number_links = uint64(u.Nlink) - s.Has_number_links = true - s.Uid = uint64(u.Uid) - s.Gid = uint64(u.Gid) - s.Has_uid = true - s.Has_gid = true - } - return -} diff --git a/tools/stat/get1.go b/tools/stat/get1.go deleted file mode 100644 index 68ff68b74..000000000 --- a/tools/stat/get1.go +++ /dev/null @@ -1,22 +0,0 @@ -//go:build aix || linux || dragonfly || openbsd || solaris - -package stat - -import ( - "fmt" - "io/fs" - "time" -) - -var _ = fmt.Print - -func Get(f fs.FileInfo) (ans StatResult) { - u := ans.setup_common(f) - if u != nil { - ans.Has_atime = true - ans.Has_ctime = true - ans.Ctime = time.Unix(u.Ctim.Unix()) - ans.Atime = time.Unix(u.Atim.Unix()) - } - return -} diff --git a/tools/stat/get2.go b/tools/stat/get2.go deleted file mode 100644 index 20bf99631..000000000 --- a/tools/stat/get2.go +++ /dev/null @@ -1,22 +0,0 @@ -//go:build darwin || freebsd || netbsd - -package stat - -import ( - "fmt" - "io/fs" - "time" -) - -var _ = fmt.Print - -func Get(f fs.FileInfo) (ans StatResult) { - u := ans.setup_common(f) - if u != nil { - ans.Has_atime = true - ans.Has_ctime = true - ans.Ctime = time.Unix(u.Ctimespec.Unix()) - ans.Atime = time.Unix(u.Atimespec.Unix()) - } - return -}