2018-02-05 16:05:59 -05:00
|
|
|
package archive // import "github.com/docker/docker/pkg/archive"
|
2016-04-06 18:07:29 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/system"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2016-04-06 18:07:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func getWhiteoutConverter(format WhiteoutFormat) tarWhiteoutConverter {
|
|
|
|
if format == OverlayWhiteoutFormat {
|
|
|
|
return overlayWhiteoutConverter{}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type overlayWhiteoutConverter struct{}
|
|
|
|
|
2016-10-24 20:03:56 -04:00
|
|
|
func (overlayWhiteoutConverter) ConvertWrite(hdr *tar.Header, path string, fi os.FileInfo) (wo *tar.Header, err error) {
|
2016-04-06 18:07:29 -04:00
|
|
|
// convert whiteouts to AUFS format
|
|
|
|
if fi.Mode()&os.ModeCharDevice != 0 && hdr.Devmajor == 0 && hdr.Devminor == 0 {
|
|
|
|
// we just rename the file and make it normal
|
2016-07-26 14:57:09 -04:00
|
|
|
dir, filename := filepath.Split(hdr.Name)
|
|
|
|
hdr.Name = filepath.Join(dir, WhiteoutPrefix+filename)
|
2016-04-06 18:07:29 -04:00
|
|
|
hdr.Mode = 0600
|
|
|
|
hdr.Typeflag = tar.TypeReg
|
2016-06-23 16:34:38 -04:00
|
|
|
hdr.Size = 0
|
2016-04-06 18:07:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if fi.Mode()&os.ModeDir != 0 {
|
|
|
|
// convert opaque dirs to AUFS format by writing an empty file with the prefix
|
|
|
|
opaque, err := system.Lgetxattr(path, "trusted.overlay.opaque")
|
|
|
|
if err != nil {
|
2016-10-24 20:03:56 -04:00
|
|
|
return nil, err
|
2016-04-06 18:07:29 -04:00
|
|
|
}
|
2016-10-13 16:55:38 -04:00
|
|
|
if len(opaque) == 1 && opaque[0] == 'y' {
|
2016-10-24 20:03:56 -04:00
|
|
|
if hdr.Xattrs != nil {
|
|
|
|
delete(hdr.Xattrs, "trusted.overlay.opaque")
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:07:29 -04:00
|
|
|
// create a header for the whiteout file
|
|
|
|
// it should inherit some properties from the parent, but be a regular file
|
2016-10-24 20:03:56 -04:00
|
|
|
wo = &tar.Header{
|
2016-04-06 18:07:29 -04:00
|
|
|
Typeflag: tar.TypeReg,
|
|
|
|
Mode: hdr.Mode & int64(os.ModePerm),
|
|
|
|
Name: filepath.Join(hdr.Name, WhiteoutOpaqueDir),
|
|
|
|
Size: 0,
|
|
|
|
Uid: hdr.Uid,
|
|
|
|
Uname: hdr.Uname,
|
|
|
|
Gid: hdr.Gid,
|
|
|
|
Gname: hdr.Gname,
|
|
|
|
AccessTime: hdr.AccessTime,
|
|
|
|
ChangeTime: hdr.ChangeTime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 20:03:56 -04:00
|
|
|
return
|
2016-04-06 18:07:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (overlayWhiteoutConverter) ConvertRead(hdr *tar.Header, path string) (bool, error) {
|
|
|
|
base := filepath.Base(path)
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
|
|
|
|
// if a directory is marked as opaque by the AUFS special file, we need to translate that to overlay
|
|
|
|
if base == WhiteoutOpaqueDir {
|
2017-05-23 10:22:32 -04:00
|
|
|
err := unix.Setxattr(dir, "trusted.overlay.opaque", []byte{'y'}, 0)
|
2016-04-06 18:07:29 -04:00
|
|
|
// don't write the file itself
|
2016-12-13 15:10:11 -05:00
|
|
|
return false, err
|
2016-04-06 18:07:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// if a file was deleted and we are using overlay, we need to create a character device
|
|
|
|
if strings.HasPrefix(base, WhiteoutPrefix) {
|
|
|
|
originalBase := base[len(WhiteoutPrefix):]
|
|
|
|
originalPath := filepath.Join(dir, originalBase)
|
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.Mknod(originalPath, unix.S_IFCHR, 0); err != nil {
|
2016-04-06 18:07:29 -04:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if err := os.Chown(originalPath, hdr.Uid, hdr.Gid); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't write the file itself
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|