-
Notifications
You must be signed in to change notification settings - Fork 50
/
files.go
275 lines (238 loc) · 7.27 KB
/
files.go
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2022 Canonical Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"strconv"
"strings"
"time"
)
// PushOptions contains the options for a call to Push.
type PushOptions struct {
// Source is the source of data to write (required).
Source io.Reader
// Path indicates the absolute path of the file in the destination
// machine (required).
Path string
// MakeDirs, if true, will create any non-existing directories in the path
// to the remote file. If false, the default, the call to Push will
// fail if any non-existing directory is found on the remote path.
MakeDirs bool
// Permissions indicates the mode of the file in the destination machine.
// Defaults to 0644. Note that, when used together with MakeDirs, the
// directories that might be created will not use this mode, but 0755.
Permissions string
// UserID indicates the user ID of the owner for the file in the destination
// machine. When used together with MakeDirs, the directories that might be
// created will also be owned by this user.
UserID *int
// User indicates the name of the owner user for the file in the destination
// machine. When used together with MakeDirs, the directories that might be
// created will also be owned by this user.
User string
// GroupID indicates the ID of the owner group for the file in the
// destination machine. When used together with MakeDirs, the directories
// that might be created will also be owned by this group.
GroupID *int
// Group indicates the name of the owner group for the file in the
// destination machine. When used together with MakeDirs, the directories
// that might be created will also be owned by this group.
Group string
}
// PullOptions contains the options for a call to Pull.
type PullOptions struct {
// Path indicates the absolute path of the file in the remote system
// (required).
Path string
}
// PullResult contains information about the result of a call to Pull.
type PullResult struct {
// Reader is an io.ReadCloser for the file retrieved from the remote system.
Reader io.ReadCloser
// Size is the length in bytes of the file retrieved from the remote system.
Size int64
}
type writeFilesPayload struct {
Action string `json:"action"`
Files []writeFilesItem `json:"files"`
}
type writeFilesItem struct {
Path string `json:"path"`
MakeDirs bool `json:"make-dirs"`
Permissions string `json:"permissions"`
UserID *int `json:"user-id"`
User string `json:"user"`
GroupID *int `json:"group-id"`
Group string `json:"group"`
}
type fileResult struct {
Path string `json:"path"`
Error *errorResult `json:"error,omitempty"`
}
type errorResult struct {
Message string `json:"message"`
Kind string `json:"kind,omitempty"`
Value interface{} `json:"value,omitempty"`
}
// Push writes content to a path on the remote system.
func (client *Client) Push(opts *PushOptions) error {
// Buffer for multipart header/footer
var b bytes.Buffer
mw := multipart.NewWriter(&b)
// Encode metadata part of the header
part, err := mw.CreatePart(textproto.MIMEHeader{
"Content-Type": {"application/json"},
"Content-Disposition": {`form-data; name="request"`},
})
if err != nil {
return err
}
payload := writeFilesPayload{
Action: "write",
Files: []writeFilesItem{{
Path: opts.Path,
MakeDirs: opts.MakeDirs,
Permissions: opts.Permissions,
UserID: opts.UserID,
User: opts.User,
GroupID: opts.GroupID,
Group: opts.Group,
}},
}
if err = json.NewEncoder(part).Encode(&payload); err != nil {
return err
}
// Encode file part of the header
part, err = mw.CreatePart(textproto.MIMEHeader{
"Content-Type": {"application/octet-stream"},
"Content-Disposition": {fmt.Sprintf(`form-data; name="files"; filename=%q`, opts.Path)},
})
if err != nil {
return err
}
header := b.String()
// Encode multipart footer
b.Reset()
mw.Close()
footer := b.String()
body := io.MultiReader(strings.NewReader(header), opts.Source, strings.NewReader(footer))
var result []fileResult
_, err = client.doSync("POST", "/v1/files", nil, map[string]string{
"Content-Type": mw.FormDataContentType(),
}, body, &result)
if err != nil {
return err
}
if len(result) != 1 {
return fmt.Errorf("expected exactly one result from API, got %d", len(result))
}
if result[0].Error != nil {
return &Error{
Kind: result[0].Error.Kind,
Value: result[0].Error.Value,
Message: result[0].Error.Message,
}
}
return nil
}
// Pull retrieves a file from the remote system.
func (client *Client) Pull(opts *PullOptions) (*PullResult, error) {
query := url.Values{
"action": {"read"},
"path": {opts.Path},
}
headers := map[string]string{
"Accept": "multipart/form-data",
}
retry := time.NewTicker(doRetry)
defer retry.Stop()
timeout := time.After(doTimeout)
var rsp *http.Response
var err error
for {
rsp, err = client.raw(context.Background(), "GET", "/v1/files", query, headers, nil)
if err == nil {
break
}
select {
case <-retry.C:
continue
case <-timeout:
}
break
}
if err != nil {
return nil, err
}
// Obtain Content-Type to check for a multipart payload and parse its value
// in order to obtain the multipart boundary
contentType := rsp.Header.Get("Content-Type")
mediaType, params, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, err
}
if mediaType != "multipart/form-data" {
// Probably JSON-encoded error response
var res response
var fr []fileResult
if err := decodeInto(rsp.Body, &res); err != nil {
return nil, err
}
if err := res.err(client); err != nil {
return nil, err
}
if res.Type != "sync" {
return nil, fmt.Errorf("expected sync response, got %q", res.Type)
}
if err := decodeWithNumber(bytes.NewReader(res.Result), &fr); err != nil {
return nil, fmt.Errorf("cannot unmarshal: %w", err)
}
if len(fr) != 1 {
return nil, fmt.Errorf("expected exactly one result from API, got %d", len(fr))
}
if fr[0].Error != nil {
return nil, &Error{
Kind: fr[0].Error.Kind,
Value: fr[0].Error.Value,
Message: fr[0].Error.Message,
}
}
// Not an error response after all
return nil, fmt.Errorf("expected a multipart response but didn't get one")
}
// Obtain the file from the multipart payload
mr := multipart.NewReader(rsp.Body, params["boundary"])
part, err := mr.NextPart()
if err != nil {
return nil, err
}
// Obtain the file size from the Content-Length header
size, err := strconv.ParseInt(part.Header.Get("Content-Length"), 10, 64)
if err != nil {
return nil, err
}
return &PullResult{
Reader: part,
Size: size,
}, nil
}