repochecker/*-inspector: Refactor file opening

This commit is contained in:
nemunaire 2022-11-21 19:02:37 +01:00
parent 257c594dbe
commit 14f10c91db
4 changed files with 31 additions and 31 deletions

View File

@ -1,7 +1,6 @@
package main
import (
"log"
"path/filepath"
"strings"
@ -10,20 +9,12 @@ import (
)
func InspectFile(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
i, ok := sync.GlobalImporter.(sync.LocalImporter)
if !ok {
log.Printf("Unable to load `file-inspector.so` as the current Importer is not a LocalImporter (%T).", sync.GlobalImporter)
return
}
path := i.GetLocalPath(file.GetOrigin())
if filepath.Ext(file.Name) == ".tar" || strings.HasSuffix(file.Name, ".tar.gz") || strings.HasSuffix(file.Name, ".tar.bz2") {
// Check there is more than 1 file in tarball
errs = append(errs, checkTarball(path, file, exceptions)...)
errs = append(errs, checkTarball(file, exceptions)...)
} else if filepath.Ext(file.Name) == ".zip" {
// Check there is more than 1 file in zip
errs = append(errs, checkZip(path, file, exceptions)...)
errs = append(errs, checkZip(file, exceptions)...)
}
return

View File

@ -7,20 +7,19 @@ import (
"fmt"
"io"
"log"
"os"
"strings"
"srs.epita.fr/fic-server/admin/sync"
"srs.epita.fr/fic-server/libfic"
)
func checkTarball(path string, file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
fd, err := os.Open(path)
func checkTarball(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
fd, closer, err := sync.GetFile(sync.GlobalImporter, file.GetOrigin())
if err != nil {
log.Printf("Unable to open %q: %s", path, err.Error())
log.Printf("Unable to open %q: %s", file.GetOrigin(), err.Error())
return
}
defer fd.Close()
defer closer()
var rd io.Reader
if strings.HasSuffix(file.Name, ".tar.gz") {

View File

@ -3,19 +3,37 @@ package main
import (
"archive/zip"
"fmt"
"io"
"log"
"srs.epita.fr/fic-server/admin/sync"
"srs.epita.fr/fic-server/libfic"
)
func checkZip(path string, file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
r, err := zip.OpenReader(path)
func checkZip(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
fd, closer, err := sync.GetFile(sync.GlobalImporter, file.GetOrigin())
if err != nil {
log.Printf("Unable to open %q: %s", path, err.Error())
log.Printf("Unable to open %q: %s", file.GetOrigin(), err.Error())
return
}
defer closer()
fdat, ok := fd.(io.ReaderAt)
if !ok {
log.Println("The current reader doesn't allow me to check this archive")
}
size, err := sync.GetFileSize(sync.GlobalImporter, file.GetOrigin())
if err != nil {
log.Printf("Unable to calculate size of %q: %s", file.GetOrigin(), err.Error())
return
}
r, err := zip.NewReader(fdat, size)
if err != nil {
log.Printf("Unable to open %q: %s", file.GetOrigin(), err.Error())
return
}
defer r.Close()
if len(r.File) < 2 {
if !exceptions.HasException(":one-file-tarball") {

View File

@ -110,20 +110,12 @@ func CheckTextFile(fd *os.File) (errs []error) {
}
func InspectFileForIPAddr(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
i, ok := sync.GlobalImporter.(sync.LocalImporter)
if !ok {
log.Printf("Unable to load `pcap-inspector.so` as the current Importer is not a LocalImporter (%T).", sync.GlobalImporter)
return
}
path := i.GetLocalPath(file.GetOrigin())
fd, err := os.Open(path)
fd, closer, err := sync.GetFile(sync.GlobalImporter, file.GetOrigin())
if err != nil {
log.Printf("Unable to open %q: %s", path, err.Error())
log.Printf("Unable to open %q: %s", file.GetOrigin(), err.Error())
return
}
defer fd.Close()
defer closer()
switch filepath.Ext(file.Name) {
case ".pcap":