repochecker/file-inspector: Handle ZIP archives

This commit is contained in:
nemunaire 2022-11-21 18:39:11 +01:00
parent abb277210c
commit 60d790f8d3
3 changed files with 109 additions and 58 deletions

View File

@ -1,13 +1,7 @@
package main
import (
"archive/tar"
"compress/bzip2"
"compress/gzip"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
@ -24,59 +18,12 @@ func InspectFile(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []erro
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)
}
// Check there is more than 1 file in tarball
errs = append(errs, checkTarball(path, 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)...)
}
return

View File

@ -0,0 +1,71 @@
package main
import (
"archive/tar"
"compress/bzip2"
"compress/gzip"
"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)
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
}

View File

@ -0,0 +1,33 @@
package main
import (
"archive/zip"
"fmt"
"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)
if err != nil {
log.Printf("Unable to open %q: %s", path, err.Error())
return
}
defer r.Close()
if len(r.File) < 2 {
if !exceptions.HasException(":one-file-tarball") {
errs = append(errs, fmt.Errorf("don't make a ZIP archive for one file, use gzip instead"))
}
} else if len(r.File) < 5 && false {
if !exceptions.HasException(":few-files-tarball") {
errs = append(errs, fmt.Errorf("don't make a ZIP archive for so little files (:few-files-tarball)"))
}
} else {
log.Printf("%d files found in %q", len(r.File), file.Name)
}
return
}