|
| 1 | +// Package iofs provides an adapter from billy.Filesystem to a the |
| 2 | +// standard library io.fs.FS interface. |
| 3 | +package iofs |
| 4 | + |
| 5 | +import ( |
| 6 | + "io" |
| 7 | + "io/fs" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + billyfs "github.com/go-git/go-billy/v5" |
| 11 | + "github.com/go-git/go-billy/v5/helper/polyfill" |
| 12 | +) |
| 13 | + |
| 14 | +// Wrap adapts a billy.Filesystem to a io.fs.FS. |
| 15 | +func New(fs billyfs.Basic) fs.FS { |
| 16 | + return &adapterFs{fs: polyfill.New(fs)} |
| 17 | +} |
| 18 | + |
| 19 | +type adapterFs struct { |
| 20 | + fs billyfs.Filesystem |
| 21 | +} |
| 22 | + |
| 23 | +// Type assertion that adapterFS implements the following interfaces: |
| 24 | +var _ fs.FS = (*adapterFs)(nil) |
| 25 | +var _ fs.ReadDirFS = (*adapterFs)(nil) |
| 26 | +var _ fs.StatFS = (*adapterFs)(nil) |
| 27 | +var _ fs.ReadFileFS = (*adapterFs)(nil) |
| 28 | + |
| 29 | +// TODO: implement fs.GlobFS, which will be a fair bit more code. |
| 30 | + |
| 31 | +// Open opens the named file on the underlying FS, implementing fs.FS (returning a file or error). |
| 32 | +func (a *adapterFs) Open(name string) (fs.File, error) { |
| 33 | + if name[0] == '/' || name != filepath.Clean(name) { |
| 34 | + // fstest.TestFS explicitly checks that these should return error. |
| 35 | + // MemFS performs the clean internally, so we need to block that here for testing purposes. |
| 36 | + return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid} |
| 37 | + } |
| 38 | + stat, err := a.fs.Stat(name) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + if stat.IsDir() { |
| 43 | + entries, err := a.ReadDir(name) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + return makeDir(stat, entries), nil |
| 48 | + } |
| 49 | + file, err := a.fs.Open(name) |
| 50 | + return &adapterFile{file: file, info: stat}, err |
| 51 | +} |
| 52 | + |
| 53 | +// ReadDir reads the named directory, implementing fs.ReadDirFS (returning a listing or error). |
| 54 | +func (a *adapterFs) ReadDir(name string) ([]fs.DirEntry, error) { |
| 55 | + items, err := a.fs.ReadDir(name) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + entries := make([]fs.DirEntry, len(items)) |
| 60 | + for i, item := range items { |
| 61 | + entries[i] = fs.FileInfoToDirEntry(item) |
| 62 | + } |
| 63 | + return entries, nil |
| 64 | +} |
| 65 | + |
| 66 | +// Stat returns information on the named file, implementing fs.StatFS (returning FileInfo or error). |
| 67 | +func (a *adapterFs) Stat(name string) (fs.FileInfo, error) { |
| 68 | + return a.fs.Stat(name) |
| 69 | +} |
| 70 | + |
| 71 | +// ReadFile reads the named file and returns its contents, implementing fs.ReadFileFS (returning contents or error). |
| 72 | +func (a *adapterFs) ReadFile(name string) ([]byte, error) { |
| 73 | + stat, err := a.fs.Stat(name) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + b := make([]byte, stat.Size()) |
| 78 | + file, err := a.Open(name) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + defer file.Close() |
| 83 | + _, err = file.Read(b) |
| 84 | + return b, err |
| 85 | +} |
| 86 | + |
| 87 | +type adapterFile struct { |
| 88 | + file billyfs.File |
| 89 | + info fs.FileInfo |
| 90 | +} |
| 91 | + |
| 92 | +var _ fs.File = (*adapterFile)(nil) |
| 93 | + |
| 94 | +// Close closes the file, implementing fs.File (and io.Closer). |
| 95 | +func (a *adapterFile) Close() error { |
| 96 | + return a.file.Close() |
| 97 | +} |
| 98 | + |
| 99 | +// Read reads bytes from the file, implementing fs.File (and io.Reader). |
| 100 | +func (a *adapterFile) Read(b []byte) (int, error) { |
| 101 | + return a.file.Read(b) |
| 102 | +} |
| 103 | + |
| 104 | +// Stat returns file information, implementing fs.File (returning FileInfo or error). |
| 105 | +func (a *adapterFile) Stat() (fs.FileInfo, error) { |
| 106 | + return a.info, nil |
| 107 | +} |
| 108 | + |
| 109 | +type adapterDirFile struct { |
| 110 | + adapterFile |
| 111 | + entries []fs.DirEntry |
| 112 | +} |
| 113 | + |
| 114 | +var _ fs.ReadDirFile = (*adapterDirFile)(nil) |
| 115 | + |
| 116 | +func makeDir(stat fs.FileInfo, entries []fs.DirEntry) *adapterDirFile { |
| 117 | + return &adapterDirFile{ |
| 118 | + adapterFile: adapterFile{info: stat}, |
| 119 | + entries: entries, |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// Close closes the directory, implementing fs.File (and io.Closer). |
| 124 | +// Subtle: note that this is shadowing adapterFile.Close. |
| 125 | +func (a *adapterDirFile) Close() error { |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +// ReadDir reads the directory contents, implementing fs.ReadDirFile (returning directory listing or error). |
| 130 | +func (a *adapterDirFile) ReadDir(n int) ([]fs.DirEntry, error) { |
| 131 | + if len(a.entries) == 0 && n > 0 { |
| 132 | + return nil, io.EOF |
| 133 | + } |
| 134 | + if n <= 0 || n > len(a.entries) { |
| 135 | + n = len(a.entries) |
| 136 | + } |
| 137 | + entries := a.entries[:n] |
| 138 | + a.entries = a.entries[n:] |
| 139 | + return entries, nil |
| 140 | +} |
0 commit comments