-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Linux implementation of ReflinkCopy FileCopyMethod
- Loading branch information
Showing
3 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//go:build linux | ||
|
||
package copy | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// ReflinkCopy tries to copy the file by creating a reflink from the source | ||
// file to the destination file. This asks the filesystem to share the | ||
// contents between the files using a copy-on-write method. | ||
// | ||
// Reflinks are the fastest way to copy large files, but have a few limitations: | ||
// | ||
// - Requires using a supported filesystem (btrfs, xfs, apfs) | ||
// - Source and destination must be on the same filesystem. | ||
// | ||
// See: https://btrfs.readthedocs.io/en/latest/Reflink.html | ||
// | ||
// -------------------- PLATFORM SPECIFIC INFORMATION -------------------- | ||
// | ||
// Linux implementation uses the `ficlone` ioctl: | ||
// https://manpages.debian.org/testing/manpages-dev/ioctl_ficlone.2.en.html | ||
// | ||
// Support: | ||
// - BTRFS or XFS filesystem | ||
// | ||
// Considerations: | ||
// - Ownership is not preserved. | ||
// - Setuid and Setgid are not preserved. | ||
// - Times are not preserved. | ||
var ReflinkCopy = FileCopyMethod{ | ||
fcopy: func(src, dest string, info os.FileInfo, opt Options) (err error, skipFile bool) { | ||
if opt.FS != nil { | ||
return fmt.Errorf("%w: cannot create reflink from Go's fs.FS interface", ErrUnsupportedCopyMethod), false | ||
} | ||
|
||
if opt.WrapReader != nil { | ||
return fmt.Errorf("%w: cannot create reflink when WrapReader option is used", ErrUnsupportedCopyMethod), false | ||
} | ||
|
||
// Open source file. | ||
readcloser, err := os.OpenFile(src, os.O_RDONLY, 0) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return nil, true | ||
} | ||
return | ||
} | ||
defer fclose(readcloser, &err) | ||
|
||
// Open dest file. | ||
f, err := os.Create(dest) | ||
if err != nil { | ||
return | ||
} | ||
defer fclose(f, &err) | ||
|
||
// Do copy. | ||
srcFd := readcloser.Fd() | ||
destFd := f.Fd() | ||
err = unix.IoctlFileClone(int(destFd), int(srcFd)) | ||
|
||
// Return an error if cloning is not possible. | ||
if err != nil { | ||
return &os.PathError{ | ||
Op: "create reflink", | ||
Path: src, | ||
Err: err, | ||
}, false | ||
} | ||
|
||
return nil, false | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//go:build !darwin | ||
//go:build !darwin && !linux | ||
|
||
package copy | ||
|
||
|