From 13ff185ba0dbdf0c104999507a8a9b0fe5479029 Mon Sep 17 00:00:00 2001 From: Danny Canter Date: Thu, 31 Aug 2023 20:18:48 -0700 Subject: [PATCH] Blockfile: Enlighten blockfile copy on Darwin The Go stdlib does not seem to have an efficient os.File.ReadFrom routine for other platforms like it does on Linux with copy_file_range. For Darwin at least we can use clonefile in its place, otherwise if we have a sparse file we'd have a fun surprise with the io.Copy approach.. We should see if there's other platforms that we can enhance here. I've forgotten what's the right route on Windows. Signed-off-by: Danny Canter --- snapshots/blockfile/blockfile.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/snapshots/blockfile/blockfile.go b/snapshots/blockfile/blockfile.go index f1e376ae3..7763b8990 100644 --- a/snapshots/blockfile/blockfile.go +++ b/snapshots/blockfile/blockfile.go @@ -22,12 +22,14 @@ import ( "io" "os" "path/filepath" + "runtime" "github.com/containerd/containerd/log" "github.com/containerd/containerd/mount" "github.com/containerd/containerd/plugin" "github.com/containerd/containerd/snapshots" "github.com/containerd/containerd/snapshots/storage" + "github.com/containerd/continuity/fs" ) // viewHookHelper is only used in test for recover the filesystem. @@ -447,6 +449,17 @@ func (o *snapshotter) Close() error { } func copyFileWithSync(target, source string) error { + // The Go stdlib does not seem to have an efficient os.File.ReadFrom + // routine for other platforms like it does on Linux with + // copy_file_range. For Darwin at least we can use clonefile + // in its place, otherwise if we have a sparse file we'd have + // a fun surprise waiting below. + // + // TODO: Enlighten other platforms (windows?) + if runtime.GOOS == "darwin" { + return fs.CopyFile(target, source) + } + src, err := os.Open(source) if err != nil { return fmt.Errorf("failed to open source %s: %w", source, err)