Merge pull request #4990 from m-wild/exclude-cloud-files

backup: allow excluding online-only cloud files
This commit is contained in:
Michael Eischer
2025-01-13 21:24:53 +01:00
committed by GitHub
8 changed files with 154 additions and 1 deletions

View File

@@ -32,3 +32,8 @@ func extendedStat(fi os.FileInfo) *ExtendedFileInfo {
ChangeTime: time.Unix(s.Ctimespec.Unix()),
}
}
// RecallOnDataAccess checks windows-specific attributes to determine if a file is a cloud-only placeholder.
func (*ExtendedFileInfo) RecallOnDataAccess() (bool, error) {
return false, nil
}

View File

@@ -32,3 +32,8 @@ func extendedStat(fi os.FileInfo) *ExtendedFileInfo {
ChangeTime: time.Unix(s.Ctim.Unix()),
}
}
// RecallOnDataAccess checks windows-specific attributes to determine if a file is a cloud-only placeholder.
func (*ExtendedFileInfo) RecallOnDataAccess() (bool, error) {
return false, nil
}

View File

@@ -8,6 +8,8 @@ import (
"os"
"syscall"
"time"
"golang.org/x/sys/windows"
)
// extendedStat extracts info into an ExtendedFileInfo for Windows.
@@ -36,3 +38,20 @@ func extendedStat(fi os.FileInfo) *ExtendedFileInfo {
return &extFI
}
// RecallOnDataAccess checks if a file is available locally on the disk or if the file is
// just a placeholder which must be downloaded from a remote server. This is typically used
// in cloud syncing services (e.g. OneDrive) to prevent downloading files from cloud storage
// until they are accessed.
func (fi *ExtendedFileInfo) RecallOnDataAccess() (bool, error) {
attrs, ok := fi.sys.(*syscall.Win32FileAttributeData)
if !ok {
return false, fmt.Errorf("could not determine file attributes: %s", fi.Name)
}
if attrs.FileAttributes&windows.FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS > 0 {
return true, nil
}
return false, nil
}

View File

@@ -0,0 +1,80 @@
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/windows"
)
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 {
FileAttributes 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.Win32FileAttributeData{
FileAttributes: m.FileAttributes,
}
}
func TestRecallOnDataAccessMockCloudFile(t *testing.T) {
fi := mockFileInfo{
FileAttributes: windows.FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS,
}
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{
FileAttributes: windows.FILE_ATTRIBUTE_ARCHIVE,
}
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")
}