diff --git a/repochecker/file-inspector/files.go b/repochecker/file-inspector/files.go index 848ec98b..e1f7dbf0 100644 --- a/repochecker/file-inspector/files.go +++ b/repochecker/file-inspector/files.go @@ -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 diff --git a/repochecker/file-inspector/tarball.go b/repochecker/file-inspector/tarball.go new file mode 100644 index 00000000..20fa7561 --- /dev/null +++ b/repochecker/file-inspector/tarball.go @@ -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 +} diff --git a/repochecker/file-inspector/zip.go b/repochecker/file-inspector/zip.go new file mode 100644 index 00000000..d84ac95c --- /dev/null +++ b/repochecker/file-inspector/zip.go @@ -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 +}