archive: move stat-header handling into copy package

Move handling the stat header into `pkg/copy`.  All copy-related should
ideally be located in this package to increase locality and reduce
scattering where possible.

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2020-12-08 16:18:49 +01:00
parent 8472efdbd1
commit c2a5011c0d
4 changed files with 58 additions and 57 deletions

View File

@ -1,13 +1,8 @@
package compat
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/containers/podman/v2/libpod"
"github.com/containers/podman/v2/libpod/define"
@ -71,12 +66,12 @@ func handleHeadAndGet(w http.ResponseWriter, r *http.Request, decoder *schema.De
utils.Error(w, "Not found.", http.StatusNotFound, errors.Wrapf(err, "error stating container path %q", query.Path))
return
}
statHeader, err := fileInfoToDockerStats(info)
statHeader, err := copy.EncodeFileInfo(info)
if err != nil {
utils.Error(w, "Something went wrong", http.StatusInternalServerError, err)
return
}
w.Header().Add("X-Docker-Container-Path-Stat", statHeader)
w.Header().Add(copy.XDockerContainerPathStatHeader, statHeader)
// Our work is done when the user is interested in the header only.
if r.Method == http.MethodHead {
@ -98,42 +93,6 @@ func handleHeadAndGet(w http.ResponseWriter, r *http.Request, decoder *schema.De
}
}
func fileInfoToDockerStats(info *copy.FileInfo) (string, error) {
dockerStats := struct {
Name string `json:"name"`
Size int64 `json:"size"`
Mode os.FileMode `json:"mode"`
ModTime time.Time `json:"mtime"`
LinkTarget string `json:"linkTarget"`
}{
Name: info.Name,
Size: info.Size,
Mode: info.Mode,
ModTime: info.ModTime,
LinkTarget: info.LinkTarget,
}
jsonBytes, err := json.Marshal(&dockerStats)
if err != nil {
return "", errors.Wrap(err, "failed to serialize file stats")
}
buff := bytes.NewBuffer(make([]byte, 0, 128))
base64encoder := base64.NewEncoder(base64.StdEncoding, buff)
_, err = base64encoder.Write(jsonBytes)
if err != nil {
return "", err
}
err = base64encoder.Close()
if err != nil {
return "", err
}
return buff.String(), nil
}
func handlePut(w http.ResponseWriter, r *http.Request, decoder *schema.Decoder, runtime *libpod.Runtime) {
query := struct {
Path string `schema:"path"`