From 3b8276065c0e451a65980f9f5ad83dfe4efc212c Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Tue, 3 Feb 2015 16:53:00 +0100 Subject: [PATCH] tar: restore entry's uid and gid. Restore the extracted entry uid and gid. This uses tar header Uid and Gid values and ignores header's Uname and Gname. As the tests are usually run as a unprivileged user the generated test tars' entries are defaulted to the running user uid and gid. For this reason tests are implictly verifing that the os.Chown/os.LChows calls works but it's difficult to create a more comprehensive test using different uid and gid values. --- pkg/tar/tar.go | 7 +++++++ pkg/tar/tar_test.go | 3 +++ 2 files changed, 10 insertions(+) diff --git a/pkg/tar/tar.go b/pkg/tar/tar.go index fadddac..bdc93d6 100644 --- a/pkg/tar/tar.go +++ b/pkg/tar/tar.go @@ -149,6 +149,13 @@ func ExtractFile(tr *tar.Reader, hdr *tar.Header, dir string, overwrite bool) er return fmt.Errorf("unsupported type: %v", typ) } + // TODO(sgotti). lchown(2) says that, depending on the linux kernel + // version, chown can change the file mode also if executed as root. + // To be sure, Should we call os.Chmod after it? + if err := os.Lchown(p, hdr.Uid, hdr.Gid); err != nil { + return err + } + return nil } diff --git a/pkg/tar/tar_test.go b/pkg/tar/tar_test.go index 7edcb35..0d2bfe7 100644 --- a/pkg/tar/tar_test.go +++ b/pkg/tar/tar_test.go @@ -45,6 +45,9 @@ func newTestTar(entries []*testTarEntry) (string, error) { entry.header.Mode = 0644 } } + // Add calling user uid and gid or tests will fail + entry.header.Uid = os.Getuid() + entry.header.Gid = os.Getgid() if err := tw.WriteHeader(entry.header); err != nil { return "", err }