2013-11-01 09:06:35 +04:00
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
2013-12-13 18:43:50 +04:00
|
|
|
"archive/tar"
|
|
|
|
"github.com/dotcloud/docker/utils"
|
|
|
|
"io"
|
2013-11-01 09:06:35 +04:00
|
|
|
"os"
|
2013-11-08 03:58:03 +04:00
|
|
|
"path/filepath"
|
2013-11-01 09:06:35 +04:00
|
|
|
"strings"
|
2013-11-19 12:35:03 +04:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2013-11-01 09:06:35 +04:00
|
|
|
)
|
|
|
|
|
2013-12-13 18:43:50 +04:00
|
|
|
// Linux device nodes are a bit weird due to backwards compat with 16 bit device nodes
|
|
|
|
// The lower 8 bit is the lower 8 bit in the minor, the following 12 bits are the major,
|
|
|
|
// and then there is the top 12 bits of then minor
|
|
|
|
func mkdev(major int64, minor int64) uint32 {
|
|
|
|
return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
|
|
|
|
}
|
|
|
|
func timeToTimespec(time time.Time) (ts syscall.Timespec) {
|
|
|
|
if time.IsZero() {
|
|
|
|
// Return UTIME_OMIT special value
|
|
|
|
ts.Sec = 0
|
|
|
|
ts.Nsec = ((1 << 30) - 2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return syscall.NsecToTimespec(time.UnixNano())
|
|
|
|
}
|
|
|
|
|
2013-11-01 09:06:35 +04:00
|
|
|
// ApplyLayer parses a diff in the standard layer format from `layer`, and
|
|
|
|
// applies it to the directory `dest`.
|
|
|
|
func ApplyLayer(dest string, layer Archive) error {
|
2013-12-13 18:43:50 +04:00
|
|
|
// We need to be able to set any perms
|
|
|
|
oldmask := syscall.Umask(0)
|
|
|
|
defer syscall.Umask(oldmask)
|
2013-11-01 09:06:35 +04:00
|
|
|
|
2013-12-13 18:43:50 +04:00
|
|
|
tr := tar.NewReader(layer)
|
2013-11-01 09:06:35 +04:00
|
|
|
|
2013-12-13 18:43:50 +04:00
|
|
|
var dirs []*tar.Header
|
2013-11-19 12:35:03 +04:00
|
|
|
|
2013-12-13 18:43:50 +04:00
|
|
|
// Iterate through the files in the archive.
|
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// end of tar archive
|
|
|
|
break
|
|
|
|
}
|
2013-11-01 09:06:35 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-16 17:35:43 +04:00
|
|
|
// Normalize name, for safety and for a simple is-root check
|
|
|
|
hdr.Name = filepath.Clean(hdr.Name)
|
|
|
|
|
|
|
|
if !strings.HasSuffix(hdr.Name, "/") {
|
|
|
|
// Not the root directory, ensure that the parent directory exists
|
|
|
|
// This happened in some tests where an image had a tarfile without any
|
|
|
|
// parent directories
|
|
|
|
parent := filepath.Dir(hdr.Name)
|
|
|
|
parentPath := filepath.Join(dest, parent)
|
|
|
|
if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(parentPath, 600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 18:43:50 +04:00
|
|
|
// Skip AUFS metadata dirs
|
|
|
|
if strings.HasPrefix(hdr.Name, ".wh..wh.") {
|
|
|
|
continue
|
2013-11-01 09:06:35 +04:00
|
|
|
}
|
|
|
|
|
2013-12-13 18:43:50 +04:00
|
|
|
path := filepath.Join(dest, hdr.Name)
|
|
|
|
base := filepath.Base(path)
|
|
|
|
if strings.HasPrefix(base, ".wh.") {
|
|
|
|
originalBase := base[len(".wh."):]
|
|
|
|
originalPath := filepath.Join(filepath.Dir(path), originalBase)
|
|
|
|
if err := os.RemoveAll(originalPath); err != nil {
|
2013-11-19 12:35:03 +04:00
|
|
|
return err
|
|
|
|
}
|
2013-12-13 18:43:50 +04:00
|
|
|
} else {
|
|
|
|
// If path exits we almost always just want to remove and replace it
|
|
|
|
// The only exception is when it is a directory *and* the file from
|
|
|
|
// the layer is also a directory. Then we want to merge them (i.e.
|
|
|
|
// just apply the metadata from the layer).
|
|
|
|
hasDir := false
|
|
|
|
if fi, err := os.Lstat(path); err == nil {
|
|
|
|
if fi.IsDir() && hdr.Typeflag == tar.TypeDir {
|
|
|
|
hasDir = true
|
|
|
|
} else {
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-01 09:06:35 +04:00
|
|
|
|
2013-12-13 18:43:50 +04:00
|
|
|
switch hdr.Typeflag {
|
|
|
|
case tar.TypeDir:
|
|
|
|
if !hasDir {
|
|
|
|
err = os.Mkdir(path, os.FileMode(hdr.Mode))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dirs = append(dirs, hdr)
|
2013-11-19 12:35:03 +04:00
|
|
|
|
2013-12-13 18:43:50 +04:00
|
|
|
case tar.TypeReg, tar.TypeRegA:
|
|
|
|
// Source is regular file
|
|
|
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.FileMode(hdr.Mode))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(file, tr); err != nil {
|
|
|
|
file.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
case tar.TypeBlock, tar.TypeChar, tar.TypeFifo:
|
|
|
|
mode := uint32(hdr.Mode & 07777)
|
|
|
|
switch hdr.Typeflag {
|
|
|
|
case tar.TypeBlock:
|
|
|
|
mode |= syscall.S_IFBLK
|
|
|
|
case tar.TypeChar:
|
|
|
|
mode |= syscall.S_IFCHR
|
|
|
|
case tar.TypeFifo:
|
|
|
|
mode |= syscall.S_IFIFO
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.Mknod(path, mode, int(mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeLink:
|
|
|
|
if err := os.Link(filepath.Join(dest, hdr.Linkname), path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeSymlink:
|
|
|
|
if err := os.Symlink(hdr.Linkname, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
utils.Debugf("unhandled type %d\n", hdr.Typeflag)
|
2013-11-19 12:35:03 +04:00
|
|
|
}
|
2013-12-13 18:43:50 +04:00
|
|
|
|
|
|
|
if err = syscall.Lchown(path, hdr.Uid, hdr.Gid); err != nil {
|
2013-11-19 12:35:03 +04:00
|
|
|
return err
|
|
|
|
}
|
2013-12-13 18:43:50 +04:00
|
|
|
|
|
|
|
// There is no LChmod, so ignore mode for symlink. Also, this
|
|
|
|
// must happen after chown, as that can modify the file mode
|
|
|
|
if hdr.Typeflag != tar.TypeSymlink {
|
|
|
|
err = syscall.Chmod(path, uint32(hdr.Mode&07777))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directories must be handled at the end to avoid further
|
|
|
|
// file creation in them to modify the mtime
|
|
|
|
if hdr.Typeflag != tar.TypeDir {
|
|
|
|
ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
|
|
|
|
// syscall.UtimesNano doesn't support a NOFOLLOW flag atm, and
|
|
|
|
if hdr.Typeflag != tar.TypeSymlink {
|
|
|
|
if err := syscall.UtimesNano(path, ts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := LUtimesNano(path, ts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-01 09:06:35 +04:00
|
|
|
}
|
|
|
|
}
|
2013-11-19 12:35:03 +04:00
|
|
|
|
2013-12-13 18:43:50 +04:00
|
|
|
for _, hdr := range dirs {
|
|
|
|
path := filepath.Join(dest, hdr.Name)
|
|
|
|
ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
|
|
|
|
if err := syscall.UtimesNano(path, ts); err != nil {
|
2013-11-19 12:35:03 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-01 09:06:35 +04:00
|
|
|
return nil
|
|
|
|
}
|