|
| 1 | +package system |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/fs" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type FileSystem interface { |
| 12 | + Glob(pattern string) ([]string, error) |
| 13 | + ReadFile(name string) ([]byte, error) |
| 14 | + ReadDir(name string) ([]fs.DirEntry, error) |
| 15 | + Stat(name string) (fs.FileInfo, error) |
| 16 | +} |
| 17 | + |
| 18 | +type RealFileSystem struct{} |
| 19 | + |
| 20 | +func (RealFileSystem) Glob(pattern string) ([]string, error) { |
| 21 | + return filepath.Glob(pattern) |
| 22 | +} |
| 23 | + |
| 24 | +func (RealFileSystem) ReadFile(name string) ([]byte, error) { |
| 25 | + // This has a slight issue since gosec will not flag lines that call RealFileSystem.ReadFile. |
| 26 | + // #nosec G304 intended usage. |
| 27 | + return os.ReadFile(name) |
| 28 | +} |
| 29 | + |
| 30 | +func (RealFileSystem) ReadDir(name string) ([]fs.DirEntry, error) { |
| 31 | + return os.ReadDir(name) |
| 32 | +} |
| 33 | + |
| 34 | +func (RealFileSystem) Stat(name string) (fs.FileInfo, error) { |
| 35 | + return os.Stat(name) |
| 36 | +} |
| 37 | + |
| 38 | +const EmptyDirectoryMarker = "[empty directory]" |
| 39 | + |
| 40 | +type FakeFileSystem struct { |
| 41 | + Files map[string]string |
| 42 | +} |
| 43 | + |
| 44 | +func (f FakeFileSystem) allPaths() map[string]bool { |
| 45 | + paths := make(map[string]bool) |
| 46 | + for path := range f.Files { |
| 47 | + paths[path] = true |
| 48 | + for dir := filepath.Dir(path); dir != "/" && dir != "."; dir = filepath.Dir(dir) { |
| 49 | + paths[dir] = true |
| 50 | + } |
| 51 | + } |
| 52 | + return paths |
| 53 | +} |
| 54 | + |
| 55 | +func (f FakeFileSystem) Glob(pattern string) ([]string, error) { |
| 56 | + var matches []string |
| 57 | + for path := range f.allPaths() { |
| 58 | + matched, err := filepath.Match(pattern, path) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + if matched { |
| 63 | + matches = append(matches, path) |
| 64 | + } |
| 65 | + } |
| 66 | + return matches, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (f FakeFileSystem) ReadFile(name string) ([]byte, error) { |
| 70 | + content, ok := f.Files[name] |
| 71 | + if !ok { |
| 72 | + return nil, os.ErrNotExist |
| 73 | + } |
| 74 | + if content == EmptyDirectoryMarker { |
| 75 | + return nil, &os.PathError{Op: "read", Path: name, Err: os.ErrInvalid} |
| 76 | + } |
| 77 | + return []byte(content), nil |
| 78 | +} |
| 79 | + |
| 80 | +func (f FakeFileSystem) ReadDir(name string) ([]fs.DirEntry, error) { |
| 81 | + name = strings.TrimSuffix(name, "/") |
| 82 | + if content, ok := f.Files[name]; ok && content != EmptyDirectoryMarker { |
| 83 | + return nil, &os.PathError{Op: "readdir", Path: name, Err: os.ErrInvalid} |
| 84 | + } |
| 85 | + |
| 86 | + var entries []fs.DirEntry |
| 87 | + seen := make(map[string]bool) |
| 88 | + prefix := name + "/" |
| 89 | + |
| 90 | + for path := range f.Files { |
| 91 | + if !strings.HasPrefix(path, prefix) { |
| 92 | + continue |
| 93 | + } |
| 94 | + rest := strings.TrimPrefix(path, prefix) |
| 95 | + parts := strings.SplitN(rest, "/", 2) |
| 96 | + entryName := parts[0] |
| 97 | + if seen[entryName] { |
| 98 | + continue |
| 99 | + } |
| 100 | + seen[entryName] = true |
| 101 | + |
| 102 | + entryPath := name + "/" + entryName |
| 103 | + entries = append(entries, &fakeEntry{name: entryName, isDir: f.isDir(entryPath)}) |
| 104 | + } |
| 105 | + |
| 106 | + if len(entries) == 0 { |
| 107 | + if _, ok := f.Files[name]; !ok { |
| 108 | + return nil, os.ErrNotExist |
| 109 | + } |
| 110 | + } |
| 111 | + return entries, nil |
| 112 | +} |
| 113 | + |
| 114 | +func (f FakeFileSystem) isDir(name string) bool { |
| 115 | + if content, ok := f.Files[name]; ok && content == EmptyDirectoryMarker { |
| 116 | + return true |
| 117 | + } |
| 118 | + prefix := name + "/" |
| 119 | + for path := range f.Files { |
| 120 | + if strings.HasPrefix(path, prefix) { |
| 121 | + return true |
| 122 | + } |
| 123 | + } |
| 124 | + return false |
| 125 | +} |
| 126 | + |
| 127 | +func (f FakeFileSystem) Stat(name string) (fs.FileInfo, error) { |
| 128 | + name = strings.TrimSuffix(name, "/") |
| 129 | + if content, ok := f.Files[name]; ok { |
| 130 | + return &fakeFileInfo{name: filepath.Base(name), isDir: content == EmptyDirectoryMarker, size: int64(len(content))}, nil |
| 131 | + } |
| 132 | + if f.isDir(name) { |
| 133 | + return &fakeFileInfo{name: filepath.Base(name), isDir: true}, nil |
| 134 | + } |
| 135 | + return nil, os.ErrNotExist |
| 136 | +} |
| 137 | + |
| 138 | +type fakeEntry struct { |
| 139 | + name string |
| 140 | + isDir bool |
| 141 | +} |
| 142 | + |
| 143 | +func (e *fakeEntry) Name() string { return e.name } |
| 144 | +func (e *fakeEntry) IsDir() bool { return e.isDir } |
| 145 | +func (e *fakeEntry) Type() fs.FileMode { |
| 146 | + if e.isDir { |
| 147 | + return fs.ModeDir |
| 148 | + } |
| 149 | + return 0 |
| 150 | +} |
| 151 | +func (e *fakeEntry) Info() (fs.FileInfo, error) { |
| 152 | + return &fakeFileInfo{name: e.name, isDir: e.isDir}, nil |
| 153 | +} |
| 154 | + |
| 155 | +type fakeFileInfo struct { |
| 156 | + name string |
| 157 | + isDir bool |
| 158 | + size int64 |
| 159 | +} |
| 160 | + |
| 161 | +func (fi *fakeFileInfo) Name() string { return fi.name } |
| 162 | +func (fi *fakeFileInfo) Size() int64 { return fi.size } |
| 163 | +func (fi *fakeFileInfo) Mode() fs.FileMode { |
| 164 | + if fi.isDir { |
| 165 | + return fs.ModeDir | 0755 |
| 166 | + } |
| 167 | + return 0644 |
| 168 | +} |
| 169 | +func (fi *fakeFileInfo) ModTime() time.Time { return time.Time{} } |
| 170 | +func (fi *fakeFileInfo) IsDir() bool { return fi.isDir } |
| 171 | +func (fi *fakeFileInfo) Sys() any { return nil } |
0 commit comments