1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Simplify addTarFile

We can use tar.FileInfoHeader to do much of the work.

Docker-DCO-1.1-Signed-off-by: Alexander Larsson <alexl@redhat.com> (github: alexlarsson)
This commit is contained in:
Alexander Larsson 2014-01-20 11:48:47 +01:00
parent 5b77e51e0a
commit bab8efbf05

View file

@ -15,7 +15,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"syscall" "syscall"
"time"
) )
type Archive io.Reader type Archive io.Reader
@ -125,48 +124,33 @@ func (compression *Compression) Extension() string {
} }
func addTarFile(path, name string, tw *tar.Writer) error { func addTarFile(path, name string, tw *tar.Writer) error {
var stat syscall.Stat_t fi, err := os.Lstat(path)
if err := syscall.Lstat(path, &stat); err != nil { if err != nil {
return err return err
} }
mtim := getLastModification(&stat) link := ""
atim := getLastAccess(&stat) if fi.Mode() & os.ModeSymlink != 0 {
hdr := &tar.Header{ if link, err = os.Readlink(path); err != nil {
Name: name, return err
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)),
} }
if stat.Mode&syscall.S_IFDIR == syscall.S_IFDIR { hdr, err := tar.FileInfoHeader(fi, link)
hdr.Typeflag = tar.TypeDir if err != nil {
} else if stat.Mode&syscall.S_IFLNK == syscall.S_IFLNK {
hdr.Typeflag = tar.TypeSymlink
if link, err := os.Readlink(path); err != nil {
return err return err
} else {
hdr.Linkname = link
} }
} else if stat.Mode&syscall.S_IFBLK == syscall.S_IFBLK ||
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 { 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.Devmajor = int64(major(uint64(stat.Rdev)))
hdr.Devminor = int64(minor(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 { if err := tw.WriteHeader(hdr); err != nil {