1
16
17
21
22 package filesystem
23
24 import (
25 "io/fs"
26 "os"
27 "path/filepath"
28 "time"
29 )
30
31
32 type DefaultFs struct{}
33
34 var _ Filesystem = DefaultFs{}
35
36
37 func (DefaultFs) Stat(name string) (os.FileInfo, error) {
38 return os.Stat(name)
39 }
40
41
42 func (DefaultFs) Create(name string) (File, error) {
43 file, err := os.Create(name)
44 if err != nil {
45 return nil, err
46 }
47 return &defaultFile{file}, nil
48 }
49
50
51 func (DefaultFs) Open(name string) (File, error) {
52 file, err := os.Open(name)
53 if err != nil {
54 return nil, err
55 }
56
57 return &defaultFile{file}, nil
58 }
59
60
61 func (DefaultFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
62 file, err := os.OpenFile(name, flag, perm)
63 if err != nil {
64 return nil, err
65 }
66
67 return &defaultFile{file}, nil
68 }
69
70
71 func (DefaultFs) Rename(oldpath, newpath string) error {
72 return os.Rename(oldpath, newpath)
73 }
74
75
76 func (DefaultFs) MkdirAll(path string, perm os.FileMode) error {
77 return os.MkdirAll(path, perm)
78 }
79
80
81 func (DefaultFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
82 return os.Chtimes(name, atime, mtime)
83 }
84
85
86 func (DefaultFs) RemoveAll(path string) error {
87 return os.RemoveAll(path)
88 }
89
90
91 func (DefaultFs) Remove(name string) error {
92 return os.Remove(name)
93 }
94
95
96 func (DefaultFs) Getwd() (dir string, err error) {
97 return os.Getwd()
98 }
99
100
101 func (DefaultFs) ReadFile(filename string) ([]byte, error) {
102 return os.ReadFile(filename)
103 }
104
105
106 func (DefaultFs) WriteFile(filename string, data []byte, perm os.FileMode) error {
107 return os.WriteFile(filename, data, perm)
108 }
109
110
111 func (DefaultFs) TempDir(dir, prefix string) (string, error) {
112 return os.MkdirTemp(dir, prefix)
113 }
114
115
116 func (DefaultFs) TempFile(dir, prefix string) (File, error) {
117 file, err := os.CreateTemp(dir, prefix)
118 if err != nil {
119 return nil, err
120 }
121 return &defaultFile{file}, nil
122 }
123
124
125 func (DefaultFs) ReadDir(dirname string) ([]os.FileInfo, error) {
126 entries, err := os.ReadDir(dirname)
127 if err != nil {
128 return nil, err
129 }
130 infos := make([]fs.FileInfo, 0, len(entries))
131 var info os.FileInfo
132 for _, entry := range entries {
133 info, err = entry.Info()
134 if err != nil {
135 return nil, err
136 }
137 infos = append(infos, info)
138 }
139 return infos, nil
140 }
141
142
143 func (DefaultFs) Walk(root string, walkFn filepath.WalkFunc) error {
144 return filepath.Walk(root, walkFn)
145 }
146
147
148 func (f DefaultFs) Chmod(name string, mode os.FileMode) error {
149 return os.Chmod(name, mode)
150 }
151
152
153 type defaultFile struct {
154 file *os.File
155 }
156
157 var _ File = (*defaultFile)(nil)
158
159
160 func (file *defaultFile) Name() string {
161 return file.file.Name()
162 }
163
164
165 func (file *defaultFile) Write(b []byte) (n int, err error) {
166 return file.file.Write(b)
167 }
168
169
170 func (file *defaultFile) WriteString(s string) (int, error) {
171 return file.file.WriteString(s)
172 }
173
174
175 func (file *defaultFile) Sync() error {
176 return file.file.Sync()
177 }
178
179
180 func (file *defaultFile) Close() error {
181 return file.file.Close()
182 }
183
184 func (file *defaultFile) Readdir(n int) ([]os.FileInfo, error) {
185 return file.file.Readdir(n)
186 }
187
188 func (file *defaultFile) Read(b []byte) (n int, err error) {
189 return file.file.Read(b)
190 }
191
192 func (file *defaultFile) Chmod(name string, mode os.FileMode) error {
193 return file.file.Chmod(mode)
194 }
195
View as plain text