2014-08-19 09:23:55 +00:00
|
|
|
// +build linux
|
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package copy // import "github.com/docker/docker/daemon/graphdriver/copy"
|
2014-08-19 09:23:55 +00:00
|
|
|
|
2017-08-31 02:07:02 +00:00
|
|
|
/*
|
|
|
|
#include <linux/fs.h>
|
|
|
|
|
|
|
|
#ifndef FICLONE
|
|
|
|
#define FICLONE _IOW(0x94, 9, int)
|
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
import "C"
|
2014-08-19 09:23:55 +00:00
|
|
|
import (
|
2017-11-24 00:31:31 +00:00
|
|
|
"container/list"
|
2014-08-19 09:23:55 +00:00
|
|
|
"fmt"
|
2017-08-31 02:07:02 +00:00
|
|
|
"io"
|
2014-08-19 09:23:55 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
2015-10-01 17:45:32 +00:00
|
|
|
"time"
|
2014-08-19 09:23:55 +00:00
|
|
|
|
2016-01-20 22:44:40 +00:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2014-08-19 09:23:55 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-02-13 00:05:50 +00:00
|
|
|
rsystem "github.com/opencontainers/runc/libcontainer/system"
|
2017-05-23 14:22:32 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2014-08-19 09:23:55 +00:00
|
|
|
)
|
|
|
|
|
2017-08-31 02:07:02 +00:00
|
|
|
// Mode indicates whether to use hardlink or copy content
|
2017-08-29 16:01:23 +00:00
|
|
|
type Mode int
|
2014-08-19 09:23:55 +00:00
|
|
|
|
|
|
|
const (
|
2017-08-29 16:01:23 +00:00
|
|
|
// Content creates a new file, and copies the content of the file
|
|
|
|
Content Mode = iota
|
|
|
|
// Hardlink creates a new hardlink to the existing file
|
|
|
|
Hardlink
|
2014-08-19 09:23:55 +00:00
|
|
|
)
|
|
|
|
|
2017-08-31 02:07:02 +00:00
|
|
|
func copyRegular(srcPath, dstPath string, fileinfo os.FileInfo, copyWithFileRange, copyWithFileClone *bool) error {
|
2014-08-19 09:23:55 +00:00
|
|
|
srcFile, err := os.Open(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
|
2017-08-31 02:07:02 +00:00
|
|
|
// If the destination file already exists, we shouldn't blow it away
|
|
|
|
dstFile, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, fileinfo.Mode())
|
2014-08-19 09:23:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstFile.Close()
|
|
|
|
|
2017-08-31 02:07:02 +00:00
|
|
|
if *copyWithFileClone {
|
|
|
|
_, _, err = unix.Syscall(unix.SYS_IOCTL, dstFile.Fd(), C.FICLONE, srcFile.Fd())
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
*copyWithFileClone = false
|
|
|
|
if err == unix.EXDEV {
|
|
|
|
*copyWithFileRange = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *copyWithFileRange {
|
|
|
|
err = doCopyWithFileRange(srcFile, dstFile, fileinfo)
|
|
|
|
// Trying the file_clone may not have caught the exdev case
|
|
|
|
// as the ioctl may not have been available (therefore EINVAL)
|
|
|
|
if err == unix.EXDEV || err == unix.ENOSYS {
|
|
|
|
*copyWithFileRange = false
|
2017-11-22 03:11:48 +00:00
|
|
|
} else {
|
2017-08-31 02:07:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return legacyCopy(srcFile, dstFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doCopyWithFileRange(srcFile, dstFile *os.File, fileinfo os.FileInfo) error {
|
|
|
|
amountLeftToCopy := fileinfo.Size()
|
|
|
|
|
|
|
|
for amountLeftToCopy > 0 {
|
|
|
|
n, err := unix.CopyFileRange(int(srcFile.Fd()), nil, int(dstFile.Fd()), nil, int(amountLeftToCopy), 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
amountLeftToCopy = amountLeftToCopy - int64(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func legacyCopy(srcFile io.Reader, dstFile io.Writer) error {
|
|
|
|
_, err := pools.Copy(dstFile, srcFile)
|
2014-08-19 09:23:55 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyXattr(srcPath, dstPath, attr string) error {
|
|
|
|
data, err := system.Lgetxattr(srcPath, attr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if data != nil {
|
|
|
|
if err := system.Lsetxattr(dstPath, attr, data, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:11:43 +00:00
|
|
|
type fileID struct {
|
|
|
|
dev uint64
|
|
|
|
ino uint64
|
|
|
|
}
|
|
|
|
|
2017-11-24 00:31:31 +00:00
|
|
|
type dirMtimeInfo struct {
|
|
|
|
dstPath *string
|
|
|
|
stat *syscall.Stat_t
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:01:23 +00:00
|
|
|
// DirCopy copies or hardlinks the contents of one directory to another,
|
|
|
|
// properly handling xattrs, and soft links
|
2017-11-21 18:29:27 +00:00
|
|
|
//
|
|
|
|
// Copying xattrs can be opted out of by passing false for copyXattrs.
|
|
|
|
func DirCopy(srcDir, dstDir string, copyMode Mode, copyXattrs bool) error {
|
2017-08-31 02:07:02 +00:00
|
|
|
copyWithFileRange := true
|
|
|
|
copyWithFileClone := true
|
2017-11-24 00:31:31 +00:00
|
|
|
|
2017-11-21 18:11:43 +00:00
|
|
|
// This is a map of source file inodes to dst file paths
|
|
|
|
copiedFiles := make(map[fileID]string)
|
|
|
|
|
2017-11-24 00:31:31 +00:00
|
|
|
dirsToSetMtimes := list.New()
|
2014-08-19 09:23:55 +00:00
|
|
|
err := filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebase path
|
|
|
|
relPath, err := filepath.Rel(srcDir, srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dstPath := filepath.Join(dstDir, relPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, ok := f.Sys().(*syscall.Stat_t)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Unable to get raw syscall.Stat_t data for %s", srcPath)
|
|
|
|
}
|
|
|
|
|
2015-05-07 20:09:27 +00:00
|
|
|
isHardlink := false
|
|
|
|
|
Graphdriver: fix "device" mode not being detected if "character-device" bit is set
Due to a bug in Golang (github.com/golang#27640), the "character device"
bit was omitted when checking file-modes with `os.ModeType`.
This bug was resolved in Go 1.12, but as a result, graphdrivers
would no longer recognize "device" files, causing pulling of
images that have a file with this filemode to fail;
failed to register layer:
unknown file type for /var/lib/docker/vfs/dir/.../dev/console
The current code checked for an exact match of Modes to be set. The
`os.ModeCharDevice` and `os.ModeDevice` bits will always be set in
tandem, however, because the code was only looking for an exact
match, this detection broke now that `os.ModeCharDevice` was added.
This patch changes the code to be more defensive, and instead
check if the `os.ModeDevice` bit is set (either with, or without
the `os.ModeCharDevice` bit).
In addition, some information was added to the error-message if
no type was matched, to assist debugging in case additional types
are added in future.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-19 15:00:50 +00:00
|
|
|
switch mode := f.Mode(); {
|
|
|
|
case mode.IsRegular():
|
2017-11-21 18:11:43 +00:00
|
|
|
id := fileID{dev: stat.Dev, ino: stat.Ino}
|
2017-08-29 16:01:23 +00:00
|
|
|
if copyMode == Hardlink {
|
2015-05-07 20:09:27 +00:00
|
|
|
isHardlink = true
|
2017-08-31 02:07:02 +00:00
|
|
|
if err2 := os.Link(srcPath, dstPath); err2 != nil {
|
|
|
|
return err2
|
2014-08-19 09:23:55 +00:00
|
|
|
}
|
2017-11-21 18:11:43 +00:00
|
|
|
} else if hardLinkDstPath, ok := copiedFiles[id]; ok {
|
|
|
|
if err2 := os.Link(hardLinkDstPath, dstPath); err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
2014-08-19 09:23:55 +00:00
|
|
|
} else {
|
2017-08-31 02:07:02 +00:00
|
|
|
if err2 := copyRegular(srcPath, dstPath, f, ©WithFileRange, ©WithFileClone); err2 != nil {
|
|
|
|
return err2
|
2014-08-19 09:23:55 +00:00
|
|
|
}
|
2017-11-21 18:11:43 +00:00
|
|
|
copiedFiles[id] = dstPath
|
2014-08-19 09:23:55 +00:00
|
|
|
}
|
|
|
|
|
Graphdriver: fix "device" mode not being detected if "character-device" bit is set
Due to a bug in Golang (github.com/golang#27640), the "character device"
bit was omitted when checking file-modes with `os.ModeType`.
This bug was resolved in Go 1.12, but as a result, graphdrivers
would no longer recognize "device" files, causing pulling of
images that have a file with this filemode to fail;
failed to register layer:
unknown file type for /var/lib/docker/vfs/dir/.../dev/console
The current code checked for an exact match of Modes to be set. The
`os.ModeCharDevice` and `os.ModeDevice` bits will always be set in
tandem, however, because the code was only looking for an exact
match, this detection broke now that `os.ModeCharDevice` was added.
This patch changes the code to be more defensive, and instead
check if the `os.ModeDevice` bit is set (either with, or without
the `os.ModeCharDevice` bit).
In addition, some information was added to the error-message if
no type was matched, to assist debugging in case additional types
are added in future.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-19 15:00:50 +00:00
|
|
|
case mode.IsDir():
|
2014-08-19 09:23:55 +00:00
|
|
|
if err := os.Mkdir(dstPath, f.Mode()); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Graphdriver: fix "device" mode not being detected if "character-device" bit is set
Due to a bug in Golang (github.com/golang#27640), the "character device"
bit was omitted when checking file-modes with `os.ModeType`.
This bug was resolved in Go 1.12, but as a result, graphdrivers
would no longer recognize "device" files, causing pulling of
images that have a file with this filemode to fail;
failed to register layer:
unknown file type for /var/lib/docker/vfs/dir/.../dev/console
The current code checked for an exact match of Modes to be set. The
`os.ModeCharDevice` and `os.ModeDevice` bits will always be set in
tandem, however, because the code was only looking for an exact
match, this detection broke now that `os.ModeCharDevice` was added.
This patch changes the code to be more defensive, and instead
check if the `os.ModeDevice` bit is set (either with, or without
the `os.ModeCharDevice` bit).
In addition, some information was added to the error-message if
no type was matched, to assist debugging in case additional types
are added in future.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-19 15:00:50 +00:00
|
|
|
case mode&os.ModeSymlink != 0:
|
2014-08-19 09:23:55 +00:00
|
|
|
link, err := os.Readlink(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Symlink(link, dstPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Graphdriver: fix "device" mode not being detected if "character-device" bit is set
Due to a bug in Golang (github.com/golang#27640), the "character device"
bit was omitted when checking file-modes with `os.ModeType`.
This bug was resolved in Go 1.12, but as a result, graphdrivers
would no longer recognize "device" files, causing pulling of
images that have a file with this filemode to fail;
failed to register layer:
unknown file type for /var/lib/docker/vfs/dir/.../dev/console
The current code checked for an exact match of Modes to be set. The
`os.ModeCharDevice` and `os.ModeDevice` bits will always be set in
tandem, however, because the code was only looking for an exact
match, this detection broke now that `os.ModeCharDevice` was added.
This patch changes the code to be more defensive, and instead
check if the `os.ModeDevice` bit is set (either with, or without
the `os.ModeCharDevice` bit).
In addition, some information was added to the error-message if
no type was matched, to assist debugging in case additional types
are added in future.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-19 15:00:50 +00:00
|
|
|
case mode&os.ModeNamedPipe != 0:
|
2014-08-19 09:23:55 +00:00
|
|
|
fallthrough
|
Graphdriver: fix "device" mode not being detected if "character-device" bit is set
Due to a bug in Golang (github.com/golang#27640), the "character device"
bit was omitted when checking file-modes with `os.ModeType`.
This bug was resolved in Go 1.12, but as a result, graphdrivers
would no longer recognize "device" files, causing pulling of
images that have a file with this filemode to fail;
failed to register layer:
unknown file type for /var/lib/docker/vfs/dir/.../dev/console
The current code checked for an exact match of Modes to be set. The
`os.ModeCharDevice` and `os.ModeDevice` bits will always be set in
tandem, however, because the code was only looking for an exact
match, this detection broke now that `os.ModeCharDevice` was added.
This patch changes the code to be more defensive, and instead
check if the `os.ModeDevice` bit is set (either with, or without
the `os.ModeCharDevice` bit).
In addition, some information was added to the error-message if
no type was matched, to assist debugging in case additional types
are added in future.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-19 15:00:50 +00:00
|
|
|
case mode&os.ModeSocket != 0:
|
2017-05-23 14:22:32 +00:00
|
|
|
if err := unix.Mkfifo(dstPath, stat.Mode); err != nil {
|
2014-08-19 09:23:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Graphdriver: fix "device" mode not being detected if "character-device" bit is set
Due to a bug in Golang (github.com/golang#27640), the "character device"
bit was omitted when checking file-modes with `os.ModeType`.
This bug was resolved in Go 1.12, but as a result, graphdrivers
would no longer recognize "device" files, causing pulling of
images that have a file with this filemode to fail;
failed to register layer:
unknown file type for /var/lib/docker/vfs/dir/.../dev/console
The current code checked for an exact match of Modes to be set. The
`os.ModeCharDevice` and `os.ModeDevice` bits will always be set in
tandem, however, because the code was only looking for an exact
match, this detection broke now that `os.ModeCharDevice` was added.
This patch changes the code to be more defensive, and instead
check if the `os.ModeDevice` bit is set (either with, or without
the `os.ModeCharDevice` bit).
In addition, some information was added to the error-message if
no type was matched, to assist debugging in case additional types
are added in future.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-19 15:00:50 +00:00
|
|
|
case mode&os.ModeDevice != 0:
|
2018-04-01 11:16:04 +00:00
|
|
|
if rsystem.RunningInUserNS() {
|
|
|
|
// cannot create a device if running in user namespace
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-23 14:22:32 +00:00
|
|
|
if err := unix.Mknod(dstPath, stat.Mode, int(stat.Rdev)); err != nil {
|
2014-08-19 09:23:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
Graphdriver: fix "device" mode not being detected if "character-device" bit is set
Due to a bug in Golang (github.com/golang#27640), the "character device"
bit was omitted when checking file-modes with `os.ModeType`.
This bug was resolved in Go 1.12, but as a result, graphdrivers
would no longer recognize "device" files, causing pulling of
images that have a file with this filemode to fail;
failed to register layer:
unknown file type for /var/lib/docker/vfs/dir/.../dev/console
The current code checked for an exact match of Modes to be set. The
`os.ModeCharDevice` and `os.ModeDevice` bits will always be set in
tandem, however, because the code was only looking for an exact
match, this detection broke now that `os.ModeCharDevice` was added.
This patch changes the code to be more defensive, and instead
check if the `os.ModeDevice` bit is set (either with, or without
the `os.ModeCharDevice` bit).
In addition, some information was added to the error-message if
no type was matched, to assist debugging in case additional types
are added in future.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-19 15:00:50 +00:00
|
|
|
return fmt.Errorf("unknown file type (%d / %s) for %s", f.Mode(), f.Mode().String(), srcPath)
|
2014-08-19 09:23:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 20:09:27 +00:00
|
|
|
// Everything below is copying metadata from src to dst. All this metadata
|
|
|
|
// already shares an inode for hardlinks.
|
|
|
|
if isHardlink {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-19 09:23:55 +00:00
|
|
|
if err := os.Lchown(dstPath, int(stat.Uid), int(stat.Gid)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:29:27 +00:00
|
|
|
if copyXattrs {
|
|
|
|
if err := doCopyXattrs(srcPath, dstPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-19 09:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isSymlink := f.Mode()&os.ModeSymlink != 0
|
|
|
|
|
|
|
|
// There is no LChmod, so ignore mode for symlink. Also, this
|
|
|
|
// must happen after chown, as that can modify the file mode
|
|
|
|
if !isSymlink {
|
|
|
|
if err := os.Chmod(dstPath, f.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 17:45:32 +00:00
|
|
|
// system.Chtimes doesn't support a NOFOLLOW flag atm
|
2017-09-01 16:16:54 +00:00
|
|
|
// nolint: unconvert
|
2017-11-24 00:31:31 +00:00
|
|
|
if f.IsDir() {
|
|
|
|
dirsToSetMtimes.PushFront(&dirMtimeInfo{dstPath: &dstPath, stat: stat})
|
|
|
|
} else if !isSymlink {
|
2017-09-01 16:16:54 +00:00
|
|
|
aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
|
|
|
|
mTime := time.Unix(int64(stat.Mtim.Sec), int64(stat.Mtim.Nsec))
|
2015-10-01 17:45:32 +00:00
|
|
|
if err := system.Chtimes(dstPath, aTime, mTime); err != nil {
|
2014-08-19 09:23:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-01 17:45:32 +00:00
|
|
|
ts := []syscall.Timespec{stat.Atim, stat.Mtim}
|
2014-08-19 09:23:55 +00:00
|
|
|
if err := system.LUtimesNano(dstPath, ts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2017-11-24 00:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for e := dirsToSetMtimes.Front(); e != nil; e = e.Next() {
|
|
|
|
mtimeInfo := e.Value.(*dirMtimeInfo)
|
|
|
|
ts := []syscall.Timespec{mtimeInfo.stat.Atim, mtimeInfo.stat.Mtim}
|
|
|
|
if err := system.LUtimesNano(*mtimeInfo.dstPath, ts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-08-19 09:23:55 +00:00
|
|
|
}
|
2017-11-21 18:29:27 +00:00
|
|
|
|
|
|
|
func doCopyXattrs(srcPath, dstPath string) error {
|
|
|
|
if err := copyXattr(srcPath, dstPath, "security.capability"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to copy this attribute if it appears in an overlay upper layer, as
|
|
|
|
// this function is used to copy those. It is set by overlay if a directory
|
|
|
|
// is removed and then re-created and should not inherit anything from the
|
|
|
|
// same dir in the lower dir.
|
2018-01-14 23:42:25 +00:00
|
|
|
return copyXattr(srcPath, dstPath, "trusted.overlay.opaque")
|
2017-11-21 18:29:27 +00:00
|
|
|
}
|