/
helper.go
337 lines (273 loc) · 6.77 KB
/
helper.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
package winrm
import (
"context"
"encoding/base64"
"encoding/xml"
"errors"
"fmt"
"io"
"io/fs"
"strconv"
"strings"
"sync"
"time"
"github.com/masterzen/winrm"
"github.com/seal-io/terraform-provider-courier/utils/bytespool"
)
type fileTransport struct {
ctx context.Context
shell *winrm.Shell
}
func (ft *fileTransport) Close() error {
return ft.shell.Close()
}
func (ft *fileTransport) Create(path string) (*writableFile, error) {
if path == "" {
return nil, errors.New("blank path")
}
path = toWindowsPath(path)
command := winrm.Powershell(
fmt.Sprintf("New-Item -Force -ItemType File -Path %s", path),
)
err := execute(ft.ctx, ft.shell, io.Discard, command)
if err != nil {
return nil, fmt.Errorf("failed to create file %q: %w", path, err)
}
return &writableFile{
ctx: ft.ctx,
shell: ft.shell,
path: path,
}, nil
}
func (ft *fileTransport) Lstat(path string) (fs.FileInfo, error) {
if path == "" {
return nil, errors.New("blank path")
}
path = toWindowsPath(path)
f := readonlyFile{
ctx: ft.ctx,
shell: ft.shell,
path: path,
}
return f.Stat()
}
func (ft *fileTransport) Open(path string) (fs.File, error) {
if path == "" {
return nil, errors.New("blank path")
}
path = toWindowsPath(path)
command := winrm.Powershell(
fmt.Sprintf("Get-Item -Force -Path %s", path),
)
err := execute(ft.ctx, ft.shell, io.Discard, command)
if err != nil {
return nil, fmt.Errorf("failed to open file %q: %w", path, err)
}
return &readonlyFile{
ctx: ft.ctx,
shell: ft.shell,
path: path,
}, nil
}
func (ft *fileTransport) ReadDir(path string) ([]fs.DirEntry, error) {
if path == "" {
return nil, errors.New("blank path")
}
// TODO(thxCode): implement this.
return nil, nil
}
func (ft *fileTransport) MkdirAll(path string) error {
if path == "" {
return errors.New("blank path")
}
path = toWindowsPath(path)
command := winrm.Powershell(
fmt.Sprintf("New-Item -Force -ItemType Directory -Path %s", path),
)
err := execute(ft.ctx, ft.shell, io.Discard, command)
if err != nil {
return fmt.Errorf("failed to create directory %q: %w", path, err)
}
return nil
}
type fileInfo struct {
name string
size int64
mode fs.FileMode
modTime time.Time
}
func (fi fileInfo) Name() string {
return fi.name
}
func (fi fileInfo) Size() int64 {
return fi.size
}
func (fi fileInfo) Mode() fs.FileMode {
return fi.mode
}
func (fi fileInfo) ModTime() time.Time {
return fi.modTime
}
func (fi fileInfo) IsDir() bool {
return fi.mode.IsDir()
}
func (fi fileInfo) Sys() any {
return nil
}
type writableFile struct {
ctx context.Context
shell *winrm.Shell
path string
err error
}
func (f *writableFile) Close() error {
if f.err != nil || f.ctx.Err() != nil {
return f.shell.Close()
}
// NB(thxCode): Restore the file's content, inspired by
// https://github.com/packer-community/winrmcp/blob/6e900dd2c68f81845f61265562b6299a806162e0/winrmcp/cp.go.
command := winrm.Powershell(fmt.Sprintf(`
$path = "%s"
if (Test-Path ${path} -Type Leaf) {
$rd = [System.IO.File]::OpenText(${path})
$wr = [System.IO.File]::OpenWrite(${path}.tmp)
try {
for(;;) {
$bs64 = $rd.ReadLine()
if (${bs64} -eq $null) { break }
$bs = [System.Convert]::FromBase64String(${bs64})
$wr.Write(${bs}, 0, ${bs}.Length)
}
Move-Item -Path ${path}.tmp -Destination ${path} -Force
} finally {
$rd.Close()
$wr.Close()
}
} else {
throw [System.IO.FileNotFoundException]::new("could not find path: $path")
}`, f.path))
err := execute(f.ctx, f.shell, io.Discard, command)
if cerr := f.shell.Close(); err == nil {
err = cerr
}
return err
}
func (f *writableFile) Name() string {
return f.path
}
func (f *writableFile) Write(p []byte) (int, error) {
pb64 := base64.StdEncoding.EncodeToString(p)
command := fmt.Sprintf(`echo %s >> %q`, pb64, f.path)
f.err = execute(f.ctx, f.shell, io.Discard, command)
if f.err != nil {
return 0, f.err
}
return len(p), nil
}
type readonlyFile struct {
ctx context.Context
shell *winrm.Shell
path string
}
func (f *readonlyFile) Close() error {
return f.shell.Close()
}
func (f *readonlyFile) Name() string {
return f.path
}
func (f *readonlyFile) Read(p []byte) (int, error) {
// TODO(thxCode): implement this.
return 0, io.EOF
}
func (f *readonlyFile) Stat() (fs.FileInfo, error) {
command := winrm.Powershell(fmt.Sprintf("Get-ItemProperty -Path %s | "+
"Select-Object -Property FullName,LastWriteTimeUtc,Attributes,Length | "+
"ConvertTo-Xml -NoTypeInformation -As String", f.path))
buf := bytespool.GetBuffer()
defer func() { bytespool.Put(buf) }()
err := execute(f.ctx, f.shell, buf, command)
if err != nil {
return nil, fmt.Errorf("failed to stat file %q: %w", f.path, err)
}
// <?xml version="1.0" encoding="utf-8"?>
// <Objects>
// <Object>
// <Property Name="FullName">C:\Users\Administrator\test.txt</Property>
// <Property Name="LastWriteTimeUtc">2023/9/18 5:32:42</Property>
// <Property Name="Attributes">Archive</Property>
// <Property Name="Length">0</Property>
// </Object>
// </Objects>.
var r struct {
Objects []struct {
Properties []struct {
Name string `xml:"Name,attr"`
Value string `xml:",chardata"`
} `xml:"Property"`
} `xml:"Object"`
}
err = xml.Unmarshal(buf.Bytes(), &r)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal file info: %w", err)
}
if len(r.Objects) != 1 || len(r.Objects[0].Properties) == 0 {
return nil, errors.New(
"failed to unmarshal file info: no object or no properties",
)
}
var fi fileInfo
for _, prop := range r.Objects[0].Properties {
switch prop.Name {
case "FullName":
fi.name = prop.Value
case "LastWriteTimeUtc":
fi.modTime, err = time.Parse("2006/1/2 15:4:5", prop.Value)
case "Attributes":
if strings.Contains(prop.Value, "Directory") {
fi.mode |= fs.ModeDir
}
case "Length":
fi.size, err = strconv.ParseInt(prop.Value, 10, 64)
}
if err != nil {
return nil, fmt.Errorf("failed to parse file info: %w", err)
}
}
return fi, nil
}
func toWindowsPath(path string) string {
if strings.Contains(path, " ") {
path = fmt.Sprintf("'%s'", strings.Trim(path, "'\""))
}
return strings.ReplaceAll(path, "/", "\\")
}
func execute(
ctx context.Context,
shell *winrm.Shell,
out io.Writer,
command string,
) error {
c, err := shell.ExecuteWithContext(ctx, command)
if err != nil {
return err
}
defer func() { _ = c.Close() }()
var g sync.WaitGroup
cp := func(wr io.Writer, rd io.Reader) {
defer g.Done()
buf := bytespool.GetBytes()
defer func() { bytespool.Put(buf) }()
_, _ = io.CopyBuffer(wr, rd, buf)
}
stds := []io.Reader{c.Stdout, c.Stderr}
for i := range stds {
g.Add(1)
go cp(out, stds[i])
}
g.Wait()
c.Wait()
if c.ExitCode() != 0 {
return fmt.Errorf("exit %d", c.ExitCode())
}
return nil
}