summaryrefslogtreecommitdiff
path: root/pkg/copy/fileinfo.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-12-09 10:39:48 -0500
committerGitHub <noreply@github.com>2020-12-09 10:39:48 -0500
commit9abbe0728c5050914168a154622087a4dacd4dfe (patch)
tree6cba60b3f7e8feb4c3062c1865bbe5c9d2c17750 /pkg/copy/fileinfo.go
parent3cd143fc5851d5080c5dcdde5e3b227a163f85d9 (diff)
parenta12323884fd576063ba8f5785a3c9c2e48140c7f (diff)
downloadpodman-9abbe0728c5050914168a154622087a4dacd4dfe.tar.gz
podman-9abbe0728c5050914168a154622087a4dacd4dfe.tar.bz2
podman-9abbe0728c5050914168a154622087a4dacd4dfe.zip
Merge pull request #8663 from vrothberg/run-950
archive endpoint massaging
Diffstat (limited to 'pkg/copy/fileinfo.go')
-rw-r--r--pkg/copy/fileinfo.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/pkg/copy/fileinfo.go b/pkg/copy/fileinfo.go
new file mode 100644
index 000000000..08b4eb377
--- /dev/null
+++ b/pkg/copy/fileinfo.go
@@ -0,0 +1,56 @@
+package copy
+
+import (
+ "encoding/base64"
+ "encoding/json"
+ "net/http"
+ "os"
+ "strings"
+ "time"
+
+ "github.com/pkg/errors"
+)
+
+// XDockerContainerPathStatHeader is the *key* in http headers pointing to the
+// base64 encoded JSON payload of stating a path in a container.
+const XDockerContainerPathStatHeader = "X-Docker-Container-Path-Stat"
+
+// FileInfo describes a file or directory and is returned by
+// (*CopyItem).Stat().
+type FileInfo struct {
+ Name string `json:"name"`
+ Size int64 `json:"size"`
+ Mode os.FileMode `json:"mode"`
+ ModTime time.Time `json:"mtime"`
+ IsDir bool `json:"isDir"`
+ IsStream bool `json:"isStream"`
+ LinkTarget string `json:"linkTarget"`
+}
+
+// EncodeFileInfo serializes the specified FileInfo as a base64 encoded JSON
+// payload. Intended for Docker compat.
+func EncodeFileInfo(info *FileInfo) (string, error) {
+ buf, err := json.Marshal(&info)
+ if err != nil {
+ return "", errors.Wrap(err, "failed to serialize file stats")
+ }
+ return base64.URLEncoding.EncodeToString(buf), nil
+}
+
+// ExtractFileInfoFromHeader extracts a base64 encoded JSON payload of a
+// FileInfo in the http header. If no such header entry is found, nil is
+// returned. Intended for Docker compat.
+func ExtractFileInfoFromHeader(header *http.Header) (*FileInfo, error) {
+ rawData := header.Get(XDockerContainerPathStatHeader)
+ if len(rawData) == 0 {
+ return nil, nil
+ }
+
+ info := FileInfo{}
+ base64Decoder := base64.NewDecoder(base64.URLEncoding, strings.NewReader(rawData))
+ if err := json.NewDecoder(base64Decoder).Decode(&info); err != nil {
+ return nil, err
+ }
+
+ return &info, nil
+}