Skip to content

Commit

Permalink
Merge pull request #223 from dmcgowan/support-darwin-clonefile
Browse files Browse the repository at this point in the history
Support darwin clonefile
  • Loading branch information
AkihiroSuda authored May 17, 2023
2 parents cec82ed + 30cf84d commit 21eb927
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
4 changes: 4 additions & 0 deletions fs/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ func copyDirectory(dst, src string, inodes map[uint64]string, o *copyDirOpts) er
// CopyFile copies the source file to the target.
// The most efficient means of copying is used for the platform.
func CopyFile(target, source string) error {
return copyFile(target, source)
}

func openAndCopyFile(target, source string) error {
src, err := os.Open(source)
if err != nil {
return fmt.Errorf("failed to open source %s: %w", source, err)
Expand Down
35 changes: 35 additions & 0 deletions fs/copy_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fs

import (
"errors"
"fmt"

"golang.org/x/sys/unix"
)

func copyFile(target, source string) error {
if err := unix.Clonefile(source, target, unix.CLONE_NOFOLLOW); err != nil {
if !errors.Is(err, unix.ENOTSUP) {
return fmt.Errorf("clonefile failed: %w", err)
}

return openAndCopyFile(target, source)
}
return nil
}
22 changes: 22 additions & 0 deletions fs/copy_nondarwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build !darwin
// +build !darwin

/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fs

var copyFile = openAndCopyFile
35 changes: 35 additions & 0 deletions fs/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package fs
import (
_ "crypto/sha256"
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -89,3 +90,37 @@ func testCopy(t testing.TB, apply fstest.Applier) error {

return fstest.CheckDirectoryEqual(t1, t2)
}

func BenchmarkLargeCopy100MB(b *testing.B) {
benchmarkLargeCopyFile(b, 100*1024*1024)
}

func BenchmarkLargeCopy1GB(b *testing.B) {
benchmarkLargeCopyFile(b, 1024*1024*1024)
}

func benchmarkLargeCopyFile(b *testing.B, size int64) {
b.StopTimer()
base := b.TempDir()
apply := fstest.Apply(
fstest.CreateRandomFile("/large", time.Now().UnixNano(), size, 0o644),
)
if err := apply.Apply(base); err != nil {
b.Fatal("failed to apply changes:", err)
}

for i := 0; i < b.N; i++ {
copied := b.TempDir()
b.StartTimer()
if err := CopyDir(copied, base); err != nil {
b.Fatal("failed to copy:", err)
}
b.StopTimer()
if i == 0 {
if err := fstest.CheckDirectoryEqual(base, copied); err != nil {
b.Fatal("check failed:", err)
}
}
os.RemoveAll(copied)
}
}

0 comments on commit 21eb927

Please sign in to comment.