2022-11-21 17:39:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"compress/bzip2"
|
|
|
|
"compress/gzip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
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 checkTarball(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())
|
2022-11-21 17:39:11 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-21 18:02:37 +00:00
|
|
|
defer closer()
|
2022-11-21 17:39:11 +00:00
|
|
|
|
|
|
|
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") {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("don't make a tarball for one file"))
|
2022-11-21 17:39:11 +00:00
|
|
|
}
|
|
|
|
} else if nbFile < 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 tarball for so little files (:few-files-tarball)"))
|
2022-11-21 17:39:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("%d files found in %q", nbFile, file.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|