-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_fs.go
More file actions
284 lines (238 loc) · 7.3 KB
/
client_fs.go
File metadata and controls
284 lines (238 loc) · 7.3 KB
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
276
277
278
279
280
281
282
283
284
package slicer
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
)
// ReadFile downloads a file from the VM and returns its contents and optional mode.
func (c *SlicerClient) ReadFile(ctx context.Context, vmName, vmPath string) ([]byte, string, error) {
u, err := url.Parse(c.baseURL)
if err != nil {
return nil, "", fmt.Errorf("failed to parse API URL: %w", err)
}
u.Path = fmt.Sprintf("/vm/%s/cp", vmName)
q := url.Values{}
q.Set("path", vmPath)
u.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, "", fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Accept", "application/octet-stream")
c.setAuthHeaders(req)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, "", fmt.Errorf("request failed: %w", err)
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
}()
if res.StatusCode == http.StatusNotFound {
return nil, "", os.ErrNotExist
}
if res.StatusCode != http.StatusOK {
body, _ := io.ReadAll(res.Body)
return nil, "", fmt.Errorf("failed to read file from VM: %s: %s", res.Status, strings.TrimSpace(string(body)))
}
data, err := io.ReadAll(res.Body)
if err != nil {
return nil, "", fmt.Errorf("failed to read response body: %w", err)
}
mode := strings.TrimSpace(res.Header.Get(fileModeHeader))
if mode == "" {
mode = "0600"
}
return data, mode, nil
}
// WriteFile uploads a binary file to the VM.
func (c *SlicerClient) WriteFile(ctx context.Context, vmName, vmPath string, data []byte, uid, gid uint32, permissions string) error {
u, err := url.Parse(c.baseURL)
if err != nil {
return fmt.Errorf("failed to parse API URL: %w", err)
}
u.Path = fmt.Sprintf("/vm/%s/cp", vmName)
q := url.Values{}
q.Set("path", vmPath)
if uid == 0 && gid == 0 {
uid, gid = getCurrentUIDGID()
}
if uid != NonRootUser {
q.Set("uid", strconv.FormatUint(uint64(uid), 10))
}
if gid != NonRootUser {
q.Set("gid", strconv.FormatUint(uint64(gid), 10))
}
if len(permissions) > 0 {
q.Set("permissions", permissions)
}
u.RawQuery = q.Encode()
reader := bytes.NewReader(data)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), reader)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/octet-stream")
c.setAuthHeaders(req)
res, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to perform POST request: %w", err)
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
body, _ := io.ReadAll(res.Body)
return fmt.Errorf("failed to write file to VM: %s: %s", res.Status, strings.TrimSpace(string(body)))
}
return nil
}
// ReadDir lists entries in a VM path.
func (c *SlicerClient) ReadDir(ctx context.Context, vmName, path string) ([]SlicerFSInfo, error) {
u, err := url.Parse(c.baseURL)
if err != nil {
return nil, fmt.Errorf("failed to parse API URL: %w", err)
}
u.Path = fmt.Sprintf("/vm/%s/fs/readdir", vmName)
q := url.Values{}
q.Set("path", path)
u.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
c.setAuthHeaders(req)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
body, _ := io.ReadAll(res.Body)
return nil, fmt.Errorf("failed to read directory: %s: %s", res.Status, strings.TrimSpace(string(body)))
}
var entries []SlicerFSInfo
if err := json.NewDecoder(res.Body).Decode(&entries); err != nil {
return nil, fmt.Errorf("failed to decode directory listing: %w", err)
}
return entries, nil
}
// Stat fetches metadata for a single path inside a VM.
func (c *SlicerClient) Stat(ctx context.Context, vmName, path string) (*SlicerFSInfo, error) {
u, err := url.Parse(c.baseURL)
if err != nil {
return nil, fmt.Errorf("failed to parse API URL: %w", err)
}
u.Path = fmt.Sprintf("/vm/%s/fs/stat", vmName)
q := url.Values{}
q.Set("path", path)
u.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
c.setAuthHeaders(req)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
}()
if res.StatusCode == http.StatusNotFound {
return nil, os.ErrNotExist
}
if res.StatusCode != http.StatusOK {
body, _ := io.ReadAll(res.Body)
return nil, fmt.Errorf("failed to stat path: %s: %s", res.Status, strings.TrimSpace(string(body)))
}
var entry SlicerFSInfo
if err := json.NewDecoder(res.Body).Decode(&entry); err != nil {
return nil, fmt.Errorf("failed to decode stat result: %w", err)
}
return &entry, nil
}
// Exists checks if a path exists in a VM.
func (c *SlicerClient) Exists(ctx context.Context, vmName, path string) (bool, error) {
_, err := c.Stat(ctx, vmName, path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// Mkdir creates a directory in a VM.
func (c *SlicerClient) Mkdir(ctx context.Context, vmName string, request SlicerFSMkdirRequest) error {
u, err := url.Parse(c.baseURL)
if err != nil {
return fmt.Errorf("failed to parse API URL: %w", err)
}
u.Path = fmt.Sprintf("/vm/%s/fs/mkdir", vmName)
reqBody, err := json.Marshal(request)
if err != nil {
return fmt.Errorf("failed to marshal request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), bytes.NewReader(reqBody))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
c.setAuthHeaders(req)
res, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
body, _ := io.ReadAll(res.Body)
return fmt.Errorf("failed to create directory: %s: %s", res.Status, strings.TrimSpace(string(body)))
}
return nil
}
// Remove deletes a file or directory in a VM.
func (c *SlicerClient) Remove(ctx context.Context, vmName, path string, recursive bool) error {
u, err := url.Parse(c.baseURL)
if err != nil {
return fmt.Errorf("failed to parse API URL: %w", err)
}
u.Path = fmt.Sprintf("/vm/%s/fs/remove", vmName)
q := url.Values{}
q.Set("path", path)
q.Set("recursive", strconv.FormatBool(recursive))
u.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, u.String(), nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
c.setAuthHeaders(req)
res, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
body, _ := io.ReadAll(res.Body)
return fmt.Errorf("failed to remove path: %s: %s", res.Status, strings.TrimSpace(string(body)))
}
return nil
}