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
+49 -13
View File
@@ -13,6 +13,7 @@ import (
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"time"
@@ -627,8 +628,8 @@ func (f *sendReceiveFolder) handleDir(file protocol.FileInfo, snap *db.Snapshot,
return err
}
// Copy the parent owner and group, if we are supposed to do that.
if err := f.maybeCopyOwner(path); err != nil {
// Adjust the ownership, if we are supposed to do that.
if err := f.maybeAdjustOwnership(&file, path); err != nil {
return err
}
@@ -754,7 +755,7 @@ func (f *sendReceiveFolder) handleSymlink(file protocol.FileInfo, snap *db.Snaps
if err := f.mtimefs.CreateSymlink(file.SymlinkTarget, path); err != nil {
return err
}
return f.maybeCopyOwner(path)
return f.maybeAdjustOwnership(&file, path)
}
if err = f.inWritableDir(createLink, file.Name); err == nil {
@@ -989,7 +990,13 @@ func (f *sendReceiveFolder) renameFile(cur, source, target protocol.FileInfo, sn
default:
var fi protocol.FileInfo
if fi, err = scanner.CreateFileInfo(stat, target.Name, f.mtimefs); err == nil {
if !fi.IsEquivalentOptional(curTarget, f.modTimeWindow, f.IgnorePerms, true, protocol.LocalAllFlags) {
if !fi.IsEquivalentOptional(curTarget, protocol.FileInfoComparison{
ModTimeWindow: f.modTimeWindow,
IgnorePerms: f.IgnorePerms,
IgnoreBlocks: true,
IgnoreFlags: protocol.LocalAllFlags,
IgnoreOwnership: !f.SyncOwnership,
}) {
// Target changed
scanChan <- target.Name
err = errModified
@@ -1227,6 +1234,11 @@ func (f *sendReceiveFolder) shortcutFile(file protocol.FileInfo, dbUpdateChan ch
}
}
if err := f.maybeAdjustOwnership(&file, file.Name); err != nil {
f.newPullError(file.Name, err)
return
}
// Still need to re-write the trailer with the new encrypted fileinfo.
if f.Type == config.FolderTypeReceiveEncrypted {
err = inWritableDir(func(path string) error {
@@ -1592,8 +1604,8 @@ func (f *sendReceiveFolder) performFinish(file, curFile protocol.FileInfo, hasCu
}
}
// Copy the parent owner and group, if we are supposed to do that.
if err := f.maybeCopyOwner(tempName); err != nil {
// Set ownership based on file metadata or parent, maybe.
if err := f.maybeAdjustOwnership(&file, tempName); err != nil {
return err
}
@@ -1972,7 +1984,13 @@ func (f *sendReceiveFolder) deleteDirOnDiskHandleChildren(dir string, snap *db.S
hasToBeScanned = true
return nil
}
if !cf.IsEquivalentOptional(diskFile, f.modTimeWindow, f.IgnorePerms, true, protocol.LocalAllFlags) {
if !cf.IsEquivalentOptional(diskFile, protocol.FileInfoComparison{
ModTimeWindow: f.modTimeWindow,
IgnorePerms: f.IgnorePerms,
IgnoreBlocks: true,
IgnoreFlags: protocol.LocalAllFlags,
IgnoreOwnership: !f.SyncOwnership,
}) {
// File on disk changed compared to what we have in db
// -> schedule scan.
scanChan <- path
@@ -2041,7 +2059,13 @@ func (f *sendReceiveFolder) scanIfItemChanged(name string, stat fs.FileInfo, ite
return errors.Wrap(err, "comparing item on disk to db")
}
if !statItem.IsEquivalentOptional(item, f.modTimeWindow, f.IgnorePerms, true, protocol.LocalAllFlags) {
if !statItem.IsEquivalentOptional(item, protocol.FileInfoComparison{
ModTimeWindow: f.modTimeWindow,
IgnorePerms: f.IgnorePerms,
IgnoreBlocks: true,
IgnoreFlags: protocol.LocalAllFlags,
IgnoreOwnership: !f.SyncOwnership,
}) {
return errModified
}
@@ -2074,11 +2098,23 @@ func (f *sendReceiveFolder) checkToBeDeleted(file, cur protocol.FileInfo, hasCur
return f.scanIfItemChanged(file.Name, stat, cur, hasCur, scanChan)
}
func (f *sendReceiveFolder) maybeCopyOwner(path string) error {
if !f.CopyOwnershipFromParent {
// Not supposed to do anything.
return nil
func (f *sendReceiveFolder) maybeAdjustOwnership(file *protocol.FileInfo, name string) error {
if f.SyncOwnership {
// Set ownership based on file metadata.
if err := f.syncOwnership(file, name); err != nil {
return err
}
} else if f.CopyOwnershipFromParent {
// Copy the parent owner and group.
if err := f.copyOwnershipFromParent(name); err != nil {
return err
}
}
// Nothing to do
return nil
}
func (f *sendReceiveFolder) copyOwnershipFromParent(path string) error {
if runtime.GOOS == "windows" {
// Can't do anything.
return nil
@@ -2088,7 +2124,7 @@ func (f *sendReceiveFolder) maybeCopyOwner(path string) error {
if err != nil {
return errors.Wrap(err, "copy owner from parent")
}
if err := f.mtimefs.Lchown(path, info.Owner(), info.Group()); err != nil {
if err := f.mtimefs.Lchown(path, strconv.Itoa(info.Owner()), strconv.Itoa(info.Group())); err != nil {
return errors.Wrap(err, "copy owner from parent")
}
return nil