Refactor RealFileSystem to use WorkingDir, and ensure clean shutdown with FlushAll

This commit is contained in:
gmp
2026-06-05 11:42:39 -04:00
parent ac2e0e444d
commit 1324772215
6 changed files with 78 additions and 36 deletions
+7
View File
@@ -413,3 +413,10 @@ func (l *Logic) FlushAll() {
}
}
}
// Shutdown gracefully shuts down the logic goroutine and worker pool.
func (l *Logic) Shutdown() {
l.FlushAll()
l.Done()
l.workerPool.Stop()
}
+22 -9
View File
@@ -9,11 +9,24 @@ import (
// RealFileSystem implements the pool.FileSystem interface using the real OS filesystem.
type RealFileSystem struct {
Root string
WorkingDir string
}
// NewRealFileSystem creates a RealFileSystem rooted at the given working directory.
// If workingDir is empty, it defaults to "/".
func NewRealFileSystem(workingDir string) *RealFileSystem {
if workingDir == "" {
workingDir = "/"
}
abs, err := filepath.Abs(workingDir)
if err != nil {
abs = workingDir
}
return &RealFileSystem{WorkingDir: abs}
}
func (fs *RealFileSystem) ReadDir(path string) ([]types.DirEntry, error) {
entries, err := os.ReadDir(filepath.Join(fs.Root, path))
entries, err := os.ReadDir(filepath.Join(fs.WorkingDir, path))
if err != nil {
return nil, err
}
@@ -25,21 +38,21 @@ func (fs *RealFileSystem) ReadDir(path string) ([]types.DirEntry, error) {
}
func (fs *RealFileSystem) DirExists(path string) bool {
info, err := os.Stat(filepath.Join(fs.Root, path))
info, err := os.Stat(filepath.Join(fs.WorkingDir, path))
return err == nil && info.IsDir()
}
func (fs *RealFileSystem) FileExists(path string) bool {
info, err := os.Stat(filepath.Join(fs.Root, path))
info, err := os.Stat(filepath.Join(fs.WorkingDir, path))
return err == nil && !info.IsDir()
}
func (fs *RealFileSystem) ReadFile(path string) ([]byte, error) {
return os.ReadFile(filepath.Join(fs.Root, path))
return os.ReadFile(filepath.Join(fs.WorkingDir, path))
}
func (fs *RealFileSystem) ReadFileAt(path string, offset, size int) ([]byte, error) {
fullPath := filepath.Join(fs.Root, path)
fullPath := filepath.Join(fs.WorkingDir, path)
f, err := os.Open(fullPath)
if err != nil {
return nil, err
@@ -61,7 +74,7 @@ func (fs *RealFileSystem) WriteFile(path string, content []byte) error {
func (fs *RealFileSystem) WriteFileAtomic(path string, content []byte) error {
// Atomic write implementation
fullPath := filepath.Join(fs.Root, path)
fullPath := filepath.Join(fs.WorkingDir, path)
// Temp file in the same directory as the target to ensure same filesystem rename
tmpPath := filepath.Join(filepath.Dir(fullPath), "."+filepath.Base(path)+".tmp")
@@ -77,11 +90,11 @@ func (fs *RealFileSystem) WriteFileAtomic(path string, content []byte) error {
}
func (fs *RealFileSystem) DeleteFile(path string) error {
return os.Remove(filepath.Join(fs.Root, path))
return os.Remove(filepath.Join(fs.WorkingDir, path))
}
func (fs *RealFileSystem) CreateDir(path string) error {
return os.MkdirAll(filepath.Join(fs.Root, path), 0755)
return os.MkdirAll(filepath.Join(fs.WorkingDir, path), 0755)
}
// realDirEntry wraps os.DirEntry