repochecker/*-inspector: Refactor file opening
This commit is contained in:
parent
257c594dbe
commit
14f10c91db
4 changed files with 31 additions and 31 deletions
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -10,20 +9,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func InspectFile(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
|
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") {
|
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
|
// 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" {
|
} else if filepath.Ext(file.Name) == ".zip" {
|
||||||
// Check there is more than 1 file in zip
|
// Check there is more than 1 file in zip
|
||||||
errs = append(errs, checkZip(path, file, exceptions)...)
|
errs = append(errs, checkZip(file, exceptions)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
|
@ -7,20 +7,19 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"srs.epita.fr/fic-server/admin/sync"
|
"srs.epita.fr/fic-server/admin/sync"
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkTarball(path string, file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
|
func checkTarball(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
|
||||||
fd, err := os.Open(path)
|
fd, closer, err := sync.GetFile(sync.GlobalImporter, file.GetOrigin())
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
defer fd.Close()
|
defer closer()
|
||||||
|
|
||||||
var rd io.Reader
|
var rd io.Reader
|
||||||
if strings.HasSuffix(file.Name, ".tar.gz") {
|
if strings.HasSuffix(file.Name, ".tar.gz") {
|
||||||
|
|
|
@ -3,19 +3,37 @@ package main
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"srs.epita.fr/fic-server/admin/sync"
|
"srs.epita.fr/fic-server/admin/sync"
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkZip(path string, file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
|
func checkZip(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
|
||||||
r, err := zip.OpenReader(path)
|
fd, closer, err := sync.GetFile(sync.GlobalImporter, file.GetOrigin())
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
defer r.Close()
|
|
||||||
|
|
||||||
if len(r.File) < 2 {
|
if len(r.File) < 2 {
|
||||||
if !exceptions.HasException(":one-file-tarball") {
|
if !exceptions.HasException(":one-file-tarball") {
|
||||||
|
|
|
@ -110,20 +110,12 @@ func CheckTextFile(fd *os.File) (errs []error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func InspectFileForIPAddr(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
|
func InspectFileForIPAddr(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
|
||||||
i, ok := sync.GlobalImporter.(sync.LocalImporter)
|
fd, closer, err := sync.GetFile(sync.GlobalImporter, file.GetOrigin())
|
||||||
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)
|
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
defer fd.Close()
|
defer closer()
|
||||||
|
|
||||||
switch filepath.Ext(file.Name) {
|
switch filepath.Ext(file.Name) {
|
||||||
case ".pcap":
|
case ".pcap":
|
||||||
|
|
Reference in a new issue