-
Notifications
You must be signed in to change notification settings - Fork 3
/
bag.go
431 lines (366 loc) · 10.5 KB
/
bag.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package go_bagit
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
)
const (
dirMode os.FileMode = 0o755
fileMode os.FileMode = 0o644
)
type Bag struct {
Path string
Name string
AbsPath string
Payload Payload
BagInfo TagSet
BagIt TagSet
Manifests ManifestRefs
TagManifests ManifestRefs
}
func GetExistingBag(path string) (Bag, error) {
bag := Bag{}
//check directory exists
if err := directoryExists(path); err != nil {
return bag, err
}
//set path
bag.Path = path
//set absolute path
var err error
bag.AbsPath, err = getABS(bag.Path)
if err != nil {
return bag, err
}
//set name
pathSplit := strings.Split(bag.AbsPath, string(os.PathSeparator))
bag.Name = pathSplit[len(pathSplit)-1]
//get Bagit.txt
bag.BagIt, err = NewTagSet("bagit.txt", bag.Path)
if err != nil {
return bag, err
}
//get Baginfo.txt
bag.BagInfo, err = NewTagSet("bag-info.txt", bag.Path)
if err != nil {
return bag, err
}
//get Manifests
bag.Manifests, err = GetManifests(bag.Path)
if err != nil {
return bag, err
}
//get Tag Manifests
bag.TagManifests, err = GetTagManifests(bag.Path)
if err != nil {
return bag, err
}
bag.Payload = make(map[string]os.FileInfo)
if err := loadPayload(&bag); err != nil {
return bag, err
}
return bag, nil
}
func (b Bag) ListPayloadFiles() {
for _, fi := range b.Payload {
fmt.Println(fi.Name())
}
}
func (b Bag) GetAbsolutePath() (string, error) {
abs, err := filepath.Abs(b.Path)
if err != nil {
return abs, err
}
return abs, nil
}
func (b Bag) String() string {
pathSplit := strings.Split(b.AbsPath, string(os.PathSeparator))
bagPath := strings.Join(pathSplit[:len(pathSplit)-1], string(os.PathSeparator))
return fmt.Sprintf("%s: %s\n", b.Name, bagPath)
}
type getFilesOrDirsParams struct {
Location string
Matcher *regexp.Regexp
FindFiles bool
ReturnFirst bool
}
func (b Bag) ValidateBag(fast bool, complete bool) error {
errs := []error{}
storedOxum, err := GetOxum(b.Path)
if err != nil {
log.Printf("- ERROR - %s", err.Error())
return err
}
err = ValidateOxum(b.Path, storedOxum)
if err != nil {
log.Printf("- ERROR - %s", err.Error())
return err
}
if fast {
log.Printf("- INFO - %s valid according to Payload Oxum", b.Path)
return nil
}
//validate ant manifest files
bagFiles, err := os.ReadDir(b.Path)
if err != nil {
return err
}
dataFiles := map[string]bool{}
for _, bagFile := range bagFiles {
if tagmanifestPtn.MatchString(bagFile.Name()) {
manifestLoc := filepath.Join(b.Path, bagFile.Name())
_, e := ValidateManifest(manifestLoc, complete)
if len(e) > 0 {
errs = append(errs, e...)
errorMsgs := gatherErrors(errs, b.Path)
return errors.New(errorMsgs)
}
}
if manifestPtn.MatchString(bagFile.Name()) {
manifestLoc := filepath.Join(b.Path, bagFile.Name())
entries, e := ValidateManifest(manifestLoc, complete)
if len(e) > 0 {
errs = append(errs, e...)
}
for path := range entries {
dataFiles[path] = true
}
}
}
dataDirName := filepath.Join(b.Path, "data")
if err := filepath.WalkDir(dataDirName, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() || dataDirName == path {
return nil
}
rel, err := filepath.Rel(b.Path, path)
if err != nil {
return err
}
if _, ok := dataFiles[rel]; !ok {
return fmt.Errorf("%s exists on filesystem but is not in the manifest", rel)
}
return nil
}); err != nil {
errs = append(errs, err)
}
if len(errs) == 0 {
log.Printf("- INFO - %s is valid", b.Name)
return nil
}
errorMsgs := gatherErrors(errs, b.Name)
return errors.New(errorMsgs)
}
func gatherErrors(errs []error, bagLocation string) string {
errorMsgs := fmt.Sprintf("- ERROR - %s is invalid: Bag validation failed: ", bagLocation)
for i, e := range errs {
errorMsgs = errorMsgs + e.Error()
if i < len(errs)-1 {
errorMsgs = errorMsgs + "; "
}
}
log.Println(errorMsgs)
return errorMsgs
}
func CreateBag(inputDir string, algorithm string, numProcesses int) (Bag, error) {
//check that input exists and is a directory
if err := directoryExists(inputDir); err != nil {
return Bag{}, err
}
log.Printf("- INFO - Creating Bag for directory %s", inputDir)
//create a slice of files
filesToBag, err := os.ReadDir(inputDir)
if err != nil {
return Bag{}, err
}
//check there is at least one file to be bagged.
if len(filesToBag) < 1 {
errMsg := fmt.Errorf("could not create a bag, no files present in %s", inputDir)
log.Println("- ERROR -", errMsg)
return Bag{}, errMsg
}
//create a data directory for payload
log.Println("- INFO - Creating data directory")
dataDirName := filepath.Join(inputDir, "data")
if err := os.Mkdir(dataDirName, dirMode); err != nil {
log.Println("- ERROR -", err)
return Bag{}, err
}
//move the payload files into data dir
for _, file := range filesToBag {
originalLocation := filepath.Join(inputDir, file.Name())
newLocation := filepath.Join(dataDirName, file.Name())
log.Printf("- INFO - Moving %s to %s", originalLocation, newLocation)
if err := os.Rename(originalLocation, newLocation); err != nil {
log.Println("- ERROR -", err.Error())
return Bag{}, err
}
if err := os.Chmod(newLocation, fileMode); err != nil {
log.Println("- WARNING -", err.Error())
}
}
//Generate the manifest
if err := CreateManifest("manifest", inputDir, algorithm, numProcesses); err != nil {
return Bag{}, err
}
//Generate and serialize bagit.txt
log.Println("- INFO - Creating bagit.txt")
bagit := CreateBagit()
bagitBytes := bagit.GetTagSetAsByteSlice()
if err = os.WriteFile(filepath.Join(inputDir, bagit.Filename), bagitBytes, fileMode); err != nil {
return Bag{}, err
}
bagit.Path = inputDir //this can be deleted
//generate and serialize bag-info.txt
log.Println("- INFO - Creating bag-info.txt")
//get the oxum
oxum, err := CalculateOxum(inputDir)
if err != nil {
return Bag{}, err
}
bagInfo := CreateBagInfo(timeNow())
bagInfo.Tags[StandardTags.PayloadOxum] = oxum.String()
bagInfo.Path = inputDir
bagInfoBytes := bagInfo.GetTagSetAsByteSlice()
if err = os.WriteFile(filepath.Join(inputDir, bagInfo.Filename), bagInfoBytes, fileMode); err != nil {
return Bag{}, err
}
//Generate TagManifest
if err := CreateTagManifest(inputDir, algorithm, numProcesses); err != nil {
return Bag{}, err
}
//you are done
bag, err := GetExistingBag(inputDir)
if err != nil {
return bag, err
}
return bag, err
}
// Adds a file to the bag root and registers it in the tagmanifest file
func (b Bag) AddFileToBagRoot(file string) error {
//check if source file is valid
if err := fileExists(file); err != nil {
return err
}
//check if there is already a source file with the same name in the bag
sourceFileInfo, err := os.Stat(file)
if err != nil {
return err
}
targetFilePath := filepath.Join(b.Path, sourceFileInfo.Name())
log.Println(targetFilePath)
err = fileExists(targetFilePath)
if err == nil {
return fmt.Errorf("- ERROR - cannot create target file %s already exists", targetFilePath)
}
//create the target file
targetFile, err := os.Create(targetFilePath)
if err != nil {
return err
}
defer targetFile.Close()
//read the source file
sourceFile, err := os.Open(file)
if err != nil {
return err
}
defer sourceFile.Close()
//write the contents of the source file to the target file
log.Printf("- INFO - copying file %s to %s", file, b.Path)
if _, err := io.Copy(targetFile, sourceFile); err != nil {
return err
}
//ensure the new file exists
if err := fileExists(targetFilePath); err != nil {
return err
}
targetFile.Close()
//locate the tagmanifest
tagmanifest, err := FindFileInBag(b.Path, regexp.MustCompile("tagmanifest"))
if err != nil {
return err
}
//append new file to tagmanifest
log.Println(targetFilePath, b.Path, tagmanifest)
if err := appendToTagManifest(targetFilePath, b.Path, filepath.Base(tagmanifest)); err != nil {
return err
}
return nil
}
func GetFilesInBag(bagLocation string) ([]string, error) {
return getFilesOrDirsInBag(getFilesOrDirsParams{bagLocation, regexp.MustCompile(`.*`), true, false})
}
func FindFileInBag(bagLocation string, matcher *regexp.Regexp) (string, error) {
results, err := getFilesOrDirsInBag(getFilesOrDirsParams{bagLocation, matcher, true, true})
if err != nil {
return "", err
}
if len(results) == 0 {
return "", fmt.Errorf("Could not locate file pattern in bag")
}
return results[0], nil
}
func FindFilesInBag(bagLocation string, matcher *regexp.Regexp) ([]string, error) {
return getFilesOrDirsInBag(getFilesOrDirsParams{bagLocation, matcher, true, false})
}
func GetDirsInBag(bagLocation string) ([]string, error) {
return getFilesOrDirsInBag(getFilesOrDirsParams{bagLocation, regexp.MustCompile(`.*`), false, false})
}
func FindDirInBag(bagLocation string, matcher *regexp.Regexp) (string, error) {
results, err := getFilesOrDirsInBag(getFilesOrDirsParams{bagLocation, matcher, false, true})
if err != nil {
return "", err
}
if len(results) == 0 {
return "", fmt.Errorf("Could not locate directory pattern in bag")
}
return results[0], nil
}
func FindDirsInBag(bagLocation string, matcher *regexp.Regexp) ([]string, error) {
return getFilesOrDirsInBag(getFilesOrDirsParams{bagLocation, matcher, false, false})
}
// getFilesOrDirsInBag returns a slice of strings of matching files or directories.
// What is returned is controlled by the findFiles boolean.
// findFiles = true --> return matching files
// findFiles = false --> return matching directories
//
// How many matches are returned is determined by the returnFirst boolean.
// returnFirst = true --> halts search and returns with first match
// returnFirst = false --> returns all matching files or directories
func getFilesOrDirsInBag(params getFilesOrDirsParams) ([]string, error) {
results := []string{}
bagLocation := params.Location
matcher := params.Matcher
findFiles := params.FindFiles
returnFirst := params.ReturnFirst
err := filepath.Walk(bagLocation,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// if looking for files, but this is a directory, move on...
if findFiles && info.IsDir() {
return nil
}
// if looking for directories, but this is NOT a directory, move on...
if !findFiles && !info.IsDir() {
return nil
}
// OK, we found something that we might be looking for...
if matcher.MatchString(path) {
results = append(results, path)
if returnFirst {
return filepath.SkipAll
}
}
return nil
})
if err != nil {
return nil, err
}
return results, nil
}