| 38 | } |
| 39 | |
| 40 | func getHandle(fn string) (*handle, error) { |
| 41 | f, err := os.OpenFile(fn, O_PATH, 0) |
| 42 | if err != nil { |
| 43 | return nil, fmt.Errorf("failed to open %v with O_PATH: %w", fn, err) |
| 44 | } |
| 45 | |
| 46 | var ( |
| 47 | stat syscall.Stat_t |
| 48 | fd = f.Fd() |
| 49 | ) |
| 50 | if err := syscall.Fstat(int(fd), &stat); err != nil { |
| 51 | f.Close() |
| 52 | return nil, fmt.Errorf("failed to stat handle %v: %w", fd, err) |
| 53 | } |
| 54 | |
| 55 | h := &handle{ |
| 56 | f: f, |
| 57 | name: fn, |
| 58 | //nolint:unconvert |
| 59 | dev: uint64(stat.Dev), |
| 60 | ino: stat.Ino, |
| 61 | fd: fd, |
| 62 | } |
| 63 | |
| 64 | // check /proc just in case |
| 65 | if _, err := os.Stat(h.procPath()); err != nil { |
| 66 | f.Close() |
| 67 | return nil, fmt.Errorf("couldn't stat %v: %w", h.procPath(), err) |
| 68 | } |
| 69 | |
| 70 | return h, nil |
| 71 | } |
| 72 | |
| 73 | func (h *handle) procPath() string { |
| 74 | return fmt.Sprintf("/proc/self/fd/%d", h.fd) |