Skip to content

Commit 30cf84d

Browse files
committed
Add support for Darwin clonefile
Signed-off-by: Derek McGowan <[email protected]>
1 parent ebdd069 commit 30cf84d

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

fs/copy.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ func copyDirectory(dst, src string, inodes map[uint64]string, o *copyDirOpts) er
184184
// CopyFile copies the source file to the target.
185185
// The most efficient means of copying is used for the platform.
186186
func CopyFile(target, source string) error {
187+
return copyFile(target, source)
188+
}
189+
190+
func openAndCopyFile(target, source string) error {
187191
src, err := os.Open(source)
188192
if err != nil {
189193
return fmt.Errorf("failed to open source %s: %w", source, err)

fs/copy_darwin.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package fs
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
23+
"golang.org/x/sys/unix"
24+
)
25+
26+
func copyFile(target, source string) error {
27+
if err := unix.Clonefile(source, target, unix.CLONE_NOFOLLOW); err != nil {
28+
if !errors.Is(err, unix.ENOTSUP) {
29+
return fmt.Errorf("clonefile failed: %w", err)
30+
}
31+
32+
return openAndCopyFile(target, source)
33+
}
34+
return nil
35+
}

fs/copy_nondarwin.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build !darwin
2+
// +build !darwin
3+
4+
/*
5+
Copyright The containerd Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package fs
21+
22+
var copyFile = openAndCopyFile

0 commit comments

Comments
 (0)