diff --git a/controllers/gitrepository_controller.go b/controllers/gitrepository_controller.go index 5e69bf176..ca67aa261 100644 --- a/controllers/gitrepository_controller.go +++ b/controllers/gitrepository_controller.go @@ -341,8 +341,8 @@ func (r *GitRepositoryReconciler) sync(ctx context.Context, repository sourcev1. } defer unlock() - // archive artifact - err = r.Storage.Archive(artifact, tmpGit, "") + // archive artifact and check integrity + err = r.Storage.Archive(artifact, tmpGit, "", true) if err != nil { err = fmt.Errorf("storage archive error: %w", err) return sourcev1.GitRepositoryNotReady(repository, sourcev1.StorageOperationFailedReason, err.Error()), err diff --git a/controllers/storage.go b/controllers/storage.go index 48904b844..a9098dae5 100644 --- a/controllers/storage.go +++ b/controllers/storage.go @@ -113,7 +113,7 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool { } // Archive creates a tar.gz to the artifact path from the given dir excluding the provided file extensions -func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes string) error { +func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes string, integrityCheck bool) error { if excludes == "" { excludes = "jpg,jpeg,gif,png,wmv,flv,tar.gz,zip" } @@ -129,6 +129,23 @@ func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes strin if err != nil { return fmt.Errorf("command '%s' failed: %w", cmd, err) } + + if integrityCheck { + cmd = fmt.Sprintf("gunzip -t %s", artifact.Path) + command = exec.CommandContext(ctx, "/bin/sh", "-c", cmd) + err = command.Run() + if err != nil { + return fmt.Errorf("gzip integrity check failed") + } + + cmd = fmt.Sprintf("tar -tzf %s >/dev/null", artifact.Path) + command = exec.CommandContext(ctx, "/bin/sh", "-c", cmd) + err = command.Run() + if err != nil { + return fmt.Errorf("tar integrity check failed") + } + } + return nil }