-
Notifications
You must be signed in to change notification settings - Fork 23
/
posixfile.go
222 lines (185 loc) · 5.55 KB
/
posixfile.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
package remotefs
import (
"bytes"
"fmt"
"io"
"io/fs"
"os"
"path"
"strconv"
"strings"
"github.com/k0sproject/rig/v2/cmd"
"github.com/k0sproject/rig/v2/iostream"
"github.com/k0sproject/rig/v2/sh/shellescape"
)
var (
_ fs.File = (*PosixFile)(nil)
_ File = (*PosixFile)(nil)
)
// PosixFile implements fs.File for a remote file.
type PosixFile struct {
withPath
fs *PosixFS
isOpen bool
isEOF bool
pos int64
size int64
mode fs.FileMode
flags int
blockSize int
}
func (f *PosixFile) fsBlockSize() int {
if f.blockSize > 0 {
return f.blockSize
}
out, err := f.fs.ExecOutput(fmt.Sprintf(`stat -c "%%s" %[1]s 2> /dev/null || stat -f "%%k" %[1]s`, shellescape.Quote(path.Dir(f.path))))
if err != nil {
// fall back to default
f.blockSize = defaultBlockSize
} else if bs, err := strconv.Atoi(strings.TrimSpace(out)); err == nil {
f.blockSize = bs
}
return f.blockSize
}
func (f *PosixFile) isReadable() bool {
return f.isOpen && (f.flags&os.O_WRONLY != os.O_WRONLY || f.flags&os.O_RDWR == os.O_RDWR)
}
func (f *PosixFile) isWritable() bool {
return f.isOpen && f.flags&os.O_WRONLY != 0
}
func (f *PosixFile) ddParams(offset int64, numBytes int) (blocksize int, skip int64, count int) { //nolint:nonamedreturns // for readability
optimalBs := f.fsBlockSize()
// if numBytes aligns with the optimal block size, use it; otherwise, use bs = 1
bs := optimalBs
if numBytes%optimalBs != 0 {
bs = 1
}
s := offset / int64(bs)
c := (numBytes + bs - 1) / bs
return bs, s, c
}
// Stat returns a FileInfo describing the named file.
func (f *PosixFile) Stat() (fs.FileInfo, error) {
return f.fs.Stat(f.path)
}
// Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered.
func (f *PosixFile) Read(p []byte) (int, error) {
if f.isEOF {
return 0, io.EOF
}
if !f.isReadable() {
return 0, fmt.Errorf("%w: file %s is not open for reading", fs.ErrClosed, f.path)
}
buf := bytes.NewBuffer(nil)
bs, skip, count := f.ddParams(f.pos, len(p))
if err := f.fs.Exec(fmt.Sprintf("dd if=%s bs=%d skip=%d count=%d", shellescape.Quote(f.path), bs, skip, count), cmd.Stdout(buf), cmd.HideOutput()); err != nil {
return 0, fmt.Errorf("failed to execute dd: %w", err)
}
readBytes := buf.Bytes()
// Trim extra data if readBytes is larger than the requested size
if len(readBytes) > len(p) {
readBytes = readBytes[:len(p)]
}
copied := copy(p, readBytes)
f.pos += int64(copied)
if copied < len(p) {
f.isEOF = true
}
return copied, nil
}
func (f *PosixFile) Write(p []byte) (int, error) {
if !f.isWritable() {
return 0, fmt.Errorf("%w: file %s is not open for writing", fs.ErrClosed, f.path)
}
var written int
remaining := p
for written < len(p) {
bs, skip, count := f.ddParams(f.pos, len(remaining))
toWrite := bs * count
limitedReader := bytes.NewReader(remaining[:toWrite])
err := f.fs.Exec(
fmt.Sprintf("dd if=/dev/stdin of=%s bs=%d count=%d seek=%d conv=notrunc", f.path, bs, count, skip),
cmd.Stdin(limitedReader),
)
if err != nil {
return 0, fmt.Errorf("write (dd): %w", err)
}
written += toWrite
remaining = remaining[toWrite:]
f.pos += int64(toWrite)
if f.pos > f.size {
f.size = f.pos
}
}
if written < len(p) {
return written, io.ErrShortWrite
}
return written, nil
}
// CopyTo copies the remote file to the writer dst.
func (f *PosixFile) CopyTo(dst io.Writer) (int64, error) {
if f.isEOF {
return 0, io.EOF
}
if !f.isReadable() {
return 0, f.pathErr(OpCopyTo, fmt.Errorf("%w: file %s is not open for reading", fs.ErrClosed, f.path))
}
bs, skip, count := f.ddParams(f.pos, int(f.size-f.pos))
counter := &iostream.ByteCounter{}
writer := io.MultiWriter(dst, counter)
err := f.fs.Exec(
fmt.Sprintf("dd if=%s bs=%d skip=%d count=%d", shellescape.Quote(f.path), bs, skip, count),
cmd.Stdout(writer),
cmd.HideOutput(),
)
if err != nil {
return 0, f.pathErr(OpCopyTo, fmt.Errorf("failed to execute dd: %w", err))
}
f.pos += counter.Count()
f.isEOF = true
return counter.Count(), nil
}
// CopyFrom copies the local reader src to the remote file.
func (f *PosixFile) CopyFrom(src io.Reader) (int64, error) {
if !f.isWritable() {
return 0, f.pathErr(OpCopyFrom, fmt.Errorf("%w: file %s is not open for writing", fs.ErrClosed, f.path))
}
if err := f.fs.Truncate(f.Name(), f.pos); err != nil {
return 0, f.pathErr(OpCopyFrom, fmt.Errorf("truncate: %w", err))
}
counter := &iostream.ByteCounter{}
err := f.fs.Exec(
fmt.Sprintf("dd if=/dev/stdin of=%s bs=%d seek=%d conv=notrunc", shellescape.Quote(f.path), f.fsBlockSize(), f.pos),
cmd.Stdin(io.TeeReader(src, counter)),
)
if err != nil {
return 0, f.pathErr(OpCopyFrom, fmt.Errorf("exec dd: %w", err))
}
f.pos += counter.Count()
f.size = f.pos
return counter.Count(), nil
}
// Close closes the file, rendering it unusable for I/O. It returns an error, if any.
func (f *PosixFile) Close() error {
f.isOpen = false
return nil
}
// Seek sets the offset for the next Read or Write to offset, interpreted according to whence:
// io.SeekStart means relative to the origin of the file,
// io.SeekCurrent means relative to the current offset, and
// io.SeekEnd means relative to the end.
// Seek returns the new offset relative to the start of the file and an error, if any.
func (f *PosixFile) 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, fmt.Errorf("%w: whence: %d", errInvalid, whence)
}
f.isEOF = f.pos >= f.size
return f.pos, nil
}