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.
This commit is contained in:
2023-10-06 01:58:28 -05:00
parent 9ff5484ec0
commit 3481814562
7 changed files with 36 additions and 54 deletions

View File

@@ -98,7 +98,7 @@ func CreateEntryFromFileInfo(fileInfo os.FileInfo, directory string) *Entry {
Mode: uint32(mode), Mode: uint32(mode),
} }
GetOwner(entry, &fileInfo) GetOwner(entry, fileInfo)
return entry return entry
} }

View File

@@ -10,7 +10,6 @@ package duplicacy
import ( import (
"context" "context"
"errors" "errors"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"regexp" "regexp"
@@ -136,7 +135,7 @@ func CreateShadowCopy(top string, shadowCopy bool, timeoutInSeconds int) (shadow
} }
// Create mount point // Create mount point
snapshotPath, err = ioutil.TempDir("/tmp/", "snp_") snapshotPath, err = os.MkdirTemp("/tmp/", "snp_")
if err != nil { if err != nil {
LOG_ERROR("VSS_CREATE", "Failed to create temporary mount directory") LOG_ERROR("VSS_CREATE", "Failed to create temporary mount directory")
return top return top

View File

@@ -23,7 +23,7 @@ const (
var darwinIsSuperUser bool var darwinIsSuperUser bool
func init() { func init() {
darwinIsSuperUser = syscall.Geteuid() == 0 darwinIsSuperUser = unix.Geteuid() == 0
} }
func excludedByAttribute(attributes map[string][]byte) bool { 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 { func (entry *Entry) ReadFileFlags(fullPath string, fileInfo os.FileInfo) error {
stat, _ := fileInfo.Sys().(*syscall.Stat_t) stat := fileInfo.Sys().(*syscall.Stat_t)
if stat != nil && stat.Flags != 0 { if stat.Flags != 0 {
if entry.Attributes == nil { if entry.Attributes == nil {
entry.Attributes = &map[string][]byte{} entry.Attributes = &map[string][]byte{}
} }
@@ -132,22 +132,20 @@ func (entry *Entry) RestoreLateFileFlags(fullPath string, fileInfo os.FileInfo,
var flags uint32 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 { if v, have := (*entry.Attributes)[darwinFileFlagsKey]; have {
flags = binary.LittleEndian.Uint32(v) flags = binary.LittleEndian.Uint32(v)
} }
stat := fileInfo.Sys().(*syscall.Stat_t)
flags = (flags & ^mask) | (stat.Flags & mask) flags = (flags & ^mask) | (stat.Flags & mask)
if flags != stat.Flags { 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 { if err != nil {
return err return err
} }
err = syscall.Fchflags(int(f.Fd()), int(flags)) err = unix.Fchflags(int(f.Fd()), int(flags))
f.Close() f.Close()
return err return err
} }
@@ -158,13 +156,13 @@ func (entry *Entry) RestoreSpecial(fullPath string) error {
mode := entry.Mode & uint32(fileModeMask) mode := entry.Mode & uint32(fileModeMask)
if entry.Mode&uint32(os.ModeNamedPipe) != 0 { if entry.Mode&uint32(os.ModeNamedPipe) != 0 {
mode |= syscall.S_IFIFO mode |= unix.S_IFIFO
} else if entry.Mode&uint32(os.ModeCharDevice) != 0 { } else if entry.Mode&uint32(os.ModeCharDevice) != 0 {
mode |= syscall.S_IFCHR mode |= unix.S_IFCHR
} else if entry.Mode&uint32(os.ModeDevice) != 0 { } else if entry.Mode&uint32(os.ModeDevice) != 0 {
mode |= syscall.S_IFBLK mode |= unix.S_IFBLK
} else { } else {
return nil return nil
} }
return syscall.Mknod(fullPath, mode, int(entry.GetRdev())) return unix.Mknod(fullPath, mode, int(entry.GetRdev()))
} }

View File

@@ -10,10 +10,10 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"syscall"
"unsafe" "unsafe"
"github.com/pkg/xattr" "github.com/pkg/xattr"
"golang.org/x/sys/unix"
) )
const ( const (
@@ -46,7 +46,7 @@ const (
func ioctl(f *os.File, request uintptr, attrp *uint32) error { func ioctl(f *os.File, request uintptr, attrp *uint32) error {
argp := uintptr(unsafe.Pointer(attrp)) 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 os.NewSyscallError("ioctl", errno)
} }
return nil return nil
@@ -88,7 +88,7 @@ func (entry *Entry) ReadFileFlags(fullPath string, fileInfo os.FileInfo) error {
return nil 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 { if err != nil {
return err return err
} }
@@ -159,7 +159,7 @@ func (entry *Entry) RestoreEarlyDirFlags(fullPath string, mask uint32) error {
} }
if flags != 0 { 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 { if err != nil {
return err return err
} }
@@ -202,7 +202,7 @@ func (entry *Entry) RestoreLateFileFlags(fullPath string, fileInfo os.FileInfo,
} }
if flags != 0 { 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 { if err != nil {
return err return err
} }
@@ -219,15 +219,15 @@ func (entry *Entry) RestoreSpecial(fullPath string) error {
mode := entry.Mode & uint32(fileModeMask) mode := entry.Mode & uint32(fileModeMask)
if entry.Mode&uint32(os.ModeNamedPipe) != 0 { if entry.Mode&uint32(os.ModeNamedPipe) != 0 {
mode |= syscall.S_IFIFO mode |= unix.S_IFIFO
} else if entry.Mode&uint32(os.ModeCharDevice) != 0 { } else if entry.Mode&uint32(os.ModeCharDevice) != 0 {
mode |= syscall.S_IFCHR mode |= unix.S_IFCHR
} else if entry.Mode&uint32(os.ModeDevice) != 0 { } else if entry.Mode&uint32(os.ModeDevice) != 0 {
mode |= syscall.S_IFBLK mode |= unix.S_IFBLK
} else if entry.Mode&uint32(os.ModeSocket) != 0 { } else if entry.Mode&uint32(os.ModeSocket) != 0 {
mode |= syscall.S_IFSOCK mode |= unix.S_IFSOCK
} else { } else {
return nil return nil
} }
return syscall.Mknod(fullPath, mode, int(entry.GetRdev())) return unix.Mknod(fullPath, mode, int(entry.GetRdev()))
} }

View File

@@ -8,7 +8,6 @@
package duplicacy package duplicacy
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path" "path"
@@ -22,20 +21,15 @@ func Readlink(path string) (isRegular bool, s string, err error) {
return false, s, err return false, s, err
} }
func GetOwner(entry *Entry, fileInfo *os.FileInfo) { func GetOwner(entry *Entry, fileInfo os.FileInfo) {
stat, ok := (*fileInfo).Sys().(*syscall.Stat_t) stat := fileInfo.Sys().(*syscall.Stat_t)
if ok && stat != nil { entry.UID = int(stat.Uid)
entry.UID = int(stat.Uid) entry.GID = int(stat.Gid)
entry.GID = int(stat.Gid)
} else {
entry.UID = -1
entry.GID = -1
}
} }
func SetOwner(fullPath string, entry *Entry, fileInfo os.FileInfo) bool { func SetOwner(fullPath string, entry *Entry, fileInfo os.FileInfo) bool {
stat, ok := fileInfo.Sys().(*syscall.Stat_t) stat := fileInfo.Sys().(*syscall.Stat_t)
if ok && stat != nil && (int(stat.Uid) != entry.UID || int(stat.Gid) != entry.GID) { if (int(stat.Uid) != entry.UID || int(stat.Gid) != entry.GID) {
if entry.UID != -1 && entry.GID != -1 { if entry.UID != -1 && entry.GID != -1 {
err := os.Lchown(fullPath, entry.UID, entry.GID) err := os.Lchown(fullPath, entry.UID, entry.GID)
if err != nil { if err != nil {
@@ -58,7 +52,7 @@ func (entry *Entry) getHardLinkKey(f os.FileInfo) (key listEntryLinkKey, linked
return return
} }
stat := f.Sys().(*syscall.Stat_t) stat := f.Sys().(*syscall.Stat_t)
if stat == nil || stat.Nlink <= 1 { if stat.Nlink <= 1 {
return return
} }
key.dev = uint64(stat.Dev) 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 { if fileInfo.Mode()&(os.ModeDevice|os.ModeCharDevice) == 0 {
return nil return nil
} }
stat := fileInfo.Sys().(*syscall.Stat_t) rdev := uint64(fileInfo.Sys().(*syscall.Stat_t).Rdev)
if stat == nil {
return errors.New("file stat info missing")
}
entry.Size = 0 entry.Size = 0
rdev := uint64(stat.Rdev)
entry.StartChunk = int(rdev & 0xFFFFFFFF) entry.StartChunk = int(rdev & 0xFFFFFFFF)
entry.StartOffset = int(rdev >> 32) entry.StartOffset = int(rdev >> 32)
return nil return nil
@@ -88,9 +78,6 @@ func (entry *Entry) GetRdev() uint64 {
func (entry *Entry) IsSameSpecial(fileInfo os.FileInfo) bool { func (entry *Entry) IsSameSpecial(fileInfo os.FileInfo) bool {
stat := fileInfo.Sys().(*syscall.Stat_t) stat := fileInfo.Sys().(*syscall.Stat_t)
if stat == nil {
return false
}
return (uint32(fileInfo.Mode()) == entry.Mode) && (uint64(stat.Rdev) == entry.GetRdev()) return (uint32(fileInfo.Mode()) == entry.Mode) && (uint64(stat.Rdev) == entry.GetRdev())
} }

View File

@@ -105,7 +105,7 @@ func Readlink(path string) (isRegular bool, s string, err error) {
return false, s, nil return false, s, nil
} }
func GetOwner(entry *Entry, fileInfo *os.FileInfo) { func GetOwner(entry *Entry, fileInfo os.FileInfo) {
entry.UID = -1 entry.UID = -1
entry.GID = -1 entry.GID = -1
} }

View File

@@ -68,8 +68,8 @@ func (entry *Entry) ReadAttributes(fullPath string, fi os.FileInfo) error {
} }
func (entry *Entry) ReadFileFlags(fullPath string, fileInfo os.FileInfo) error { func (entry *Entry) ReadFileFlags(fullPath string, fileInfo os.FileInfo) error {
stat, _ := fileInfo.Sys().(*syscall.Stat_t) stat := fileInfo.Sys().(*syscall.Stat_t)
if stat != nil && stat.Flags != 0 { if stat.Flags != 0 {
if entry.Attributes == nil { if entry.Attributes == nil {
entry.Attributes = &map[string][]byte{} entry.Attributes = &map[string][]byte{}
} }
@@ -137,14 +137,12 @@ func (entry *Entry) RestoreLateFileFlags(fullPath string, fileInfo os.FileInfo,
var flags uint32 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 { if v, have := (*entry.Attributes)[bsdFileFlagsKey]; have {
flags = binary.LittleEndian.Uint32(v) flags = binary.LittleEndian.Uint32(v)
} }
stat := fileInfo.Sys().(*syscall.Stat_t)
flags = (flags & ^mask) | (stat.Flags & mask) flags = (flags & ^mask) | (stat.Flags & mask)
if flags != stat.Flags { if flags != stat.Flags {