1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 checkPathCase = darwinCheckPathCase
21 func darwinCheckPathCase(path string) error {
22 // Darwin provides fcntl(F_GETPATH) to get a path for an arbitrary FD.
23 // Conveniently for our purposes, it gives the canonical case back. But
24 // there's no guarantee that it will follow the same route through the
25 // filesystem that the original path did.
27 path, err := filepath.Abs(path)
31 fd, err := syscall.Open(path, os.O_RDONLY, 0)
35 defer syscall.Close(fd)
36 buf := make([]byte, 4096) // No MAXPATHLEN in syscall, I think it's 1024, this is bigger.
38 // Wheeee! syscall doesn't expose a way to call Fcntl except FcntlFlock.
39 // As of writing, it just passes the pointer through, so we can just lie.
40 if err := syscall.FcntlFlock(uintptr(fd), syscall.F_GETPATH, (*syscall.Flock_t)(unsafe.Pointer(&buf[0]))); err != nil {
43 buf = buf[:bytes.IndexByte(buf, 0)]
45 isRoot := func(p string) bool {
46 return p[len(p)-1] == filepath.Separator
48 // Darwin seems to like having multiple names for the same folder. Match as much of the suffix as we can.
49 for got, want := path, string(buf); !isRoot(got) && !isRoot(want); got, want = filepath.Dir(got), filepath.Dir(want) {
50 g, w := filepath.Base(got), filepath.Base(want)
51 if !strings.EqualFold(g, w) {
55 return fmt.Errorf("case mismatch in path %q: component %q should be %q", path, g, w)