From 65932839099b84fe93ac868a554cc1ee9aac9f3a Mon Sep 17 00:00:00 2001 From: Adphi Date: Mon, 2 Oct 2023 19:45:18 +0200 Subject: [PATCH] cleanup tmp directories when the repository is closed Signed-off-by: Adphi --- pkg/packages/rpm/provider.go | 7 ++++++- pkg/repository/oci_repository.go | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/packages/rpm/provider.go b/pkg/packages/rpm/provider.go index 67cecab..719521d 100644 --- a/pkg/packages/rpm/provider.go +++ b/pkg/packages/rpm/provider.go @@ -56,7 +56,7 @@ func (p *provider) repositoryConfig(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } - _ = repo + defer repo.Close() scheme := "http" if r.TLS != nil { scheme = "https" @@ -79,6 +79,7 @@ func (p *provider) repositoryKey(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + defer repo.Close() rc, err := repo.Open(ctx, RepositoryPublicKey) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -108,6 +109,7 @@ func (p *provider) uploadPackage(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + defer repo.Close() pkg, err := NewPackage(reader, size, repo.Key()) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -133,6 +135,7 @@ func (p *provider) downloadPackage(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + defer repo.Close() if err := repo.ServeFile(w, r, file); err != nil { if errors.Is(err, os.ErrNotExist) { http.Error(w, err.Error(), http.StatusNotFound) @@ -152,6 +155,7 @@ func (p *provider) deletePackage(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + defer repo.Close() if err := repo.Delete(ctx, file); err != nil { if errors.Is(err, os.ErrNotExist) { http.Error(w, err.Error(), http.StatusNotFound) @@ -171,6 +175,7 @@ func (p *provider) repositoryFile(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + defer repo.Close() if err := repo.ServeFile(w, r, file); err != nil { if errors.Is(err, os.ErrNotExist) { http.Error(w, err.Error(), http.StatusNotFound) diff --git a/pkg/repository/oci_repository.go b/pkg/repository/oci_repository.go index cca8aa4..1c4ce8c 100644 --- a/pkg/repository/oci_repository.go +++ b/pkg/repository/oci_repository.go @@ -320,7 +320,7 @@ func (s *storage[T, U]) Key() string { } func (s *storage[T, U]) Close() error { - return nil + return os.RemoveAll(s.tmp) } func (s *storage[T, U]) updateIndex(ctx context.Context, store *file.Store, m ocispec.Manifest, pkgs []T, layers []ocispec.Descriptor) error {