sync: Use errors instead of string to report

This commit is contained in:
nemunaire 2022-07-02 00:01:05 +02:00
parent d8943ba1f3
commit b0129e5239
9 changed files with 186 additions and 178 deletions

View file

@ -14,7 +14,7 @@ import (
"srs.epita.fr/fic-server/libfic"
)
func BuildFilesListInto(i Importer, exercice *fic.Exercice, into string) (files []string, digests map[string][]byte, errs []string) {
func BuildFilesListInto(i Importer, exercice *fic.Exercice, into string) (files []string, digests map[string][]byte, errs []error) {
// If no files directory, don't display error
if !i.exists(path.Join(exercice.Path, into)) {
return
@ -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.Sprintf("%q: unable to read DIGESTS.txt: %s", path.Base(exercice.Path), err))
errs = append(errs, fmt.Errorf("%q: unable to read DIGESTS.txt: %w", path.Base(exercice.Path), 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.Sprintf("%q: unable to parse DIGESTS.txt line %d: invalid format", path.Base(exercice.Path), nline+1))
errs = append(errs, fmt.Errorf("%q: unable to parse DIGESTS.txt line %d: invalid format", path.Base(exercice.Path), nline+1))
continue
} else if hash, err := hex.DecodeString(dsplt[0]); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to parse DIGESTS.txt line %d: %s", path.Base(exercice.Path), nline+1, err))
errs = append(errs, fmt.Errorf("%q: unable to parse DIGESTS.txt line %d: %w", path.Base(exercice.Path), 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.Error())
errs = append(errs, err)
} else {
for _, fname := range flist {
if fname == "DIGESTS.txt" || fname == ".gitattributes" {
@ -73,15 +73,15 @@ func BuildFilesListInto(i Importer, exercice *fic.Exercice, into string) (files
}
// CheckExerciceFilesPresence limits remote checks to presence, don't get it to check digest.
func CheckExerciceFilesPresence(i Importer, exercice *fic.Exercice) (files []string, errs []string) {
func CheckExerciceFilesPresence(i Importer, exercice *fic.Exercice) (files []string, errs []error) {
flist, digests, berrs := BuildFilesListInto(i, exercice, "files")
errs = append(errs, berrs...)
for _, fname := range flist {
if !i.exists(path.Join(exercice.Path, "files", fname)) {
errs = append(errs, fmt.Sprintf("%q: unable to read file %q: No such file or directory", path.Base(exercice.Path), fname))
errs = append(errs, fmt.Errorf("%q: unable to read file %q: No such file or directory", path.Base(exercice.Path), fname))
} else if _, ok := digests[fname]; !ok {
errs = append(errs, fmt.Sprintf("%q: unable to import file %q: No digest given", path.Base(exercice.Path), fname))
errs = append(errs, fmt.Errorf("%q: unable to import file %q: No digest given", path.Base(exercice.Path), fname))
} 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.Sprintf("%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, 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))
}
}
@ -97,7 +97,7 @@ func CheckExerciceFilesPresence(i Importer, exercice *fic.Exercice) (files []str
}
// CheckExerciceFiles checks that remote files have the right digest.
func CheckExerciceFiles(i Importer, exercice *fic.Exercice) (files []string, errs []string) {
func CheckExerciceFiles(i Importer, exercice *fic.Exercice) (files []string, errs []error) {
flist, digests, berrs := BuildFilesListInto(i, exercice, "files")
errs = append(errs, berrs...)
@ -105,10 +105,10 @@ 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.Sprintf("%q: unable to read file %q: %s", path.Base(exercice.Path), fname, err))
errs = append(errs, fmt.Errorf("%q: unable to read file %q: %w", path.Base(exercice.Path), fname, err))
continue
} else if _, err := fic.CheckBufferHash(hash160, hash512, digests[fname]); err != nil {
errs = append(errs, fmt.Sprintf("%q: %s: %s", path.Base(exercice.Path), fname, err))
errs = append(errs, fmt.Errorf("%q: %s: %w", path.Base(exercice.Path), fname, err))
}
files = append(files, fname)
@ -118,9 +118,9 @@ func CheckExerciceFiles(i Importer, exercice *fic.Exercice) (files []string, err
// SyncExerciceFiles reads the content of files/ directory and import it as EFile for the given challenge.
// It takes care of DIGESTS.txt and ensure imported files match.
func SyncExerciceFiles(i Importer, exercice *fic.Exercice) (errs []string) {
func SyncExerciceFiles(i Importer, exercice *fic.Exercice) (errs []error) {
if _, err := exercice.WipeFiles(); err != nil {
errs = append(errs, err.Error())
errs = append(errs, err)
}
files, digests, berrs := BuildFilesListInto(i, exercice, "files")
@ -130,17 +130,17 @@ func SyncExerciceFiles(i Importer, exercice *fic.Exercice) (errs []string) {
for _, fname := range files {
// Enforce file format
if path.Ext(fname) == "rar" || path.Ext(fname) == "7z" {
errs = append(errs, fmt.Sprintf("%q: WARNING %q use a forbidden archive type.", path.Base(exercice.Path), fname))
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.Sprintf("%q: unable to import file %q: %s", path.Base(exercice.Path), fname, err))
errs = append(errs, fmt.Errorf("%q: unable to import file %q: %w", path.Base(exercice.Path), fname, err))
continue
} else if f.(*fic.EFile).Size == 0 {
errs = append(errs, fmt.Sprintf("%q: WARNING imported file %q is empty!", path.Base(exercice.Path), fname))
errs = append(errs, fmt.Errorf("%q: WARNING imported file %q is empty!", path.Base(exercice.Path), fname))
}
}
return