14
"github.com/spf13/afero/internal/common"
22
func NewIOFS(fs Fs) IOFS {
29
_ fs.ReadDirFS = IOFS{}
30
_ fs.ReadFileFS = IOFS{}
35
func (iofs IOFS) Open(name string) (fs.File, error) {
39
if !fs.ValidPath(name) {
40
return nil, iofs.wrapError(op, name, fs.ErrInvalid)
43
file, err := iofs.Fs.Open(name)
45
return nil, iofs.wrapError(op, name, err)
49
if _, ok := file.(fs.ReadDirFile); !ok {
50
file = readDirFile{file}
56
func (iofs IOFS) Glob(pattern string) ([]string, error) {
60
if _, err := path.Match(pattern, ""); err != nil {
61
return nil, iofs.wrapError(op, pattern, err)
64
items, err := Glob(iofs.Fs, pattern)
66
return nil, iofs.wrapError(op, pattern, err)
72
func (iofs IOFS) ReadDir(name string) ([]fs.DirEntry, error) {
73
f, err := iofs.Fs.Open(name)
75
return nil, iofs.wrapError("readdir", name, err)
80
if rdf, ok := f.(fs.ReadDirFile); ok {
81
items, err := rdf.ReadDir(-1)
83
return nil, iofs.wrapError("readdir", name, err)
85
sort.Slice(items, func(i, j int) bool { return items[i].Name() < items[j].Name() })
89
items, err := f.Readdir(-1)
91
return nil, iofs.wrapError("readdir", name, err)
93
sort.Sort(byName(items))
95
ret := make([]fs.DirEntry, len(items))
96
for i := range items {
97
ret[i] = common.FileInfoDirEntry{FileInfo: items[i]}
103
func (iofs IOFS) ReadFile(name string) ([]byte, error) {
104
const op = "readfile"
106
if !fs.ValidPath(name) {
107
return nil, iofs.wrapError(op, name, fs.ErrInvalid)
110
bytes, err := ReadFile(iofs.Fs, name)
112
return nil, iofs.wrapError(op, name, err)
118
func (iofs IOFS) Sub(dir string) (fs.FS, error) { return IOFS{NewBasePathFs(iofs.Fs, dir)}, nil }
120
func (IOFS) wrapError(op, path string, err error) error {
121
if _, ok := err.(*fs.PathError); ok {
125
return &fs.PathError{
133
type readDirFile struct {
137
var _ fs.ReadDirFile = readDirFile{}
139
func (r readDirFile) ReadDir(n int) ([]fs.DirEntry, error) {
140
items, err := r.File.Readdir(n)
145
ret := make([]fs.DirEntry, len(items))
146
for i := range items {
147
ret[i] = common.FileInfoDirEntry{FileInfo: items[i]}
156
type FromIOFS struct {
162
func (f FromIOFS) Create(name string) (File, error) { return nil, notImplemented("create", name) }
164
func (f FromIOFS) Mkdir(name string, perm os.FileMode) error { return notImplemented("mkdir", name) }
166
func (f FromIOFS) MkdirAll(path string, perm os.FileMode) error {
167
return notImplemented("mkdirall", path)
170
func (f FromIOFS) Open(name string) (File, error) {
171
file, err := f.FS.Open(name)
176
return fromIOFSFile{File: file, name: name}, nil
179
func (f FromIOFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
183
func (f FromIOFS) Remove(name string) error {
184
return notImplemented("remove", name)
187
func (f FromIOFS) RemoveAll(path string) error {
188
return notImplemented("removeall", path)
191
func (f FromIOFS) Rename(oldname, newname string) error {
192
return notImplemented("rename", oldname)
195
func (f FromIOFS) Stat(name string) (os.FileInfo, error) { return fs.Stat(f.FS, name) }
197
func (f FromIOFS) Name() string { return "fromiofs" }
199
func (f FromIOFS) Chmod(name string, mode os.FileMode) error {
200
return notImplemented("chmod", name)
203
func (f FromIOFS) Chown(name string, uid, gid int) error {
204
return notImplemented("chown", name)
207
func (f FromIOFS) Chtimes(name string, atime time.Time, mtime time.Time) error {
208
return notImplemented("chtimes", name)
211
type fromIOFSFile struct {
216
func (f fromIOFSFile) ReadAt(p []byte, off int64) (n int, err error) {
217
readerAt, ok := f.File.(io.ReaderAt)
219
return -1, notImplemented("readat", f.name)
222
return readerAt.ReadAt(p, off)
225
func (f fromIOFSFile) Seek(offset int64, whence int) (int64, error) {
226
seeker, ok := f.File.(io.Seeker)
228
return -1, notImplemented("seek", f.name)
231
return seeker.Seek(offset, whence)
234
func (f fromIOFSFile) Write(p []byte) (n int, err error) {
235
return -1, notImplemented("write", f.name)
238
func (f fromIOFSFile) WriteAt(p []byte, off int64) (n int, err error) {
239
return -1, notImplemented("writeat", f.name)
242
func (f fromIOFSFile) Name() string { return f.name }
244
func (f fromIOFSFile) Readdir(count int) ([]os.FileInfo, error) {
245
rdfile, ok := f.File.(fs.ReadDirFile)
247
return nil, notImplemented("readdir", f.name)
250
entries, err := rdfile.ReadDir(count)
255
ret := make([]os.FileInfo, len(entries))
256
for i := range entries {
257
ret[i], err = entries[i].Info()
267
func (f fromIOFSFile) Readdirnames(n int) ([]string, error) {
268
rdfile, ok := f.File.(fs.ReadDirFile)
270
return nil, notImplemented("readdir", f.name)
273
entries, err := rdfile.ReadDir(n)
278
ret := make([]string, len(entries))
279
for i := range entries {
280
ret[i] = entries[i].Name()
286
func (f fromIOFSFile) Sync() error { return nil }
288
func (f fromIOFSFile) Truncate(size int64) error {
289
return notImplemented("truncate", f.name)
292
func (f fromIOFSFile) WriteString(s string) (ret int, err error) {
293
return -1, notImplemented("writestring", f.name)
296
func notImplemented(op, path string) error {
297
return &fs.PathError{Op: op, Path: path, Err: fs.ErrPermission}