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
+8 -1
View File
@@ -602,7 +602,13 @@ func (b *scanBatch) Update(fi protocol.FileInfo, snap *db.Snapshot) bool {
b.Remove(fi.Name)
return true
}
case gf.IsEquivalentOptional(fi, b.f.modTimeWindow, false, false, protocol.FlagLocalReceiveOnly):
case gf.IsEquivalentOptional(fi, protocol.FileInfoComparison{
ModTimeWindow: b.f.modTimeWindow,
IgnorePerms: b.f.IgnorePerms,
IgnoreBlocks: true,
IgnoreFlags: protocol.FlagLocalReceiveOnly,
IgnoreOwnership: !b.f.SyncOwnership,
}):
// What we have locally is equivalent to the global file.
l.Debugf("%v scanning: Merging identical locally changed item with global", b.f, fi)
fi = gf
@@ -632,6 +638,7 @@ func (f *folder) scanSubdirsChangedAndNew(subDirs []string, batch *scanBatch) (i
CurrentFiler: cFiler{snap},
Filesystem: f.mtimefs,
IgnorePerms: f.IgnorePerms,
IgnoreOwnership: !f.SyncOwnership,
AutoNormalize: f.AutoNormalize,
Hashers: f.model.numHashers(f.ID),
ShortID: f.shortID,
+5 -1
View File
@@ -126,7 +126,11 @@ func (f *receiveOnlyFolder) revert() error {
}
fi.SetDeleted(f.shortID)
fi.Version = protocol.Vector{} // if this file ever resurfaces anywhere we want our delete to be strictly older
case gf.IsEquivalentOptional(fi, f.modTimeWindow, false, false, protocol.FlagLocalReceiveOnly):
case gf.IsEquivalentOptional(fi, protocol.FileInfoComparison{
ModTimeWindow: f.modTimeWindow,
IgnoreFlags: protocol.FlagLocalReceiveOnly,
IgnoreOwnership: !f.SyncOwnership,
}):
// What we have locally is equivalent to the global file.
fi = gf
default:
+5 -1
View File
@@ -72,7 +72,11 @@ func (f *sendOnlyFolder) pull() (bool, error) {
return true
}
if !file.IsEquivalentOptional(curFile, f.modTimeWindow, f.IgnorePerms, false, 0) {
if !file.IsEquivalentOptional(curFile, protocol.FileInfoComparison{
ModTimeWindow: f.modTimeWindow,
IgnorePerms: f.IgnorePerms,
IgnoreOwnership: !f.SyncOwnership,
}) {
return true
}
+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
+2 -1
View File
@@ -16,6 +16,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
@@ -811,7 +812,7 @@ func TestCopyOwner(t *testing.T) {
// Create a parent dir with a certain owner/group.
f.mtimefs.Mkdir("foo", 0755)
f.mtimefs.Lchown("foo", expOwner, expGroup)
f.mtimefs.Lchown("foo", strconv.Itoa(expOwner), strconv.Itoa(expGroup))
dir := protocol.FileInfo{
Name: "foo/bar",
+45
View File
@@ -0,0 +1,45 @@
// Copyright (C) 2022 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
//go:build !windows
// +build !windows
package model
import (
"os/user"
"strconv"
"github.com/syncthing/syncthing/lib/protocol"
)
func (f *sendReceiveFolder) syncOwnership(file *protocol.FileInfo, path string) error {
if file.Platform.Unix == nil {
// No owner data, nothing to do
return nil
}
// Try to look up the user and group by name, defaulting to the
// numerical UID and GID if there is no match.
uid := strconv.Itoa(file.Platform.Unix.UID)
if file.Platform.Unix.OwnerName != "" {
us, err := user.Lookup(file.Platform.Unix.OwnerName)
if err == nil && us.Uid != "" {
uid = us.Uid
}
}
gid := strconv.Itoa(file.Platform.Unix.GID)
if file.Platform.Unix.GroupName != "" {
gr, err := user.LookupGroup(file.Platform.Unix.GroupName)
if err == nil && gr.Gid != "" {
gid = gr.Gid
}
}
return f.mtimefs.Lchown(path, uid, gid)
}
+82
View File
@@ -0,0 +1,82 @@
// Copyright (C) 2022 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package model
import (
"errors"
"os/user"
"strings"
"github.com/syncthing/syncthing/lib/protocol"
)
func (f *sendReceiveFolder) syncOwnership(file *protocol.FileInfo, path string) error {
if file.Platform.Windows == nil || file.Platform.Windows.OwnerName == "" {
// No owner data, nothing to do
return nil
}
l.Debugln("Owner name for %s is %s (group=%v)", path, file.Platform.Windows.OwnerName, file.Platform.Windows.OwnerIsGroup)
usid, gsid, err := lookupUserAndGroup(file.Platform.Windows.OwnerName, file.Platform.Windows.OwnerIsGroup)
if err != nil {
return err
}
l.Debugln("Owner for %s resolved to uid=%q gid=%q", path, usid, gsid)
return f.mtimefs.Lchown(path, usid, gsid)
}
func lookupUserAndGroup(name string, group bool) (string, string, error) {
// Look up either the the user or the group, returning the other kind as
// blank. This might seem an odd maneuver, but it matches what Chown
// wants as input and hides the ugly nested if:s down here.
if group {
gr, err := lookupWithoutDomain(name, func(name string) (string, error) {
gr, err := user.LookupGroup(name)
if err == nil {
return gr.Gid, nil
}
return "", err
})
if err != nil {
return "", "", err
}
return "", gr, nil
}
us, err := lookupWithoutDomain(name, func(name string) (string, error) {
us, err := user.Lookup(name)
if err == nil {
return us.Uid, nil
}
return "", err
})
if err != nil {
return "", "", err
}
return us, "", nil
}
func lookupWithoutDomain(name string, lookup func(s string) (string, error)) (string, error) {
// Try to look up the user by name. The username will be either a plain
// username or a qualified DOMAIN\user. We'll first try to look up
// whatever we got, if that fails, we'll try again with just the user
// part without domain.
v, err := lookup(name)
if err == nil {
return v, nil
}
parts := strings.Split(name, `\`)
if len(parts) == 2 {
if v, err := lookup(parts[1]); err == nil {
return v, nil
}
}
return "", errors.New("lookup failed")
}