84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"archive/tar"
|
|
"compress/bzip2"
|
|
"compress/gzip"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"srs.epita.fr/fic-server/admin/sync"
|
|
"srs.epita.fr/fic-server/libfic"
|
|
)
|
|
|
|
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())
|
|
|
|
// Check there is more than 1 file in tarball
|
|
if filepath.Ext(file.Name) == ".tar" || strings.HasSuffix(file.Name, ".tar.gz") || strings.HasSuffix(file.Name, ".tar.bz2") {
|
|
fd, err := os.Open(path)
|
|
if err != nil {
|
|
log.Printf("Unable to open %q: %s", path, err.Error())
|
|
return
|
|
}
|
|
defer fd.Close()
|
|
|
|
var rd io.Reader
|
|
if strings.HasSuffix(file.Name, ".tar.gz") {
|
|
archive, err := gzip.NewReader(fd)
|
|
if err != nil {
|
|
log.Printf("Unable to uncompress gzip file %q: %s", file.Name, err.Error())
|
|
return
|
|
}
|
|
defer archive.Close()
|
|
rd = archive
|
|
} else if strings.HasSuffix(file.Name, ".tar.bz2") {
|
|
rd = bzip2.NewReader(fd)
|
|
} else {
|
|
rd = fd
|
|
}
|
|
|
|
nbFile := 0
|
|
|
|
tarrd := tar.NewReader(rd)
|
|
for {
|
|
header, err := tarrd.Next()
|
|
if err == io.EOF {
|
|
break
|
|
} else if err != nil {
|
|
log.Printf("An error occurs when analyzing the tarball %q: %s", file.Name, err.Error())
|
|
return
|
|
}
|
|
|
|
info := header.FileInfo()
|
|
if !info.IsDir() {
|
|
nbFile += 1
|
|
}
|
|
}
|
|
|
|
if nbFile < 2 {
|
|
if !exceptions.HasException(":one-file-tarball") {
|
|
errs = append(errs, fmt.Errorf("don't make a tarball for one file"))
|
|
}
|
|
} else if nbFile < 5 && false {
|
|
if !exceptions.HasException(":few-files-tarball") {
|
|
errs = append(errs, fmt.Errorf("don't make a tarball for so little files (:few-files-tarball)"))
|
|
}
|
|
} else {
|
|
log.Printf("%d files found in %q", nbFile, file.Name)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|