| 1 | //go:build !js |
| 2 | |
| 3 | // Package osfs provides a billy filesystem for the OS. |
| 4 | package osfs |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "io" |
| 9 | "io/fs" |
| 10 | "os" |
| 11 | "sync" |
| 12 | |
| 13 | "github.com/go-git/go-billy/v6" |
| 14 | ) |
| 15 | |
| 16 | const ( |
| 17 | defaultDirectoryMode = 0o777 |
| 18 | defaultCreateMode = 0o666 |
| 19 | ) |
| 20 | |
| 21 | // Default Filesystem representing the root of the os filesystem. |
| 22 | var Default billy.Filesystem = newBoundOS(string(os.PathSeparator)) |
| 23 | |
| 24 | // New returns a new OS filesystem rooted at baseDir. |
| 25 | // |
| 26 | // The returned filesystem is always a [BoundOS]: containment is enforced |
| 27 | // via [os.Root], opened and closed per operation. For better performance |
| 28 | // with caller-managed lifecycle, use [FromRoot] instead. |
| 29 | // |
| 30 | // [WithMmap] enables an mmap-backed implementation of [BoundOS.Open] on |
| 31 | // supported platforms; all other [Option] values are accepted for API |
| 32 | // compatibility but have no effect on the returned implementation. |
| 33 | func New(baseDir string, opts ...Option) billy.Filesystem { |
| 34 | o := &options{} |
| 35 | for _, opt := range opts { |
| 36 | opt(o) |
| 37 | } |
| 38 | |
| 39 | fs := newBoundOS(baseDir) |
| 40 | fs.mmap = o.mmap |
| 41 | return fs |
| 42 | } |
| 43 | |
| 44 | // WithBoundOS selects the [BoundOS] implementation. |
| 45 | // |
| 46 | // [BoundOS] is the only OS-backed implementation returned by [New], so this |
| 47 | // option is the default and is kept for API compatibility. |
| 48 | func WithBoundOS() Option { |
| 49 | return func(o *options) { |
| 50 | o.Type = BoundOSFS |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | type options struct { |
| 55 | Type |
| 56 | mmap bool |
| 57 | } |
| 58 | |
| 59 | // errMmapUnavailable is returned by newMmapFile when the file cannot |
| 60 | // be memory-mapped for benign reasons (zero size, size beyond |
| 61 | // platform int, mmap rejected by the kernel, platform without mmap |
| 62 | // support). Callers in osfs treat this as a fall-through signal and |
| 63 | // return the regular *file wrapper instead. |
| 64 | var errMmapUnavailable = errors.New("osfs: mmap unavailable for this file") |
| 65 | |
| 66 | // Type identifies an osfs implementation. |
| 67 | type Type int |
| 68 | |
| 69 | const ( |
| 70 | // BoundOSFS selects the [BoundOS] implementation. |
| 71 | BoundOSFS Type = iota |
| 72 | ) |
| 73 | |
| 74 | // file is a wrapper for an os.File which adds support for file locking. |
| 75 | type file struct { |
| 76 | *os.File |
| 77 | name string |
| 78 | m sync.Mutex |
| 79 | } |
| 80 | |
| 81 | func (f *file) Name() string { |
| 82 | if f.name != "" { |
| 83 | return f.name |
| 84 | } |
| 85 | return f.File.Name() |
| 86 | } |
| 87 | |
| 88 | func (f *file) WriteTo(w io.Writer) error { |
| 89 | _, err := f.File.WriteTo(w) |
| 90 | return err |
| 91 | } |
| 92 | |
| 93 | type fileInfo struct { |
| 94 | fs.FileInfo |
| 95 | name string |
| 96 | } |
| 97 | |
| 98 | func (fi fileInfo) Name() string { |
| 99 | return fi.name |
| 100 | } |