forked from k0sproject/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unixfsys.go
364 lines (331 loc) · 9.92 KB
/
unixfsys.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package rig
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"io"
"io/fs"
"math/big"
"strings"
"time"
"github.com/alessio/shellescape"
"github.com/k0sproject/rig/errstring"
"github.com/k0sproject/rig/exec"
)
// rigHelper is a helper script to avoid having to write complex bash oneliners in Go
// it's not a read-loop "daemon" like the windows counterpart rigrcp.ps1
//
//go:embed script/righelper.bash
var rigHelper string
var (
_ fs.File = &unixFSFile{}
_ fs.ReadDirFile = &unixFSDir{}
_ fs.FS = &unixFsys{}
)
type unixFsys struct {
conn *Connection
opts []exec.Option
}
func newUnixFsys(conn *Connection, opts ...exec.Option) *unixFsys {
return &unixFsys{conn: conn, opts: opts}
}
type unixFSFile struct {
fsys *unixFsys
path string
isOpen bool
isEOF bool
pos int64
size int64
mode FileMode
}
type unixFSDir struct {
unixFSFile
entries []fs.DirEntry
hw int
}
func (f *unixFSDir) ReadDir(n int) ([]fs.DirEntry, error) {
if n == 0 {
return f.unixFSFile.fsys.ReadDir(f.path)
}
if f.entries == nil {
entries, err := f.unixFSFile.fsys.ReadDir(f.path)
if err != nil {
return nil, err
}
f.entries = entries
f.hw = 0
}
if f.hw >= len(f.entries) {
return nil, io.EOF
}
var min int
if n > len(f.entries)-f.hw {
min = len(f.entries) - f.hw
} else {
min = n
}
old := f.hw
f.hw += min
return f.entries[old:f.hw], nil
}
func (f *unixFSFile) isReadable() bool {
return f.mode&ModeRead != 0
}
func (f *unixFSFile) isWritable() bool {
return f.mode&ModeWrite != 0
}
// ddParams returns "optimal" parameters for a dd command to extract bytesToRead bytes at offset
// from a file with fileSize length
func (f *unixFSFile) ddParams(offset int64, toRead int) (int, int64, int) {
offsetB := big.NewInt(offset)
toReadB := big.NewInt(int64(toRead))
// find the greatest common divisor of the offset and the number of bytes to read
gcdB := big.NewInt(0)
gcdB.GCD(nil, nil, offsetB, toReadB)
blockSize := int(gcdB.Int64())
skip := offset / int64(blockSize)
count := toRead / blockSize
return blockSize, skip, count
}
func (f *unixFSFile) Stat() (fs.FileInfo, error) {
return f.fsys.Stat(f.path)
}
func (f *unixFSFile) Read(p []byte) (int, error) {
if f.isEOF {
return 0, io.EOF
}
if !f.isReadable() {
return 0, ErrCommandFailed.Wrapf("file %s is not open for reading", f.path)
}
bs, skip, count := f.ddParams(f.pos, len(p))
errbuf := bytes.NewBuffer(nil)
buf := bytes.NewBuffer(nil)
cmd, err := f.fsys.conn.ExecStreams(fmt.Sprintf("dd if=%s bs=%d skip=%d count=%d", shellescape.Quote(f.path), bs, skip, count), nil, buf, errbuf, f.fsys.opts...)
if err != nil {
return 0, ErrCommandFailed.Wrapf("failed to execute dd: %w (%s)", err, errbuf.String())
}
if err := cmd.Wait(); err != nil {
return 0, ErrCommandFailed.Wrapf("read (dd): %w (%s)", err, errbuf.String())
}
f.pos += int64(buf.Len())
if buf.Len() < len(p) {
f.isEOF = true
}
return copy(p, buf.Bytes()), nil
}
func (f *unixFSFile) Write(p []byte) (int, error) {
if !f.isWritable() {
return 0, ErrCommandFailed.Wrapf("file %s is not open for writing", f.path)
}
bs, skip, count := f.ddParams(f.pos, len(p))
errbuf := bytes.NewBuffer(nil)
cmd, err := f.fsys.conn.ExecStreams(fmt.Sprintf("dd if=/dev/stdin of=%s bs=%d count=%d seek=%d", shellescape.Quote(f.path), bs, count, skip), io.NopCloser(bytes.NewReader(p)), io.Discard, errbuf, f.fsys.opts...)
if err != nil {
return 0, ErrCommandFailed.Wrapf("write (dd): %w", err)
}
if err := cmd.Wait(); err != nil {
return 0, ErrCommandFailed.Wrapf("write (dd): %w (%s)", err, errbuf.String())
}
f.pos += int64(len(p))
if f.pos > f.size {
f.size = f.pos
f.isEOF = true
}
return len(p), nil
}
func (f *unixFSFile) CopyFromN(src io.Reader, num int64, alt io.Writer) (int64, error) {
if !f.isWritable() {
return 0, ErrCommandFailed.Wrapf("file %s is not open for writing", f.path)
}
var ddCmd string
if f.pos+num >= f.size {
if _, err := f.fsys.helper("truncate", f.path, fmt.Sprintf("%d", f.pos)); err != nil {
return 0, ErrCommandFailed.Wrapf("truncate for writing: %w", err)
}
ddCmd = fmt.Sprintf("dd if=/dev/stdin of=%s bs=16M oflag=append conv=notrunc", shellescape.Quote(f.path))
} else {
ddCmd = fmt.Sprintf("dd if=/dev/stdin of=%s bs=1 seek=%d conv=notrunc", shellescape.Quote(f.path), f.pos)
}
limited := io.LimitReader(src, num)
var reader io.Reader
if alt != nil {
reader = io.TeeReader(limited, alt)
} else {
reader = limited
}
errbuf := bytes.NewBuffer(nil)
cmd, err := f.fsys.conn.ExecStreams(ddCmd, io.NopCloser(reader), io.Discard, errbuf, f.fsys.opts...)
if err != nil {
return 0, ErrCommandFailed.Wrapf("failed to execute dd (copy-from): %w (%s)", err, errbuf.String())
}
if err != nil {
return 0, ErrCommandFailed.Wrapf("copy-from: %w", err)
}
f.pos += num
if f.pos >= f.size {
f.isEOF = true
f.size = f.pos
}
if err != nil {
return 0, &fs.PathError{Op: "copy-from", Path: f.path, Err: ErrRcpCommandFailed.Wrapf("error while copying: %w", err)}
}
if err := cmd.Wait(); err != nil {
return 0, &fs.PathError{Op: "copy-from", Path: f.path, Err: ErrRcpCommandFailed.Wrapf("error while copying: %w (%s)", err, errbuf.String())}
}
return num, nil
}
func (f *unixFSFile) Copy(dst io.Writer) (int, error) {
if f.isEOF {
return 0, io.EOF
}
if !f.isReadable() {
return 0, ErrCommandFailed.Wrapf("file %s is not open for reading", f.path)
}
bs, skip, count := f.ddParams(f.pos, int(f.size-f.pos))
errbuf := bytes.NewBuffer(nil)
cmd, err := f.fsys.conn.ExecStreams(fmt.Sprintf("dd if=%s bs=%d skip=%d count=%d", shellescape.Quote(f.path), bs, skip, count), nil, dst, errbuf, f.fsys.opts...)
if err != nil {
return 0, ErrCommandFailed.Wrapf("failed to execute dd (copy): %w (%s)", err, errbuf.String())
}
if err := cmd.Wait(); err != nil {
return 0, ErrCommandFailed.Wrapf("copy (dd): %w (%s)", err, errbuf.String())
}
f.pos = f.size
f.isEOF = true
return int(f.size - f.pos), nil
}
func (f *unixFSFile) Close() error {
f.isOpen = false
return nil
}
func (f *unixFSFile) Seek(offset int64, whence int) (int64, error) {
switch whence {
case io.SeekStart:
f.pos = offset
case io.SeekCurrent:
f.pos += offset
case io.SeekEnd:
f.pos = f.size + offset
default:
return 0, ErrCommandFailed.Wrapf("invalid whence: %d", whence)
}
f.isEOF = f.pos >= f.size
return f.pos, nil
}
type helperResponse struct {
Err error `json:"-"`
ErrString string `json:"error"`
Stat *FileInfo `json:"stat"`
Dir []*FileInfo `json:"dir"`
Sum *sumResponse `json:"sum"`
}
func (h *helperResponse) UnmarshalJSON(b []byte) error {
type helperresponse *helperResponse
hr := helperresponse(h)
if err := json.Unmarshal(b, hr); err != nil {
return ErrCommandFailed.Wrapf("unmarshal helper response: %w", err)
}
if hr.ErrString != "" {
hr.Err = errstring.New(strings.TrimSpace(hr.ErrString))
}
return nil
}
func (fsys *unixFsys) helper(args ...string) (*helperResponse, error) {
var res helperResponse
opts := fsys.opts
opts = append(opts, exec.Stdin(rigHelper))
out, err := fsys.conn.ExecOutput(fmt.Sprintf("bash -s -- %s", shellescape.QuoteCommand(args)), opts...)
if err != nil {
return nil, ErrCommandFailed.Wrapf("failed to execute helper: %w", err)
}
if err := json.Unmarshal([]byte(out), &res); err != nil {
return nil, ErrCommandFailed.Wrapf("helper response unmarshal: %w", err)
}
if res.Err != nil {
return &res, res.Err
}
return &res, nil
}
func (fsys *unixFsys) Stat(name string) (fs.FileInfo, error) {
res, err := fsys.helper("stat", name)
if err != nil {
return nil, &fs.PathError{Op: "stat", Path: name, Err: fmt.Errorf("%w: %s", fs.ErrNotExist, err)}
}
if res.Stat == nil {
return nil, ErrCommandFailed.Wrapf("helper stat response empty")
}
return res.Stat, nil
}
func (fsys *unixFsys) Sha256(name string) (string, error) {
res, err := fsys.helper("sum", name)
if err != nil {
return "", err
}
if res.Sum == nil {
return "", ErrCommandFailed.Wrapf("helper sum response empty")
}
return res.Sum.Sha256, nil
}
func (fsys *unixFsys) Open(name string) (fs.File, error) {
info, err := fsys.Stat(name)
if err != nil {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
}
file := unixFSFile{fsys: fsys, path: name, isOpen: true, size: info.Size(), mode: ModeRead}
if info.IsDir() {
return &unixFSDir{unixFSFile: file}, nil
}
return &file, nil
}
func (fsys *unixFsys) OpenFile(name string, mode FileMode, perm int) (File, error) {
var pos int64
info, err := fsys.Stat(name)
if err != nil {
switch {
case mode&ModeRead == ModeRead:
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
case mode&ModeCreate == ModeCreate:
if _, err := fsys.helper("touch", name, fmt.Sprintf("%#o", perm)); err != nil {
return nil, err
}
}
info = &FileInfo{FName: name, FUnix: fs.FileMode(perm), FSize: 0, FIsDir: false, FModTime: time.Now(), fsys: fsys}
}
if info.IsDir() {
return nil, &fs.PathError{Op: "open", Path: name, Err: ErrCommandFailed.Wrapf("%w: is a directory", fs.ErrPermission)}
}
switch {
case mode&ModeAppend == ModeAppend:
pos = info.Size()
case mode&ModeCreate == ModeCreate:
if _, err := fsys.helper("truncate", name, "0"); err != nil {
return nil, err
}
}
return &unixFSFile{fsys: fsys, path: name, isOpen: true, size: info.Size(), pos: pos, mode: mode}, nil
}
func (fsys *unixFsys) ReadDir(name string) ([]fs.DirEntry, error) {
if name == "" {
name = "."
}
res, err := fsys.helper("dir", name)
if err != nil {
return nil, err
}
if res.Dir == nil {
return nil, ErrCommandFailed.Wrapf("helper dir response empty")
}
entries := make([]fs.DirEntry, len(res.Dir))
for i, entry := range res.Dir {
entries[i] = entry
}
return entries, nil
}
// Delete removes the named file or (empty) directory.
func (fsys *unixFsys) Delete(name string) error {
if err := fsys.conn.Exec(fmt.Sprintf("rm -f %s", shellescape.Quote(name)), fsys.opts...); err != nil {
return ErrCommandFailed.Wrapf("delete %s: %w", name, err)
}
return nil
}