mirror of
https://github.com/restic/restic.git
synced 2026-11-21 13:02:42 +00:00
fs: fall back to numeric ids when --ownership-by-name lookup fails (#22185)
Signed-off-by: Dean Chen <862469039@qq.com>
This commit is contained in:
1 parent
2126756613
commit
5cda063cbb
5 files changed
+89
-27
No files matched your search
+24
-17
@@ -130,21 +130,27 @@ func lookupUsername(uid uint32) string {
|
||||
return username
|
||||
}
|
||||
|
||||
type idLookupResult struct {
|
||||
id uint32
|
||||
found bool
|
||||
}
|
||||
|
||||
var (
|
||||
userNameLookupCache = make(map[string]uint32)
|
||||
userNameLookupCache = make(map[string]idLookupResult)
|
||||
userNameLookupCacheMutex = sync.RWMutex{}
|
||||
)
|
||||
|
||||
// Cached uid lookup by user name. Returns 0 when no id can be found.
|
||||
// Cached uid lookup by user name. The second return value is false when no
|
||||
// id can be found for the name.
|
||||
//
|
||||
//nolint:revive // capitalization is correct as is
|
||||
func lookupUid(userName string) uint32 {
|
||||
func lookupUid(userName string) (uint32, bool) {
|
||||
userNameLookupCacheMutex.RLock()
|
||||
uid, ok := userNameLookupCache[userName]
|
||||
res, ok := userNameLookupCache[userName]
|
||||
userNameLookupCacheMutex.RUnlock()
|
||||
|
||||
if ok {
|
||||
return uid
|
||||
return res.id, res.found
|
||||
}
|
||||
|
||||
u, err := user.Lookup(userName)
|
||||
@@ -152,15 +158,15 @@ func lookupUid(userName string) uint32 {
|
||||
var s int
|
||||
s, err = strconv.Atoi(u.Uid)
|
||||
if err == nil {
|
||||
uid = uint32(s)
|
||||
res = idLookupResult{id: uint32(s), found: true}
|
||||
}
|
||||
}
|
||||
|
||||
userNameLookupCacheMutex.Lock()
|
||||
userNameLookupCache[userName] = uid
|
||||
userNameLookupCache[userName] = res
|
||||
userNameLookupCacheMutex.Unlock()
|
||||
|
||||
return uid
|
||||
return res.id, res.found
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -191,18 +197,19 @@ func lookupGroup(gid uint32) string {
|
||||
}
|
||||
|
||||
var (
|
||||
groupNameLookupCache = make(map[string]uint32)
|
||||
groupNameLookupCache = make(map[string]idLookupResult)
|
||||
groupNameLookupCacheMutex = sync.RWMutex{}
|
||||
)
|
||||
|
||||
// Cached uid lookup by group name. Returns 0 when no id can be found.
|
||||
func lookupGid(groupName string) uint32 {
|
||||
// Cached gid lookup by group name. The second return value is false when no
|
||||
// id can be found for the name.
|
||||
func lookupGid(groupName string) (uint32, bool) {
|
||||
groupNameLookupCacheMutex.RLock()
|
||||
gid, ok := groupNameLookupCache[groupName]
|
||||
res, ok := groupNameLookupCache[groupName]
|
||||
groupNameLookupCacheMutex.RUnlock()
|
||||
|
||||
if ok {
|
||||
return gid
|
||||
return res.id, res.found
|
||||
}
|
||||
|
||||
g, err := user.LookupGroup(groupName)
|
||||
@@ -210,15 +217,15 @@ func lookupGid(groupName string) uint32 {
|
||||
var s int
|
||||
s, err = strconv.Atoi(g.Gid)
|
||||
if err == nil {
|
||||
gid = uint32(s)
|
||||
res = idLookupResult{id: uint32(s), found: true}
|
||||
}
|
||||
}
|
||||
|
||||
groupNameLookupCacheMutex.Lock()
|
||||
groupNameLookupCache[groupName] = gid
|
||||
groupNameLookupCache[groupName] = res
|
||||
groupNameLookupCacheMutex.Unlock()
|
||||
|
||||
return gid
|
||||
return res.id, res.found
|
||||
}
|
||||
|
||||
// NodeCreateAt creates the node at the given path but does NOT restore node meta data.
|
||||
@@ -313,7 +320,7 @@ func NodeRestoreMetadata(node *data.Node, path string, warn func(msg string), xa
|
||||
func nodeRestoreMetadata(node *data.Node, path string, warn func(msg string), xattrSelectFilter func(xattrName string) bool, ownershipByName bool) error {
|
||||
var firsterr error
|
||||
|
||||
if err := lchown(path, node, ownershipByName); err != nil {
|
||||
if err := lchown(path, node, ownershipByName, warn); err != nil {
|
||||
firsterr = errors.WithStack(err)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,19 +3,32 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/restic/restic/internal/data"
|
||||
)
|
||||
|
||||
func lchown(name string, node *data.Node, lookupByName bool) error {
|
||||
var uid, gid uint32
|
||||
func lchown(name string, node *data.Node, lookupByName bool, warn func(msg string)) error {
|
||||
uid := node.UID
|
||||
gid := node.GID
|
||||
if lookupByName {
|
||||
uid = lookupUid(node.User)
|
||||
gid = lookupGid(node.Group)
|
||||
} else {
|
||||
uid = node.UID
|
||||
gid = node.GID
|
||||
// Fall back to the numeric ids stored in the snapshot if a name is
|
||||
// missing or cannot be resolved on this host.
|
||||
if node.User != "" {
|
||||
if id, ok := lookupUid(node.User); ok {
|
||||
uid = id
|
||||
} else if warn != nil {
|
||||
warn(fmt.Sprintf("cannot resolve user %q for %v, using uid %d", node.User, name, uid))
|
||||
}
|
||||
}
|
||||
if node.Group != "" {
|
||||
if id, ok := lookupGid(node.Group); ok {
|
||||
gid = id
|
||||
} else if warn != nil {
|
||||
warn(fmt.Sprintf("cannot resolve group %q for %v, using gid %d", node.Group, name, gid))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return os.Lchown(name, int(uid), int(gid))
|
||||
|
||||
@@ -167,7 +167,7 @@ func TestLchown(t *testing.T) {
|
||||
GID: uint32(gid),
|
||||
}
|
||||
|
||||
err = lchown(f, n, false)
|
||||
err = lchown(f, n, false, nil)
|
||||
rtest.OK(t, err)
|
||||
})
|
||||
|
||||
@@ -180,7 +180,37 @@ func TestLchown(t *testing.T) {
|
||||
Group: group.Name,
|
||||
}
|
||||
|
||||
err = lchown(f, n, true)
|
||||
err = lchown(f, n, true, nil)
|
||||
rtest.OK(t, err)
|
||||
})
|
||||
|
||||
t.Run("unresolvable names fall back to UID/GID", func(t *testing.T) {
|
||||
n := &data.Node{
|
||||
UID: uint32(uid),
|
||||
GID: uint32(gid),
|
||||
User: "restic-no-such-user",
|
||||
Group: "restic-no-such-group",
|
||||
}
|
||||
|
||||
var warnings []string
|
||||
err = lchown(f, n, true, func(msg string) { warnings = append(warnings, msg) })
|
||||
rtest.OK(t, err)
|
||||
rtest.Equals(t, 2, len(warnings))
|
||||
|
||||
fi, err := os.Lstat(f)
|
||||
rtest.OK(t, err)
|
||||
st := fi.Sys().(*syscall.Stat_t)
|
||||
rtest.Equals(t, uint32(uid), st.Uid)
|
||||
rtest.Equals(t, uint32(gid), st.Gid)
|
||||
})
|
||||
|
||||
t.Run("empty names fall back to UID/GID", func(t *testing.T) {
|
||||
n := &data.Node{
|
||||
UID: uint32(uid),
|
||||
GID: uint32(gid),
|
||||
}
|
||||
|
||||
err = lchown(f, n, true, func(msg string) { t.Errorf("unexpected warning: %v", msg) })
|
||||
rtest.OK(t, err)
|
||||
})
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func mknod(_ string, _ uint32, _ uint64) (err error) {
|
||||
}
|
||||
|
||||
// Windows doesn't need lchown
|
||||
func lchown(_ string, _ *data.Node, _ bool) (err error) {
|
||||
func lchown(_ string, _ *data.Node, _ bool, _ func(msg string)) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user