server/libfic/file.go

251 lines
8.0 KiB
Go

package fic
import (
"bufio"
"crypto"
_ "crypto/sha1"
"encoding/hex"
"errors"
"io"
"os"
"path"
"strings"
_ "golang.org/x/crypto/blake2b"
)
// FilesDir stores the location where files to be served are stored.
var FilesDir string = "./FILES/"
// OptionalDigest permits to avoid importation failure if no digest are given.
var OptionalDigest bool = false
// StrongDigest forces the use of BLAKE2b hash in place of SHA1 (or mixed SHA1/BLAKE2b).
var StrongDigest bool = false
// EFile represents a challenge file.
type EFile struct {
Id int64 `json:"id"`
// origin holds the import relative path of the file
origin string
// Path is the location where the file is stored, relatively to FilesDir
Path string `json:"path"`
// IdExercice is the identifier of the underlying challenge
IdExercice int64 `json:"idExercice"`
// Name is the title displayed to players
Name string `json:"name"`
// Checksum stores the cached hash of the file
Checksum []byte `json:"checksum"`
// Size contains the cached size of the file
Size int64 `json:"size"`
}
// GetFiles returns a list of all files living in the database.
func GetFiles() ([]EFile, error) {
if rows, err := DBQuery("SELECT id_file, id_exercice, origin, path, name, cksum, size FROM exercice_files"); err != nil {
return nil, err
} else {
defer rows.Close()
var files = make([]EFile, 0)
for rows.Next() {
var f EFile
if err := rows.Scan(&f.Id, &f.IdExercice, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.Size); err != nil {
return nil, err
}
files = append(files, f)
}
if err := rows.Err(); err != nil {
return nil, err
}
return files, nil
}
}
// GetFile retrieves the file with the given id.
func GetFile(id int) (f EFile, err error) {
err = DBQueryRow("SELECT id_file, origin, path, name, cksum, size FROM exercice_files WHERE id_file = ?", id).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.Size)
return f, err
}
// GetFileByPath retrieves the file that should be found at the given location.
func GetFileByPath(path string) (EFile, error) {
path = strings.TrimPrefix(path, FilesDir)
var f EFile
if err := DBQueryRow("SELECT id_file, origin, path, name, cksum, size FROM exercice_files WHERE path = ?", path).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.Size); err != nil {
return f, err
}
return f, nil
}
// GetFiles returns a list of files coming with the challenge.
func (e Exercice) GetFiles() ([]EFile, error) {
if rows, err := DBQuery("SELECT id_file, origin, path, name, cksum, size FROM exercice_files WHERE id_exercice = ?", e.Id); err != nil {
return nil, err
} else {
defer rows.Close()
var files = make([]EFile, 0)
for rows.Next() {
var f EFile
f.IdExercice = e.Id
if err := rows.Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.Size); err != nil {
return nil, err
}
files = append(files, f)
}
if err := rows.Err(); err != nil {
return nil, err
}
return files, nil
}
}
// GetFileByPath retrieves the file that should be found at the given location, limited to the challenge files.
func (e Exercice) GetFileByPath(path string) (EFile, error) {
path = strings.TrimPrefix(path, FilesDir)
var f EFile
if err := DBQueryRow("SELECT id_file, origin, path, name, cksum, size FROM exercice_files WHERE id_exercice = ? AND path = ?", e.Id, path).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.Size); err != nil {
return f, err
}
return f, 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) {
if digest == nil {
return []byte{}, 0, errors.New("No digest given.")
} else if fi, err := os.Stat(filePath); err != nil {
return []byte{}, fi.Size(), err
} else if fd, err := os.Open(filePath); err != nil {
return []byte{}, fi.Size(), err
} else {
defer fd.Close()
reader := bufio.NewReader(fd)
hash160 := crypto.SHA1.New()
hash512 := crypto.BLAKE2b_512.New()
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:" + hex.EncodeToString(result160) + " & blake2b:" + 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:" + hex.EncodeToString(result160) + " & blake2b:" + 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:" + hex.EncodeToString(result160) + " & blake2b:" + 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: " + hex.EncodeToString(result512) + " vs. given: " + hex.EncodeToString(digest))
}
}
}
return result512, fi.Size(), nil
}
}
// ImportFile registers (ou updates if it already exists in database) the file in database.
func (e Exercice) ImportFile(filePath string, origin string, digest []byte) (interface{}, error) {
if result512, size, err := checkFileHash(filePath, digest); !OptionalDigest && err != nil {
return EFile{}, err
} else {
dPath := strings.TrimPrefix(filePath, FilesDir)
if f, err := e.GetFileByPath(dPath); err != nil {
return e.AddFile(dPath, origin, path.Base(filePath), result512, size)
} else {
// Don't need to update Path and Name, because they are related to dPath
f.IdExercice = e.Id
f.origin = origin
f.Checksum = result512
f.Size = size
if _, err := f.Update(); err != nil {
return nil, err
} else {
return f, nil
}
}
}
}
// AddFile creates and fills a new struct File and registers it into the database.
func (e Exercice) AddFile(path string, origin string, name string, checksum []byte, size int64) (EFile, error) {
if res, err := DBExec("INSERT INTO exercice_files (id_exercice, origin, path, name, cksum, size) VALUES (?, ?, ?, ?, ?, ?)", e.Id, origin, path, name, checksum, size); err != nil {
return EFile{}, err
} else if fid, err := res.LastInsertId(); err != nil {
return EFile{}, err
} else {
return EFile{fid, origin, path, e.Id, name, checksum, size}, nil
}
}
// Update applies modifications back to the database.
func (f EFile) Update() (int64, error) {
if res, err := DBExec("UPDATE exercice_files SET id_exercice = ?, origin = ?, path = ?, name = ?, cksum = ?, size = ? WHERE id_file = ?", f.IdExercice, f.origin, f.Path, f.Name, f.Checksum, f.Size, f.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
// Delete the file from the database.
func (f EFile) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM exercice_files WHERE id_file = ?", f.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
// WipeFiles deletes (only in the database, not on disk) files coming with the challenge.
func (e Exercice) WipeFiles() (int64, error) {
if res, err := DBExec("DELETE FROM exercice_files WHERE id_exercice = ?", e.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
// ClearFiles removes all certificates from database (but not files on disks).
func ClearFiles() (int64, error) {
if res, err := DBExec("DELETE FROM exercice_files"); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
// GetOrigin access the private field origin of the file.
func (f EFile) GetOrigin() string {
return f.origin
}