Documentation
¶
Overview ¶
Package fs provides safe file system operations with consistent error handling.
This package wraps the standard os package to provide atomic file operations, consistent error handling, and thread-safe file system access. All file operations return structured errors with context and suggestions for resolution.
Key Features ¶
- Atomic file writes that prevent partial writes and corruption
- Consistent error handling with structured errors
- Thread-safe operations without race conditions
- Minimal performance overhead (<5% compared to os package)
Usage Examples ¶
Basic file operations:
errorHandler := errors.NewDefaultErrorHandlerWithoutMasking()
fs := fs.NewDefaultFileSystem(errorHandler)
// Read a file
data, err := fs.ReadFile("/path/to/config.yaml")
if err != nil {
// Error includes operation context and suggestions
log.Fatal(err)
}
// Write a file atomically
err = fs.WriteFileAtomic("/path/to/config.yaml", data, 0644)
if err != nil {
log.Fatal(err)
}
Creating directories:
// Create directory with all parent directories
err := fs.MkdirAll("/path/to/nested/dir", 0755)
if err != nil {
log.Fatal(err)
}
Checking file existence:
if fs.Exists("/path/to/file") {
fmt.Println("File exists")
}
Atomic Writes ¶
The WriteFileAtomic method ensures that file writes are atomic:
- Data is written to a temporary file with a unique name
- The temporary file is atomically renamed to the target path
- If any step fails, the temporary file is cleaned up
- The target file is either fully written or unchanged
This prevents configuration corruption from partial writes due to crashes, disk full errors, or other failures.
Error Handling ¶
All file operations return structured errors that include:
- Operation type (read, write, mkdir, etc.)
- File path that caused the error
- Underlying cause error
- Retryability information
- Actionable suggestions for resolution
Example error:
file operation failed: write (path: /etc/config.yaml): permission denied Suggestions: - Run: chmod +w /etc/config.yaml to grant write permissions - Check ownership with: ls -la /etc/config.yaml - Ensure you're running with appropriate user privileges
Thread Safety ¶
The FileSystem wrapper is thread-safe:
- Each operation is independent with no shared mutable state
- Atomic writes use unique temporary file names to prevent collisions
- Underlying os package operations are thread-safe at the syscall level
Multiple goroutines can safely use the same FileSystem instance concurrently.
Performance ¶
The FileSystem wrapper adds minimal overhead:
- ReadFile: <1% overhead compared to os.ReadFile
- WriteFile: <2% overhead compared to os.WriteFile
- WriteFileAtomic: ~5% overhead due to temp file and rename
The atomic write overhead is acceptable for configuration files where correctness is more important than raw performance.
When to Use ¶
Use this package when:
- Writing configuration files that must not be corrupted
- You need consistent error handling across file operations
- You want actionable error messages with suggestions
- You need thread-safe file operations
Use the standard os package when:
- Performance is critical and atomicity is not required
- You're working with large files where atomic writes are impractical
- You need low-level file operations not provided by this package
Index ¶
- type DefaultFileSystem
- func (dfs *DefaultFileSystem) Exists(path string) bool
- func (dfs *DefaultFileSystem) MkdirAll(path string, perm os.FileMode) error
- func (dfs *DefaultFileSystem) ReadFile(path string) ([]byte, error)
- func (dfs *DefaultFileSystem) Remove(path string) error
- func (dfs *DefaultFileSystem) Stat(path string) (fs.FileInfo, error)
- func (dfs *DefaultFileSystem) WriteFile(path string, data []byte, perm os.FileMode) error
- func (dfs *DefaultFileSystem) WriteFileAtomic(path string, data []byte, perm os.FileMode) error
- type FileSystem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DefaultFileSystem ¶
type DefaultFileSystem struct {
// contains filtered or unexported fields
}
DefaultFileSystem implements FileSystem using os package
func NewDefaultFileSystem ¶
func NewDefaultFileSystem(errorHandler errors.ErrorHandler) *DefaultFileSystem
NewDefaultFileSystem creates a new DefaultFileSystem with the given error handler
func (*DefaultFileSystem) Exists ¶
func (dfs *DefaultFileSystem) Exists(path string) bool
Exists checks if a file or directory exists at path
func (*DefaultFileSystem) MkdirAll ¶
func (dfs *DefaultFileSystem) MkdirAll(path string, perm os.FileMode) error
MkdirAll creates directory and all parent directories
func (*DefaultFileSystem) ReadFile ¶
func (dfs *DefaultFileSystem) ReadFile(path string) ([]byte, error)
ReadFile reads the entire file at path
func (*DefaultFileSystem) Remove ¶
func (dfs *DefaultFileSystem) Remove(path string) error
Remove removes the file or directory at path
func (*DefaultFileSystem) Stat ¶
func (dfs *DefaultFileSystem) Stat(path string) (fs.FileInfo, error)
Stat returns file information
func (*DefaultFileSystem) WriteFileAtomic ¶
WriteFileAtomic writes data atomically to prevent corruption
type FileSystem ¶
type FileSystem interface {
// ReadFile reads the entire file at path
ReadFile(path string) ([]byte, error)
// WriteFile writes data to path with given permissions
WriteFile(path string, data []byte, perm os.FileMode) error
// WriteFileAtomic writes data atomically to prevent corruption
WriteFileAtomic(path string, data []byte, perm os.FileMode) error
// Exists checks if a file or directory exists at path
Exists(path string) bool
// MkdirAll creates directory and all parent directories
MkdirAll(path string, perm os.FileMode) error
// Remove removes the file or directory at path
Remove(path string) error
// Stat returns file information
Stat(path string) (fs.FileInfo, error)
}
FileSystem provides safe file operations with consistent error handling