2022-11-21 17:39:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"fmt"
|
2022-11-21 18:02:37 +00:00
|
|
|
"io"
|
2022-11-21 17:39:11 +00:00
|
|
|
"log"
|
2022-11-23 15:59:00 +00:00
|
|
|
"strings"
|
2022-11-21 17:39:11 +00:00
|
|
|
|
2023-11-22 11:16:53 +00:00
|
|
|
"go.uber.org/multierr"
|
|
|
|
|
2022-11-21 17:39:11 +00:00
|
|
|
"srs.epita.fr/fic-server/admin/sync"
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
|
|
|
)
|
|
|
|
|
2023-11-22 11:16:53 +00:00
|
|
|
func checkZip(file *fic.EFile, exceptions *sync.CheckExceptions) (errs error) {
|
2022-11-21 18:02:37 +00:00
|
|
|
fd, closer, err := sync.GetFile(sync.GlobalImporter, file.GetOrigin())
|
2022-11-21 17:39:11 +00:00
|
|
|
if err != nil {
|
2022-11-21 18:02:37 +00:00
|
|
|
log.Printf("Unable to open %q: %s", file.GetOrigin(), err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
fdat, ok := fd.(io.ReaderAt)
|
|
|
|
if !ok {
|
2022-11-21 18:02:37 +00:00
|
|
|
log.Printf("The current Importer (%t) doesn't allow me to check the archive: %s. Please test-it yourself", sync.GlobalImporter, file.GetOrigin())
|
|
|
|
return
|
2022-11-21 18:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2022-11-21 17:39:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.File) < 2 {
|
|
|
|
if !exceptions.HasException(":one-file-tarball") {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("don't make a ZIP archive for one file, use gzip instead"))
|
2022-11-21 17:39:11 +00:00
|
|
|
}
|
|
|
|
} else if len(r.File) < 5 && false {
|
|
|
|
if !exceptions.HasException(":few-files-tarball") {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("don't make a ZIP archive for so little files (:few-files-tarball)"))
|
2022-11-21 17:39:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("%d files found in %q", len(r.File), file.Name)
|
|
|
|
}
|
|
|
|
|
2022-11-23 15:59:00 +00:00
|
|
|
foundEtcDir := false
|
|
|
|
foundHomeDir := false
|
|
|
|
foundRootDir := false
|
|
|
|
foundVarDir := false
|
|
|
|
for _, file := range r.File {
|
|
|
|
if strings.HasPrefix(file.Name, "etc") {
|
|
|
|
foundEtcDir = true
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(file.Name, "home") {
|
|
|
|
foundHomeDir = true
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(file.Name, "root") {
|
|
|
|
foundRootDir = true
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(file.Name, "var") {
|
|
|
|
foundVarDir = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nbLinuxDirFound := 0
|
|
|
|
if foundEtcDir {
|
|
|
|
nbLinuxDirFound += 1
|
|
|
|
}
|
|
|
|
if foundHomeDir {
|
|
|
|
nbLinuxDirFound += 1
|
|
|
|
}
|
|
|
|
if foundRootDir {
|
|
|
|
nbLinuxDirFound += 1
|
|
|
|
}
|
|
|
|
if foundVarDir {
|
|
|
|
nbLinuxDirFound += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if nbLinuxDirFound > 2 && !exceptions.HasException(":not-a-linux-rootfs") {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("don't use a ZIP archive to store an Unix file system, prefer a tarball (:not-a-linux-rootfs)"))
|
2022-11-23 15:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 17:39:11 +00:00
|
|
|
return
|
|
|
|
}
|