aboutsummaryrefslogtreecommitdiffsponsor
path: root/archive.go
blob: fa1886f000c273a1dda3a8f5e42b59205fca73cc (plain)
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
package pkgtools

import (
	"archive/tar"
	"fmt"
	"io"
	"os"
	"path"
	"path/filepath"

	"github.com/pkg/errors"
	"golang.org/x/sys/unix"
)

type ArchiveReader interface {
	Next() (*tar.Header, error)
	Read([]byte) (int, error)
}

const NewDirMod = 0755

func tarCreateBlockDev(target string, header *tar.Header) error {
	mode := uint32(header.Mode & 07777)
	mode |= unix.S_IFBLK
	dev := unix.Mkdev(uint32(header.Devmajor), uint32(header.Devminor))
	err := unix.Mknod(target, mode, int(dev))
	return err
}

func tarCreateCharDev(target string, header *tar.Header) error {
	mode := uint32(header.Mode & 07777)
	mode |= unix.S_IFCHR
	dev := unix.Mkdev(uint32(header.Devmajor), uint32(header.Devminor))
	err := unix.Mknod(target, mode, int(dev))
	return err
}

func tarCreateDir(target string) error {
	err := os.MkdirAll(target, NewDirMod)
	return err
}

func tarCreateFifo(target string, header *tar.Header) error {
	mode := uint32(header.Mode & 07777)
	mode |= unix.S_IFIFO
	dev := unix.Mkdev(uint32(header.Devmajor), uint32(header.Devminor))
	err := unix.Mknod(target, mode, int(dev))
	return err
}

func tarCreateReg(target string, r io.Reader, header *tar.Header) error {
	f, err := os.OpenFile(
		target,
		os.O_CREATE|os.O_RDWR,
		os.FileMode(header.Mode),
	)
	if err != nil {
		// it could be an error due to writing over a running executable.
		err = os.Remove(target)
		if err != nil {
			return err
		}

		f, err = os.OpenFile(
			target,
			os.O_CREATE|os.O_RDWR,
			os.FileMode(header.Mode),
		)
		if err != nil {
			return err
		}
	}
	defer f.Close()

	// copy over contents
	if _, err := io.Copy(f, r); err != nil {
		return err
	}
	return nil
}

func tarCreateLink(root string, header *tar.Header) error {
	oldName := filepath.Join(root, header.Linkname)
	if filepath.IsAbs(header.Linkname) {
		filepath.Join(root, oldName)
	}
	newName := filepath.Join(root, header.Name)
	err := os.Link(oldName, newName)
	if err != nil {
		_ = os.Remove(newName)
		err = os.Link(oldName, newName)
	}
	return err
}

func tarCreateSymlink(root string, header *tar.Header) error {
	oldName := filepath.Join(root, header.Linkname)
	if filepath.IsAbs(header.Linkname) {
		filepath.Join(root, oldName)
	}
	newName := filepath.Join(root, header.Name)
	err := os.Symlink(oldName, newName)
	if err != nil {
		_ = os.Remove(newName)
		err = os.Symlink(oldName, newName)
	}
	return err
}

type InstallArchiveCfg struct {
	Root  string
	Chown bool
	Chmod bool
}

func InstallArchive(
	r ArchiveReader,
	cfg *InstallArchiveCfg,
) error {
	for {
		header, err := r.Next()
		if err == io.EOF {
			break
		} else if err != nil {
			return err
		}
		if header == nil {
			continue
		}
		target := path.Join(cfg.Root, header.Name)

		switch header.Typeflag {
		case tar.TypeBlock:
			err = tarCreateBlockDev(target, header)
		case tar.TypeChar:
			err = tarCreateCharDev(target, header)
		case tar.TypeFifo:
			err = tarCreateFifo(target, header)
		case tar.TypeDir:
			err = tarCreateDir(target)
		case tar.TypeReg:
			err = tarCreateReg(target, r, header)
		case tar.TypeLink:
			err = tarCreateLink(cfg.Root, header)
		case tar.TypeSymlink:
			err = tarCreateSymlink(cfg.Root, header)
		default:
			err = errors.Errorf("unhandled file type '%c'", header.Typeflag)
		}
		if err != nil {
			return errors.Wrap(err, "unpacking tar archive")
		}
		if cfg.Chown {
			if err = os.Chown(target, header.Uid, header.Gid); err != nil {
				fmt.Fprintln(os.Stderr, err)
			}
		}
		if cfg.Chmod {
			if err = os.Chmod(target, header.FileInfo().Mode()); err != nil {
				fmt.Fprintln(os.Stderr, err)
			}
		}
	}

	return nil
}