Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy: add fast copy path for darwin #66

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,21 +329,6 @@ func ensureEmptyFileTarget(dst string) error {
return os.Remove(dst)
}

func copyFile(source, target string) error {
src, err := os.Open(source)
if err != nil {
return errors.Wrapf(err, "failed to open source %s", source)
}
defer src.Close()
tgt, err := os.Create(target)
if err != nil {
return errors.Wrapf(err, "failed to open target %s", target)
}
defer tgt.Close()

return copyFileContent(tgt, src)
}

func containsWildcards(name string) bool {
isWindows := runtime.GOOS == "windows"
for i := 0; i < len(name); i++ {
Expand Down
84 changes: 84 additions & 0 deletions copy/copy_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// +build darwin

package fs

import (
"io"
"os"
"syscall"
"unsafe"

"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

// <sys/clonefile.h
// int clonefileat(int, const char *, int, const char *, uint32_t) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

const CLONE_NOFOLLOW = 0x0001 /* Don't follow symbolic links */
const CLONE_NOOWNERCOPY = 0x0002 /* Don't copy ownership information from */

func copyFile(source, target string) error {
if err := clonefile(source, target); err != nil {
if err != unix.EINVAL {
return err
}
} else {
return nil
}

src, err := os.Open(source)
if err != nil {
return errors.Wrapf(err, "failed to open source %s", source)
}
defer src.Close()
tgt, err := os.Create(target)
if err != nil {
return errors.Wrapf(err, "failed to open target %s", target)
}
defer tgt.Close()

return copyFileContent(tgt, src)
}

func copyFileContent(dst, src *os.File) error {
buf := bufferPool.Get().(*[]byte)
_, err := io.CopyBuffer(dst, src, *buf)
bufferPool.Put(buf)

return err
}

// errnoErr returns common boxed Errno values, to prevent
// allocations at runtime.
func errnoErr(e syscall.Errno) error {
switch e {
case 0:
return nil
case unix.EAGAIN:
return syscall.EAGAIN
case unix.EINVAL:
return syscall.EINVAL
case unix.ENOENT:
return syscall.ENOENT
}
return e
}

func clonefile(src, dst string) (err error) {
var _p0, _p1 *byte
_p0, err = unix.BytePtrFromString(src)
if err != nil {
return
}
_p1, err = unix.BytePtrFromString(dst)
if err != nil {
return
}
fdcwd := unix.AT_FDCWD
_, _, e1 := unix.Syscall6(unix.SYS_CLONEFILEAT, uintptr(fdcwd), uintptr(unsafe.Pointer(_p0)), uintptr(fdcwd), uintptr(unsafe.Pointer(_p1)), uintptr(CLONE_NOFOLLOW), 0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}
15 changes: 15 additions & 0 deletions copy/copy_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ func (c *copier) copyFileInfo(fi os.FileInfo, name string) error {
return nil
}

func copyFile(source, target string) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could call this copyFileNaive in copy.go and call it as a fallback from darwin.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this function is not naive in linux. It does copy_file_range on linux.

src, err := os.Open(source)
if err != nil {
return errors.Wrapf(err, "failed to open source %s", source)
}
defer src.Close()
tgt, err := os.Create(target)
if err != nil {
return errors.Wrapf(err, "failed to open target %s", target)
}
defer tgt.Close()

return copyFileContent(tgt, src)
}

func copyFileContent(dst, src *os.File) error {
st, err := src.Stat()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions copy/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ func TestCopyWildcards(t *testing.T) {
}

func TestCopyExistingDirDest(t *testing.T) {
if os.Getuid() != 0 {
t.Skip()
}

t1, err := ioutil.TempDir("", "test")
require.NoError(t, err)
defer os.RemoveAll(t1)
Expand Down
9 changes: 0 additions & 9 deletions copy/copy_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package fs

import (
"io"
"os"
"syscall"

Expand Down Expand Up @@ -51,14 +50,6 @@ func (c *copier) copyFileInfo(fi os.FileInfo, name string) error {
return nil
}

func copyFileContent(dst, src *os.File) error {
buf := bufferPool.Get().(*[]byte)
_, err := io.CopyBuffer(dst, src, *buf)
bufferPool.Put(buf)

return err
}

func copyDevice(dst string, fi os.FileInfo) error {
st, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
Expand Down
15 changes: 15 additions & 0 deletions copy/copy_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ func (c *copier) copyFileInfo(fi os.FileInfo, name string) error {
return nil
}

func copyFile(source, target string) error {
src, err := os.Open(source)
if err != nil {
return errors.Wrapf(err, "failed to open source %s", source)
}
defer src.Close()
tgt, err := os.Create(target)
if err != nil {
return errors.Wrapf(err, "failed to open target %s", target)
}
defer tgt.Close()

return copyFileContent(tgt, src)
}

func copyFileContent(dst, src *os.File) error {
buf := bufferPool.Get().(*[]byte)
_, err := io.CopyBuffer(dst, src, *buf)
Expand Down