|
| 1 | +//go:build darwin || linux |
| 2 | + |
| 3 | +package osfs |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "math" |
| 9 | + "os" |
| 10 | + "runtime" |
| 11 | + "sync" |
| 12 | + |
| 13 | + "golang.org/x/sys/unix" |
| 14 | +) |
| 15 | + |
| 16 | +// mmapFile is a billy.File backed by a read-only memory map. It is |
| 17 | +// returned from BoundOS/RootOS.OpenFile when the filesystem was |
| 18 | +// constructed with [WithMmap] and the file is opened without write |
| 19 | +// flags. Read and Seek track a real cursor over the mapped bytes, |
| 20 | +// ReadAt is concurrent-safe (multiple goroutines may call it in |
| 21 | +// parallel against the same handle) and serialised against Close |
| 22 | +// via an RWMutex so munmap cannot run while a read is in flight. |
| 23 | +// Write/WriteAt/Truncate return [os.ErrPermission] — the file is |
| 24 | +// read-only by construction. |
| 25 | +type mmapFile struct { |
| 26 | + f *os.File |
| 27 | + data []byte |
| 28 | + name string |
| 29 | + |
| 30 | + mu sync.RWMutex |
| 31 | + cursor int64 |
| 32 | + closed bool |
| 33 | +} |
| 34 | + |
| 35 | +// newMmapFile maps f read-only and returns an [*mmapFile] that owns |
| 36 | +// f. On success the returned handle is responsible for closing the |
| 37 | +// underlying [*os.File] via [(*mmapFile).Close]. |
| 38 | +// |
| 39 | +// If mmap is unavailable for this particular file (zero size, size |
| 40 | +// beyond platform int, mmap rejected by the kernel for pipes/devices |
| 41 | +// etc.) the function returns [errMmapUnavailable] without closing f |
| 42 | +// so the caller can fall back to a regular [*file] wrapper. |
| 43 | +// |
| 44 | +// Any other error (e.g. fstat failing) is propagated as-is and f is |
| 45 | +// closed before returning — the caller must not use it. |
| 46 | +func newMmapFile(f *os.File, name string) (*mmapFile, error) { |
| 47 | + info, err := f.Stat() |
| 48 | + if err != nil { |
| 49 | + _ = f.Close() |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + size := info.Size() |
| 54 | + if size <= 0 || size > int64(math.MaxInt) { |
| 55 | + // unix.Mmap rejects size 0, and 32-bit platforms can't |
| 56 | + // represent very large mappings as an int. Either case |
| 57 | + // is fine for the regular fd wrapper. |
| 58 | + return nil, errMmapUnavailable |
| 59 | + } |
| 60 | + |
| 61 | + data, err := unix.Mmap(int(f.Fd()), 0, int(size), unix.PROT_READ, unix.MAP_SHARED) |
| 62 | + if err != nil { |
| 63 | + // Many failure modes here are legitimate (pipes, devices, |
| 64 | + // FS quirks). Defer to the fd wrapper. |
| 65 | + return nil, errMmapUnavailable |
| 66 | + } |
| 67 | + |
| 68 | + m := &mmapFile{f: f, data: data, name: name} |
| 69 | + // Belt and braces for callers that forget to Close: the runtime |
| 70 | + // will munmap and close the fd when m becomes unreachable. Close |
| 71 | + // clears this finalizer on the orderly path. |
| 72 | + runtime.SetFinalizer(m, (*mmapFile).Close) |
| 73 | + return m, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (m *mmapFile) Name() string { return m.name } |
| 77 | + |
| 78 | +// Stat returns the underlying *os.File's FileInfo unchanged so that |
| 79 | +// f.Stat().Name() matches the basename returned by the fd-backed |
| 80 | +// *file across both backings. |
| 81 | +func (m *mmapFile) Stat() (os.FileInfo, error) { |
| 82 | + return m.f.Stat() |
| 83 | +} |
| 84 | + |
| 85 | +// Read implements [io.Reader]. It holds the write lock because it |
| 86 | +// mutates the shared cursor; concurrent Read+Read would otherwise |
| 87 | +// race on m.cursor even though both could read m.data under RLock. |
| 88 | +// Random-access callers should use ReadAt, which is the parallel API. |
| 89 | +func (m *mmapFile) Read(p []byte) (int, error) { |
| 90 | + if len(p) == 0 { |
| 91 | + return 0, nil |
| 92 | + } |
| 93 | + m.mu.Lock() |
| 94 | + defer m.mu.Unlock() |
| 95 | + if m.closed { |
| 96 | + return 0, os.ErrClosed |
| 97 | + } |
| 98 | + if m.cursor >= int64(len(m.data)) { |
| 99 | + return 0, io.EOF |
| 100 | + } |
| 101 | + n := copy(p, m.data[m.cursor:]) |
| 102 | + m.cursor += int64(n) |
| 103 | + return n, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (m *mmapFile) ReadAt(p []byte, off int64) (int, error) { |
| 107 | + if len(p) == 0 { |
| 108 | + return 0, nil |
| 109 | + } |
| 110 | + m.mu.RLock() |
| 111 | + defer m.mu.RUnlock() |
| 112 | + if m.closed { |
| 113 | + return 0, os.ErrClosed |
| 114 | + } |
| 115 | + if off < 0 { |
| 116 | + return 0, &os.PathError{Op: "readat", Path: m.name, Err: errors.New("negative offset")} |
| 117 | + } |
| 118 | + if off >= int64(len(m.data)) { |
| 119 | + return 0, io.EOF |
| 120 | + } |
| 121 | + n := copy(p, m.data[off:]) |
| 122 | + if n < len(p) { |
| 123 | + return n, io.EOF |
| 124 | + } |
| 125 | + return n, nil |
| 126 | +} |
| 127 | + |
| 128 | +func (m *mmapFile) Seek(offset int64, whence int) (int64, error) { |
| 129 | + m.mu.Lock() |
| 130 | + defer m.mu.Unlock() |
| 131 | + if m.closed { |
| 132 | + return 0, os.ErrClosed |
| 133 | + } |
| 134 | + var abs int64 |
| 135 | + switch whence { |
| 136 | + case io.SeekStart: |
| 137 | + abs = offset |
| 138 | + case io.SeekCurrent: |
| 139 | + abs = m.cursor + offset |
| 140 | + case io.SeekEnd: |
| 141 | + abs = int64(len(m.data)) + offset |
| 142 | + default: |
| 143 | + return 0, &os.PathError{Op: "seek", Path: m.name, Err: errors.New("invalid whence")} |
| 144 | + } |
| 145 | + if abs < 0 { |
| 146 | + return 0, &os.PathError{Op: "seek", Path: m.name, Err: errors.New("negative position")} |
| 147 | + } |
| 148 | + m.cursor = abs |
| 149 | + return abs, nil |
| 150 | +} |
| 151 | + |
| 152 | +func (m *mmapFile) Write(p []byte) (int, error) { |
| 153 | + return 0, &os.PathError{Op: "write", Path: m.name, Err: os.ErrPermission} |
| 154 | +} |
| 155 | + |
| 156 | +func (m *mmapFile) WriteAt(p []byte, off int64) (int, error) { |
| 157 | + return 0, &os.PathError{Op: "writeat", Path: m.name, Err: os.ErrPermission} |
| 158 | +} |
| 159 | + |
| 160 | +func (m *mmapFile) Truncate(size int64) error { |
| 161 | + return &os.PathError{Op: "truncate", Path: m.name, Err: os.ErrPermission} |
| 162 | +} |
| 163 | + |
| 164 | +func (m *mmapFile) Close() error { |
| 165 | + m.mu.Lock() |
| 166 | + defer m.mu.Unlock() |
| 167 | + if m.closed { |
| 168 | + return os.ErrClosed |
| 169 | + } |
| 170 | + m.closed = true |
| 171 | + runtime.SetFinalizer(m, nil) |
| 172 | + |
| 173 | + munmapErr := unix.Munmap(m.data) |
| 174 | + m.data = nil |
| 175 | + closeErr := m.f.Close() |
| 176 | + return errors.Join(munmapErr, closeErr) |
| 177 | +} |
0 commit comments