From e42d2d1da81cd766478ee29d3fc07e80ef7cadac Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 9 Sep 2018 11:43:18 +0200 Subject: [PATCH] restore: suppress lchown errors when not running as root Like "cp -a" and "rsync -a" do, only report lchown errors if we run as root. Like cp from GNU coreutils does, we check Geteuid() to determine if we are running as root ( http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/copy.c#n3012 ). On Windows, lchown errors are always reported. Fixes https://github.com/restic/restic/issues/1766 --- changelog/unreleased/issue-1766 | 7 +++++++ internal/restic/node.go | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/issue-1766 diff --git a/changelog/unreleased/issue-1766 b/changelog/unreleased/issue-1766 new file mode 100644 index 000000000..6d8e312dd --- /dev/null +++ b/changelog/unreleased/issue-1766 @@ -0,0 +1,7 @@ +Enhancement: restore: suppress lchown errors when not running as root + +Like "cp" and "rsync" do, restic now only reports errors for changing +the ownership of files during restore if it is run as root, on non-Windows +operating systems. On Windows, the error is reported as usual. + +https://github.com/restic/restic/issues/1766 diff --git a/internal/restic/node.go b/internal/restic/node.go index cec7938e5..c8b089791 100644 --- a/internal/restic/node.go +++ b/internal/restic/node.go @@ -186,7 +186,16 @@ func (node Node) restoreMetadata(path string) error { var firsterr error if err := lchown(path, int(node.UID), int(node.GID)); err != nil { - firsterr = errors.Wrap(err, "Lchown") + // Like "cp -a" and "rsync -a" do, we only report lchown permission errors + // if we run as root. + // On Windows, Geteuid always returns -1, and we always report lchown + // permission errors. + if os.Geteuid() > 0 && os.IsPermission(err) { + debug.Log("not running as root, ignoring lchown permission error for %v: %v", + path, err) + } else { + firsterr = errors.Wrap(err, "Lchown") + } } if node.Type != "symlink" {