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),
}
GetOwner(entry, &fileInfo)
GetOwner(entry, fileInfo)
return entry
}

View File

@@ -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

View File

@@ -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()))
}

View File

@@ -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()))
}

View File

@@ -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())
}

View File

@@ -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
}

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 {
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 {