| 1 | //go:build !js |
| 2 | |
| 3 | package osfs |
| 4 | |
| 5 | type Option func(*options) |
| 6 | |
| 7 | // WithMmap opts the filesystem returned by [New] (and any [RootOS] |
| 8 | // returned by [FromRoot]) into an mmap-backed implementation of |
| 9 | // [billy.File] for read-only opens on platforms that support it |
| 10 | // (currently darwin and linux). On other platforms the option is |
| 11 | // accepted but has no effect. |
| 12 | // |
| 13 | // EXPERIMENTAL: this option's semantics may change. Today every |
| 14 | // read-only [BoundOS.Open] / [RootOS.Open] (and any [BoundOS.OpenFile] |
| 15 | // / [RootOS.OpenFile] without write flags) returns a mmap-backed |
| 16 | // handle when the option is set. That includes small files and |
| 17 | // sequential-read workloads where mmap can be neutral or net- |
| 18 | // negative compared to plain file I/O. If we find a meaningful |
| 19 | // reason to make the opt-in finer-grained (per-call rather than |
| 20 | // per-FS), the option's effect on [Open]/[OpenFile] will narrow |
| 21 | // accordingly. |
| 22 | // |
| 23 | // mmap also changes failure semantics: truncating the underlying |
| 24 | // file while a read is in flight raises SIGBUS instead of returning |
| 25 | // an error, and replacing the file via rename leaves the mapping |
| 26 | // pointing at the old inode. Callers that may see the file mutate |
| 27 | // underneath them should leave the option off. |
| 28 | // |
| 29 | // The mmap-backed file is read-only by construction; it does not |
| 30 | // satisfy [billy.Syncer] (Sync is meaningless on a read-only |
| 31 | // mapping) or the [Locker] interface even though the surrounding |
| 32 | // filesystem advertises both capabilities. Code that type-asserts |
| 33 | // against those interfaces on a handle returned by an open with |
| 34 | // [WithMmap] active should be prepared for the assertion to fail. |
| 35 | func WithMmap() Option { |
| 36 | return func(o *options) { |
| 37 | o.mmap = true |
| 38 | } |
| 39 | } |