Debian Codegraph @

helper/iofs/iofs.go

raw symbols SCIP
1 // Package iofs provides an adapter from billy.Filesystem to a the
2 // standard library io.fs.FS interface.
3 package iofsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/
4
5 import (
6 "ioscip-go gomod github.com/golang/go/src go1.25.0 io/"
7 "io/fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/"
8 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
9
10 billyfsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/ "github.com/go-git/go-billy/v6scip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/"
11 "github.com/go-git/go-billy/v6/helper/polyfillscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/polyfill`/"
12 "github.com/go-git/go-billy/v6/utilscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/util`/"
13 )
14
15 // Wrap adapts a billy.Filesystem to a io.fs.FS.
16 func Newscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/New().(fs billyfsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/.Basicscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/Basic#) fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.FSscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FS# {
17 return &adapterFsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#{fsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#fs.: polyfillscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/polyfill`/.Newscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/polyfill`/New().(fs)}
18 }
19
20 type adapterFsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs# struct {
21 fsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#fs. billyfsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/.Filesystemscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/Filesystem#
22 }
23
24 // Type assertion that adapterFS implements the following interfaces:
25 var (
26 _ fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.FSscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FS# = (*adapterFsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#)(nil)
27 _ fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ReadDirFSscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadDirFS# = (*adapterFsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#)(nil)
28 _ fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.StatFSscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/StatFS# = (*adapterFsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#)(nil)
29 _ fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ReadFileFSscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadFileFS# = (*adapterFsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#)(nil)
30 )
31
32 // TODO: implement fs.GlobFS, which will be a fair bit more code.
33
34 // validPath checks that name is a valid io/fs path. In addition to the
35 // standard fs.ValidPath checks, it rejects backslashes which on Windows
36 // would be interpreted as path separators by the underlying billy filesystem.
37 func validPathscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/validPath().(name string) bool {
38 return fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ValidPathscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ValidPath().(name) && !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(name, `\`)
39 }
40
41 // Open opens the named file on the underlying FS, implementing fs.FS (returning a file or error).
42 func (a *adapterFsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#) Openscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#Open().(name string) (fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/File#, error) {
43 if !validPathscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/validPath().(name) {
44 // fstest.TestFS explicitly checks that these should return error.
45 // MemFS performs the clean internally, so we need to block that here for testing purposes.
46 return nil, &fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.PathErrorscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#{Opscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Op.: "open", Pathscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Path.: name, Errscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Err.: fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ErrInvalidscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ErrInvalid.}
47 }
48 stat, err := a.fsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#fs..Statscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/Basic#Stat.(name)
49 if err != nil {
50 return nil, err
51 }
52 if stat.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#IsDir().() {
53 entries, err := a.ReadDirscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#ReadDir().(name)
54 if err != nil {
55 return nil, err
56 }
57 return makeDirscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/makeDir().(stat, entries), nil
58 }
59 file, err := a.fsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#fs..Openscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/Basic#Open.(name)
60 return &adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#{filescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#file.: file, infoscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#info.: stat}, err
61 }
62
63 // ReadDir reads the named directory, implementing fs.ReadDirFS (returning a listing or error).
64 func (a *adapterFsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#) ReadDirscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#ReadDir().(name string) ([]fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#, error) {
65 if !validPathscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/validPath().(name) {
66 return nil, &fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.PathErrorscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#{Opscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Op.: "readdir", Pathscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Path.: name, Errscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Err.: fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ErrInvalidscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ErrInvalid.}
67 }
68 return a.fsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#fs..ReadDirscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/Dir#ReadDir.(name)
69 }
70
71 // Stat returns information on the named file, implementing fs.StatFS (returning FileInfo or error).
72 func (a *adapterFsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#) Statscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#Stat().(name string) (fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.FileInfoscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#, error) {
73 if !validPathscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/validPath().(name) {
74 return nil, &fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.PathErrorscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#{Opscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Op.: "stat", Pathscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Path.: name, Errscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Err.: fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ErrInvalidscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ErrInvalid.}
75 }
76 return a.fsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#fs..Statscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/Basic#Stat.(name)
77 }
78
79 // ReadFile reads the named file and returns its contents, implementing fs.ReadFileFS (returning contents or error).
80 func (a *adapterFsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#) ReadFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#ReadFile().(name string) ([]byte, error) {
81 if !validPathscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/validPath().(name) {
82 return nil, &fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.PathErrorscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#{Opscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Op.: "readfile", Pathscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Path.: name, Errscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Err.: fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ErrInvalidscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ErrInvalid.}
83 }
84
85 return utilscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/util`/.ReadFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/util`/ReadFile().(a.fsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFs#fs., name)
86 }
87
88 type adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile# struct {
89 filescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#file. billyfsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/.Filescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/File#
90 infoscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#info. fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.FileInfoscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#
91 }
92
93 var _ fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/File# = (*adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#)(nil)
94
95 var _ ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReaderAtscip-go gomod github.com/golang/go/src go1.25.0 io/ReaderAt# = (*adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#)(nil)
96
97 var _ ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Seekerscip-go gomod github.com/golang/go/src go1.25.0 io/Seeker# = (*adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#)(nil)
98
99 // Close closes the file, implementing fs.File (and io.Closer).
100 func (a *adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#) Closescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#Close().() error {
101 return a.filescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#file..Closescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/File#Close().()
102 }
103
104 // Read reads bytes from the file, implementing fs.File (and io.Reader).
105 func (a *adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#) Readscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#Read().(b []byte) (int, error) {
106 return a.filescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#file..Readscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/File#Read().(b)
107 }
108
109 func (a *adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#) ReadAtscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#ReadAt().(p []byte, off int64) (int, error) {
110 return a.filescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#file..ReadAtscip-go gomod github.com/golang/go/src go1.25.0 io/ReaderAt#ReadAt().(p, off)
111 }
112
113 func (a *adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#) Seekscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#Seek().(offset int64, whence int) (int64, error) {
114 return a.filescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#file..Seekscip-go gomod github.com/golang/go/src go1.25.0 io/Seeker#Seek().(offset, whence)
115 }
116
117 // Stat returns file information, implementing fs.File (returning FileInfo or error).
118 func (a *adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#) Statscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#Stat().() (fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.FileInfoscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#, error) {
119 return a.infoscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#info., nil
120 }
121
122 type adapterDirFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile# struct {
123 adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#adapterFile.
124 entriesscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#entries. []fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#
125 }
126
127 var _ fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ReadDirFilescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadDirFile# = (*adapterDirFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#)(nil)
128
129 func makeDirscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/makeDir().(stat fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.FileInfoscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#, entries []fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#) *adapterDirFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile# {
130 return &adapterDirFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#{
131 adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#adapterFile.: adapterFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#{infoscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterFile#info.: stat},
132 entriesscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#entries.: entries,
133 }
134 }
135
136 // Close closes the directory, implementing fs.File (and io.Closer).
137 // Subtle: note that this is shadowing adapterFile.Close.
138 func (a *adapterDirFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#) Closescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#Close().() error {
139 return nil
140 }
141
142 // ReadDir reads the directory contents, implementing fs.ReadDirFile (returning directory listing or error).
143 func (a *adapterDirFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#) ReadDirscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#ReadDir().(n int) ([]fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#, error) {
144 if len(a.entriesscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#entries.) == 0 && n > 0 {
145 return nil, ioscip-go gomod github.com/golang/go/src go1.25.0 io/.EOFscip-go gomod github.com/golang/go/src go1.25.0 io/EOF.
146 }
147 if n <= 0 || n > len(a.entriesscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#entries.) {
148 n = len(a.entriesscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#entries.)
149 }
150 entries := a.entriesscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#entries.[:n]
151 a.entriesscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#entries. = a.entriesscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/adapterDirFile#entries.[n:]
152 return entries, nil
153 }