/
helpers.go
523 lines (453 loc) · 13.6 KB
/
helpers.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* 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 helpers
import (
"archive/tar"
"bytes"
"crypto/sha512"
"encoding/hex"
"fmt"
"io"
"math/rand"
"os"
"os/exec"
"os/user"
"path/filepath"
"reflect"
"strings"
"syscall"
"text/template"
"time"
"github.com/ubuntu-core/snappy/logger"
)
func init() {
// golang does not init Seed() itself
rand.Seed(time.Now().UTC().UnixNano())
}
// ChDir runs runs "f" inside the given directory
func ChDir(newDir string, f func() error) (err error) {
cwd, err := os.Getwd()
if err != nil {
return err
}
if err := os.Chdir(newDir); err != nil {
return err
}
defer os.Chdir(cwd)
return f()
}
// ExitCode extract the exit code from the error of a failed cmd.Run() or the
// original error if its not a exec.ExitError
func ExitCode(runErr error) (e int, err error) {
// golang, you are kidding me, right?
if exitErr, ok := runErr.(*exec.ExitError); ok {
waitStatus := exitErr.Sys().(syscall.WaitStatus)
e = waitStatus.ExitStatus()
return e, nil
}
return e, runErr
}
// TarIterFunc is called for each file inside a tar archive
type TarIterFunc func(r *tar.Reader, hdr *tar.Header) error
// TarIterate will take a io.Reader and call the fn callback on each tar
// archive member
func TarIterate(r io.Reader, fn TarIterFunc) error {
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err == io.EOF {
// end of tar archive
break
}
if err != nil {
return err
}
if err := fn(tr, hdr); err != nil {
return err
}
}
return nil
}
// IsSymlink checks whether the given os.FileMode corresponds to a symlink
func IsSymlink(mode os.FileMode) bool {
return (mode & os.ModeSymlink) == os.ModeSymlink
}
// IsDevice checks if the given os.FileMode coresponds to a device (char/block)
func IsDevice(mode os.FileMode) bool {
return (mode & (os.ModeDevice | os.ModeCharDevice)) != 0
}
// UnpackTarTransformFunc can be used to change the names during unpack
// or to return a error for files that are not acceptable
type UnpackTarTransformFunc func(path string) (newPath string, err error)
var mknod = syscall.Mknod
// UnpackTar unpacks the given tar file into the target directory
func UnpackTar(r io.Reader, targetDir string, fn UnpackTarTransformFunc) error {
// ensure we we extract with the original permissions
oldUmask := syscall.Umask(0)
defer syscall.Umask(oldUmask)
return TarIterate(r, func(tr *tar.Reader, hdr *tar.Header) (err error) {
// run tar transform func
name := hdr.Name
if fn != nil {
name, err = fn(hdr.Name)
if err != nil {
return err
}
}
path := filepath.Join(targetDir, name)
mode := hdr.FileInfo().Mode()
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
return err
}
switch {
case mode.IsDir():
err := os.Mkdir(path, mode)
if err != nil {
return nil
}
case IsSymlink(mode):
if err := os.Symlink(hdr.Linkname, path); err != nil {
return err
}
case IsDevice(mode):
switch {
case (mode & os.ModeCharDevice) != 0:
mode |= syscall.S_IFCHR
case (mode & os.ModeDevice) != 0:
mode |= syscall.S_IFBLK
}
devNum := Makedev(uint32(hdr.Devmajor), uint32(hdr.Devminor))
err = mknod(path, uint32(mode), int(devNum))
if err != nil {
return err
}
case mode.IsRegular():
out, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, mode)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, tr)
if err != nil {
return err
}
default:
return &ErrUnsupportedFileType{path, mode}
}
return nil
})
}
// ErrUnsupportedFileType is returned when trying to extract a file
// that is not a regular file, a directory, or a symlink.
type ErrUnsupportedFileType struct {
Name string
Mode os.FileMode
}
func (e ErrUnsupportedFileType) Error() string {
return fmt.Sprintf("%s: unsupported filetype %s", e.Name, e.Mode)
}
// Sha512sum returns the sha512 of the given file as a hexdigest
func Sha512sum(infile string) (hexdigest string, err error) {
r, err := os.Open(infile)
if err != nil {
return "", err
}
defer r.Close()
hasher := sha512.New()
if _, err := io.Copy(hasher, r); err != nil {
return "", err
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
// MakeMapFromEnvList takes a string list of the form "key=value"
// and returns a map[string]string from that list
// This is useful for os.Environ() manipulation
func MakeMapFromEnvList(env []string) map[string]string {
envMap := map[string]string{}
for _, l := range env {
split := strings.SplitN(l, "=", 2)
if len(split) != 2 {
return nil
}
envMap[split[0]] = split[1]
}
return envMap
}
// FileExists return true if given path can be stat()ed by us. Note that
// it may return false on e.g. permission issues.
func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// IsDirectory return true if the given path can be stat()ed by us and
// is a directory. Note that it may return false on e.g. permission issues.
func IsDirectory(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return fileInfo.IsDir()
}
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxy"
// MakeRandomString returns a random string of length length
var MakeRandomString = func(length int) string {
out := ""
for i := 0; i < length; i++ {
out += string(letters[rand.Intn(len(letters))])
}
return out
}
// NewSideloadVersion returns a version number such that later calls
// should return versions that compare larger.
func NewSideloadVersion() string {
n := time.Now().UTC().UnixNano()
bs := make([]byte, 12)
for i := 11; i >= 0; i-- {
bs[i] = letters[n&31]
n = n >> 5
}
return string(bs)
}
// AtomicWriteFlags are a bitfield of flags for AtomicWriteFile
type AtomicWriteFlags uint
const (
// AtomicWriteFollow makes AtomicWriteFile follow symlinks
AtomicWriteFollow AtomicWriteFlags = 1 << iota
)
// AtomicWriteFile updates the filename atomically and works otherwise
// like io/ioutil.WriteFile()
//
// Note that it won't follow symlinks and will replace existing symlinks
// with the real file
func AtomicWriteFile(filename string, data []byte, perm os.FileMode, flags AtomicWriteFlags) (err error) {
if flags&AtomicWriteFollow != 0 {
if fn, err := os.Readlink(filename); err == nil || (fn != "" && os.IsNotExist(err)) {
if filepath.IsAbs(fn) {
filename = fn
} else {
filename = filepath.Join(filepath.Dir(filename), fn)
}
}
}
tmp := filename + "." + MakeRandomString(12)
// XXX: if go switches to use aio_fsync, we need to open the dir for writing
dir, err := os.Open(filepath.Dir(filename))
if err != nil {
return err
}
defer dir.Close()
fd, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_EXCL, perm)
if err != nil {
return err
}
defer func() {
e := fd.Close()
if err == nil {
err = e
}
if err != nil {
os.Remove(tmp)
}
}()
// according to the docs, Write returns a non-nil error when n !=
// len(b), so don't worry about short writes.
if _, err := fd.Write(data); err != nil {
return err
}
if err := fd.Sync(); err != nil {
return err
}
if err := os.Rename(tmp, filename); err != nil {
return err
}
return dir.Sync()
}
// CurrentHomeDir returns the homedir of the current user. It looks at
// $HOME first and then at passwd
func CurrentHomeDir() (string, error) {
home := os.Getenv("HOME")
if home != "" {
return home, nil
}
user, err := user.Current()
if err != nil {
return "", err
}
return user.HomeDir, nil
}
// ShouldDropPrivs returns true if the application runs with sufficient
// privileges so that it should drop them
func ShouldDropPrivs() bool {
if groups, err := syscall.Getgroups(); err == nil {
for _, gid := range groups {
if gid == 0 {
return true
}
}
}
return syscall.Getuid() == 0 || syscall.Getgid() == 0
}
// MajorMinor returns the major/minor number of the given os.FileInfo
func MajorMinor(info os.FileInfo) (uint32, uint32, error) {
if !IsDevice(info.Mode()) {
return 0, 0, fmt.Errorf("No device %s", info.Name())
}
unixStat, ok := info.Sys().(*syscall.Stat_t)
if ok {
// see glibc: sysdeps/unix/sysv/linux/makedev.c
dev := unixStat.Rdev
major := uint32((dev>>8)&0xfff) | (uint32(dev>>32) & ^uint32(0xfff))
minor := uint32(dev&0xff) | (uint32(dev>>12) & ^uint32(0xff))
return major, minor, nil
}
return 0, 0, fmt.Errorf("failed to get syscall.stat_t for %v", info.Name())
}
// Makedev implements makedev(3)
func Makedev(major, minor uint32) uint32 {
return uint32((minor & 0xff) | ((major & 0xfff) << 8))
}
// Getattr get the attribute of the given name
func Getattr(i interface{}, name string) interface{} {
v := reflect.ValueOf(i)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
return v.FieldByName(name).Interface()
}
func fillSnapEnvVars(desc interface{}, vars []string) []string {
for i, v := range vars {
var templateOut bytes.Buffer
t := template.Must(template.New("wrapper").Parse(v))
if err := t.Execute(&templateOut, desc); err != nil {
// this can never happen, except we forget a variable
logger.Panicf("Unable to execute template: %v", err)
}
vars[i] = templateOut.String()
}
return vars
}
// GetBasicSnapEnvVars returns the app-level environment variables for a snap.
// Despite this being a bit snap-specific, this is in helpers.go because it's
// used by so many other modules, we run into circular dependencies if it's
// somewhere more reasonable like the snappy module.
func GetBasicSnapEnvVars(desc interface{}) []string {
return fillSnapEnvVars(desc, []string{
"SNAP={{.AppPath}}",
"SNAP_DATA=/var/lib{{.AppPath}}",
"TMPDIR=/tmp/snaps/{{.UdevAppName}}/{{.Version}}/tmp",
"TEMPDIR=/tmp/snaps/{{.UdevAppName}}/{{.Version}}/tmp",
"SNAP_NAME={{.AppName}}",
"SNAP_VERSION={{.Version}}",
"SNAP_ORIGIN={{.Origin}}",
"SNAP_FULLNAME={{.UdevAppName}}",
"SNAP_ARCH={{.AppArch}}",
})
}
// GetUserSnapEnvVars returns the user-level environment variables for a snap.
// Despite this being a bit snap-specific, this is in helpers.go because it's
// used by so many other modules, we run into circular dependencies if it's
// somewhere more reasonable like the snappy module.
func GetUserSnapEnvVars(desc interface{}) []string {
return fillSnapEnvVars(desc, []string{
"SNAP_USER_DATA={{.Home}}{{.AppPath}}",
})
}
// GetDeprecatedBasicSnapEnvVars returns the app-level deprecated environment
// variables for a snap.
// Despite this being a bit snap-specific, this is in helpers.go because it's
// used by so many other modules, we run into circular dependencies if it's
// somewhere more reasonable like the snappy module.
func GetDeprecatedBasicSnapEnvVars(desc interface{}) []string {
return fillSnapEnvVars(desc, []string{
// SNAP_
"SNAP_APP_PATH={{.AppPath}}",
"SNAP_APP_DATA_PATH=/var/lib{{.AppPath}}",
"SNAP_APP_TMPDIR=/tmp/snaps/{{.UdevAppName}}/{{.Version}}/tmp",
})
}
// GetDeprecatedUserSnapEnvVars returns the user-level deprecated environment
// variables for a snap.
// Despite this being a bit snap-specific, this is in helpers.go because it's
// used by so many other modules, we run into circular dependencies if it's
// somewhere more reasonable like the snappy module.
func GetDeprecatedUserSnapEnvVars(desc interface{}) []string {
return fillSnapEnvVars(desc, []string{
"SNAP_APP_USER_DATA_PATH={{.Home}}{{.AppPath}}",
})
}
// RSyncWithDelete syncs srcDir to destDir
func RSyncWithDelete(srcDirName, destDirName string) error {
// first remove everything thats not in srcdir
err := filepath.Walk(destDirName, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// relative to the root "destDirName"
relPath := path[len(destDirName):]
if !FileExists(filepath.Join(srcDirName, relPath)) {
if err := os.RemoveAll(path); err != nil {
return err
}
if info.IsDir() {
return filepath.SkipDir
}
}
return nil
})
if err != nil {
return err
}
// then copy or update the data from srcdir to destdir
err = filepath.Walk(srcDirName, func(src string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// relative to the root "srcDirName"
relPath := src[len(srcDirName):]
dst := filepath.Join(destDirName, relPath)
if info.IsDir() {
if err := os.MkdirAll(dst, info.Mode()); err != nil {
return err
}
// this can panic. The alternative would be to use the "st, ok" pattern, and then if !ok... panic?
st := info.Sys().(*syscall.Stat_t)
ts := []syscall.Timespec{st.Atim, st.Mtim}
return syscall.UtimesNano(dst, ts)
}
if !FilesAreEqual(src, dst) {
// XXX: we should (eventually) use CopyFile here,
// but we need to teach it about preserving
// of atime/mtime and permissions
output, err := exec.Command("cp", "-va", src, dst).CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to copy %s to %s (%s)", src, dst, output)
}
}
return nil
})
return err
}
// CopyIfDifferent copies src to dst only if dst is different that src
func CopyIfDifferent(src, dst string) error {
if !FilesAreEqual(src, dst) {
if err := CopyFile(src, dst, CopyFlagSync|CopyFlagOverwrite); err != nil {
return err
}
}
return nil
}