aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/pkg/tar.go
blob: 24411c50f73ec84e311a837299c41ddb4a2819b0 (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
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
package pkg

import (
	"archive/tar"
	"bytes"
	"compress/gzip"
	"context"
	"encoding/binary"
	"errors"
	"io"
	"io/fs"
	"net/http"
	"os"

	"hakurei.app/container/check"
)

const (
	// TarUncompressed denotes an uncompressed tarball.
	TarUncompressed = iota
	// TarGzip denotes a tarball compressed via [gzip].
	TarGzip
)

// A tarArtifact is an [Artifact] unpacking a tarball backed by a [File].
type tarArtifact struct {
	// Caller-supplied backing tarball.
	f File
	// Compression on top of the tarball.
	compression uint64
}

// NewTar returns a new [Artifact] backed by the supplied [File] and
// compression method.
func NewTar(f File, compression uint64) Artifact {
	return &tarArtifact{f: f, compression: compression}
}

// NewHTTPGetTar is abbreviation for NewHTTPGet passed to NewTar.
func NewHTTPGetTar(
	ctx context.Context,
	hc *http.Client,
	url string,
	checksum Checksum,
	compression uint64,
) Artifact {
	return NewTar(NewHTTPGet(ctx, hc, url, checksum), compression)
}

// Kind returns the hardcoded [Kind] constant.
func (a *tarArtifact) Kind() Kind { return KindTar }

// Params returns compression encoded in little endian.
func (a *tarArtifact) Params() []byte {
	return binary.LittleEndian.AppendUint64(nil, a.compression)
}

// Dependencies returns a slice containing the backing file.
func (a *tarArtifact) Dependencies() []Artifact {
	return []Artifact{a.f}
}

// A DisallowedTypeflagError describes a disallowed typeflag encountered while
// unpacking a tarball.
type DisallowedTypeflagError byte

func (e DisallowedTypeflagError) Error() string {
	return "disallowed typeflag '" + string(e) + "'"
}

// Cure cures the [Artifact], producing a directory located at work.
func (a *tarArtifact) Cure(work *check.Absolute, loadData CacheDataFunc) (err error) {
	var tr io.ReadCloser

	{
		var data []byte
		data, err = loadData(a.f)
		if err != nil {
			return
		}
		tr = io.NopCloser(bytes.NewReader(data))
	}

	defer func() {
		closeErr := tr.Close()
		if err == nil {
			err = closeErr
		}
	}()

	switch a.compression {
	case TarUncompressed:
		break

	case TarGzip:
		if tr, err = gzip.NewReader(tr); err != nil {
			return
		}
		break

	default:
		return os.ErrInvalid
	}

	type dirTargetPerm struct {
		path *check.Absolute
		mode fs.FileMode
	}
	var madeDirectories []dirTargetPerm

	var header *tar.Header
	r := tar.NewReader(tr)
	for header, err = r.Next(); err == nil; header, err = r.Next() {
		typeflag := header.Typeflag
		for {
			switch typeflag {
			case 0:
				if len(header.Name) > 0 && header.Name[len(header.Name)-1] == '/' {
					typeflag = tar.TypeDir
				} else {
					typeflag = tar.TypeReg
				}
				continue

			case tar.TypeReg:
				var f *os.File
				if f, err = os.OpenFile(
					work.Append(header.Name).String(),
					os.O_CREATE|os.O_EXCL|os.O_WRONLY,
					header.FileInfo().Mode()&0400,
				); err != nil {
					return
				}
				if _, err = io.Copy(f, r); err != nil {
					_ = f.Close()
					return
				} else if err = f.Close(); err != nil {
					return
				}
				break

			case tar.TypeLink:
				if err = os.Link(
					header.Linkname,
					work.Append(header.Name).String(),
				); err != nil {
					return
				}
				break

			case tar.TypeSymlink:
				if err = os.Symlink(
					header.Linkname,
					work.Append(header.Name).String(),
				); err != nil {
					return
				}
				break

			case tar.TypeDir:
				pathname := work.Append(header.Name)
				madeDirectories = append(madeDirectories, dirTargetPerm{
					path: pathname,
					mode: header.FileInfo().Mode(),
				})
				if err = os.MkdirAll(
					pathname.String(),
					0700,
				); err != nil {
					return
				}
				break

			case tar.TypeXGlobalHeader:
				// ignore
				break

			default:
				return DisallowedTypeflagError(typeflag)
			}

			break
		}
	}
	if errors.Is(err, io.EOF) {
		err = nil
	}

	if err == nil {
		for _, e := range madeDirectories {
			if err = os.Chmod(e.path.String(), e.mode&0500); err != nil {
				return
			}
		}
		err = os.Chmod(work.String(), 0500)
	}
	return
}