package sync import ( "encoding/hex" "fmt" "path" "strings" "unicode" "srs.epita.fr/fic-server/libfic" ) func SyncExerciceFiles(i Importer, exercice fic.Exercice) []string { var errs []string if digs, err := getFileContent(i, path.Join(exercice.Path, "files", "DIGESTS.txt")); err != nil { errs = append(errs, fmt.Sprintf("%q: unable to read DIGESTS.txt: %s", path.Base(exercice.Path), err)) } else if _, err := exercice.WipeFiles(); err != nil { errs = append(errs, err.Error()) } else if files, err := i.listDir(path.Join(exercice.Path, "files")); err != nil { errs = append(errs, err.Error()) } else { // Parse DIGESTS.txt 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)) 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)) continue } else { digests[strings.TrimLeftFunc(dsplt[1], unicode.IsSpace)] = hash } } // Import files for _, fname := range files { if _, err := 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)) continue } } } return errs }