1
0
Fork 0

archive/tar: move round-trip reading into common os file

Fixes #11426

Change-Id: I77368b0e852149ed4533e139cc43887508ac7f78
Reviewed-on: https://go-review.googlesource.com/11662
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Alex Brainman 2015-06-29 16:42:28 +10:00 committed by Vincent Batts
parent 27e18409b9
commit 3b34dbd368
3 changed files with 40 additions and 39 deletions

View File

@ -249,6 +249,30 @@ func FileInfoHeader(fi os.FileInfo, link string) (*Header, error) {
if fm&os.ModeSticky != 0 { if fm&os.ModeSticky != 0 {
h.Mode |= c_ISVTX h.Mode |= c_ISVTX
} }
// If possible, populate additional fields from OS-specific
// FileInfo fields.
if sys, ok := fi.Sys().(*Header); ok {
// This FileInfo came from a Header (not the OS). Use the
// original Header to populate all remaining fields.
h.Uid = sys.Uid
h.Gid = sys.Gid
h.Uname = sys.Uname
h.Gname = sys.Gname
h.AccessTime = sys.AccessTime
h.ChangeTime = sys.ChangeTime
if sys.Xattrs != nil {
h.Xattrs = make(map[string]string)
for k, v := range sys.Xattrs {
h.Xattrs[k] = v
}
}
if sys.Typeflag == TypeLink {
// hard link
h.Typeflag = TypeLink
h.Size = 0
h.Linkname = sys.Linkname
}
}
if sysStat != nil { if sysStat != nil {
return h, sysStat(fi, h) return h, sysStat(fi, h)
} }

View File

@ -16,41 +16,22 @@ func init() {
} }
func statUnix(fi os.FileInfo, h *Header) error { func statUnix(fi os.FileInfo, h *Header) error {
switch sys := fi.Sys().(type) { sys, ok := fi.Sys().(*syscall.Stat_t)
case *syscall.Stat_t: if !ok {
h.Uid = int(sys.Uid) return nil
h.Gid = int(sys.Gid) }
// TODO(bradfitz): populate username & group. os/user h.Uid = int(sys.Uid)
// doesn't cache LookupId lookups, and lacks group h.Gid = int(sys.Gid)
// lookup functions. // TODO(bradfitz): populate username & group. os/user
h.AccessTime = statAtime(sys) // doesn't cache LookupId lookups, and lacks group
h.ChangeTime = statCtime(sys) // lookup functions.
// TODO(bradfitz): major/minor device numbers? h.AccessTime = statAtime(sys)
if fi.Mode().IsRegular() && sys.Nlink > 1 { h.ChangeTime = statCtime(sys)
h.Typeflag = TypeLink // TODO(bradfitz): major/minor device numbers?
h.Size = 0 if fi.Mode().IsRegular() && sys.Nlink > 1 {
// TODO(vbatts): Linkname? h.Typeflag = TypeLink
} h.Size = 0
case *Header: // TODO(vbatts): Linkname?
// for the roundtrip logic
h.Uid = sys.Uid
h.Gid = sys.Gid
h.Uname = sys.Uname
h.Gname = sys.Gname
h.AccessTime = sys.AccessTime
h.ChangeTime = sys.ChangeTime
if sys.Xattrs != nil {
h.Xattrs = make(map[string]string)
for k, v := range sys.Xattrs {
h.Xattrs[k] = v
}
}
if sys.Typeflag == TypeLink {
// hard link
h.Typeflag = TypeLink
h.Size = 0
h.Linkname = sys.Linkname
}
} }
return nil return nil
} }

View File

@ -10,7 +10,6 @@ import (
"os" "os"
"path" "path"
"reflect" "reflect"
"runtime"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -136,9 +135,6 @@ type headerRoundTripTest struct {
} }
func TestHeaderRoundTrip(t *testing.T) { func TestHeaderRoundTrip(t *testing.T) {
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" || runtime.GOOS == "nacl" {
t.Skipf("skipping on %s; issue 11426", runtime.GOOS)
}
golden := []headerRoundTripTest{ golden := []headerRoundTripTest{
// regular file. // regular file.
{ {