- Define pool.FileSystem interface in internal/io/pool/filesystem.go - Move DirEntry interface to internal/io/pool/types/types.go to break the pool <-> mock import cycle - Implement real.FileSystem with atomic writes (temp file + os.Rename) - Update mock.FileSystem to satisfy pool.FileSystem interface - Update worker pool tasks to accept pool.FileSystem interface - Update main.go to use RealFileSystem by default, with -root flag - Update all browser/editor/test references to types.DirEntry
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package real
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"pad/internal/io/pool/types"
|
|
)
|
|
|
|
// RealFileSystem implements the pool.FileSystem interface using the real OS filesystem.
|
|
type RealFileSystem struct {
|
|
Root string
|
|
}
|
|
|
|
func (fs *RealFileSystem) ReadDir(path string) ([]types.DirEntry, error) {
|
|
entries, err := os.ReadDir(filepath.Join(fs.Root, path))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var res []types.DirEntry
|
|
for _, e := range entries {
|
|
res = append(res, &realDirEntry{e})
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (fs *RealFileSystem) DirExists(path string) bool {
|
|
info, err := os.Stat(filepath.Join(fs.Root, path))
|
|
return err == nil && info.IsDir()
|
|
}
|
|
|
|
func (fs *RealFileSystem) FileExists(path string) bool {
|
|
info, err := os.Stat(filepath.Join(fs.Root, path))
|
|
return err == nil && !info.IsDir()
|
|
}
|
|
|
|
func (fs *RealFileSystem) ReadFile(path string) ([]byte, error) {
|
|
return os.ReadFile(filepath.Join(fs.Root, path))
|
|
}
|
|
|
|
func (fs *RealFileSystem) WriteFile(path string, content []byte) error {
|
|
// Atomic write implementation
|
|
fullPath := filepath.Join(fs.Root, path)
|
|
tmpPath := filepath.Join(fs.Root, ".tmp", filepath.Base(path))
|
|
|
|
if err := os.MkdirAll(filepath.Dir(tmpPath), 0755); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := os.WriteFile(tmpPath, content, 0644); err != nil {
|
|
return err
|
|
}
|
|
|
|
return os.Rename(tmpPath, fullPath)
|
|
}
|
|
|
|
func (fs *RealFileSystem) DeleteFile(path string) error {
|
|
return os.Remove(filepath.Join(fs.Root, path))
|
|
}
|
|
|
|
func (fs *RealFileSystem) CreateDir(path string) error {
|
|
return os.MkdirAll(filepath.Join(fs.Root, path), 0755)
|
|
}
|
|
|
|
// realDirEntry wraps os.DirEntry
|
|
type realDirEntry struct {
|
|
entry os.DirEntry
|
|
}
|
|
|
|
func (e *realDirEntry) Name() string { return e.entry.Name() }
|
|
func (e *realDirEntry) IsDir() bool { return e.entry.IsDir() }
|
|
func (e *realDirEntry) Info() (os.FileInfo, error) { return e.entry.Info() }
|