mirror of
https://github.com/restic/restic.git
synced 2026-11-03 14:05:33 +00:00
feat(backup): add possibility to exclude macOS cloud-only files
This commit is contained in:
1 parent
a2a49cf784
commit
f3d95893b2
8 files changed
+180
-12
No files matched your search
+1
-1
@@ -25,7 +25,7 @@ type ExtendedFileInfo struct {
|
||||
ModTime time.Time // last (content) modification time stamp
|
||||
ChangeTime time.Time // last status change time stamp
|
||||
|
||||
//nolint:unused // only used on Windows
|
||||
//nolint:unused // only used on Windows/Darwin
|
||||
sys any // Value returned by os.FileInfo.Sys()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//go:build freebsd || darwin || netbsd
|
||||
// +build freebsd darwin netbsd
|
||||
//go:build freebsd || netbsd
|
||||
// +build freebsd netbsd
|
||||
|
||||
package fs
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package fs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// extendedStat extracts info into an ExtendedFileInfo for macOS.
|
||||
func extendedStat(fi os.FileInfo) *ExtendedFileInfo {
|
||||
s := fi.Sys().(*syscall.Stat_t)
|
||||
|
||||
return &ExtendedFileInfo{
|
||||
Name: fi.Name(),
|
||||
Mode: fi.Mode(),
|
||||
|
||||
DeviceID: uint64(s.Dev),
|
||||
Inode: uint64(s.Ino),
|
||||
Links: uint64(s.Nlink),
|
||||
UID: s.Uid,
|
||||
GID: s.Gid,
|
||||
Device: uint64(s.Rdev),
|
||||
BlockSize: int64(s.Blksize),
|
||||
Blocks: s.Blocks,
|
||||
Size: s.Size,
|
||||
|
||||
AccessTime: time.Unix(s.Atimespec.Unix()),
|
||||
ModTime: time.Unix(s.Mtimespec.Unix()),
|
||||
ChangeTime: time.Unix(s.Ctimespec.Unix()),
|
||||
|
||||
sys: s,
|
||||
}
|
||||
}
|
||||
|
||||
// RecallOnDataAccess checks if a file is available locally on the disk or if the file is
|
||||
// just a dataless files which must be downloaded from a remote server. This is typically used
|
||||
// in cloud syncing services (e.g. iCloud drive) to prevent downloading files from cloud storage
|
||||
// until they are accessed.
|
||||
func (fi *ExtendedFileInfo) RecallOnDataAccess() (bool, error) {
|
||||
extAttribute, ok := fi.sys.(*syscall.Stat_t)
|
||||
if !ok {
|
||||
return false, fmt.Errorf("could not determine file attributes: %s", fi.Name)
|
||||
}
|
||||
const mask uint32 = unix.SF_DATALESS // 0x40000000
|
||||
if extAttribute.Flags&mask == mask {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package fs_test
|
||||
|
||||
import (
|
||||
iofs "io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/restic/restic/internal/fs"
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func TestRecallOnDataAccessRealFile(t *testing.T) {
|
||||
// create a temp file for testing
|
||||
tempdir := rtest.TempDir(t)
|
||||
filename := filepath.Join(tempdir, "regular-file")
|
||||
err := os.WriteFile(filename, []byte("foobar"), 0640)
|
||||
rtest.OK(t, err)
|
||||
|
||||
fi, err := os.Stat(filename)
|
||||
rtest.OK(t, err)
|
||||
|
||||
xs := fs.ExtendedStat(fi)
|
||||
|
||||
// ensure we can check attrs without error
|
||||
recall, err := xs.RecallOnDataAccess()
|
||||
rtest.Assert(t, err == nil, "err should be nil", err)
|
||||
rtest.Assert(t, recall == false, "RecallOnDataAccess should be false")
|
||||
}
|
||||
|
||||
// mockFileInfo implements os.FileInfo for mocking file attributes
|
||||
type mockFileInfo struct {
|
||||
Flags uint32
|
||||
}
|
||||
|
||||
func (m mockFileInfo) IsDir() bool {
|
||||
return false
|
||||
}
|
||||
func (m mockFileInfo) ModTime() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
func (m mockFileInfo) Mode() iofs.FileMode {
|
||||
return 0
|
||||
}
|
||||
func (m mockFileInfo) Name() string {
|
||||
return "test"
|
||||
}
|
||||
func (m mockFileInfo) Size() int64 {
|
||||
return 0
|
||||
}
|
||||
func (m mockFileInfo) Sys() any {
|
||||
return &syscall.Stat_t{
|
||||
Flags: m.Flags,
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecallOnDataAccessMockCloudFile(t *testing.T) {
|
||||
fi := mockFileInfo{
|
||||
Flags: unix.SF_DATALESS,
|
||||
}
|
||||
xs := fs.ExtendedStat(fi)
|
||||
|
||||
recall, err := xs.RecallOnDataAccess()
|
||||
rtest.Assert(t, err == nil, "err should be nil", err)
|
||||
rtest.Assert(t, recall, "RecallOnDataAccess should be true")
|
||||
}
|
||||
|
||||
func TestRecallOnDataAccessMockRegularFile(t *testing.T) {
|
||||
fi := mockFileInfo{
|
||||
Flags: 0,
|
||||
}
|
||||
xs := fs.ExtendedStat(fi)
|
||||
|
||||
recall, err := xs.RecallOnDataAccess()
|
||||
rtest.Assert(t, err == nil, "err should be nil", err)
|
||||
rtest.Assert(t, recall == false, "RecallOnDataAccess should be false")
|
||||
}
|
||||
|
||||
func TestRecallOnDataAccessMockError(t *testing.T) {
|
||||
efi := &fs.ExtendedFileInfo{
|
||||
Name: "test-file-name",
|
||||
}
|
||||
|
||||
recall, err := efi.RecallOnDataAccess()
|
||||
rtest.Assert(t, err != nil, "err should be set", err)
|
||||
rtest.Assert(t, err.Error() == "could not determine file attributes: test-file-name", "err message not correct", err)
|
||||
rtest.Assert(t, recall == false, "RecallOnDataAccess should be false")
|
||||
}
|
||||
@@ -27,3 +27,14 @@ func TestExtendedStat(t *testing.T) {
|
||||
t.Errorf("extFI.ModTime does not match, want %v, got %v", fi.ModTime(), extFI.ModTime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNilExtendPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
rtest.Assert(t, r == "os.FileInfo is nil", "Panic message does not match, want %v, got %v", "os.FileInfo is nil", r)
|
||||
} else {
|
||||
rtest.Assert(t, false, "Expected panic, but no panic occurred")
|
||||
}
|
||||
}()
|
||||
_ = ExtendedStat(nil)
|
||||
}
|
||||
Reference in new issue
Block a user