all: Support syncing ownership (fixes #1329) (#8434)

This adds support for syncing ownership on Unixes and on Windows. The
scanner always picks up ownership information, but it is not applied
unless the new folder option "Sync Ownership" is set.

Ownership data is stored in a new FileInfo field called "platform data". This
is intended to hold further platform-specific data in the future
(specifically, extended attributes), which is why the whole design is a
bit overkill for just ownership.
This commit is contained in:
Jakob Borg
2022-07-26 08:24:58 +02:00
committed by GitHub
parent 34a5f087c8
commit adce6fa473
35 changed files with 1896 additions and 500 deletions
+9
View File
@@ -15,6 +15,7 @@ import (
"time"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/protocol"
)
type infiniteFS struct {
@@ -54,6 +55,10 @@ func (i infiniteFS) Open(name string) (fs.File, error) {
return &fakeFile{name, i.filesize, 0}, nil
}
func (i infiniteFS) PlatformData(name string) (protocol.PlatformData, error) {
return protocol.PlatformData{}, nil
}
type singleFileFS struct {
fs.Filesystem
name string
@@ -100,6 +105,10 @@ func (s singleFileFS) Options() []fs.Option {
return nil
}
func (s singleFileFS) PlatformData(name string) (protocol.PlatformData, error) {
return protocol.PlatformData{}, nil
}
type fakeInfo struct {
name string
size int64
+56 -15
View File
@@ -40,9 +40,10 @@ type Config struct {
// The Filesystem provides an abstraction on top of the actual filesystem.
Filesystem fs.Filesystem
// If IgnorePerms is true, changes to permission bits will not be
// detected. Scanned files will get zero permission bits and the
// NoPermissionBits flag set.
// detected.
IgnorePerms bool
// If IgnoreOwnership is true, changes to ownership will not be detected.
IgnoreOwnership bool
// When AutoNormalize is set, file names that are in UTF8 but incorrect
// normalization form will be corrected.
AutoNormalize bool
@@ -381,13 +382,22 @@ func (w *walker) walkRegular(ctx context.Context, relPath string, info fs.FileIn
}
}
f, _ := CreateFileInfo(info, relPath, nil)
f, err := CreateFileInfo(info, relPath, w.Filesystem)
if err != nil {
return err
}
f = w.updateFileInfo(f, curFile)
f.NoPermissions = w.IgnorePerms
f.RawBlockSize = blockSize
if hasCurFile {
if curFile.IsEquivalentOptional(f, w.ModTimeWindow, w.IgnorePerms, true, w.LocalFlags) {
if curFile.IsEquivalentOptional(f, protocol.FileInfoComparison{
ModTimeWindow: w.ModTimeWindow,
IgnorePerms: w.IgnorePerms,
IgnoreBlocks: true,
IgnoreFlags: w.LocalFlags,
IgnoreOwnership: w.IgnoreOwnership,
}) {
l.Debugln(w, "unchanged:", curFile, info.ModTime().Unix(), info.Mode()&fs.ModePerm)
return nil
}
@@ -416,12 +426,21 @@ func (w *walker) walkRegular(ctx context.Context, relPath string, info fs.FileIn
func (w *walker) walkDir(ctx context.Context, relPath string, info fs.FileInfo, finishedChan chan<- ScanResult) error {
curFile, hasCurFile := w.CurrentFiler.CurrentFile(relPath)
f, _ := CreateFileInfo(info, relPath, nil)
f, err := CreateFileInfo(info, relPath, w.Filesystem)
if err != nil {
return err
}
f = w.updateFileInfo(f, curFile)
f.NoPermissions = w.IgnorePerms
if hasCurFile {
if curFile.IsEquivalentOptional(f, w.ModTimeWindow, w.IgnorePerms, true, w.LocalFlags) {
if curFile.IsEquivalentOptional(f, protocol.FileInfoComparison{
ModTimeWindow: w.ModTimeWindow,
IgnorePerms: w.IgnorePerms,
IgnoreBlocks: true,
IgnoreFlags: w.LocalFlags,
IgnoreOwnership: w.IgnoreOwnership,
}) {
l.Debugln(w, "unchanged:", curFile, info.ModTime().Unix(), info.Mode()&fs.ModePerm)
return nil
}
@@ -466,7 +485,13 @@ func (w *walker) walkSymlink(ctx context.Context, relPath string, info fs.FileIn
f = w.updateFileInfo(f, curFile)
if hasCurFile {
if curFile.IsEquivalentOptional(f, w.ModTimeWindow, w.IgnorePerms, true, w.LocalFlags) {
if curFile.IsEquivalentOptional(f, protocol.FileInfoComparison{
ModTimeWindow: w.ModTimeWindow,
IgnorePerms: w.IgnorePerms,
IgnoreBlocks: true,
IgnoreFlags: w.LocalFlags,
IgnoreOwnership: w.IgnoreOwnership,
}) {
l.Debugln(w, "unchanged:", curFile, info.ModTime().Unix(), info.Mode()&fs.ModePerm)
return nil
}
@@ -550,17 +575,28 @@ func (w *walker) normalizePath(path string, info fs.FileInfo) (normPath string,
return "", errUTF8Conflict
}
// updateFileInfo updates walker specific members of protocol.FileInfo that do not depend on type
func (w *walker) updateFileInfo(file, curFile protocol.FileInfo) protocol.FileInfo {
if file.Type == protocol.FileInfoTypeFile && runtime.GOOS == "windows" {
// updateFileInfo updates walker specific members of protocol.FileInfo that
// do not depend on type, and things that should be preserved from the
// previous version of the FileInfo.
func (w *walker) updateFileInfo(dst, src protocol.FileInfo) protocol.FileInfo {
if dst.Type == protocol.FileInfoTypeFile && runtime.GOOS == "windows" {
// If we have an existing index entry, copy the executable bits
// from there.
file.Permissions |= (curFile.Permissions & 0111)
dst.Permissions |= (src.Permissions & 0111)
}
file.Version = curFile.Version.Update(w.ShortID)
file.ModifiedBy = w.ShortID
file.LocalFlags = w.LocalFlags
return file
dst.Version = src.Version.Update(w.ShortID)
dst.ModifiedBy = w.ShortID
dst.LocalFlags = w.LocalFlags
// Copy OS data from src to dst, unless it was already set on dst.
if dst.Platform.Unix == nil {
dst.Platform.Unix = src.Platform.Unix
}
if dst.Platform.Windows == nil {
dst.Platform.Windows = src.Platform.Windows
}
return dst
}
func handleError(ctx context.Context, context, path string, err error, finishedChan chan<- ScanResult) {
@@ -632,6 +668,11 @@ func (noCurrentFiler) CurrentFile(name string) (protocol.FileInfo, bool) {
func CreateFileInfo(fi fs.FileInfo, name string, filesystem fs.Filesystem) (protocol.FileInfo, error) {
f := protocol.FileInfo{Name: name}
if plat, err := filesystem.PlatformData(name); err == nil {
f.Platform = plat
} else {
return protocol.FileInfo{}, fmt.Errorf("reading platform data: %w", err)
}
if fi.IsSymlink() {
f.Type = protocol.FileInfoTypeSymlink
target, err := filesystem.ReadSymlink(name)
+73
View File
@@ -543,6 +543,79 @@ func TestWalkReceiveOnly(t *testing.T) {
}
}
func TestScanOwnershipPOSIX(t *testing.T) {
// This test works on all operating systems because the FakeFS is always POSIXy.
fakeFS := fs.NewFilesystem(fs.FilesystemTypeFake, "TestScanOwnership")
current := make(fakeCurrentFiler)
fakeFS.Create("root-owned")
fakeFS.Create("user-owned")
fakeFS.Lchown("user-owned", "1234", "5678")
fakeFS.Mkdir("user-owned-dir", 0755)
fakeFS.Lchown("user-owned-dir", "2345", "6789")
expected := []struct {
name string
uid, gid int
}{
{"root-owned", 0, 0},
{"user-owned", 1234, 5678},
{"user-owned-dir", 2345, 6789},
}
files := walkDir(fakeFS, ".", current, nil, 0)
if len(files) != len(expected) {
t.Fatalf("expected %d items, not %d", len(expected), len(files))
}
for i := range expected {
if files[i].Name != expected[i].name {
t.Errorf("expected %s, got %s", expected[i].name, files[i].Name)
continue
}
if files[i].Platform.Unix == nil {
t.Error("failed to load POSIX data on", files[i].Name)
continue
}
if files[i].Platform.Unix.UID != expected[i].uid {
t.Errorf("expected %d, got %d", expected[i].uid, files[i].Platform.Unix.UID)
}
if files[i].Platform.Unix.GID != expected[i].gid {
t.Errorf("expected %d, got %d", expected[i].gid, files[i].Platform.Unix.GID)
}
}
}
func TestScanOwnershipWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("This test only works on Windows")
}
testFS := fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir())
current := make(fakeCurrentFiler)
fd, err := testFS.Create("user-owned")
if err != nil {
t.Fatal(err)
}
fd.Close()
files := walkDir(testFS, ".", current, nil, 0)
if len(files) != 1 {
t.Fatalf("expected %d items, not %d", 1, len(files))
}
t.Log(files[0])
// The file should have an owner name set.
if files[0].Platform.Windows == nil {
t.Fatal("failed to load Windows data")
}
if files[0].Platform.Windows.OwnerName == "" {
t.Errorf("expected owner name to be set")
}
}
func walkDir(fs fs.Filesystem, dir string, cfiler CurrentFiler, matcher *ignore.Matcher, localFlags uint32) []protocol.FileInfo {
cfg, cancel := testConfig()
defer cancel()