From fd0f544c04709643874767748ceb283c2505f330 Mon Sep 17 00:00:00 2001 From: "John K. Luebs" Date: Fri, 6 Oct 2023 01:58:28 -0500 Subject: [PATCH] Cleanup some of the unix code and don't pass ptr to iface to GetOwner Don't bother using checked type assertion for stat since we are guarded by specific build configuration, we should know the correct type, and if not, panicing is fine. Despite syscall being deprecated a decade ago, we still need it for FileInfo and sys/windows still calls it as well. --- src/duplicacy_entry.go | 2 +- src/duplicacy_shadowcopy_darwin.go | 3 +-- src/duplicacy_utils_darwin.go | 24 +++++++++++------------- src/duplicacy_utils_linux.go | 20 ++++++++++---------- src/duplicacy_utils_others.go | 29 ++++++++--------------------- src/duplicacy_utils_windows.go | 2 +- src/duplicacy_utils_xbsd.go | 10 ++++------ 7 files changed, 36 insertions(+), 54 deletions(-) diff --git a/src/duplicacy_entry.go b/src/duplicacy_entry.go index 2a2d947..20a69df 100644 --- a/src/duplicacy_entry.go +++ b/src/duplicacy_entry.go @@ -98,7 +98,7 @@ func CreateEntryFromFileInfo(fileInfo os.FileInfo, directory string) *Entry { Mode: uint32(mode), } - GetOwner(entry, &fileInfo) + GetOwner(entry, fileInfo) return entry } diff --git a/src/duplicacy_shadowcopy_darwin.go b/src/duplicacy_shadowcopy_darwin.go index 1d8a3b6..3a4d9c2 100755 --- a/src/duplicacy_shadowcopy_darwin.go +++ b/src/duplicacy_shadowcopy_darwin.go @@ -10,7 +10,6 @@ package duplicacy import ( "context" "errors" - "io/ioutil" "os" "os/exec" "regexp" @@ -136,7 +135,7 @@ func CreateShadowCopy(top string, shadowCopy bool, timeoutInSeconds int) (shadow } // Create mount point - snapshotPath, err = ioutil.TempDir("/tmp/", "snp_") + snapshotPath, err = os.MkdirTemp("/tmp/", "snp_") if err != nil { LOG_ERROR("VSS_CREATE", "Failed to create temporary mount directory") return top diff --git a/src/duplicacy_utils_darwin.go b/src/duplicacy_utils_darwin.go index a5c8c27..c820f69 100644 --- a/src/duplicacy_utils_darwin.go +++ b/src/duplicacy_utils_darwin.go @@ -23,7 +23,7 @@ const ( var darwinIsSuperUser bool func init() { - darwinIsSuperUser = syscall.Geteuid() == 0 + darwinIsSuperUser = unix.Geteuid() == 0 } func excludedByAttribute(attributes map[string][]byte) bool { @@ -63,8 +63,8 @@ func (entry *Entry) ReadAttributes(fullPath string, fi os.FileInfo) error { } func (entry *Entry) ReadFileFlags(fullPath string, fileInfo os.FileInfo) error { - stat, _ := fileInfo.Sys().(*syscall.Stat_t) - if stat != nil && stat.Flags != 0 { + stat := fileInfo.Sys().(*syscall.Stat_t) + if stat.Flags != 0 { if entry.Attributes == nil { entry.Attributes = &map[string][]byte{} } @@ -132,22 +132,20 @@ func (entry *Entry) RestoreLateFileFlags(fullPath string, fileInfo os.FileInfo, var flags uint32 - stat := fileInfo.Sys().(*syscall.Stat_t) - if stat == nil { - return errors.New("file stat info missing") - } if v, have := (*entry.Attributes)[darwinFileFlagsKey]; have { flags = binary.LittleEndian.Uint32(v) } + stat := fileInfo.Sys().(*syscall.Stat_t) + flags = (flags & ^mask) | (stat.Flags & mask) if flags != stat.Flags { - f, err := os.OpenFile(fullPath, os.O_RDONLY|syscall.O_SYMLINK, 0) + f, err := os.OpenFile(fullPath, os.O_RDONLY|unix.O_SYMLINK, 0) if err != nil { return err } - err = syscall.Fchflags(int(f.Fd()), int(flags)) + err = unix.Fchflags(int(f.Fd()), int(flags)) f.Close() return err } @@ -158,13 +156,13 @@ func (entry *Entry) RestoreSpecial(fullPath string) error { mode := entry.Mode & uint32(fileModeMask) if entry.Mode&uint32(os.ModeNamedPipe) != 0 { - mode |= syscall.S_IFIFO + mode |= unix.S_IFIFO } else if entry.Mode&uint32(os.ModeCharDevice) != 0 { - mode |= syscall.S_IFCHR + mode |= unix.S_IFCHR } else if entry.Mode&uint32(os.ModeDevice) != 0 { - mode |= syscall.S_IFBLK + mode |= unix.S_IFBLK } else { return nil } - return syscall.Mknod(fullPath, mode, int(entry.GetRdev())) + return unix.Mknod(fullPath, mode, int(entry.GetRdev())) } diff --git a/src/duplicacy_utils_linux.go b/src/duplicacy_utils_linux.go index 9d9560e..57042d0 100644 --- a/src/duplicacy_utils_linux.go +++ b/src/duplicacy_utils_linux.go @@ -10,10 +10,10 @@ import ( "errors" "fmt" "os" - "syscall" "unsafe" "github.com/pkg/xattr" + "golang.org/x/sys/unix" ) const ( @@ -46,7 +46,7 @@ const ( func ioctl(f *os.File, request uintptr, attrp *uint32) error { argp := uintptr(unsafe.Pointer(attrp)) - if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), request, argp); errno != 0 { + if _, _, errno := unix.Syscall(unix.SYS_IOCTL, f.Fd(), request, argp); errno != 0 { return os.NewSyscallError("ioctl", errno) } return nil @@ -88,7 +88,7 @@ func (entry *Entry) ReadFileFlags(fullPath string, fileInfo os.FileInfo) error { return nil } - f, err := os.OpenFile(fullPath, os.O_RDONLY|syscall.O_NOFOLLOW, 0) + f, err := os.OpenFile(fullPath, os.O_RDONLY|unix.O_NOFOLLOW, 0) if err != nil { return err } @@ -159,7 +159,7 @@ func (entry *Entry) RestoreEarlyDirFlags(fullPath string, mask uint32) error { } if flags != 0 { - f, err := os.OpenFile(fullPath, os.O_RDONLY|syscall.O_DIRECTORY, 0) + f, err := os.OpenFile(fullPath, os.O_RDONLY|unix.O_DIRECTORY, 0) if err != nil { return err } @@ -202,7 +202,7 @@ func (entry *Entry) RestoreLateFileFlags(fullPath string, fileInfo os.FileInfo, } if flags != 0 { - f, err := os.OpenFile(fullPath, os.O_RDONLY|syscall.O_NOFOLLOW, 0) + f, err := os.OpenFile(fullPath, os.O_RDONLY|unix.O_NOFOLLOW, 0) if err != nil { return err } @@ -219,15 +219,15 @@ func (entry *Entry) RestoreSpecial(fullPath string) error { mode := entry.Mode & uint32(fileModeMask) if entry.Mode&uint32(os.ModeNamedPipe) != 0 { - mode |= syscall.S_IFIFO + mode |= unix.S_IFIFO } else if entry.Mode&uint32(os.ModeCharDevice) != 0 { - mode |= syscall.S_IFCHR + mode |= unix.S_IFCHR } else if entry.Mode&uint32(os.ModeDevice) != 0 { - mode |= syscall.S_IFBLK + mode |= unix.S_IFBLK } else if entry.Mode&uint32(os.ModeSocket) != 0 { - mode |= syscall.S_IFSOCK + mode |= unix.S_IFSOCK } else { return nil } - return syscall.Mknod(fullPath, mode, int(entry.GetRdev())) + return unix.Mknod(fullPath, mode, int(entry.GetRdev())) } diff --git a/src/duplicacy_utils_others.go b/src/duplicacy_utils_others.go index 1c87887..152ea56 100644 --- a/src/duplicacy_utils_others.go +++ b/src/duplicacy_utils_others.go @@ -8,7 +8,6 @@ package duplicacy import ( - "errors" "fmt" "os" "path" @@ -22,20 +21,15 @@ func Readlink(path string) (isRegular bool, s string, err error) { return false, s, err } -func GetOwner(entry *Entry, fileInfo *os.FileInfo) { - stat, ok := (*fileInfo).Sys().(*syscall.Stat_t) - if ok && stat != nil { - entry.UID = int(stat.Uid) - entry.GID = int(stat.Gid) - } else { - entry.UID = -1 - entry.GID = -1 - } +func GetOwner(entry *Entry, fileInfo os.FileInfo) { + stat := fileInfo.Sys().(*syscall.Stat_t) + entry.UID = int(stat.Uid) + entry.GID = int(stat.Gid) } func SetOwner(fullPath string, entry *Entry, fileInfo os.FileInfo) bool { - stat, ok := fileInfo.Sys().(*syscall.Stat_t) - if ok && stat != nil && (int(stat.Uid) != entry.UID || int(stat.Gid) != entry.GID) { + stat := fileInfo.Sys().(*syscall.Stat_t) + if (int(stat.Uid) != entry.UID || int(stat.Gid) != entry.GID) { if entry.UID != -1 && entry.GID != -1 { err := os.Lchown(fullPath, entry.UID, entry.GID) if err != nil { @@ -58,7 +52,7 @@ func (entry *Entry) getHardLinkKey(f os.FileInfo) (key listEntryLinkKey, linked return } stat := f.Sys().(*syscall.Stat_t) - if stat == nil || stat.Nlink <= 1 { + if stat.Nlink <= 1 { return } key.dev = uint64(stat.Dev) @@ -71,12 +65,8 @@ func (entry *Entry) ReadSpecial(fullPath string, fileInfo os.FileInfo) error { if fileInfo.Mode()&(os.ModeDevice|os.ModeCharDevice) == 0 { return nil } - stat := fileInfo.Sys().(*syscall.Stat_t) - if stat == nil { - return errors.New("file stat info missing") - } + rdev := uint64(fileInfo.Sys().(*syscall.Stat_t).Rdev) entry.Size = 0 - rdev := uint64(stat.Rdev) entry.StartChunk = int(rdev & 0xFFFFFFFF) entry.StartOffset = int(rdev >> 32) return nil @@ -88,9 +78,6 @@ func (entry *Entry) GetRdev() uint64 { func (entry *Entry) IsSameSpecial(fileInfo os.FileInfo) bool { stat := fileInfo.Sys().(*syscall.Stat_t) - if stat == nil { - return false - } return (uint32(fileInfo.Mode()) == entry.Mode) && (uint64(stat.Rdev) == entry.GetRdev()) } diff --git a/src/duplicacy_utils_windows.go b/src/duplicacy_utils_windows.go index d7c1d76..1ea573d 100644 --- a/src/duplicacy_utils_windows.go +++ b/src/duplicacy_utils_windows.go @@ -105,7 +105,7 @@ func Readlink(path string) (isRegular bool, s string, err error) { return false, s, nil } -func GetOwner(entry *Entry, fileInfo *os.FileInfo) { +func GetOwner(entry *Entry, fileInfo os.FileInfo) { entry.UID = -1 entry.GID = -1 } diff --git a/src/duplicacy_utils_xbsd.go b/src/duplicacy_utils_xbsd.go index 3ef891f..3182173 100644 --- a/src/duplicacy_utils_xbsd.go +++ b/src/duplicacy_utils_xbsd.go @@ -68,8 +68,8 @@ func (entry *Entry) ReadAttributes(fullPath string, fi os.FileInfo) error { } func (entry *Entry) ReadFileFlags(fullPath string, fileInfo os.FileInfo) error { - stat, _ := fileInfo.Sys().(*syscall.Stat_t) - if stat != nil && stat.Flags != 0 { + stat := fileInfo.Sys().(*syscall.Stat_t) + if stat.Flags != 0 { if entry.Attributes == nil { entry.Attributes = &map[string][]byte{} } @@ -137,14 +137,12 @@ func (entry *Entry) RestoreLateFileFlags(fullPath string, fileInfo os.FileInfo, var flags uint32 - stat := fileInfo.Sys().(*syscall.Stat_t) - if stat == nil { - return errors.New("file stat info missing") - } if v, have := (*entry.Attributes)[bsdFileFlagsKey]; have { flags = binary.LittleEndian.Uint32(v) } + stat := fileInfo.Sys().(*syscall.Stat_t) + flags = (flags & ^mask) | (stat.Flags & mask) if flags != stat.Flags {