generated from kurtosis-tech/package-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 8
/
files.go
373 lines (288 loc) · 10.1 KB
/
files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package common
import (
"encoding/json"
"io/fs"
"os"
"path/filepath"
"strings"
)
type diveFileHandler struct{}
// The function returns a new instance of the diveFileHandler struct.
func NewDiveFileHandler() *diveFileHandler {
return &diveFileHandler{}
}
// The `ReadFile` method is responsible for reading the contents of a file given its file path.
func (df *diveFileHandler) ReadFile(filePath string) ([]byte, error) {
fileData, err := os.ReadFile(filePath)
if os.IsNotExist(err) {
_, err := df.OpenFile(filePath, "append|write|create", 0644)
if err != nil {
return nil, WrapMessageToErrorf(ErrNotFound, "Error While Creating File %s", err.Error())
}
return []byte{}, nil
} else if err != nil {
return nil, WrapMessageToErrorf(ErrOpenFile, "Error While Reading File %s", err.Error())
}
return fileData, nil
}
// The `ReadJson` method is responsible for reading a JSON file and unmarshaling its contents into
// the provided object.
func (df *diveFileHandler) ReadJson(fileName string, obj interface{}) error {
var filePath string
if filepath.IsAbs(fileName) {
filePath = fileName
} else {
pwd, err := df.GetPwd()
if err != nil {
return WrapMessageToErrorf(ErrPath, "Failed to get present working dir %s", err.Error())
}
outputDirPath := filepath.Join(pwd, DiveOutFileDirectory, EnclaveName)
err = df.MkdirAll(outputDirPath, 0755)
if err != nil {
return WrapMessageToError(err, "Failed to Create Output Directory")
}
filePath = filepath.Join(outputDirPath, fileName)
}
data, err := df.ReadFile(filePath)
if err != nil {
return err
}
if len(data) != 0 {
if err := json.Unmarshal(data, obj); err != nil {
return WrapMessageToErrorf(ErrDataUnMarshall, " %s object %v", err.Error(), obj)
}
}
return nil
}
// The `ReadAppFile` method is responsible for reading the contents of a file located in the
// application directory.
func (df *diveFileHandler) ReadAppFile(fileName string) ([]byte, error) {
appFilePath, err := df.GetAppDirPathOrAppFilePath(fileName)
if err != nil {
return nil, WrapMessageToErrorf(ErrPath, "%s. path:%s", err, fileName)
}
data, err := df.ReadFile(appFilePath)
if err != nil {
return nil, WrapMessageToErrorf(err, "Invalid file path %s", appFilePath)
}
return data, nil
}
// The `WriteAppFile` method is responsible for writing data to a file located in the application
// directory.
func (df *diveFileHandler) WriteAppFile(fileName string, data []byte) error {
appFileDir, err := df.GetAppDirPathOrAppFilePath("")
if err != nil {
return WrapMessageToErrorf(ErrPath, "%s. path:%s", err, fileName)
}
err = df.MkdirAll(appFileDir, os.ModePerm)
if err != nil {
return WrapMessageToErrorf(ErrWriteFile, "%s. path:%s", err, appFileDir)
}
appFilePath, err := df.GetAppDirPathOrAppFilePath(fileName)
if err != nil {
return WrapMessageToErrorf(err, "Invalid file path %s", appFilePath)
}
file, err := df.OpenFile(appFilePath, "append|write|create|truncate", 0644)
if err != nil {
return WrapMessageToErrorf(ErrOpenFile, "%s . Failed To Open App File %s for write", err, fileName)
}
defer file.Close()
_, err = file.Write(data)
if err != nil {
return WrapMessageToErrorf(ErrWriteFile, "%s . Failed To Write to App File %s", err, fileName)
}
return nil
}
// The `WriteFile` method is responsible for writing data to a file. It takes the file name and the
// data to be written as parameters.
func (df *diveFileHandler) WriteFile(fileName string, data []byte) error {
pwd, err := df.GetPwd()
if err != nil {
return WrapMessageToErrorf(ErrWriteFile, "%s .Failed to Write File %s", err, fileName)
}
outputDirPath := filepath.Join(pwd, DiveOutFileDirectory, EnclaveName)
filePath := filepath.Join(outputDirPath, fileName)
err = df.MkdirAll(outputDirPath, 0755)
if err != nil {
return WrapMessageToError(err, "Failed to Create Output Directory")
}
file, err := df.OpenFile(filePath, "write|append|create|truncate", 0644)
if err != nil {
return WrapMessageToError(err, "Failed")
}
defer file.Close()
_, err = file.Write(data)
if err != nil {
return WrapMessageToErrorf(err, "Failed To Write App File %s", fileName)
}
return nil
}
// The `WriteJson` method is responsible for serializing the provided data object into JSON format and
// writing it to a file.
func (df *diveFileHandler) WriteJson(fileName string, data interface{}) error {
serializedData, err := json.Marshal(data)
if err != nil {
return ErrDataMarshall
}
err = df.WriteFile(fileName, serializedData)
if err != nil {
return ErrWriteFile
}
return nil
}
// The `GetPwd()` function is a method of the `diveFileHandler` struct. It is responsible for
// retrieving the present working directory (PWD) and returning it as a string. It uses the
// `os.Getwd()` function to get the PWD and returns it along with any error that occurred during the
// process.
func (df *diveFileHandler) GetPwd() (string, error) {
pwd, err := os.Getwd()
if err != nil {
return "", ErrPath
}
return pwd, err
}
// The `MkdirAll` function is a method of the `diveFileHandler` struct. It is responsible for creating
// a directory at the specified `dirPath` if it does not already exist.
func (df *diveFileHandler) MkdirAll(dirPath string, permission fs.FileMode) error {
_, err := os.Stat(dirPath)
if os.IsNotExist(err) {
if err := os.MkdirAll(dirPath, permission); err != nil {
return WrapMessageToError(ErrWriteFile, err.Error())
}
} else if err != nil {
return WrapMessageToError(ErrPath, "Failed to check directory existence")
}
return nil
}
// The `OpenFile` method is responsible for opening a file given its file path, file open mode, and
// permission. It uses the `os.OpenFile` function to open the file with the specified mode and
// permission. If there is an error during the file opening process, it returns an error with a wrapped
// message. Otherwise, it returns the opened file.
func (df *diveFileHandler) OpenFile(filePath string, fileOpenMode string, permission int) (*os.File, error) {
mode := parseFileOpenMode(fileOpenMode)
file, err := os.OpenFile(filePath, mode, fs.FileMode(permission))
if err != nil {
return nil, WrapMessageToErrorf(ErrOpenFile, "%s. Failed to Open File %s", err, filePath)
}
return file, nil
}
// The `GetHomeDir()` function is a method of the `diveFileHandler` struct. It is responsible for
// retrieving the user's home directory and returning it as a string. It uses the `os.UserHomeDir()`
// function to get the home directory and returns it along with any error that occurred during the
// process.
func (df *diveFileHandler) GetHomeDir() (string, error) {
uhd, err := os.UserHomeDir()
if err != nil {
return "", WrapMessageToError(ErrPath, err.Error())
}
return uhd, err
}
// The function `parseFileOpenMode` takes a string representing file open modes separated by "|" and
// returns the corresponding integer value.
func parseFileOpenMode(fileOpenMode string) int {
modes := strings.Split(fileOpenMode, "|")
var mode int
for _, m := range modes {
switch strings.TrimSpace(m) {
case "append":
mode |= os.O_APPEND
case "create":
mode |= os.O_CREATE
case "truncate":
mode |= os.O_TRUNC
case "write":
mode |= os.O_WRONLY
case "readwrite":
mode |= os.O_RDWR
case "read":
mode |= os.O_RDONLY
}
}
return mode
}
// The `RemoveFile` function is a method of the `diveFileHandler` struct. It is responsible for
// removing a file from the file system.
func (df *diveFileHandler) RemoveFile(fileName string) error {
pwd, err := df.GetPwd()
if err != nil {
return WrapMessageToErrorf(err, "Failed To Remove File")
}
filePath := filepath.Join(pwd, fileName)
_, err = os.Stat(filePath)
if err != nil {
return WrapMessageToErrorf(ErrNotExistsFile, "%s. PATH:%s", err, filePath)
}
err = os.Remove(filePath)
if err != nil {
return WrapMessageToErrorf(ErrPath, "%s.Failed To Remove File %s", err, filePath)
}
return nil
}
// The `RemoveFiles` function is a method of the `diveFileHandler` struct. It is responsible for
// removing multiple files from the file system.
func (df *diveFileHandler) RemoveFiles(fileNames []string) error {
pwd, err := df.GetPwd()
if err != nil {
return WrapMessageToErrorf(ErrPath, "Failed To Remove File")
}
for _, fileName := range fileNames {
filePath := filepath.Join(pwd, fileName)
_, err = os.Stat(filePath)
if err == nil {
err = os.Remove(filePath)
if err != nil {
return WrapMessageToErrorf(ErrInvalidFile, "%s Failed To Remove File %s", err, filePath)
}
}
}
return nil
}
// The `RemoveDir` function is a method of the `diveFileHandler` struct. It is responsible for
// removing output directories from the file system.
func (df *diveFileHandler) RemoveDir(enclaveName string) error {
pwd, err := df.GetPwd()
if err != nil {
return WrapMessageToErrorf(ErrPath, "Failed To Remove Directory")
}
dirPath := filepath.Join(pwd, DiveOutFileDirectory, enclaveName)
_, err = os.Stat(dirPath)
if err == nil {
err = os.RemoveAll(dirPath)
if err != nil {
return WrapMessageToErrorf(ErrInvalidFile, "%s Failed To Remove Directory %s", err, enclaveName)
}
}
return nil
}
// The `RemoveAllDir` function is a method of the `diveFileHandler` struct. It is responsible for
// removing all output directories from the file system.
func (df *diveFileHandler) RemoveAllDir() error {
pwd, err := df.GetPwd()
if err != nil {
return WrapMessageToErrorf(ErrPath, "Failed To Remove Directory")
}
dirPath := filepath.Join(pwd, DiveOutFileDirectory)
_, err = os.Stat(dirPath)
if err == nil {
err = os.RemoveAll(dirPath)
if err != nil {
return WrapMessageToErrorf(ErrInvalidFile, "%s Failed To Remove Output Directory", err)
}
}
return nil
}
// The `GetAppDirPathOrAppFilePath` function is a method of the `diveFileHandler` struct. It is
// responsible for returning the file path of a file located in the application directory.
func (df *diveFileHandler) GetAppDirPathOrAppFilePath(fileName string) (string, error) {
var path string
uhd, err := df.GetHomeDir()
if err != nil {
return "", WrapMessageToErrorf(err, "Failed To Write App File %s", fileName)
}
if fileName == "" {
path = filepath.Join(uhd, DiveAppDir)
} else {
path = filepath.Join(uhd, DiveAppDir, fileName)
}
return path, nil
}