File: util_darwin.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (118 lines) | stat: -rw-r--r-- 2,735 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//go:build darwin
// +build darwin

package tools

import (
	"io"
	"os"
	"strconv"
	"strings"

	"github.com/git-lfs/git-lfs/v3/errors"
	"github.com/git-lfs/git-lfs/v3/tr"
	"golang.org/x/sys/unix"
)

var cloneFileSupported bool

func init() {
	cloneFileSupported = checkCloneFileSupported()
}

// checkCloneFileSupported return iff Mac OS version is greater or equal to 10.12.x Sierra.
//
// clonefile is supported since Mac OS X 10.12
// https://www.manpagez.com/man/2/clonefile/
//
// kern.osrelease mapping
// 17.x.x. macOS 10.13.x High Sierra.
// 16.x.x  macOS 10.12.x Sierra.
// 15.x.x  OS X  10.11.x El Capitan.
func checkCloneFileSupported() bool {
	bytes, err := unix.Sysctl("kern.osrelease")
	if err != nil {
		return false
	}

	versionString := strings.Split(string(bytes), ".") // major.minor.patch
	if len(versionString) < 2 {
		return false
	}

	major, err := strconv.Atoi(versionString[0])
	if err != nil {
		return false
	}

	return major >= 16
}

// CheckCloneFileSupported runs explicit test of clone file on supplied directory.
// This function creates some (src and dst) file in the directory and remove after test finished.
//
// If check failed (e.g. directory is read-only), returns err.
func CheckCloneFileSupported(dir string) (supported bool, err error) {
	if !cloneFileSupported {
		return false, errors.New(tr.Tr.Get("Unsupported OS version. 10.12.x Sierra or higher required."))
	}

	src, err := os.CreateTemp(dir, "src")
	if err != nil {
		return false, err
	}
	defer os.Remove(src.Name())
	src.Close()

	dst, err := os.CreateTemp(dir, "dst")
	if err != nil {
		return false, err
	}
	defer os.Remove(dst.Name())
	dst.Close()

	return CloneFileByPath(dst.Name(), src.Name())
}

type CloneFileError struct {
	Unsupported bool
	errorString string
}

func (c *CloneFileError) Error() string {
	return c.errorString
}

func CloneFile(_ io.Writer, _ io.Reader) (bool, error) {
	return false, nil // Cloning from io.Writer(file descriptor) is not supported by Darwin.
}

func CloneFileByPath(dst, src string) (bool, error) {
	if !cloneFileSupported {
		return false, &CloneFileError{Unsupported: true, errorString: tr.Tr.Get("clonefile is not supported")}
	}

	if FileExists(dst) {
		if err := os.Remove(dst); err != nil {
			return false, err // File should be not exists before create
		}
	}

	if err := cloneFileSyscall(dst, src); err != nil {
		return false, err
	}

	return true, nil
}

func cloneFileSyscall(dst, src string) *CloneFileError {
	err := unix.Clonefileat(unix.AT_FDCWD, src, unix.AT_FDCWD, dst, unix.CLONE_NOFOLLOW)
	if err != nil {
		return &CloneFileError{
			Unsupported: err == unix.ENOTSUP,
			errorString: tr.Tr.Get("error cloning from %v to %v: %s", src, dst, err),
		}
	}

	return nil
}