From bab8efbf050e1bc2d5c2ff64c6161ef4d323bc3f Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Mon, 20 Jan 2014 11:48:47 +0100 Subject: [PATCH] Simplify addTarFile We can use tar.FileInfoHeader to do much of the work. Docker-DCO-1.1-Signed-off-by: Alexander Larsson (github: alexlarsson) --- archive/archive.go | 58 +++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/archive/archive.go b/archive/archive.go index cae5833039..ee8c419781 100644 --- a/archive/archive.go +++ b/archive/archive.go @@ -15,7 +15,6 @@ import ( "path/filepath" "strings" "syscall" - "time" ) type Archive io.Reader @@ -125,48 +124,33 @@ func (compression *Compression) Extension() string { } func addTarFile(path, name string, tw *tar.Writer) error { - var stat syscall.Stat_t - if err := syscall.Lstat(path, &stat); err != nil { + fi, err := os.Lstat(path) + if err != nil { return err } - mtim := getLastModification(&stat) - atim := getLastAccess(&stat) - hdr := &tar.Header{ - Name: name, - Mode: int64(stat.Mode & 07777), - Uid: int(stat.Uid), - Gid: int(stat.Gid), - ModTime: time.Unix(int64(mtim.Sec), int64(mtim.Nsec)), - AccessTime: time.Unix(int64(atim.Sec), int64(atim.Nsec)), + link := "" + if fi.Mode() & os.ModeSymlink != 0 { + if link, err = os.Readlink(path); err != nil { + return err + } } - if stat.Mode&syscall.S_IFDIR == syscall.S_IFDIR { - hdr.Typeflag = tar.TypeDir - } else if stat.Mode&syscall.S_IFLNK == syscall.S_IFLNK { - hdr.Typeflag = tar.TypeSymlink - if link, err := os.Readlink(path); err != nil { - return err - } else { - hdr.Linkname = link + hdr, err := tar.FileInfoHeader(fi, link) + if err != nil { + return err + } + + hdr.Name = name + + stat, ok := fi.Sys().(*syscall.Stat_t) + if ok { + // Currently go does not fill in the major/minors + if stat.Mode&syscall.S_IFBLK == syscall.S_IFBLK || + stat.Mode&syscall.S_IFCHR == syscall.S_IFCHR { + hdr.Devmajor = int64(major(uint64(stat.Rdev))) + hdr.Devminor = int64(minor(uint64(stat.Rdev))) } - } else if stat.Mode&syscall.S_IFBLK == syscall.S_IFBLK || - stat.Mode&syscall.S_IFCHR == syscall.S_IFCHR { - if stat.Mode&syscall.S_IFBLK == syscall.S_IFBLK { - hdr.Typeflag = tar.TypeBlock - } else { - hdr.Typeflag = tar.TypeChar - } - hdr.Devmajor = int64(major(uint64(stat.Rdev))) - hdr.Devminor = int64(minor(uint64(stat.Rdev))) - } else if stat.Mode&syscall.S_IFIFO == syscall.S_IFIFO || - stat.Mode&syscall.S_IFSOCK == syscall.S_IFSOCK { - hdr.Typeflag = tar.TypeFifo - } else if stat.Mode&syscall.S_IFREG == syscall.S_IFREG { - hdr.Typeflag = tar.TypeReg - hdr.Size = stat.Size - } else { - return fmt.Errorf("Unknown file type: %s\n", path) } if err := tw.WriteHeader(hdr); err != nil {