local.go RawHistoryFolder
1package filesystem
2
3import (
4 "fmt"
5 "os"
6
7 "github.com/bnema/archup/internal/domain/ports"
8)
9
10// LocalFileSystem implements the FileSystem port using the OS filesystem
11type LocalFileSystem struct{}
12
13// NewLocalFileSystem creates a new local filesystem adapter
14func NewLocalFileSystem() *LocalFileSystem {
15 return &LocalFileSystem{}
16}
17
18// Stat returns file info for the given path
19func (lfs *LocalFileSystem) Stat(name string) (os.FileInfo, error) {
20 return os.Stat(name)
21}
22
23// ReadFile reads entire file content
24func (lfs *LocalFileSystem) ReadFile(name string) ([]byte, error) {
25 return os.ReadFile(name)
26}
27
28// WriteFile writes data to a file with the given permissions
29func (lfs *LocalFileSystem) WriteFile(name string, data []byte, perm os.FileMode) error {
30 return os.WriteFile(name, data, perm)
31}
32
33// Create creates or truncates a file
34func (lfs *LocalFileSystem) Create(name string) (ports.File, error) {
35 file, err := os.Create(name)
36 if err != nil {
37 return nil, err
38 }
39 return &localFileHandle{file: file}, nil
40}
41
42// Chmod changes file permissions
43func (lfs *LocalFileSystem) Chmod(name string, perm os.FileMode) error {
44 return os.Chmod(name, perm)
45}
46
47// MkdirAll creates directory path with parents
48func (lfs *LocalFileSystem) MkdirAll(path string, perm os.FileMode) error {
49 return os.MkdirAll(path, perm)
50}
51
52// RemoveAll removes a file or directory recursively
53func (lfs *LocalFileSystem) RemoveAll(path string) error {
54 return os.RemoveAll(path)
55}
56
57// Exists checks if a file/directory exists
58func (lfs *LocalFileSystem) Exists(path string) (bool, error) {
59 _, err := os.Stat(path)
60 if err == nil {
61 return true, nil
62 }
63 if os.IsNotExist(err) {
64 return false, nil
65 }
66 return false, fmt.Errorf("failed to check file existence: %w", err)
67}
68
69// localFileHandle wraps os.File to implement the ports.File interface
70type localFileHandle struct {
71 file *os.File
72}
73
74// Read reads up to len(b) bytes from the file
75func (lfh *localFileHandle) Read(b []byte) (n int, err error) {
76 return lfh.file.Read(b)
77}
78
79// Write writes len(b) bytes to the file
80func (lfh *localFileHandle) Write(b []byte) (n int, err error) {
81 return lfh.file.Write(b)
82}
83
84// Close closes the file
85func (lfh *localFileHandle) Close() error {
86 return lfh.file.Close()
87}