aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/pack/pack.go
blob: 82546ea7dcbba68caa8dad7d77c52562e944a899 (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
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
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"cmd/internal/archive"
	"fmt"
	"io"
	"io/fs"
	"log"
	"os"
	"path/filepath"
)

const usageMessage = `Usage: pack op file.a [name....]
Where op is one of cprtx optionally followed by v for verbose output.
For compatibility with old Go build environments the op string grc is
accepted as a synonym for c.

For more information, run
	go doc cmd/pack`

func usage() {
	fmt.Fprintln(os.Stderr, usageMessage)
	os.Exit(2)
}

func main() {
	log.SetFlags(0)
	log.SetPrefix("pack: ")
	// need "pack op archive" at least.
	if len(os.Args) < 3 {
		log.Print("not enough arguments")
		fmt.Fprintln(os.Stderr)
		usage()
	}
	setOp(os.Args[1])
	var ar *Archive
	switch op {
	case 'p':
		ar = openArchive(os.Args[2], os.O_RDONLY, os.Args[3:])
		ar.scan(ar.printContents)
	case 'r':
		ar = openArchive(os.Args[2], os.O_RDWR, os.Args[3:])
		ar.addFiles()
	case 'c':
		ar = openArchive(os.Args[2], os.O_RDWR|os.O_TRUNC|os.O_CREATE, os.Args[3:])
		ar.addPkgdef()
		ar.addFiles()
	case 't':
		ar = openArchive(os.Args[2], os.O_RDONLY, os.Args[3:])
		ar.scan(ar.tableOfContents)
	case 'x':
		ar = openArchive(os.Args[2], os.O_RDONLY, os.Args[3:])
		ar.scan(ar.extractContents)
	default:
		log.Printf("invalid operation %q", os.Args[1])
		fmt.Fprintln(os.Stderr)
		usage()
	}
	if len(ar.files) > 0 {
		log.Fatalf("file %q not in archive", ar.files[0])
	}
}

// The unusual ancestry means the arguments are not Go-standard.
// These variables hold the decoded operation specified by the first argument.
// op holds the operation we are doing (prtx).
// verbose tells whether the 'v' option was specified.
var (
	op      rune
	verbose bool
)

// setOp parses the operation string (first argument).
func setOp(arg string) {
	// Recognize 'go tool pack grc' because that was the
	// formerly canonical way to build a new archive
	// from a set of input files. Accepting it keeps old
	// build systems working with both Go 1.2 and Go 1.3.
	if arg == "grc" {
		arg = "c"
	}

	for _, r := range arg {
		switch r {
		case 'c', 'p', 'r', 't', 'x':
			if op != 0 {
				// At most one can be set.
				usage()
			}
			op = r
		case 'v':
			if verbose {
				// Can be set only once.
				usage()
			}
			verbose = true
		default:
			usage()
		}
	}
}

const (
	arHeader = "!<arch>\n"
)

// An Archive represents an open archive file. It is always scanned sequentially
// from start to end, without backing up.
type Archive struct {
	a        *archive.Archive
	files    []string // Explicit list of files to be processed.
	pad      int      // Padding bytes required at end of current archive file
	matchAll bool     // match all files in archive
}

// archive opens (and if necessary creates) the named archive.
func openArchive(name string, mode int, files []string) *Archive {
	f, err := os.OpenFile(name, mode, 0666)
	if err != nil {
		log.Fatal(err)
	}
	var a *archive.Archive
	if mode&os.O_CREATE != 0 { // the c command
		a, err = archive.New(f)
	} else {
		a, err = archive.Parse(f, verbose)
	}
	if err != nil {
		log.Fatal(err)
	}
	return &Archive{
		a:        a,
		files:    files,
		matchAll: len(files) == 0,
	}
}

// scan scans the archive and executes the specified action on each entry.
func (ar *Archive) scan(action func(*archive.Entry)) {
	for i := range ar.a.Entries {
		e := &ar.a.Entries[i]
		action(e)
	}
}

// listEntry prints to standard output a line describing the entry.
func listEntry(e *archive.Entry, verbose bool) {
	if verbose {
		fmt.Fprintf(stdout, "%s\n", e.String())
	} else {
		fmt.Fprintf(stdout, "%s\n", e.Name)
	}
}

// output copies the entry to the specified writer.
func (ar *Archive) output(e *archive.Entry, w io.Writer) {
	r := io.NewSectionReader(ar.a.File(), e.Offset, e.Size)
	n, err := io.Copy(w, r)
	if err != nil {
		log.Fatal(err)
	}
	if n != e.Size {
		log.Fatal("short file")
	}
}

