| 1 | // embedfs exposes an embed.FS as a read-only billy.Filesystem. |
| 2 | package embedfs |
| 3 | |
| 4 | import ( |
| 5 | "bytes" |
| 6 | "embed" |
| 7 | "fmt" |
| 8 | iofs "io/fs" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "sync" |
| 13 | |
| 14 | "github.com/go-git/go-billy/v6" |
| 15 | ) |
| 16 | |
| 17 | // Embed is a read-only billy.Filesystem backed by an [embed.FS]. Write |
| 18 | // operations return [billy.ErrReadOnly]; symlink, chroot, lstat and tempfile |
| 19 | // operations return [billy.ErrNotSupported]. |
| 20 | type Embed struct { |
| 21 | underlying iofs.FS |
| 22 | } |
| 23 | |
| 24 | // New returns a read-only [billy.Filesystem] backed by efs. A nil efs is |
| 25 | // treated as an empty [embed.FS]. |
| 26 | func New(efs *embed.FS) billy.Filesystem { |
| 27 | fs := &Embed{ |
| 28 | underlying: efs, |
| 29 | } |
| 30 | |
| 31 | if efs == nil { |
| 32 | fs.underlying = &embed.FS{} |
| 33 | } |
| 34 | |
| 35 | return fs |
| 36 | } |
| 37 | |
| 38 | // Root returns the empty string. [Embed] has no notion of a base directory: |
| 39 | // paths are passed straight through to the wrapped [embed.FS], which uses |
| 40 | // forward-slash paths rooted at the embedding package. |
| 41 | func (fs *Embed) Root() string { |
| 42 | return "" |
| 43 | } |
| 44 | |
| 45 | func (fs *Embed) Stat(filename string) (os.FileInfo, error) { |
| 46 | return iofs.Stat(fs.underlying, filename) |
| 47 | } |
| 48 | |
| 49 | func (fs *Embed) Open(filename string) (billy.File, error) { |
| 50 | return fs.OpenFile(filename, os.O_RDONLY, 0) |
| 51 | } |
| 52 | |
| 53 | func (fs *Embed) OpenFile(filename string, flag int, _ os.FileMode) (billy.File, error) { |
| 54 | if flag&(os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_RDWR|os.O_EXCL|os.O_TRUNC) != 0 { |
| 55 | return nil, billy.ErrReadOnly |
| 56 | } |
| 57 | |
| 58 | fi, err := iofs.Stat(fs.underlying, filename) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | |
| 63 | if fi.IsDir() { |
| 64 | return nil, fmt.Errorf("cannot open directory: %s", filename) |
| 65 | } |
| 66 | |
| 67 | data, err := iofs.ReadFile(fs.underlying, filename) |
| 68 | if err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | |
| 72 | // Only load the bytes to memory if the files is needed. |
| 73 | lazyFunc := func() *bytes.Reader { return bytes.NewReader(data) } |
| 74 | return toFile(lazyFunc, fi), nil |
| 75 | } |
| 76 | |
| 77 | // Join return a path with all elements joined by forward slashes. |
| 78 | // |
| 79 | // This behaviour is OS-agnostic. |
| 80 | func (fs *Embed) Join(elem ...string) string { |
| 81 | for i, el := range elem { |
| 82 | if el != "" { |
| 83 | clean := filepath.Clean(strings.Join(elem[i:], "/")) |
| 84 | return filepath.ToSlash(clean) |
| 85 | } |
| 86 | } |
| 87 | return "" |
| 88 | } |
| 89 | |
| 90 | func (fs *Embed) ReadDir(path string) ([]iofs.DirEntry, error) { |
| 91 | return iofs.ReadDir(fs.underlying, path) |
| 92 | } |
| 93 | |
| 94 | func (fs *Embed) Capabilities() billy.Capability { |
| 95 | return billy.ReadCapability | billy.SeekCapability |
| 96 | } |
| 97 | |
| 98 | // Chroot is not supported. |
| 99 | // |
| 100 | // Calls will always return billy.ErrNotSupported. |
| 101 | func (fs *Embed) Chroot(_ string) (billy.Filesystem, error) { |
| 102 | return nil, billy.ErrNotSupported |
| 103 | } |
| 104 | |
| 105 | // Lstat is not supported. |
| 106 | // |
| 107 | // Calls will always return billy.ErrNotSupported. |
| 108 | func (fs *Embed) Lstat(_ string) (os.FileInfo, error) { |
| 109 | return nil, billy.ErrNotSupported |
| 110 | } |
| 111 | |
| 112 | // Readlink is not supported. |
| 113 | // |
| 114 | // Calls will always return billy.ErrNotSupported. |
| 115 | func (fs *Embed) Readlink(_ string) (string, error) { |
| 116 | return "", billy.ErrNotSupported |
| 117 | } |
| 118 | |
| 119 | // TempFile is not supported. |
| 120 | // |
| 121 | // Calls will always return billy.ErrNotSupported. |
| 122 | func (fs *Embed) TempFile(_, _ string) (billy.File, error) { |
| 123 | return nil, billy.ErrNotSupported |
| 124 | } |
| 125 | |
| 126 | // Symlink is not supported. |
| 127 | // |
| 128 | // Calls will always return billy.ErrReadOnly. |
| 129 | func (fs *Embed) Symlink(_, _ string) error { |
| 130 | return billy.ErrReadOnly |
| 131 | } |
| 132 | |
| 133 | // Create is not supported. |
| 134 | // |
| 135 | // Calls will always return billy.ErrReadOnly. |
| 136 | func (fs *Embed) Create(_ string) (billy.File, error) { |
| 137 | return nil, billy.ErrReadOnly |
| 138 | } |
| 139 | |
| 140 | // Rename is not supported. |
| 141 | // |
| 142 | // Calls will always return billy.ErrReadOnly. |
| 143 | func (fs *Embed) Rename(_, _ string) error { |
| 144 | return billy.ErrReadOnly |
| 145 | } |
| 146 | |
| 147 | // Remove is not supported. |
| 148 | // |
| 149 | // Calls will always return billy.ErrReadOnly. |
| 150 | func (fs *Embed) Remove(_ string) error { |
| 151 | return billy.ErrReadOnly |
| 152 | } |
| 153 | |
| 154 | // MkdirAll is not supported. |
| 155 | // |
| 156 | // Calls will always return billy.ErrReadOnly. |
| 157 | func (fs *Embed) MkdirAll(_ string, _ os.FileMode) error { |
| 158 | return billy.ErrReadOnly |
| 159 | } |
| 160 | |
| 161 | func toFile(lazy func() *bytes.Reader, fi iofs.FileInfo) billy.File { |
| 162 | return &file{ |
| 163 | lazy: lazy, |
| 164 | fi: fi, |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | type file struct { |
| 169 | lazy func() *bytes.Reader |
| 170 | reader *bytes.Reader |
| 171 | fi iofs.FileInfo |
| 172 | once sync.Once |
| 173 | } |
| 174 | |
| 175 | func (f *file) loadReader() { |
| 176 | f.reader = f.lazy() |
| 177 | } |
| 178 | |
| 179 | func (f *file) Name() string { |
| 180 | return f.fi.Name() |
| 181 | } |
| 182 | |
| 183 | func (f *file) Read(b []byte) (int, error) { |
| 184 | f.once.Do(f.loadReader) |
| 185 | |
| 186 | return f.reader.Read(b) |
| 187 | } |
| 188 | |
| 189 | func (f *file) ReadAt(b []byte, off int64) (int, error) { |
| 190 | f.once.Do(f.loadReader) |
| 191 | |
| 192 | return f.reader.ReadAt(b, off) |
| 193 | } |
| 194 | |
| 195 | func (f *file) Seek(offset int64, whence int) (int64, error) { |
| 196 | f.once.Do(f.loadReader) |
| 197 | |
| 198 | return f.reader.Seek(offset, whence) |
| 199 | } |
| 200 | |
| 201 | func (f *file) Stat() (os.FileInfo, error) { |
| 202 | return f.fi, nil |
| 203 | } |
| 204 | |
| 205 | // Close for embedfs file is a no-op. |
| 206 | func (f *file) Close() error { |
| 207 | return nil |
| 208 | } |
| 209 | |
| 210 | // Truncate is not supported. |
| 211 | // |
| 212 | // Calls will always return billy.ErrReadOnly. |
| 213 | func (f *file) Truncate(_ int64) error { |
| 214 | return billy.ErrReadOnly |
| 215 | } |
| 216 | |
| 217 | // Write is not supported. |
| 218 | // |
| 219 | // Calls will always return billy.ErrReadOnly. |
| 220 | func (f *file) Write(_ []byte) (int, error) { |
| 221 | return 0, billy.ErrReadOnly |
| 222 | } |
| 223 | |
| 224 | // WriteAt is not supported. |
| 225 | // |
| 226 | // Calls will always return billy.ErrReadOnly. |
| 227 | func (f *file) WriteAt([]byte, int64) (int, error) { |
| 228 | return 0, billy.ErrReadOnly |
| 229 | } |