12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- package utils
15
+ package storage
16
16
17
17
import (
18
18
"bytes"
@@ -49,9 +49,22 @@ func nameFromDirent(dirent *syscall.Dirent) []byte {
49
49
return name
50
50
}
51
51
52
+ type Storage struct {
53
+ Root string
54
+ }
55
+
56
+ func NewStorage (root string ) Storage {
57
+ if root == "" || os .MkdirAll (filepath .Clean (root ), os .ModePerm ) != nil {
58
+ panic ("unable to assert root storage directory" )
59
+ }
60
+ return Storage {
61
+ Root : root ,
62
+ }
63
+ }
64
+
52
65
// ListDirectory returns sorted slice of item names in given absolute path
53
66
// default sorting is ascending
54
- func ListDirectory (absPath string , ascending bool ) (result []string , err error ) {
67
+ func ( storage Storage ) ListDirectory (absPath string , ascending bool ) (result []string , err error ) {
55
68
defer func () {
56
69
if r := recover (); r != nil {
57
70
if err == nil {
@@ -69,7 +82,7 @@ func ListDirectory(absPath string, ascending bool) (result []string, err error)
69
82
de * syscall.Dirent
70
83
)
71
84
72
- dh , err = os .Open (filepath .Clean (absPath ))
85
+ dh , err = os .Open (filepath .Clean (storage . Root + "/" + absPath ))
73
86
if err != nil {
74
87
return
75
88
}
@@ -135,7 +148,7 @@ func ListDirectory(absPath string, ascending bool) (result []string, err error)
135
148
}
136
149
137
150
// CountFiles returns number of items in directory
138
- func CountFiles (absPath string ) (result int , err error ) {
151
+ func ( storage Storage ) CountFiles (absPath string ) (result int , err error ) {
139
152
defer func () {
140
153
if r := recover (); r != nil {
141
154
if err == nil {
@@ -153,7 +166,7 @@ func CountFiles(absPath string) (result int, err error) {
153
166
de * syscall.Dirent
154
167
)
155
168
156
- dh , err = os .Open (filepath .Clean (absPath ))
169
+ dh , err = os .Open (filepath .Clean (storage . Root + "/" + absPath ))
157
170
if err != nil {
158
171
return
159
172
}
@@ -194,10 +207,10 @@ func CountFiles(absPath string) (result int, err error) {
194
207
}
195
208
196
209
// Exists returns true if absolute path exists
197
- func Exists (absPath string ) (bool , error ) {
210
+ func ( storage Storage ) Exists (absPath string ) (bool , error ) {
198
211
var (
199
212
trusted = new (syscall.Stat_t )
200
- cleaned = filepath .Clean (absPath )
213
+ cleaned = filepath .Clean (storage . Root + "/" + absPath )
201
214
err error
202
215
)
203
216
err = syscall .Stat (cleaned , trusted )
@@ -211,8 +224,8 @@ func Exists(absPath string) (bool, error) {
211
224
}
212
225
213
226
// TouchFile creates files given absolute path if file does not already exist
214
- func TouchFile (absPath string ) error {
215
- cleanedPath := filepath .Clean (absPath )
227
+ func ( storage Storage ) TouchFile (absPath string ) error {
228
+ cleanedPath := filepath .Clean (storage . Root + "/" + absPath )
216
229
if err := os .MkdirAll (filepath .Dir (cleanedPath ), os .ModePerm ); err != nil {
217
230
return err
218
231
}
@@ -225,8 +238,8 @@ func TouchFile(absPath string) error {
225
238
}
226
239
227
240
// ReadFileFully reads whole file given absolute path
228
- func ReadFileFully (absPath string ) ([]byte , error ) {
229
- f , err := os .OpenFile (filepath .Clean (absPath ), os .O_RDONLY , os .ModePerm )
241
+ func ( storage Storage ) ReadFileFully (absPath string ) ([]byte , error ) {
242
+ f , err := os .OpenFile (filepath .Clean (storage . Root + "/" + absPath ), os .O_RDONLY , os .ModePerm )
230
243
if err != nil {
231
244
return nil , err
232
245
}
@@ -245,8 +258,8 @@ func ReadFileFully(absPath string) ([]byte, error) {
245
258
246
259
// WriteFile writes data given absolute path to a file if that file does not
247
260
// already exists
248
- func WriteFile (absPath string , data []byte ) error {
249
- cleanedPath := filepath .Clean (absPath )
261
+ func ( storage Storage ) WriteFile (absPath string , data []byte ) error {
262
+ cleanedPath := filepath .Clean (storage . Root + "/" + absPath )
250
263
if err := os .MkdirAll (filepath .Dir (cleanedPath ), os .ModePerm ); err != nil {
251
264
return err
252
265
}
@@ -262,14 +275,14 @@ func WriteFile(absPath string, data []byte) error {
262
275
}
263
276
264
277
// DeleteFile removes file given absolute path if that file does exists
265
- func DeleteFile (absPath string ) error {
266
- return os .Remove (filepath .Clean (absPath ))
278
+ func ( storage Storage ) DeleteFile (absPath string ) error {
279
+ return os .Remove (filepath .Clean (storage . Root + "/" + absPath ))
267
280
}
268
281
269
282
// UpdateFile rewrite file with data given absolute path to a file if that file
270
283
// exist
271
- func UpdateFile (absPath string , data []byte ) (err error ) {
272
- cleanedPath := filepath .Clean (absPath )
284
+ func ( storage Storage ) UpdateFile (absPath string , data []byte ) (err error ) {
285
+ cleanedPath := filepath .Clean (storage . Root + "/" + absPath )
273
286
var f * os.File
274
287
f , err = os .OpenFile (cleanedPath , os .O_WRONLY | os .O_TRUNC , os .ModePerm )
275
288
if err != nil {
@@ -282,8 +295,8 @@ func UpdateFile(absPath string, data []byte) (err error) {
282
295
283
296
// AppendFile appens data given absolute path to a file, creates it if it does
284
297
// not exist
285
- func AppendFile (absPath string , data []byte ) (err error ) {
286
- cleanedPath := filepath .Clean (absPath )
298
+ func ( storage Storage ) AppendFile (absPath string , data []byte ) (err error ) {
299
+ cleanedPath := filepath .Clean (storage . Root + "/" + absPath )
287
300
err = os .MkdirAll (filepath .Dir (cleanedPath ), os .ModePerm )
288
301
if err != nil {
289
302
return err
0 commit comments