aboutsummaryrefslogtreecommitdiff
path: root/pkg/api/handlers/compat/containers_archive.go
blob: 223eb2cd5e58feb5c97a9b69e130b12d577cdc9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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"
	"github.com/containers/podman/v2/pkg/api/handlers/utils"
	"github.com/containers/podman/v2/pkg/copy"
	"github.com/gorilla/schema"
	"github.com/pkg/errors"
)

func Archive(w http.ResponseWriter, r *http.Request) {
	decoder := r.Context().Value("decoder").(*schema.Decoder)
	runtime := r.Context().Value("runtime").(*libpod.Runtime)

	switch r.Method {
	case http.MethodPut:
		handlePut(w, r, decoder, runtime)
	case http.MethodHead, http.MethodGet:
		handleHeadAndGet(w, r, decoder, runtime)
	default:
		utils.Error(w, fmt.Sprintf("unsupported method: %v", r.Method), http.StatusNotImplemented, errors.New(fmt.Sprintf("unsupported method: %v", r.Method)))
	}
}

func handleHeadAndGet(w http.ResponseWriter, r *http.Request, decoder *schema.Decoder, runtime *libpod.Runtime) {
	query := struct {
		Path string `schema:"path"`
	}{}

	err := decoder.Decode(&query, r.URL.Query())
	if err != nil {
		utils.Error(w, "Bad Request.", http.StatusBadRequest, errors.Wrap(err, "couldn't decode the query"))
		return
	}

	if query.Path == "" {
		utils.Error(w, "Bad Request.", http.StatusBadRequest, errors.New("missing `path` parameter"))
		return
	}

	containerName := utils.GetName(r)

	ctr, err := runtime.LookupContainer(containerName)
	if errors.Cause(err) == define.ErrNoSuchCtr {
		utils.Error(w, "Not found.", http.StatusNotFound, errors.Wrap(err, "the container doesn't exists"))
		return
	} else if err != nil {
		utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err)
		return
	}

	source, err := copy.CopyItemForContainer(ctr, query.Path, true, true)
	defer source.CleanUp()
	if err != nil {
		utils.Error(w, "Not found.", http.StatusNotFound, errors.Wrapf(err, "error stating container path %q", query.Path))
		return
	}

	// NOTE: Docker always sets the header.
	info, err := source.Stat()
	if err != nil {
		utils.Error(w, "Not found.", http.StatusNotFound, errors.Wrapf(err, "error stating container path %q", query.Path))
		return
	}
	statHeader, err := fileInfoToDockerStats(info)
	if err != nil {
		utils.Error(w, "Something went wrong", http.StatusInternalServerError, err)
		return
	}
	w.Header().Add("X-Docker-Container-Path-Stat", statHeader)

	// Our work is done when the user is interested in the header only.
	if r.Method == http.MethodHead {
		w.WriteHeader(http.StatusOK)
		return
	}

	// Alright, the users wants data from the container.
	destination, err := copy.CopyItemForWriter(w)
	if err != nil {
		utils.Error(w, "Something went wrong", http.StatusInternalServerError, err)
		return
	}

	w.WriteHeader(http.StatusOK)
	if err := copy.Copy(&source, &destination, false); err != nil {
		utils.Error(w, "Something went wrong", http.StatusInternalServerError, err)
		return
	}
}

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"`
		// TODO handle params below
		NoOverwriteDirNonDir bool `schema:"noOverwriteDirNonDir"`
		CopyUIDGID           bool `schema:"copyUIDGID"`
	}{}

	err := decoder.Decode(&query, r.URL.Query())
	if err != nil {
		utils.Error(w, "Bad Request.", http.StatusBadRequest, errors.Wrap(err, "couldn't decode the query"))
		return
	}

	ctrName := utils.GetName(r)

	ctr, err := runtime.LookupContainer(ctrName)
	if err != nil {
		utils.Error(w, "Not found", http.StatusNotFound, errors.Wrapf(err, "the %s container doesn't exists", ctrName))
		return
	}

	destination, err := copy.CopyItemForContainer(ctr, query.Path, true, false)
	defer destination.CleanUp()
	if err != nil {
		utils.Error(w, "Something went wrong", http.StatusInternalServerError, err)
		return
	}

	source, err := copy.CopyItemForReader(r.Body)
	defer source.CleanUp()
	if err != nil {
		utils.Error(w, "Something went wrong", http.StatusInternalServerError, err)
		return
	}

	w.WriteHeader(http.StatusOK)
	if err := copy.Copy(&source, &destination, false); err != nil {
		utils.Error(w, "Something went wrong", http.StatusInternalServerError, err)
		return
	}
}