| 1 |
package memfs |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"io/fs" |
| 9 |
"os" |
| 10 |
syspath "path" |
| 11 |
"strings" |
| 12 |
"sync" |
| 13 |
"time" |
| 14 |
) |
| 15 |
|
| 16 |
// FS is an in-memory filesystem that implements |
| 17 |
// io/fs.FS |
| 18 |
type FS struct { |
| 19 |
dir *dir |
| 20 |
openHook func(path string, existingContent []byte, origErr error) ([]byte, error) |
| 21 |
} |
| 22 |
|
| 23 |
// New creates a new in-memory FileSystem. |
| 24 |
func New(opts ...Option) *FS { |
| 25 |
var fsOpt fsOption |
| 26 |
for _, opt := range opts { |
| 27 |
opt.setOption(&fsOpt) |
| 28 |
} |
| 29 |
fs := FS{ |
| 30 |
dir: &dir{ |
| 31 |
children: make(map[string]childI), |
| 32 |
}, |
| 33 |
} |
| 34 |
|
| 35 |
fs.openHook = fsOpt.openHook |
| 36 |
|
| 37 |
return &fs |
| 38 |
} |
| 39 |
|
| 40 |
// MkdirAll creates a directory named path, |
| 41 |
// along with any necessary parents, and returns nil, |
| 42 |
// or else returns an error. |
| 43 |
// The permission bits perm (before umask) are used for all |
| 44 |
// directories that MkdirAll creates. |
| 45 |
// If path is already a directory, MkdirAll does nothing |
| 46 |
// and returns nil. |
| 47 |
func (rootFS *FS) MkdirAll(path string, perm os.FileMode) error { |
| 48 |
if !fs.ValidPath(path) { |
| 49 |
return fmt.Errorf("invalid path: %s: %w", path, fs.ErrInvalid) |
| 50 |
} |
| 51 |
|
| 52 |
if path == "." { |
| 53 |
// root dir always exists |
| 54 |
return nil |
| 55 |
} |
| 56 |
|
| 57 |
parts := strings.Split(path, "/") |
| 58 |
|
| 59 |
next := rootFS.dir |
| 60 |
for _, part := range parts { |
| 61 |
cur := next |
| 62 |
cur.mu.Lock() |
| 63 |
child := cur.children[part] |
| 64 |
if child == nil { |
| 65 |
newDir := &dir{ |
| 66 |
name: part, |
| 67 |
perm: perm, |
| 68 |
children: make(map[string]childI), |
| 69 |
} |
| 70 |
cur.children[part] = newDir |
| 71 |
next = newDir |
| 72 |
} else { |
| 73 |
childDir, ok := child.(*dir) |
| 74 |
if !ok { |
| 75 |
return fmt.Errorf("not a directory: %s: %w", part, fs.ErrInvalid) |
| 76 |
} |
| 77 |
next = childDir |
| 78 |
} |
| 79 |
cur.mu.Unlock() |
| 80 |
} |
| 81 |
|
| 82 |
return nil |
| 83 |
} |
| 84 |
|
| 85 |
func (rootFS *FS) getDir(path string) (*dir, error) { |
| 86 |
if path == "" { |
| 87 |
return rootFS.dir, nil |
| 88 |
} |
| 89 |
parts := strings.Split(path, "/") |
| 90 |
|
| 91 |
cur := rootFS.dir |
| 92 |
for _, part := range parts { |
| 93 |
err := func() error { |
| 94 |
cur.mu.Lock() |
| 95 |
defer cur.mu.Unlock() |
| 96 |
child := cur.children[part] |
| 97 |
if child == nil { |
| 98 |
return fmt.Errorf("not a directory: %s: %w", part, fs.ErrNotExist) |
| 99 |
} else { |
| 100 |
childDir, ok := child.(*dir) |
| 101 |
if !ok { |
| 102 |
return fmt.Errorf("no such file or directory: %s: %w", part, fs.ErrNotExist) |
| 103 |
} |
| 104 |
cur = childDir |
| 105 |
} |
| 106 |
return nil |
| 107 |
}() |
| 108 |
if err != nil { |
| 109 |
return nil, err |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return cur, nil |
| 114 |
} |
| 115 |
|
| 116 |
func (rootFS *FS) get(path string) (childI, error) { |
| 117 |
if path == "" { |
| 118 |
return rootFS.dir, nil |
| 119 |
} |
| 120 |
|
| 121 |
parts := strings.Split(path, "/") |
| 122 |
|
| 123 |
var ( |
| 124 |
cur = rootFS.dir |
| 125 |
|
| 126 |
chld childI |
| 127 |
err error |
| 128 |
) |
| 129 |
for i, part := range parts { |
| 130 |
chld, err = func() (childI, error) { |
| 131 |
cur.mu.Lock() |
| 132 |
defer cur.mu.Unlock() |
| 133 |
child := cur.children[part] |
| 134 |
if child == nil { |
| 135 |
return nil, fmt.Errorf("not a directory: %s: %w", part, fs.ErrNotExist) |
| 136 |
} else { |
| 137 |
_, isFile := child.(*File) |
| 138 |
if isFile { |
| 139 |
if i == len(parts)-1 { |
| 140 |
return child, nil |
| 141 |
} else { |
| 142 |
return nil, fmt.Errorf("no such file or directory: %s: %w", part, fs.ErrNotExist) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
childDir, ok := child.(*dir) |
| 147 |
if !ok { |
| 148 |
return nil, errors.New("not a directory") |
| 149 |
} |
| 150 |
cur = childDir |
| 151 |
} |
| 152 |
return child, nil |
| 153 |
}() |
| 154 |
if err != nil { |
| 155 |
return nil, err |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return chld, nil |
| 160 |
} |
| 161 |
|
| 162 |
func (rootFS *FS) create(path string) (*File, error) { |
| 163 |
if !fs.ValidPath(path) { |
| 164 |
return nil, fmt.Errorf("invalid path: %s: %w", path, fs.ErrInvalid) |
| 165 |
} |
| 166 |
|
| 167 |
if path == "." { |
| 168 |
// root dir |
| 169 |
path = "" |
| 170 |
} |
| 171 |
|
| 172 |
dirPart, filePart := syspath.Split(path) |
| 173 |
|
| 174 |
dirPart = strings.TrimSuffix(dirPart, "/") |
| 175 |
dir, err := rootFS.getDir(dirPart) |
| 176 |
if err != nil { |
| 177 |
return nil, err |
| 178 |
} |
| 179 |
|
| 180 |
dir.mu.Lock() |
| 181 |
defer dir.mu.Unlock() |
| 182 |
existing := dir.children[filePart] |
| 183 |
if existing != nil { |
| 184 |
_, ok := existing.(*File) |
| 185 |
if !ok { |
| 186 |
return nil, fmt.Errorf("path is a directory: %s: %w", path, fs.ErrExist) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
newFile := &File{ |
| 191 |
name: filePart, |
| 192 |
perm: 0666, |
| 193 |
} |
| 194 |
dir.children[filePart] = newFile |
| 195 |
|
| 196 |
return newFile, nil |
| 197 |
} |
| 198 |
|
| 199 |
// WriteFile writes data to a file named by filename. |
| 200 |
// If the file does not exist, WriteFile creates it with permissions perm |
| 201 |
// (before umask); otherwise WriteFile truncates it before writing, without changing permissions. |
| 202 |
func (rootFS *FS) WriteFile(path string, data []byte, perm os.FileMode) error { |
| 203 |
if !fs.ValidPath(path) { |
| 204 |
return fmt.Errorf("invalid path: %s: %w", path, fs.ErrInvalid) |
| 205 |
} |
| 206 |
|
| 207 |
if path == "." { |
| 208 |
// root dir |
| 209 |
path = "" |
| 210 |
} |
| 211 |
|
| 212 |
f, err := rootFS.create(path) |
| 213 |
if err != nil { |
| 214 |
return err |
| 215 |
} |
| 216 |
f.content = data |
| 217 |
f.perm = perm |
| 218 |
return nil |
| 219 |
} |
| 220 |
|
| 221 |
// Open opens the named file. |
| 222 |
func (rootFS *FS) Open(name string) (fs.File, error) { |
| 223 |
if !fs.ValidPath(name) { |
| 224 |
return nil, &fs.PathError{ |
| 225 |
Op: "open", |
| 226 |
Path: name, |
| 227 |
Err: fs.ErrInvalid, |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
child, err := rootFS.open(name) |
| 232 |
if rootFS.openHook != nil { |
| 233 |
var exitingContent []byte |
| 234 |
if child != nil { |
| 235 |
stat, _ := child.Stat() |
| 236 |
if stat.Mode().IsDir() { |
| 237 |
return child, err |
| 238 |
} |
| 239 |
|
| 240 |
exitingContent, err = io.ReadAll(child) |
| 241 |
if err != nil { |
| 242 |
return nil, err |
| 243 |
} |
| 244 |
newContent, err := rootFS.openHook(name, exitingContent, err) |
| 245 |
if err != nil { |
| 246 |
return nil, err |
| 247 |
} |
| 248 |
f := child.(*File) |
| 249 |
f.content = newContent |
| 250 |
f.reader = bytes.NewReader(newContent) |
| 251 |
return f, nil |
| 252 |
} |
| 253 |
} |
| 254 |
return child, err |
| 255 |
} |
| 256 |
|
| 257 |
func (rootFS *FS) open(name string) (fs.File, error) { |
| 258 |
if name == "." { |
| 259 |
// root dir |
| 260 |
name = "" |
| 261 |
} |
| 262 |
|
| 263 |
child, err := rootFS.get(name) |
| 264 |
if err != nil { |
| 265 |
return nil, err |
| 266 |
} |
| 267 |
|
| 268 |
switch cc := child.(type) { |
| 269 |
case *File: |
| 270 |
handle := &File{ |
| 271 |
name: cc.name, |
| 272 |
perm: cc.perm, |
| 273 |
content: cc.content, |
| 274 |
reader: bytes.NewReader(cc.content), |
| 275 |
} |
| 276 |
return handle, nil |
| 277 |
case *dir: |
| 278 |
handle := &fhDir{ |
| 279 |
dir: cc, |
| 280 |
} |
| 281 |
return handle, nil |
| 282 |
} |
| 283 |
|
| 284 |
return nil, fmt.Errorf("unexpected file type in fs: %s: %w", name, fs.ErrInvalid) |
| 285 |
} |
| 286 |
|
| 287 |
// Sub returns an FS corresponding to the subtree rooted at path. |
| 288 |
func (rootFS *FS) Sub(path string) (fs.FS, error) { |
| 289 |
dir, err := rootFS.getDir(path) |
| 290 |
if err != nil { |
| 291 |
return nil, err |
| 292 |
} |
| 293 |
return &FS{dir: dir}, nil |
| 294 |
} |
| 295 |
|
| 296 |
type dir struct { |
| 297 |
mu sync.Mutex |
| 298 |
name string |
| 299 |
perm os.FileMode |
| 300 |
modTime time.Time |
| 301 |
children map[string]childI |
| 302 |
} |
| 303 |
|
| 304 |
type fhDir struct { |
| 305 |
dir *dir |
| 306 |
idx int |
| 307 |
} |
| 308 |
|
| 309 |
func (d *fhDir) Stat() (fs.FileInfo, error) { |
| 310 |
fi := fileInfo{ |
| 311 |
name: d.dir.name, |
| 312 |
size: 4096, |
| 313 |
modTime: d.dir.modTime, |
| 314 |
mode: d.dir.perm | fs.ModeDir, |
| 315 |
} |
| 316 |
return &fi, nil |
| 317 |
} |
| 318 |
|
| 319 |
func (d *fhDir) Read(b []byte) (int, error) { |
| 320 |
return 0, errors.New("is a directory") |
| 321 |
} |
| 322 |
|
| 323 |
func (d *fhDir) Close() error { |
| 324 |
return nil |
| 325 |
} |
| 326 |
|
| 327 |
func (d *fhDir) ReadDir(n int) ([]fs.DirEntry, error) { |
| 328 |
d.dir.mu.Lock() |
| 329 |
defer d.dir.mu.Unlock() |
| 330 |
|
| 331 |
names := make([]string, 0, len(d.dir.children)) |
| 332 |
for name := range d.dir.children { |
| 333 |
names = append(names, name) |
| 334 |
} |
| 335 |
|
| 336 |
// directory already exhausted |
| 337 |
if n <= 0 && d.idx >= len(names) { |
| 338 |
return nil, nil |
| 339 |
} |
| 340 |
|
| 341 |
// read till end |
| 342 |
var err error |
| 343 |
if n > 0 && d.idx+n > len(names) { |
| 344 |
err = io.EOF |
| 345 |
if d.idx > len(names) { |
| 346 |
return nil, err |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if n <= 0 { |
| 351 |
n = len(names) |
| 352 |
} |
| 353 |
|
| 354 |
out := make([]fs.DirEntry, 0, n) |
| 355 |
|
| 356 |
for i := d.idx; i < n && i < len(names); i++ { |
| 357 |
name := names[i] |
| 358 |
child := d.dir.children[name] |
| 359 |
|
| 360 |
f, isFile := child.(*File) |
| 361 |
if isFile { |
| 362 |
stat, _ := f.Stat() |
| 363 |
out = append(out, &dirEntry{ |
| 364 |
info: stat, |
| 365 |
}) |
| 366 |
} else { |
| 367 |
d := child.(*dir) |
| 368 |
fi := fileInfo{ |
| 369 |
name: d.name, |
| 370 |
size: 4096, |
| 371 |
modTime: d.modTime, |
| 372 |
mode: d.perm | fs.ModeDir, |
| 373 |
} |
| 374 |
out = append(out, &dirEntry{ |
| 375 |
info: &fi, |
| 376 |
}) |
| 377 |
} |
| 378 |
|
| 379 |
d.idx = i + 1 |
| 380 |
} |
| 381 |
|
| 382 |
return out, err |
| 383 |
} |
| 384 |
|
| 385 |
type File struct { |
| 386 |
name string |
| 387 |
perm os.FileMode |
| 388 |
content []byte |
| 389 |
reader *bytes.Reader |
| 390 |
modTime time.Time |
| 391 |
closed bool |
| 392 |
} |
| 393 |
|
| 394 |
func (f *File) Stat() (fs.FileInfo, error) { |
| 395 |
if f.closed { |
| 396 |
return nil, fs.ErrClosed |
| 397 |
} |
| 398 |
fi := fileInfo{ |
| 399 |
name: f.name, |
| 400 |
size: int64(len(f.content)), |
| 401 |
modTime: f.modTime, |
| 402 |
mode: f.perm, |
| 403 |
} |
| 404 |
return &fi, nil |
| 405 |
} |
| 406 |
|
| 407 |
func (f *File) Read(b []byte) (int, error) { |
| 408 |
if f.closed { |
| 409 |
return 0, fs.ErrClosed |
| 410 |
} |
| 411 |
return f.reader.Read(b) |
| 412 |
} |
| 413 |
|
| 414 |
func (f *File) Seek(offset int64, whence int) (int64, error) { |
| 415 |
if f.closed { |
| 416 |
return 0, fs.ErrClosed |
| 417 |
} |
| 418 |
|
| 419 |
return f.reader.Seek(offset, whence) |
| 420 |
} |
| 421 |
|
| 422 |
func (f *File) Close() error { |
| 423 |
if f.closed { |
| 424 |
return fs.ErrClosed |
| 425 |
} |
| 426 |
f.closed = true |
| 427 |
return nil |
| 428 |
} |
| 429 |
|
| 430 |
type childI interface{} |
| 431 |
|
| 432 |
type fileInfo struct { |
| 433 |
name string |
| 434 |
size int64 |
| 435 |
modTime time.Time |
| 436 |
mode fs.FileMode |
| 437 |
} |
| 438 |
|
| 439 |
// base name of the file |
| 440 |
func (fi *fileInfo) Name() string { |
| 441 |
return fi.name |
| 442 |
} |
| 443 |
|
| 444 |
// length in bytes for regular files; system-dependent for others |
| 445 |
func (fi *fileInfo) Size() int64 { |
| 446 |
return fi.size |
| 447 |
} |
| 448 |
|
| 449 |
// file mode bits |
| 450 |
func (fi *fileInfo) Mode() fs.FileMode { |
| 451 |
return fi.mode |
| 452 |
} |
| 453 |
|
| 454 |
// modification time |
| 455 |
func (fi *fileInfo) ModTime() time.Time { |
| 456 |
return fi.modTime |
| 457 |
} |
| 458 |
|
| 459 |
// abbreviation for Mode().IsDir() |
| 460 |
func (fi *fileInfo) IsDir() bool { |
| 461 |
return fi.mode&fs.ModeDir > 0 |
| 462 |
} |
| 463 |
|
| 464 |
// underlying data source (can return nil) |
| 465 |
func (fi *fileInfo) Sys() interface{} { |
| 466 |
return nil |
| 467 |
} |
| 468 |
|
| 469 |
type dirEntry struct { |
| 470 |
info fs.FileInfo |
| 471 |
} |
| 472 |
|
| 473 |
func (de *dirEntry) Name() string { |
| 474 |
return de.info.Name() |
| 475 |
} |
| 476 |
|
| 477 |
func (de *dirEntry) IsDir() bool { |
| 478 |
return de.info.IsDir() |
| 479 |
} |
| 480 |
|
| 481 |
func (de *dirEntry) Type() fs.FileMode { |
| 482 |
return de.info.Mode() & fs.ModeType |
| 483 |
} |
| 484 |
|
| 485 |
func (de *dirEntry) Info() (fs.FileInfo, error) { |
| 486 |
return de.info, nil |
| 487 |
} |