Files
podman/pkg/api/handlers/compat/images_prune.go
Miloslav Trmač 4c3027c149 Make most of libpod, and everything that relies on it, non-darwin
Require (linux || freebsd), because the code already does that, in practice.
This just means macOS users of IDEs aren't hit with thousands of compilation
errors (and then the IDE can open an Linux-specific file and then process it
under the Linux assumption, which works much better).

This commit ONLY replaces
	//go:build !remote
with
	//go:build !remote && (linux || freebsd)

and is split from the rest to allow mechanically verifying that fact,
and focusing a review on the other kinds of changes.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2026-02-27 22:18:36 +01:00

76 lines
2.0 KiB
Go

//go:build !remote && (linux || freebsd)
package compat
import (
"bytes"
"errors"
"fmt"
"net/http"
"github.com/containers/podman/v6/libpod"
"github.com/containers/podman/v6/pkg/api/handlers"
"github.com/containers/podman/v6/pkg/api/handlers/utils"
api "github.com/containers/podman/v6/pkg/api/types"
"github.com/containers/podman/v6/pkg/domain/entities"
"github.com/containers/podman/v6/pkg/domain/infra/abi"
"github.com/containers/podman/v6/pkg/util"
dockerImage "github.com/docker/docker/api/types/image"
)
func PruneImages(w http.ResponseWriter, r *http.Request) {
var filters []string
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
filterMap, err := util.PrepareFilters(r)
if err != nil {
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("failed to parse parameters for %s: %w", r.URL.String(), err))
return
}
for k, v := range *filterMap {
for _, val := range v {
filters = append(filters, fmt.Sprintf("%s=%s", k, val))
}
}
imageEngine := abi.ImageEngine{Libpod: runtime}
pruneOptions := entities.ImagePruneOptions{Filter: filters}
imagePruneReports, err := imageEngine.Prune(r.Context(), pruneOptions)
if err != nil {
utils.InternalServerError(w, err)
return
}
idr := make([]dockerImage.DeleteResponse, 0, len(imagePruneReports))
var reclaimedSpace uint64
var errorMsg bytes.Buffer
for _, p := range imagePruneReports {
if p.Err != nil {
// Docker stops on first error vs. libpod which keeps going. Given API constraints, concatenate all errors
// and return that string.
errorMsg.WriteString(p.Err.Error())
errorMsg.WriteString("; ")
continue
}
idr = append(idr, dockerImage.DeleteResponse{
Deleted: p.Id,
})
reclaimedSpace += p.Size
}
if errorMsg.Len() > 0 {
utils.InternalServerError(w, errors.New(errorMsg.String()))
return
}
payload := handlers.ImagesPruneReport{
PruneReport: dockerImage.PruneReport{
ImagesDeleted: idr,
SpaceReclaimed: reclaimedSpace,
},
}
utils.WriteResponse(w, http.StatusOK, payload)
}