sync: Extract function that import files from importer
This commit is contained in:
parent
3104bf4e65
commit
3f55845374
3 changed files with 131 additions and 85 deletions
101
libfic/file.go
101
libfic/file.go
|
|
@ -6,6 +6,7 @@ import (
|
|||
_ "crypto/sha1"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"hash"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
|
|
@ -28,19 +29,19 @@ var PlainDigest bool = false
|
|||
|
||||
// EFile represents a challenge file.
|
||||
type EFile struct {
|
||||
Id int64 `json:"id"`
|
||||
Id int64 `json:"id"`
|
||||
// origin holds the import relative path of the file
|
||||
origin string
|
||||
origin string
|
||||
// Path is the location where the file is stored, relatively to FilesDir
|
||||
Path string `json:"path"`
|
||||
Path string `json:"path"`
|
||||
// IdExercice is the identifier of the underlying challenge
|
||||
IdExercice int64 `json:"idExercice"`
|
||||
IdExercice int64 `json:"idExercice"`
|
||||
// Name is the title displayed to players
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name"`
|
||||
// Checksum stores the cached hash of the file
|
||||
Checksum []byte `json:"checksum"`
|
||||
Checksum []byte `json:"checksum"`
|
||||
// Size contains the cached size of the file
|
||||
Size int64 `json:"size"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
// GetFiles returns a list of all files living in the database.
|
||||
|
|
@ -88,7 +89,7 @@ func GetFileByPath(path string) (EFile, error) {
|
|||
func (e Exercice) GetFileByFilename(filename string) (f EFile, err error) {
|
||||
filename = path.Base(filename)
|
||||
|
||||
err = DBQueryRow("SELECT id_file, origin, path, id_exercice, name, cksum, size FROM exercice_files WHERE id_exercice = ? AND origin LIKE ?", e.Id, "%/" + filename).Scan(&f.Id, &f.origin, &f.Path, &f.IdExercice, &f.Name, &f.Checksum, &f.Size)
|
||||
err = DBQueryRow("SELECT id_file, origin, path, id_exercice, name, cksum, size FROM exercice_files WHERE id_exercice = ? AND origin LIKE ?", e.Id, "%/"+filename).Scan(&f.Id, &f.origin, &f.Path, &f.IdExercice, &f.Name, &f.Checksum, &f.Size)
|
||||
|
||||
return
|
||||
}
|
||||
|
|
@ -139,50 +140,66 @@ func minifyHash(hash string) string {
|
|||
}
|
||||
}
|
||||
|
||||
// CheckBufferHash checks if the bufio has the given digest.
|
||||
func CreateHashBuffers() (io.Writer, *hash.Hash, *hash.Hash) {
|
||||
hash160 := crypto.SHA1.New()
|
||||
hash512 := crypto.BLAKE2b_512.New()
|
||||
|
||||
w := io.MultiWriter(hash160, hash512)
|
||||
|
||||
return w, &hash160, &hash512
|
||||
}
|
||||
|
||||
// CheckBufferHash checks if the bufio has the given digest.
|
||||
func CheckBufferHash(hash160 *hash.Hash, hash512 *hash.Hash, digest []byte) ([]byte, error) {
|
||||
result160 := (*hash160).Sum(nil)
|
||||
result512 := (*hash512).Sum(nil)
|
||||
|
||||
if len(digest) != len(result512) {
|
||||
if len(digest) != len(result160) {
|
||||
return []byte{}, errors.New("Digests doesn't match: calculated: sha1:" + minifyHash(hex.EncodeToString(result160)) + " & blake2b:" + minifyHash(hex.EncodeToString(result512)) + " vs. given: " + hex.EncodeToString(digest))
|
||||
} else if StrongDigest {
|
||||
return []byte{}, errors.New("Invalid digests: SHA-1 checksums are no more accepted. Calculated sha1:" + minifyHash(hex.EncodeToString(result160)) + " & blake2b:" + minifyHash(hex.EncodeToString(result512)) + " vs. given: " + hex.EncodeToString(digest))
|
||||
}
|
||||
|
||||
for k := range result160 {
|
||||
if result160[k] != digest[k] {
|
||||
return []byte{}, errors.New("Digests doesn't match: calculated: sha1:" + minifyHash(hex.EncodeToString(result160)) + " & blake2b:" + minifyHash(hex.EncodeToString(result512)) + " vs. given: " + hex.EncodeToString(digest))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for k := range result512 {
|
||||
if result512[k] != digest[k] {
|
||||
return []byte{}, errors.New("Digests doesn't match: calculated: " + minifyHash(hex.EncodeToString(result512)) + " vs. given: " + hex.EncodeToString(digest))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result512, nil
|
||||
}
|
||||
|
||||
// checkFileHash checks if the file at the given filePath has the given digest.
|
||||
// It also returns the file's size.
|
||||
func checkFileHash(filePath string, digest []byte) ([]byte, int64, error) {
|
||||
func checkFileHash(filePath string, digest []byte) (dgst []byte, size int64, err error) {
|
||||
if digest == nil {
|
||||
return []byte{}, 0, errors.New("No digest given.")
|
||||
} else if fi, err := os.Stat(filePath); err != nil {
|
||||
return []byte{}, 0, err
|
||||
} else if fd, err := os.Open(filePath); err != nil {
|
||||
return []byte{}, fi.Size(), err
|
||||
} else if fi, errr := os.Stat(filePath); err != nil {
|
||||
return []byte{}, 0, errr
|
||||
} else if fd, errr := os.Open(filePath); err != nil {
|
||||
return []byte{}, fi.Size(), errr
|
||||
} else {
|
||||
defer fd.Close()
|
||||
size = fi.Size()
|
||||
|
||||
reader := bufio.NewReader(fd)
|
||||
hash160 := crypto.SHA1.New()
|
||||
hash512 := crypto.BLAKE2b_512.New()
|
||||
w, hash160, hash512 := CreateHashBuffers()
|
||||
|
||||
w := io.MultiWriter(hash160, hash512)
|
||||
if _, err := io.Copy(w, reader); err != nil {
|
||||
return []byte{}, fi.Size(), err
|
||||
}
|
||||
result160 := hash160.Sum(nil)
|
||||
result512 := hash512.Sum(nil)
|
||||
|
||||
if len(digest) != len(result512) {
|
||||
if len(digest) != len(result160) {
|
||||
return []byte{}, fi.Size(), errors.New("Digests doesn't match: calculated: sha1:" + minifyHash(hex.EncodeToString(result160)) + " & blake2b:" + minifyHash(hex.EncodeToString(result512)) + " vs. given: " + hex.EncodeToString(digest))
|
||||
} else if StrongDigest {
|
||||
return []byte{}, fi.Size(), errors.New("Invalid digests: SHA-1 checksums are no more accepted. Calculated sha1:" + minifyHash(hex.EncodeToString(result160)) + " & blake2b:" + minifyHash(hex.EncodeToString(result512)) + " vs. given: " + hex.EncodeToString(digest))
|
||||
}
|
||||
|
||||
for k := range result160 {
|
||||
if result160[k] != digest[k] {
|
||||
return []byte{}, fi.Size(), errors.New("Digests doesn't match: calculated: sha1:" + minifyHash(hex.EncodeToString(result160)) + " & blake2b:" + minifyHash(hex.EncodeToString(result512)) + " vs. given: " + hex.EncodeToString(digest))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for k := range result512 {
|
||||
if result512[k] != digest[k] {
|
||||
return []byte{}, fi.Size(), errors.New("Digests doesn't match: calculated: " + minifyHash(hex.EncodeToString(result512)) + " vs. given: " + hex.EncodeToString(digest))
|
||||
}
|
||||
}
|
||||
if _, err = io.Copy(w, bufio.NewReader(fd)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return result512, fi.Size(), nil
|
||||
dgst, err = CheckBufferHash(hash160, hash512, digest)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in a new issue