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.
This commit is contained in:
Simone Gotti
2015-02-03 21:49:12 +01:00
parent 6a34a4a6a2
commit 3b8276065c
2 changed files with 10 additions and 0 deletions
+7
View File
@@ -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
}
+3
View File
@@ -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
}