From 5e7353bb6d4d91a71f2147fa057112a7b42bff10 Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Fri, 13 Dec 2024 17:39:27 +0100 Subject: [PATCH] Improve file copy on copyfile_darwin Make use of unix.CloneFile() --- pkg/machinedriver/copyfile_darwin.go | 17 +++++++++++++++++ pkg/machinedriver/copyfile_nondarwin.go | 11 +++++++++++ pkg/machinedriver/crcimageprovider.go | 3 +-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 pkg/machinedriver/copyfile_darwin.go create mode 100644 pkg/machinedriver/copyfile_nondarwin.go diff --git a/pkg/machinedriver/copyfile_darwin.go b/pkg/machinedriver/copyfile_darwin.go new file mode 100644 index 00000000..d175b461 --- /dev/null +++ b/pkg/machinedriver/copyfile_darwin.go @@ -0,0 +1,17 @@ +package macadam + +import ( + crcos "github.com/crc-org/crc/v2/pkg/os" + "golang.org/x/sys/unix" +) + +func copyFile(src, dst string) error { + if err := unix.Clonefile(src, dst, 0); err != nil { + // fall back to a regular sparse copy, the error may be caused by the filesystem not supporting unix.CloneFile + if err := crcos.CopyFileSparse(src, dst); err != nil { + return err + } + } + + return nil +} diff --git a/pkg/machinedriver/copyfile_nondarwin.go b/pkg/machinedriver/copyfile_nondarwin.go new file mode 100644 index 00000000..67f6695c --- /dev/null +++ b/pkg/machinedriver/copyfile_nondarwin.go @@ -0,0 +1,11 @@ +//go:build !darwin + +package macadam + +import ( + crcos "github.com/crc-org/crc/v2/pkg/os" +) + +func copyFile(src, dst string) error { + return crcos.CopyFile(src, dst) +} diff --git a/pkg/machinedriver/crcimageprovider.go b/pkg/machinedriver/crcimageprovider.go index 8ed22894..fc91c3fc 100644 --- a/pkg/machinedriver/crcimageprovider.go +++ b/pkg/machinedriver/crcimageprovider.go @@ -8,7 +8,6 @@ import ( "github.com/containers/podman/v5/pkg/machine/define" "github.com/containers/podman/v5/pkg/machine/vmconfigs" - crcos "github.com/crc-org/crc/v2/pkg/os" ) type CrcImagePuller struct { @@ -93,7 +92,7 @@ func (puller *CrcImagePuller) Download() error { slog.Info(fmt.Sprintf("%+v", puller)) slog.Info("file copy", "source", puller.sourcePath, "dest", imagePath.GetPath()) - if err := crcos.CopyFile(puller.sourcePath, imagePath.GetPath()); err != nil { + if err := copyFile(puller.sourcePath, imagePath.GetPath()); err != nil { return err } /*