Files
podman/pkg/api/handlers/compat/containers_export.go
Paul Holzinger 3ac5d10098 export: use io.Writer instead of file
This allows use to use STDOUT directly without having to call open
again, also this makes the export API endpoint much more performant
since it no longer needs to copy to a temp file.
I noticed that there was no export API test so I added one.

And lastly opening /dev/stdout will not work on windows.

Fixes #16870

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2022-12-20 14:38:41 +01:00

31 lines
825 B
Go

package compat
import (
"fmt"
"net/http"
"github.com/containers/podman/v4/libpod"
"github.com/containers/podman/v4/pkg/api/handlers/utils"
api "github.com/containers/podman/v4/pkg/api/types"
)
func ExportContainer(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
name := utils.GetName(r)
con, err := runtime.LookupContainer(name)
if err != nil {
utils.ContainerNotFound(w, name, err)
return
}
// set the correct header
w.Header().Set("Content-Type", "application/x-tar")
// NOTE: As described in w.Write() it automatically sets the http code to
// 200 on first write if no other code was set.
if err := con.Export(w); err != nil {
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("failed to export container: %w", err))
return
}
}