2013-11-01 09:06:35 +04:00
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
2014-02-14 02:11:39 +04:00
|
|
|
"code.google.com/p/go/src/pkg/archive/tar"
|
2013-12-13 18:43:50 +04:00
|
|
|
"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-25 04:37:00 +04:00
|
|
|
// Linux device nodes are a bit weird due to backwards compat with 16 bit device nodes.
|
|
|
|
// They are, from low to high: the lower 8 bits of the minor, then 12 bits of the major,
|
|
|
|
// then the top 12 bits of the minor
|
2013-12-13 18:43:50 +04:00
|
|
|
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`.
|
2014-02-14 15:41:46 +04:00
|
|
|
func ApplyLayer(dest string, layer ArchiveReader) 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-17 17:19:48 +04:00
|
|
|
layer, err := DecompressStream(layer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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, "/") {
|
2013-12-25 04:37:00 +04:00
|
|
|
// Not the root directory, ensure that the parent directory exists.
|
2013-12-16 17:35:43 +04:00
|
|
|
// This happened in some tests where an image had a tarfile without any
|
2013-12-25 04:37:00 +04:00
|
|
|
// parent directories.
|
2013-12-16 17:35:43 +04:00
|
|
|
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 {
|
2013-12-25 04:37:00 +04:00
|
|
|
// If path exits we almost always just want to remove and replace it.
|
2013-12-13 18:43:50 +04:00
|
|
|
// 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).
|
|
|
|
if fi, err := os.Lstat(path); err == nil {
|
2013-12-20 14:30:45 +04:00
|
|
|
if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
|
2013-12-13 18:43:50 +04:00
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-01 09:06:35 +04:00
|
|
|
|
2013-12-20 14:30:45 +04:00
|
|
|
if err := createTarFile(path, dest, hdr, tr); err != nil {
|
2013-11-19 12:35:03 +04:00
|
|
|
return err
|
|
|
|
}
|
2013-12-13 18:43:50 +04:00
|
|
|
|
2013-12-20 14:30:45 +04:00
|
|
|
// Directory mtimes must be handled at the end to avoid further
|
|
|
|
// file creation in them to modify the directory mtime
|
|
|
|
if hdr.Typeflag == tar.TypeDir {
|
|
|
|
dirs = append(dirs, hdr)
|
2013-12-13 18:43:50 +04:00
|
|
|
}
|
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
|
|
|
|
}
|