| 1 | //go:build darwin |
| 2 | |
| 3 | package pathidentity |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "syscall" |
| 10 | |
| 11 | "golang.org/x/sys/unix" |
| 12 | "golang.org/x/text/unicode/norm" |
| 13 | ) |
| 14 | |
| 15 | const pathconfCaseSensitive = 11 |
| 16 | |
| 17 | func platformIdentityKey(path string) (string, error) { |
| 18 | parent, err := closestExistingDirectory(path) |
| 19 | if err != nil { |
| 20 | return "", err |
| 21 | } |
| 22 | var stat unix.Statfs_t |
| 23 | if err := unix.Statfs(parent, &stat); err != nil { |
| 24 | return "", err |
| 25 | } |
| 26 | fsType := strings.TrimRight(string(stat.Fstypename[:]), "\x00") |
| 27 | if fsType == "apfs" || fsType == "hfs" { |
| 28 | path = norm.NFD.String(path) |
| 29 | } |
| 30 | caseSensitive, err := syscall.Pathconf(parent, pathconfCaseSensitive) |
| 31 | if err != nil { |
| 32 | return "", err |
| 33 | } |
| 34 | if caseSensitive == 0 { |
| 35 | path = strings.ToLower(path) |
| 36 | } |
| 37 | return path, nil |
| 38 | } |
| 39 | |
| 40 | func closestExistingDirectory(path string) (string, error) { |
| 41 | for current := path; ; current = filepath.Dir(current) { |
| 42 | info, err := os.Stat(current) |
| 43 | if err == nil && info.IsDir() { |
| 44 | return current, nil |
| 45 | } |
| 46 | if err != nil && !os.IsNotExist(err) { |
| 47 | return "", err |
| 48 | } |
| 49 | if filepath.Dir(current) == current { |
| 50 | return "", os.ErrNotExist |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 |