| 1 | //go:build !windows |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | |
| 10 | "golang.org/x/sys/unix" |
| 11 | ) |
| 12 | |
| 13 | func openWorkspaceTallyFile(root *os.Root, path string) (*os.File, error) { |
| 14 | parent, err := root.Open(".") |
| 15 | if err != nil { |
| 16 | return nil, err |
| 17 | } |
| 18 | parts := strings.Split(filepath.Clean(path), string(filepath.Separator)) |
| 19 | for i, part := range parts { |
| 20 | flags := unix.O_RDONLY | unix.O_CLOEXEC | unix.O_NOFOLLOW | unix.O_NONBLOCK |
| 21 | if i < len(parts)-1 { |
| 22 | flags |= unix.O_DIRECTORY |
| 23 | } |
| 24 | fd, openErr := unix.Openat(int(parent.Fd()), part, flags, 0) |
| 25 | _ = parent.Close() |
| 26 | if openErr != nil { |
| 27 | return nil, openErr |
| 28 | } |
| 29 | parent = os.NewFile(uintptr(fd), path) |
| 30 | } |
| 31 | return parent, nil |
| 32 | } |
| 33 |