Use pointer receiver more offen

This commit is contained in:
nemunaire 2021-11-22 15:35:07 +01:00
commit c7569b5e54
59 changed files with 688 additions and 672 deletions

View file

@ -45,15 +45,15 @@ type EFile struct {
}
// GetFiles returns a list of all files living in the database.
func GetFiles() ([]EFile, error) {
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)
files := []*EFile{}
for rows.Next() {
var f EFile
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
}
@ -68,16 +68,17 @@ func GetFiles() ([]EFile, error) {
}
// GetFile retrieves the file with the given id.
func GetFile(id int64) (f EFile, err error) {
func GetFile(id int64) (f *EFile, err error) {
f = &EFile{}
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
}
// GetFileByPath retrieves the file that should be found at the given location.
func GetFileByPath(path string) (EFile, error) {
func GetFileByPath(path string) (*EFile, error) {
path = strings.TrimPrefix(path, FilesDir)
var f EFile
f := &EFile{}
if err := DBQueryRow("SELECT id_file, origin, path, id_exercice, name, cksum, size FROM exercice_files WHERE path = ?", path).Scan(&f.Id, &f.origin, &f.Path, &f.IdExercice, &f.Name, &f.Checksum, &f.Size); err != nil {
return f, err
}
@ -86,24 +87,25 @@ func GetFileByPath(path string) (EFile, error) {
}
// GetFileByFilename retrieves the file that should be called so.
func (e Exercice) GetFileByFilename(filename string) (f EFile, err error) {
func (e *Exercice) GetFileByFilename(filename string) (f *EFile, err error) {
filename = path.Base(filename)
f = &EFile{}
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
}
// GetFiles returns a list of files coming with the challenge.
func (e Exercice) GetFiles() ([]EFile, error) {
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)
files := []*EFile{}
for rows.Next() {
var f EFile
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
@ -119,12 +121,12 @@ func (e Exercice) GetFiles() ([]EFile, error) {
}
// 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) {
func (e *Exercice) GetFileByPath(path string) (*EFile, error) {
path = strings.TrimPrefix(path, FilesDir)
var f EFile
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 nil, err
}
return f, nil
@ -182,7 +184,7 @@ func CheckBufferHash(hash160 *hash.Hash, hash512 *hash.Hash, digest []byte) ([]b
// It also returns the file's size.
func checkFileHash(filePath string, digest []byte) (dgst []byte, size int64, err error) {
if digest == nil {
return []byte{}, 0, errors.New("No digest given.")
return []byte{}, 0, errors.New("no digest given")
} else if fi, errr := os.Stat(filePath); errr != nil {
return []byte{}, 0, errr
} else if fd, errr := os.Open(filePath); errr != nil {
@ -204,7 +206,7 @@ func checkFileHash(filePath string, digest []byte) (dgst []byte, size int64, err
}
// 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) {
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 {
@ -229,18 +231,18 @@ func (e Exercice) ImportFile(filePath string, origin string, digest []byte) (int
}
// 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) {
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
return nil, err
} else if fid, err := res.LastInsertId(); err != nil {
return EFile{}, err
return nil, err
} else {
return EFile{fid, origin, path, e.Id, name, checksum, size}, nil
return &EFile{fid, origin, path, e.Id, name, checksum, size}, nil
}
}
// Update applies modifications back to the database.
func (f EFile) Update() (int64, error) {
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 {
@ -288,36 +290,36 @@ func ClearFiles() (int64, error) {
}
// GetOrigin access the private field origin of the file.
func (f EFile) GetOrigin() string {
func (f *EFile) GetOrigin() string {
return f.origin
}
// AddDepend insert a new dependency to a given flag.
func (f EFile) AddDepend(j Flag) (err error) {
if k, ok := j.(FlagKey); ok {
func (f *EFile) AddDepend(j Flag) (err error) {
if k, ok := j.(*FlagKey); ok {
_, err = DBExec("INSERT INTO exercice_files_okey_deps (id_file, id_flag) VALUES (?, ?)", f.Id, k.Id)
} else if m, ok := j.(MCQ); ok {
} else if m, ok := j.(*MCQ); ok {
_, err = DBExec("INSERT INTO exercice_files_omcq_deps (id_file, id_mcq) VALUES (?, ?)", f.Id, m.Id)
} else {
err = errors.New("Dependancy type not implemented for this file.")
err = errors.New("dependancy type not implemented for this file")
}
return
}
// DeleteDepend insert a new dependency to a given flag.
func (f EFile) DeleteDepend(j Flag) (err error) {
if k, ok := j.(FlagKey); ok {
func (f *EFile) DeleteDepend(j Flag) (err error) {
if k, ok := j.(*FlagKey); ok {
_, err = DBExec("DELETE FROM exercice_files_okey_deps WHERE id_file = ? AND id_flag = ?", f.Id, k.Id)
} else if m, ok := j.(MCQ); ok {
} else if m, ok := j.(*MCQ); ok {
_, err = DBExec("DELETE FROM exercice_files_omcq_deps WHERE id_file = ? AND id_mcq = ?", f.Id, m.Id)
} else {
err = errors.New("Dependancy type not implemented for this file.")
err = errors.New("dependancy type not implemented for this file")
}
return
}
// GetDepends retrieve the flag's dependency list.
func (f EFile) GetDepends() ([]Flag, error) {
func (f *EFile) GetDepends() ([]Flag, error) {
var deps = make([]Flag, 0)
if rows, err := DBQuery("SELECT id_flag FROM exercice_files_okey_deps WHERE id_file = ?", f.Id); err != nil {
@ -330,7 +332,7 @@ func (f EFile) GetDepends() ([]Flag, error) {
if err := rows.Scan(&d); err != nil {
return nil, err
}
deps = append(deps, FlagKey{d, f.IdExercice, 0, "", "", "", "", "", false, false, nil, []byte{}, 0})
deps = append(deps, &FlagKey{d, f.IdExercice, 0, "", "", "", "", "", false, false, nil, []byte{}, 0})
}
if err := rows.Err(); err != nil {
return nil, err
@ -347,7 +349,7 @@ func (f EFile) GetDepends() ([]Flag, error) {
if err := rows.Scan(&d); err != nil {
return nil, err
}
deps = append(deps, MCQ{d, f.IdExercice, 0, "", []MCQ_entry{}})
deps = append(deps, &MCQ{d, f.IdExercice, 0, "", []*MCQ_entry{}})
}
if err := rows.Err(); err != nil {
return nil, err
@ -358,11 +360,11 @@ func (f EFile) GetDepends() ([]Flag, error) {
}
// CheckFileOnDisk recalculates the hash of the file on disk.
func (f EFile) CheckFileOnDisk() error {
func (f *EFile) CheckFileOnDisk() error {
if _, size, err := checkFileHash(path.Join(FilesDir, f.Path), f.Checksum); err != nil {
return err
} else if size == 0 {
return errors.New("Empty file!")
return errors.New("empty file")
} else {
return nil
}