sync: Report custom errors
This commit is contained in:
parent
08ea1bac0d
commit
c78545c18b
17 changed files with 510 additions and 137 deletions
|
@ -22,15 +22,15 @@ func BuildFilesListInto(i Importer, exercice *fic.Exercice, into string) (files
|
|||
|
||||
// Parse DIGESTS.txt
|
||||
if digs, err := GetFileContent(i, path.Join(exercice.Path, into, "DIGESTS.txt")); err != nil {
|
||||
errs = append(errs, fmt.Errorf("%q: unable to read DIGESTS.txt: %w", path.Base(exercice.Path), err))
|
||||
errs = append(errs, NewExerciceError(exercice, fmt.Errorf("unable to read %s: %w", path.Join(into, "DIGESTS.txt"), err)))
|
||||
} else {
|
||||
digests = map[string][]byte{}
|
||||
for nline, d := range strings.Split(digs, "\n") {
|
||||
if dsplt := strings.SplitN(d, " ", 2); len(dsplt) < 2 {
|
||||
errs = append(errs, fmt.Errorf("%q: unable to parse DIGESTS.txt line %d: invalid format", path.Base(exercice.Path), nline+1))
|
||||
errs = append(errs, NewExerciceError(exercice, fmt.Errorf("unable to parse %s line %d: invalid format", path.Join(into, "DIGESTS.txt"), nline+1)))
|
||||
continue
|
||||
} else if hash, err := hex.DecodeString(dsplt[0]); err != nil {
|
||||
errs = append(errs, fmt.Errorf("%q: unable to parse DIGESTS.txt line %d: %w", path.Base(exercice.Path), nline+1, err))
|
||||
errs = append(errs, NewExerciceError(exercice, fmt.Errorf("unable to parse %s line %d: %w", path.Join(into, "DIGESTS.txt"), nline+1, err)))
|
||||
continue
|
||||
} else {
|
||||
digests[strings.TrimFunc(dsplt[1], unicode.IsSpace)] = hash
|
||||
|
@ -40,7 +40,7 @@ func BuildFilesListInto(i Importer, exercice *fic.Exercice, into string) (files
|
|||
|
||||
// Read file list
|
||||
if flist, err := i.listDir(path.Join(exercice.Path, into)); err != nil {
|
||||
errs = append(errs, err)
|
||||
errs = append(errs, NewExerciceError(exercice, err))
|
||||
} else {
|
||||
for _, fname := range flist {
|
||||
if fname == "DIGESTS.txt" || fname == ".gitattributes" {
|
||||
|
@ -79,9 +79,9 @@ func CheckExerciceFilesPresence(i Importer, exercice *fic.Exercice) (files []str
|
|||
|
||||
for _, fname := range flist {
|
||||
if !i.exists(path.Join(exercice.Path, "files", fname)) {
|
||||
errs = append(errs, fmt.Errorf("%q: unable to read file %q: No such file or directory", path.Base(exercice.Path), fname))
|
||||
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("No such file or directory")))
|
||||
} else if _, ok := digests[fname]; !ok {
|
||||
errs = append(errs, fmt.Errorf("%q: unable to import file %q: No digest given", path.Base(exercice.Path), fname))
|
||||
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("unable to import file: No digest given")))
|
||||
} else {
|
||||
files = append(files, fname)
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ func CheckExerciceFilesPresence(i Importer, exercice *fic.Exercice) (files []str
|
|||
|
||||
for fname := range digests {
|
||||
if !i.exists(path.Join(exercice.Path, "files", fname)) {
|
||||
errs = append(errs, fmt.Errorf("%q: unable to read file %q: No such file or directory. Check your DIGESTS.txt for legacy entries.", path.Base(exercice.Path), fname))
|
||||
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("unable to read file: No such file or directory. Check your DIGESTS.txt for legacy entries.")))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,10 +105,21 @@ func CheckExerciceFiles(i Importer, exercice *fic.Exercice) (files []string, err
|
|||
w, hash160, hash512 := fic.CreateHashBuffers()
|
||||
|
||||
if err := GetFile(i, path.Join(exercice.Path, "files", fname), bufio.NewWriter(w)); err != nil {
|
||||
errs = append(errs, fmt.Errorf("%q: unable to read file %q: %w", path.Base(exercice.Path), fname, err))
|
||||
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("unable to read file: %w", err)))
|
||||
continue
|
||||
} else if _, err := fic.CheckBufferHash(hash160, hash512, digests[fname]); err != nil {
|
||||
errs = append(errs, fmt.Errorf("%q: %s: %w", path.Base(exercice.Path), fname, err))
|
||||
errs = append(errs, NewFileError(exercice, fname, err))
|
||||
} else if size, err := getFileSize(i, path.Join(exercice.Path, "files", fname)); err != nil {
|
||||
errs = append(errs, NewFileError(exercice, fname, err))
|
||||
} else {
|
||||
file := exercice.NewDummyFile(path.Join(exercice.Path, "files", fname), getDestinationFilePath(path.Join(exercice.Path, "files", fname)), (*hash512).Sum(nil), size)
|
||||
|
||||
// Call checks hooks
|
||||
for _, h := range hooks.fileHooks {
|
||||
for _, e := range h(file) {
|
||||
errs = append(errs, NewFileError(exercice, fname, e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
files = append(files, fname)
|
||||
|
@ -128,19 +139,21 @@ func SyncExerciceFiles(i Importer, exercice *fic.Exercice) (errs []error) {
|
|||
|
||||
// Import standard files
|
||||
for _, fname := range files {
|
||||
// Enforce file format
|
||||
if path.Ext(fname) == "rar" || path.Ext(fname) == "7z" {
|
||||
errs = append(errs, fmt.Errorf("%q: WARNING %q use a forbidden archive type.", path.Base(exercice.Path), fname))
|
||||
}
|
||||
|
||||
if f, err := i.importFile(path.Join(exercice.Path, "files", fname),
|
||||
func(filePath string, origin string) (interface{}, error) {
|
||||
return exercice.ImportFile(filePath, origin, digests[fname])
|
||||
}); err != nil {
|
||||
errs = append(errs, fmt.Errorf("%q: unable to import file %q: %w", path.Base(exercice.Path), fname, err))
|
||||
errs = append(errs, NewFileError(exercice, fname, err))
|
||||
continue
|
||||
} else if f.(*fic.EFile).Size == 0 {
|
||||
errs = append(errs, fmt.Errorf("%q: WARNING imported file %q is empty!", path.Base(exercice.Path), fname))
|
||||
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("imported file is empty!")))
|
||||
} else {
|
||||
// Call checks hooks
|
||||
for _, h := range hooks.fileHooks {
|
||||
for _, e := range h(f.(*fic.EFile)) {
|
||||
errs = append(errs, NewFileError(exercice, fname, e))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
|
|
Reference in a new issue