// match reports whether the entry matches the argument list.
// If it does, it also drops the file from the to-be-processed list.
func (ar *Archive) match(e *archive.Entry) bool {
	if ar.matchAll {
		return true
	}
	for i, name := range ar.files {
		if e.Name == name {
			copy(ar.files[i:], ar.files[i+1:])
			ar.files = ar.files[:len(ar.files)-1]
			return true
		}
	}
	return false
}

// addFiles adds files to the archive. The archive is known to be
// sane and we are positioned at the end. No attempt is made
// to check for existing files.
func (ar *Archive) addFiles() {
	if len(ar.files) == 0 {
		usage()
	}
	for _, file := range ar.files {
		if verbose {
			fmt.Printf("%s\n", file)
		}

		f, err := os.Open(file)
		if err != nil {
			log.Fatal(err)
		}
		aro, err := archive.Parse(f, false)
		if err != nil || !isGoCompilerObjFile(aro) {
			f.Seek(0, io.SeekStart)
			ar.addFile(f)
			goto close
		}

		for _, e := range aro.Entries {
			if e.Type != archive.EntryGoObj || e.Name != "_go_.o" {
				continue
			}
			ar.a.AddEntry(archive.EntryGoObj, filepath.Base(file), 0, 0, 0, 0644, e.Size, io.NewSectionReader(f, e.Offset, e.Size))
		}
	close:
		f.Close()
	}
	ar.files = nil
}

// FileLike abstracts the few methods we need, so we can test without needing real files.
type FileLike interface {
	Name() string
	Stat() (fs.FileInfo, error)
	Read([]byte) (int, error)
	Close() error
}

// addFile adds a single file to the archive
func (ar *Archive) addFile(fd FileLike) {
	// Format the entry.
	// First, get its info.
	info, err := fd.Stat()
	if err != nil {
		log.Fatal(err)
	}
	// mtime, uid, gid are all zero so repeated builds produce identical output.
	mtime := int64(0)
	uid := 0
	gid := 0
	ar.a.AddEntry(archive.EntryNativeObj, info.Name(), mtime, uid, gid, info.Mode(), info.Size(), fd)
}

// addPkgdef adds the __.PKGDEF file to the archive, copied
// from the first Go object file on the file list, if any.
// The archive is known to be empty.
func (ar *Archive) addPkgdef() {
	done := false
	for _, file := range ar.files {
		f, err := os.Open(file)
		if err != nil {
			log.Fatal(err)
		}
		aro, err := archive.Parse(f, false)
		if err != nil || !isGoCompilerObjFile(aro) {
			goto close
		}

		for _, e := range aro.Entries {
			if e.Type != archive.EntryPkgDef {
				continue
			}
			if verbose {
				fmt.Printf("__.PKGDEF # %s\n", file)
			}
			ar.a.AddEntry(archive.EntryPkgDef, "__.PKGDEF", 0, 0, 0, 0644, e.Size, io.NewSectionReader(f, e.Offset, e.Size))
			done = true
		}
	close:
		f.Close()
		if done {
			break
		}
	}
}

// Finally, the actual commands. Each is an action.

// can be modified for testing.
var stdout io.Writer = os.Stdout

// printContents implements the 'p' command.
func (ar *Archive) printContents(e *archive.Entry) {
	ar.extractContents1(e, stdout)
}

// tableOfContents implements the 't' command.
func (ar *Archive) tableOfContents(e *archive.Entry) {
	if ar.match(e) {
		listEntry(e, verbose)
	}
}

// extractContents implements the 'x' command.
func (ar *Archive) extractContents(e *archive.Entry) {
	ar.extractContents1(e, nil)
}

func (ar *Archive) extractContents1(e *archive.Entry, out io.Writer) {
	if ar.match(e) {
		if verbose {
			listEntry(e, false)
		}
		if out == nil {
			f, err := os.OpenFile(e.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0444 /*e.Mode*/)
			if err != nil {
				log.Fatal(err)
			}
			defer f.Close()
			out = f
		}
		ar.output(e, out)
	}
}

// isGoCompilerObjFile reports whether file is an object file created
// by the Go compiler, which is an archive file with exactly two entries:
// __.PKGDEF and _go_.o.
func isGoCompilerObjFile(a *archive.Archive) bool {
	if len(a.Entries) != 2 {
		return false
	}
	var foundPkgDef, foundGo bool
	for _, e := range a.Entries {
		if e.Type == archive.EntryPkgDef && e.Name == "__.PKGDEF" {
			foundPkgDef = true
		}
		if e.Type == archive.EntryGoObj && e.Name == "_go_.o" {
			foundGo = true
		}
	}
	return foundPkgDef && foundGo
}