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"
"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 {
hdr, err := tar.FileInfoHeader(fi, link)
if err != nil {
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 {
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 {