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.
4
5package cache
6
7import (
8	"bytes"
9	"fmt"
10	"os"
11	"path/filepath"
12	"strings"
13	"syscall"
14	"unsafe"
15)
16
17func init() {
18	checkPathCase = darwinCheckPathCase
19}
20
21func 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.
26
27	path, err := filepath.Abs(path)
28	if err != nil {
29		return err
30	}
31	fd, err := syscall.Open(path, os.O_RDONLY, 0)
32	if err != nil {
33		return err
34	}
35	defer syscall.Close(fd)
36	buf := make([]byte, 4096) // No MAXPATHLEN in syscall, I think it's 1024, this is bigger.
37
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 {
41		return err
42	}
43	buf = buf[:bytes.IndexByte(buf, 0)]
44
45	isRoot := func(p string) bool {
46		return p[len(p)-1] == filepath.Separator
47	}
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) {
52			break
53		}
54		if g != w {
55			return fmt.Errorf("case mismatch in path %q: component %q should be %q", path, g, w)
56		}
57	}
58	return nil
59}
60