-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
file.go
393 lines (328 loc) · 10 KB
/
file.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
Copyright (C) 2020-2023 Daniele Rondina <geaaru@funtoo.org>
Credits goes also to Gogs authors, some code portions and re-implemented design
are also coming from the Gogs project, which is using the go-macaron framework
and was really source of ispiration. Kudos to them!
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 executor
import (
"bytes"
"container/list"
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
log "github.com/MottainaiCI/lxd-compose/pkg/logger"
lxd "github.com/lxc/lxd/client"
lxd_shared "github.com/lxc/lxd/shared"
lxd_cli "github.com/lxc/lxd/shared/cmd"
"github.com/lxc/lxd/shared/ioprogress"
lxd_units "github.com/lxc/lxd/shared/units"
)
// Based on code of lxc client tool https://github.com/lxc/lxd/blob/master/lxc/file.go
func (e *LxdCExecutor) RecursiveMkdir(nameContainer string, dir string, mode *os.FileMode, uid int64, gid int64) error {
/* special case, every container has a /, we don't need to do anything */
if dir == "/" {
return nil
}
// Remove trailing "/" e.g. /A/B/C/. Otherwise we will end up with an
// empty array entry "" which will confuse the Mkdir() loop below.
pclean := filepath.Clean(dir)
parts := strings.Split(pclean, "/")
i := len(parts)
for ; i >= 1; i-- {
cur := filepath.Join(parts[:i]...)
_, resp, err := e.LxdClient.GetContainerFile(nameContainer, cur)
if err != nil {
continue
}
if resp.Type != "directory" {
return fmt.Errorf("%s is not a directory", cur)
}
i++
break
}
for ; i <= len(parts); i++ {
cur := filepath.Join(parts[:i]...)
if cur == "" {
continue
}
cur = "/" + cur
modeArg := -1
if mode != nil {
modeArg = int(mode.Perm())
}
args := lxd.ContainerFileArgs{
UID: uid,
GID: gid,
Mode: modeArg,
Type: "directory",
}
e.Emitter.DebugLog(false, fmt.Sprintf("Creating %s (%s)", cur, args.Type))
err := e.LxdClient.CreateContainerFile(nameContainer, cur, args)
if err != nil {
return err
}
}
return nil
}
// Based on code of lxc client tool https://github.com/lxc/lxd/blob/master/lxc/file.go
func (e *LxdCExecutor) RecursivePushFile(nameContainer, source, target string) error {
var targetIsFile bool = true
var sourceIsFile bool = true
if strings.HasSuffix(source, "/") {
sourceIsFile = false
}
if strings.HasSuffix(target, "/") {
targetIsFile = false
}
dir := filepath.Dir(target)
sourceDir := filepath.Dir(filepath.Clean(source))
if !sourceIsFile && targetIsFile {
dir = target
sourceDir = source
}
sourceLen := len(sourceDir)
// Determine the target mode
mode := os.FileMode(0755)
// Create directory as root. TODO: see if we can use a specific user.
var uid int64 = 0
var gid int64 = 0
err := e.RecursiveMkdir(nameContainer, dir, &mode, uid, gid)
if err != nil {
return errors.New("Error on create dir " + filepath.Dir(target) + ": " + err.Error())
}
sendFile := func(p string, fInfo os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("Failed to walk path for %s: %s", p, err)
}
// Detect unsupported files
if !fInfo.Mode().IsRegular() && !fInfo.Mode().IsDir() && fInfo.Mode()&os.ModeSymlink != os.ModeSymlink {
return fmt.Errorf("'%s' isn't a supported file type", p)
}
// Prepare for file transfer
targetPath := path.Join(target, filepath.ToSlash(p[sourceLen:]))
if p == source {
if targetIsFile && sourceIsFile {
targetPath = target
} else if targetIsFile && !sourceIsFile {
// Nothing to do. The directory is already been created.
e.Emitter.DebugLog(false, fmt.Sprintf("Skipping dir %s. Already created.", p))
return nil
}
}
mode, uid, gid := lxd_shared.GetOwnerMode(fInfo)
args := lxd.ContainerFileArgs{
UID: int64(uid),
GID: int64(gid),
Mode: int(mode.Perm()),
}
var readCloser io.ReadCloser
logger := log.GetDefaultLogger()
if fInfo.IsDir() {
// Directory handling
args.Type = "directory"
} else if fInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
// Symlink handling
symlinkTarget, err := os.Readlink(p)
if err != nil {
return err
}
args.Type = "symlink"
args.Content = bytes.NewReader([]byte(symlinkTarget))
readCloser = io.NopCloser(args.Content)
} else {
// File handling
f, err := os.Open(p)
if err != nil {
return err
}
defer f.Close()
args.Type = "file"
args.Content = f
readCloser = f
}
progress := lxd_cli.ProgressRenderer{
Format: fmt.Sprintf("Pushing %s to %s: %%s", p, targetPath),
Quiet: false,
}
if args.Type != "directory" {
contentLength, err := args.Content.Seek(0, io.SeekEnd)
if err != nil {
return err
}
_, err = args.Content.Seek(0, io.SeekStart)
if err != nil {
return err
}
args.Content = lxd_shared.NewReadSeeker(&ioprogress.ProgressReader{
ReadCloser: readCloser,
Tracker: &ioprogress.ProgressTracker{
Length: contentLength,
Handler: func(percent int64, speed int64) {
if logger.Config.GetLogging().PushProgressBar {
e.Emitter.InfoLog(true,
logger.Aurora.Italic(
logger.Aurora.BrightMagenta(
fmt.Sprintf("%d%% (%s/s)", percent,
lxd_units.GetByteSizeString(speed, 2)))))
}
progress.UpdateProgress(ioprogress.ProgressData{
Text: fmt.Sprintf("%d%% (%s/s)", percent,
lxd_units.GetByteSizeString(speed, 2))})
},
},
}, args.Content)
}
if logger.Config.GetLogging().PushProgressBar {
e.Emitter.InfoLog(true,
logger.Aurora.Italic(
logger.Aurora.BrightMagenta(
fmt.Sprintf(">>> [%s] Pushing %s -> %s (%s)",
nameContainer, p, targetPath, args.Type))))
}
err = e.LxdClient.CreateContainerFile(nameContainer, targetPath, args)
if err != nil {
if args.Type != "directory" {
progress.Done("")
}
return err
}
if args.Type != "directory" {
progress.Done("")
}
return nil
}
return filepath.Walk(source, sendFile)
}
// Based on code of lxc client tool https://github.com/lxc/lxd/blob/master/lxc/file.go
func (l *LxdCExecutor) RecursivePullFile(nameContainer string, destPath string, localPath string, localAsTarget bool) error {
buf, resp, err := l.LxdClient.GetContainerFile(nameContainer, destPath)
if err != nil {
return err
}
var target string
// Default loging is to append tree to target directory
if localAsTarget {
target = localPath
} else {
target = filepath.Join(localPath, filepath.Base(destPath))
}
//target := localPath
l.Emitter.DebugLog(false, fmt.Sprintf("Pulling %s from %s (%s)\n", target, destPath, resp.Type))
if resp.Type == "directory" {
err := os.MkdirAll(target, os.FileMode(resp.Mode))
if err != nil {
l.Emitter.InfoLog(false, fmt.Sprintf("directory %s is already present. Nothing to do.\n", target))
}
for _, ent := range resp.Entries {
nextP := path.Join(destPath, ent)
err = l.RecursivePullFile(nameContainer, nextP, target, false)
if err != nil {
return err
}
}
} else if resp.Type == "file" {
f, err := os.Create(target)
if err != nil {
return err
}
defer f.Close()
err = os.Chmod(target, os.FileMode(resp.Mode))
if err != nil {
return err
}
progress := lxd_cli.ProgressRenderer{
Format: fmt.Sprintf("Pulling %s from %s: %%s", destPath, target),
Quiet: false,
}
writer := &ioprogress.ProgressWriter{
WriteCloser: f,
Tracker: &ioprogress.ProgressTracker{
Handler: func(bytesReceived int64, speed int64) {
l.Emitter.DebugLog(false, fmt.Sprintf("%s (%s/s)\n",
lxd_units.GetByteSizeString(bytesReceived, 2),
lxd_units.GetByteSizeString(speed, 2)))
progress.UpdateProgress(ioprogress.ProgressData{
Text: fmt.Sprintf("%s (%s/s)",
lxd_units.GetByteSizeString(bytesReceived, 2),
lxd_units.GetByteSizeString(speed, 2))})
},
},
}
_, err = io.Copy(writer, buf)
progress.Done("")
if err != nil {
l.Emitter.ErrorLog(false, fmt.Sprintf("Error on pull file %s", target))
return err
}
} else if resp.Type == "symlink" {
linkTarget, err := io.ReadAll(buf)
if err != nil {
return err
}
err = os.Symlink(strings.TrimSpace(string(linkTarget)), target)
if err != nil {
return err
}
} else {
return fmt.Errorf("Unknown file type '%s'", resp.Type)
}
return nil
}
func (e *LxdCExecutor) recursiveListFile(nameContainer string, targetPath string, list *list.List) error {
buf, resp, err := e.LxdClient.GetContainerFile(nameContainer, targetPath)
if err != nil {
return err
}
if buf != nil {
// Needed to avoid: dial unix /var/lib/lxd/unix.socket: socket: too many open files
buf.Close()
}
if resp.Type == "directory" {
for _, ent := range resp.Entries {
nextP := path.Join(targetPath, ent)
err = e.recursiveListFile(nameContainer, nextP, list)
if err != nil {
return err
}
}
list.PushBack(targetPath)
} else if resp.Type == "file" || resp.Type == "symlink" {
list.PushFront(targetPath)
} else {
e.Emitter.WarnLog(false, "Find unsupported file type "+resp.Type+". Skipped.")
}
return nil
}
func (e *LxdCExecutor) DeleteContainerDir(name, dir string) error {
var err error
var list *list.List = list.New()
// Create list of files/directories to remove. (files are pushed before directories)
err = e.recursiveListFile(name, dir, list)
if err != nil {
return err
}
for f := list.Front(); f != nil; f = f.Next() {
e.Emitter.DebugLog(false, fmt.Sprintf("Removing file %s...", f.Value.(string)))
err = e.LxdClient.DeleteContainerFile(name, f.Value.(string))
if err != nil {
e.Emitter.ErrorLog(false, fmt.Sprintf("ERROR: Error on removing %s: %s",
f.Value, err.Error()))
}
}
return nil
}