From 74ea136a49e78442c8ec14307bac8e171f5dc224 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 27 Nov 2013 19:23:48 -0800 Subject: [PATCH] Move reflink to os dependent file. OSX docker client fully functionnal. --- reflink_copy_darwin.go | 14 +++++++++++ reflink_copy_linux.go | 53 ++++++++++++++++++++++++++++++++++++++++++ utils.go | 42 --------------------------------- 3 files changed, 67 insertions(+), 42 deletions(-) create mode 100644 reflink_copy_darwin.go create mode 100644 reflink_copy_linux.go diff --git a/reflink_copy_darwin.go b/reflink_copy_darwin.go new file mode 100644 index 0000000000..3f3147db0f --- /dev/null +++ b/reflink_copy_darwin.go @@ -0,0 +1,14 @@ +package docker + +import ( + "os" + "io" +) + +func CopyFile(dstFile, srcFile *os.File) error { + // No BTRFS reflink suppport, Fall back to normal copy + + // FIXME: Check the return of Copy and compare with dstFile.Stat().Size + _, err := io.Copy(dstFile, srcFile) + return err +} diff --git a/reflink_copy_linux.go b/reflink_copy_linux.go new file mode 100644 index 0000000000..8aae3abf3d --- /dev/null +++ b/reflink_copy_linux.go @@ -0,0 +1,53 @@ +package docker + +// FIXME: This could be easily rewritten in pure Go + +/* +#include +#include +#include + +// See linux.git/fs/btrfs/ioctl.h +#define BTRFS_IOCTL_MAGIC 0x94 +#define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int) + +int +btrfs_reflink(int fd_out, int fd_in) +{ + int res; + res = ioctl(fd_out, BTRFS_IOC_CLONE, fd_in); + if (res < 0) + return errno; + return 0; +} + +*/ +import "C" + +import ( + "os" + "io" + "syscall" +) + +// FIXME: Move this to btrfs package? + +func BtrfsReflink(fd_out, fd_in uintptr) error { + res := C.btrfs_reflink(C.int(fd_out), C.int(fd_in)) + if res != 0 { + return syscall.Errno(res) + } + return nil +} + +func CopyFile(dstFile, srcFile *os.File) error { + err := BtrfsReflink(dstFile.Fd(), srcFile.Fd()) + if err == nil { + return nil + } + + // Fall back to normal copy + // FIXME: Check the return of Copy and compare with dstFile.Stat().Size + _, err = io.Copy(dstFile, srcFile) + return err +} diff --git a/utils.go b/utils.go index f62e46104c..ddef841124 100644 --- a/utils.go +++ b/utils.go @@ -1,37 +1,13 @@ package docker -/* -#include -#include -#include - -// See linux.git/fs/btrfs/ioctl.h -#define BTRFS_IOCTL_MAGIC 0x94 -#define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int) - -int -btrfs_reflink(int fd_out, int fd_in) -{ - int res; - res = ioctl(fd_out, BTRFS_IOC_CLONE, fd_in); - if (res < 0) - return errno; - return 0; -} - -*/ -import "C" import ( "fmt" "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/namesgenerator" "github.com/dotcloud/docker/utils" - "io" "io/ioutil" - "os" "strconv" "strings" - "syscall" ) type Change struct { @@ -346,13 +322,6 @@ func migratePortMappings(config *Config, hostConfig *HostConfig) error { return nil } -func BtrfsReflink(fd_out, fd_in uintptr) error { - res := C.btrfs_reflink(C.int(fd_out), C.int(fd_in)) - if res != 0 { - return syscall.Errno(res) - } - return nil -} // Links come in the format of // name:alias @@ -386,14 +355,3 @@ func (c *checker) Exists(name string) bool { func generateRandomName(runtime *Runtime) (string, error) { return namesgenerator.GenerateRandomName(&checker{runtime}) } - -func CopyFile(dstFile, srcFile *os.File) error { - err := BtrfsReflink(dstFile.Fd(), srcFile.Fd()) - if err == nil { - return nil - } - - // Fall back to normal copy - _, err = io.Copy(dstFile, srcFile) - return err -}