forked from canonical/lxd
/
util.go
855 lines (716 loc) · 17.5 KB
/
util.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
package shared
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
const SnapshotDelimiter = "/"
const DefaultPort = "8443"
// AddSlash adds a slash to the end of paths if they don't already have one.
// This can be useful for rsyncing things, since rsync has behavior present on
// the presence or absence of a trailing slash.
func AddSlash(path string) string {
if path[len(path)-1] != '/' {
return path + "/"
}
return path
}
func PathExists(name string) bool {
_, err := os.Lstat(name)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
// PathIsEmpty checks if the given path is empty.
func PathIsEmpty(path string) (bool, error) {
f, err := os.Open(path)
if err != nil {
return false, err
}
defer f.Close()
// read in ONLY one file
_, err = f.Readdir(1)
// and if the file is EOF... well, the dir is empty.
if err == io.EOF {
return true, nil
}
return false, err
}
// IsDir returns true if the given path is a directory.
func IsDir(name string) bool {
stat, err := os.Lstat(name)
if err != nil {
return false
}
return stat.IsDir()
}
// IsUnixSocket returns true if the given path is either a Unix socket
// or a symbolic link pointing at a Unix socket.
func IsUnixSocket(path string) bool {
stat, err := os.Stat(path)
if err != nil {
return false
}
return (stat.Mode() & os.ModeSocket) == os.ModeSocket
}
// VarPath returns the provided path elements joined by a slash and
// appended to the end of $LXD_DIR, which defaults to /var/lib/lxd.
func VarPath(path ...string) string {
varDir := os.Getenv("LXD_DIR")
if varDir == "" {
varDir = "/var/lib/lxd"
}
items := []string{varDir}
items = append(items, path...)
return filepath.Join(items...)
}
// CachePath returns the directory that LXD should its cache under. If LXD_DIR is
// set, this path is $LXD_DIR/cache, otherwise it is /var/cache/lxd.
func CachePath(path ...string) string {
varDir := os.Getenv("LXD_DIR")
logDir := "/var/cache/lxd"
if varDir != "" {
logDir = filepath.Join(varDir, "cache")
}
items := []string{logDir}
items = append(items, path...)
return filepath.Join(items...)
}
// LogPath returns the directory that LXD should put logs under. If LXD_DIR is
// set, this path is $LXD_DIR/logs, otherwise it is /var/log/lxd.
func LogPath(path ...string) string {
varDir := os.Getenv("LXD_DIR")
logDir := "/var/log/lxd"
if varDir != "" {
logDir = filepath.Join(varDir, "logs")
}
items := []string{logDir}
items = append(items, path...)
return filepath.Join(items...)
}
func ParseLXDFileHeaders(headers http.Header) (uid int, gid int, mode int, type_ string) {
uid, err := strconv.Atoi(headers.Get("X-LXD-uid"))
if err != nil {
uid = -1
}
gid, err = strconv.Atoi(headers.Get("X-LXD-gid"))
if err != nil {
gid = -1
}
mode, err = strconv.Atoi(headers.Get("X-LXD-mode"))
if err != nil {
mode = -1
} else {
rawMode, err := strconv.ParseInt(headers.Get("X-LXD-mode"), 0, 0)
if err == nil {
mode = int(os.FileMode(rawMode) & os.ModePerm)
}
}
type_ = headers.Get("X-LXD-type")
/* backwards compat: before "type" was introduced, we could only
* manipulate files
*/
if type_ == "" {
type_ = "file"
}
return uid, gid, mode, type_
}
func ReadToJSON(r io.Reader, req interface{}) error {
buf, err := ioutil.ReadAll(r)
if err != nil {
return err
}
return json.Unmarshal(buf, req)
}
func ReaderToChannel(r io.Reader, bufferSize int) <-chan []byte {
if bufferSize <= 128*1024 {
bufferSize = 128 * 1024
}
ch := make(chan ([]byte))
go func() {
readSize := 128 * 1024
offset := 0
buf := make([]byte, bufferSize)
for {
read := buf[offset : offset+readSize]
nr, err := r.Read(read)
offset += nr
if offset > 0 && (offset+readSize >= bufferSize || err != nil) {
ch <- buf[0:offset]
offset = 0
buf = make([]byte, bufferSize)
}
if err != nil {
close(ch)
break
}
}
}()
return ch
}
// Returns a random base64 encoded string from crypto/rand.
func RandomCryptoString() (string, error) {
buf := make([]byte, 32)
n, err := rand.Read(buf)
if err != nil {
return "", err
}
if n != len(buf) {
return "", fmt.Errorf("not enough random bytes read")
}
return hex.EncodeToString(buf), nil
}
func SplitExt(fpath string) (string, string) {
b := path.Base(fpath)
ext := path.Ext(fpath)
return b[:len(b)-len(ext)], ext
}
func AtoiEmptyDefault(s string, def int) (int, error) {
if s == "" {
return def, nil
}
return strconv.Atoi(s)
}
func ReadStdin() ([]byte, error) {
buf := bufio.NewReader(os.Stdin)
line, _, err := buf.ReadLine()
if err != nil {
return nil, err
}
return line, nil
}
func WriteAll(w io.Writer, buf []byte) error {
return WriteAllBuf(w, bytes.NewBuffer(buf))
}
func WriteAllBuf(w io.Writer, buf *bytes.Buffer) error {
toWrite := int64(buf.Len())
for {
n, err := io.Copy(w, buf)
if err != nil {
return err
}
toWrite -= n
if toWrite <= 0 {
return nil
}
}
}
// FileMove tries to move a file by using os.Rename,
// if that fails it tries to copy the file and remove the source.
func FileMove(oldPath string, newPath string) error {
if err := os.Rename(oldPath, newPath); err == nil {
return nil
}
if err := FileCopy(oldPath, newPath); err != nil {
return err
}
os.Remove(oldPath)
return nil
}
// FileCopy copies a file, overwriting the target if it exists.
func FileCopy(source string, dest string) error {
s, err := os.Open(source)
if err != nil {
return err
}
defer s.Close()
d, err := os.Create(dest)
if err != nil {
if os.IsExist(err) {
d, err = os.OpenFile(dest, os.O_WRONLY, 0700)
if err != nil {
return err
}
} else {
return err
}
}
defer d.Close()
_, err = io.Copy(d, s)
return err
}
type BytesReadCloser struct {
Buf *bytes.Buffer
}
func (r BytesReadCloser) Read(b []byte) (n int, err error) {
return r.Buf.Read(b)
}
func (r BytesReadCloser) Close() error {
/* no-op since we're in memory */
return nil
}
func IsSnapshot(name string) bool {
return strings.Contains(name, SnapshotDelimiter)
}
func ExtractSnapshotName(name string) string {
return strings.SplitN(name, SnapshotDelimiter, 2)[1]
}
func ReadDir(p string) ([]string, error) {
ents, err := ioutil.ReadDir(p)
if err != nil {
return []string{}, err
}
var ret []string
for _, ent := range ents {
ret = append(ret, ent.Name())
}
return ret, nil
}
func MkdirAllOwner(path string, perm os.FileMode, uid int, gid int) error {
// This function is a slightly modified version of MkdirAll from the Go standard library.
// https://golang.org/src/os/path.go?s=488:535#L9
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
dir, err := os.Stat(path)
if err == nil {
if dir.IsDir() {
return nil
}
return fmt.Errorf("path exists but isn't a directory")
}
// Slow path: make sure parent exists and then call Mkdir for path.
i := len(path)
for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
i--
}
j := i
for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
j--
}
if j > 1 {
// Create parent
err = MkdirAllOwner(path[0:j-1], perm, uid, gid)
if err != nil {
return err
}
}
// Parent now exists; invoke Mkdir and use its result.
err = os.Mkdir(path, perm)
err_chown := os.Chown(path, uid, gid)
if err_chown != nil {
return err_chown
}
if err != nil {
// Handle arguments like "foo/." by
// double-checking that directory doesn't exist.
dir, err1 := os.Lstat(path)
if err1 == nil && dir.IsDir() {
return nil
}
return err
}
return nil
}
func StringInSlice(key string, list []string) bool {
for _, entry := range list {
if entry == key {
return true
}
}
return false
}
func IntInSlice(key int, list []int) bool {
for _, entry := range list {
if entry == key {
return true
}
}
return false
}
func IsTrue(value string) bool {
if StringInSlice(strings.ToLower(value), []string{"true", "1", "yes", "on"}) {
return true
}
return false
}
func IsOnSharedMount(pathName string) (bool, error) {
file, err := os.Open("/proc/self/mountinfo")
if err != nil {
return false, err
}
defer file.Close()
absPath, err := filepath.Abs(pathName)
if err != nil {
return false, err
}
expPath, err := os.Readlink(absPath)
if err != nil {
expPath = absPath
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
rows := strings.Fields(line)
if rows[4] != expPath {
continue
}
if strings.HasPrefix(rows[6], "shared:") {
return true, nil
} else {
return false, nil
}
}
return false, nil
}
func IsBlockdev(fm os.FileMode) bool {
return ((fm&os.ModeDevice != 0) && (fm&os.ModeCharDevice == 0))
}
func IsBlockdevPath(pathName string) bool {
sb, err := os.Stat(pathName)
if err != nil {
return false
}
fm := sb.Mode()
return ((fm&os.ModeDevice != 0) && (fm&os.ModeCharDevice == 0))
}
func BlockFsDetect(dev string) (string, error) {
out, err := exec.Command("blkid", "-s", "TYPE", "-o", "value", dev).Output()
if err != nil {
return "", fmt.Errorf("Failed to run blkid on: %s", dev)
}
return strings.TrimSpace(string(out)), nil
}
// DeepCopy copies src to dest by using encoding/gob so its not that fast.
func DeepCopy(src, dest interface{}) error {
buff := new(bytes.Buffer)
enc := gob.NewEncoder(buff)
dec := gob.NewDecoder(buff)
if err := enc.Encode(src); err != nil {
return err
}
if err := dec.Decode(dest); err != nil {
return err
}
return nil
}
func RunningInUserNS() bool {
file, err := os.Open("/proc/self/uid_map")
if err != nil {
return false
}
defer file.Close()
buf := bufio.NewReader(file)
l, _, err := buf.ReadLine()
if err != nil {
return false
}
line := string(l)
var a, b, c int64
fmt.Sscanf(line, "%d %d %d", &a, &b, &c)
if a == 0 && b == 0 && c == 4294967295 {
return false
}
return true
}
func ValidHostname(name string) bool {
// Validate length
if len(name) < 1 || len(name) > 63 {
return false
}
// Validate first character
if strings.HasPrefix(name, "-") {
return false
}
if _, err := strconv.Atoi(string(name[0])); err == nil {
return false
}
// Validate last character
if strings.HasSuffix(name, "-") {
return false
}
// Validate the character set
match, _ := regexp.MatchString("^[-a-zA-Z0-9]*$", name)
if !match {
return false
}
return true
}
func TextEditor(inPath string, inContent []byte) ([]byte, error) {
var f *os.File
var err error
var path string
// Detect the text editor to use
editor := os.Getenv("VISUAL")
if editor == "" {
editor = os.Getenv("EDITOR")
if editor == "" {
for _, p := range []string{"editor", "vi", "emacs", "nano"} {
_, err := exec.LookPath(p)
if err == nil {
editor = p
break
}
}
if editor == "" {
return []byte{}, fmt.Errorf("No text editor found, please set the EDITOR environment variable.")
}
}
}
if inPath == "" {
// If provided input, create a new file
f, err = ioutil.TempFile("", "lxd_editor_")
if err != nil {
return []byte{}, err
}
if err = f.Chmod(0600); err != nil {
f.Close()
os.Remove(f.Name())
return []byte{}, err
}
f.Write(inContent)
f.Close()
path = f.Name()
defer os.Remove(path)
} else {
path = inPath
}
cmdParts := strings.Fields(editor)
cmd := exec.Command(cmdParts[0], append(cmdParts[1:], path)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return []byte{}, err
}
content, err := ioutil.ReadFile(path)
if err != nil {
return []byte{}, err
}
return content, nil
}
func ParseMetadata(metadata interface{}) (map[string]interface{}, error) {
newMetadata := make(map[string]interface{})
s := reflect.ValueOf(metadata)
if !s.IsValid() {
return nil, nil
}
if s.Kind() == reflect.Map {
for _, k := range s.MapKeys() {
if k.Kind() != reflect.String {
return nil, fmt.Errorf("Invalid metadata provided (key isn't a string).")
}
newMetadata[k.String()] = s.MapIndex(k).Interface()
}
} else if s.Kind() == reflect.Ptr && !s.Elem().IsValid() {
return nil, nil
} else {
return nil, fmt.Errorf("Invalid metadata provided (type isn't a map).")
}
return newMetadata, nil
}
// Parse a size string in bytes (e.g. 200kB or 5GB) into the number of bytes it
// represents. Supports suffixes up to EB. "" == 0.
func ParseByteSizeString(input string) (int64, error) {
if input == "" {
return 0, nil
}
if len(input) < 3 {
return -1, fmt.Errorf("Invalid value: %s", input)
}
// Extract the suffix
suffix := input[len(input)-2:]
// Extract the value
value := input[0 : len(input)-2]
valueInt, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return -1, fmt.Errorf("Invalid integer: %s", input)
}
if valueInt < 0 {
return -1, fmt.Errorf("Invalid value: %d", valueInt)
}
// Figure out the multiplicator
multiplicator := int64(0)
switch suffix {
case "kB":
multiplicator = 1024
case "MB":
multiplicator = 1024 * 1024
case "GB":
multiplicator = 1024 * 1024 * 1024
case "TB":
multiplicator = 1024 * 1024 * 1024 * 1024
case "PB":
multiplicator = 1024 * 1024 * 1024 * 1024 * 1024
case "EB":
multiplicator = 1024 * 1024 * 1024 * 1024 * 1024 * 1024
default:
return -1, fmt.Errorf("Unsupported suffix: %s", suffix)
}
return valueInt * multiplicator, nil
}
// Parse a size string in bits (e.g. 200kbit or 5Gbit) into the number of bits
// it represents. Supports suffixes up to Ebit. "" == 0.
func ParseBitSizeString(input string) (int64, error) {
if input == "" {
return 0, nil
}
if len(input) < 5 {
return -1, fmt.Errorf("Invalid value: %s", input)
}
// Extract the suffix
suffix := input[len(input)-4:]
// Extract the value
value := input[0 : len(input)-4]
valueInt, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return -1, fmt.Errorf("Invalid integer: %s", input)
}
if valueInt < 0 {
return -1, fmt.Errorf("Invalid value: %d", valueInt)
}
// Figure out the multiplicator
multiplicator := int64(0)
switch suffix {
case "kbit":
multiplicator = 1000
case "Mbit":
multiplicator = 1000 * 1000
case "Gbit":
multiplicator = 1000 * 1000 * 1000
case "Tbit":
multiplicator = 1000 * 1000 * 1000 * 1000
case "Pbit":
multiplicator = 1000 * 1000 * 1000 * 1000 * 1000
case "Ebit":
multiplicator = 1000 * 1000 * 1000 * 1000 * 1000 * 1000
default:
return -1, fmt.Errorf("Unsupported suffix: %s", suffix)
}
return valueInt * multiplicator, nil
}
func GetByteSizeString(input int64) string {
if input < 1024 {
return fmt.Sprintf("%d bytes", input)
}
value := float64(input)
for _, unit := range []string{"kB", "MB", "GB", "TB", "PB", "EB"} {
value = value / 1024
if value < 1024 {
return fmt.Sprintf("%.2f%s", value, unit)
}
}
return fmt.Sprintf("%.2fEB", value)
}
// RemoveDuplicatesFromString removes all duplicates of the string 'sep'
// from the specified string 's'. Leading and trailing occurences of sep
// are NOT removed (duplicate leading/trailing are). Performs poorly if
// there are multiple consecutive redundant separators.
func RemoveDuplicatesFromString(s string, sep string) string {
dup := sep + sep
for s = strings.Replace(s, dup, sep, -1); strings.Contains(s, dup); s = strings.Replace(s, dup, sep, -1) {
}
return s
}
type ProgressTracker struct {
Length int64
Handler func(int64, int64)
percentage float64
total int64
start *time.Time
last *time.Time
}
func (pt *ProgressTracker) Update(n int) {
// Skip the rest if no handler attached
if pt.Handler == nil {
return
}
// Initialize start time if needed
if pt.start == nil {
cur := time.Now()
pt.start = &cur
pt.last = pt.start
}
// Skip if no data to count
if n <= 0 {
return
}
// Update interval handling
var percentage float64
if pt.Length > 0 {
// If running in relative mode, check that we increased by at least 1%
percentage = float64(pt.total) / float64(pt.Length) * float64(100)
if percentage-pt.percentage < 0.9 {
return
}
} else {
// If running in absolute mode, check that at least a second elapsed
interval := time.Since(*pt.last).Seconds()
if interval < 1 {
return
}
}
// Determine speed
speedInt := int64(0)
duration := time.Since(*pt.start).Seconds()
if duration > 0 {
speed := float64(pt.total) / duration
speedInt = int64(speed)
}
// Determine progress
progressInt := int64(0)
if pt.Length > 0 {
pt.percentage = percentage
progressInt = int64(1 - (int(percentage) % 1) + int(percentage))
if progressInt > 100 {
progressInt = 100
}
} else {
progressInt = pt.total
// Update timestamp
cur := time.Now()
pt.last = &cur
}
pt.Handler(progressInt, speedInt)
}
type ProgressReader struct {
io.ReadCloser
Tracker *ProgressTracker
}
func (pt *ProgressReader) Read(p []byte) (int, error) {
// Do normal reader tasks
n, err := pt.ReadCloser.Read(p)
// Do the actual progress tracking
if pt.Tracker != nil {
pt.Tracker.total += int64(n)
pt.Tracker.Update(n)
}
return n, err
}
type ProgressWriter struct {
io.WriteCloser
Tracker *ProgressTracker
}
func (pt *ProgressWriter) Write(p []byte) (int, error) {
// Do normal writer tasks
n, err := pt.WriteCloser.Write(p)
// Do the actual progress tracking
if pt.Tracker != nil {
pt.Tracker.total += int64(n)
pt.Tracker.Update(n)
}
return n, err
}
func RunCommand(name string, arg ...string) error {
output, err := exec.Command(name, arg...).CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to run: %s %s: %s", name, strings.Join(arg, " "), strings.TrimSpace(string(output)))
}
return nil
}