Skip to content

Commit 3689f0b

Browse files
timothy-kingGo LUCI
authored and
Go LUCI
committed
internal/gcimporter: reuse archive.ReadHeader implementation
Adds an internal copy of the GOROOT/src/cmd/internal/archive.ReadHeader to x/tools. This replaces readGopackHeader. The result is one less different implementation of reading an archive header between GOROOT and x/tools. Updates golang/go#70651 Change-Id: I69028da6472d6b06a613b554158301894326e735 Reviewed-on: https://go-review.googlesource.com/c/tools/+/633657 Commit-Queue: Tim King <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 2cc4218 commit 3689f0b

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

internal/gcimporter/exportdata.go

+5-35
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,9 @@ package gcimporter
1111
import (
1212
"bufio"
1313
"fmt"
14-
"io"
15-
"strconv"
1614
"strings"
1715
)
1816

19-
func readGopackHeader(r *bufio.Reader) (name string, size int64, err error) {
20-
// See $GOROOT/include/ar.h.
21-
hdr := make([]byte, 16+12+6+6+8+10+2)
22-
_, err = io.ReadFull(r, hdr)
23-
if err != nil {
24-
return
25-
}
26-
// leave for debugging
27-
if false {
28-
fmt.Printf("header: %s", hdr)
29-
}
30-
s := strings.TrimSpace(string(hdr[16+12+6+6+8:][:10]))
31-
length, err := strconv.Atoi(s)
32-
size = int64(length)
33-
if err != nil || hdr[len(hdr)-2] != '`' || hdr[len(hdr)-1] != '\n' {
34-
err = fmt.Errorf("invalid archive header")
35-
return
36-
}
37-
name = strings.TrimSpace(string(hdr[:16]))
38-
return
39-
}
40-
4117
// FindExportData positions the reader r at the beginning of the
4218
// export data section of an underlying cmd/compile created archive
4319
// file by reading from it. The reader must be positioned at the
@@ -61,19 +37,13 @@ func FindExportData(r *bufio.Reader) (size int64, err error) {
6137
return
6238
}
6339

64-
// Archive file. Scan to __.PKGDEF.
65-
var arsize int64
66-
var name string
67-
if name, arsize, err = readGopackHeader(r); err != nil {
68-
return
69-
}
70-
size = arsize
71-
72-
// First entry should be __.PKGDEF.
73-
if name != "__.PKGDEF" {
74-
err = fmt.Errorf("go archive is missing __.PKGDEF")
40+
// Archive file with the first file being __.PKGDEF.
41+
arsize := readArchiveHeader(r, "__.PKGDEF")
42+
if arsize <= 0 {
43+
err = fmt.Errorf("not a package file")
7544
return
7645
}
46+
size = int64(arsize)
7747

7848
// Read first line of __.PKGDEF data, so that line
7949
// is once again the first line of the input.

internal/gcimporter/support.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gcimporter
6+
7+
import (
8+
"bufio"
9+
"io"
10+
"strconv"
11+
"strings"
12+
)
13+
14+
// Copy of $GOROOT/src/cmd/internal/archive.ReadHeader.
15+
func readArchiveHeader(b *bufio.Reader, name string) int {
16+
// architecture-independent object file output
17+
const HeaderSize = 60
18+
19+
var buf [HeaderSize]byte
20+
if _, err := io.ReadFull(b, buf[:]); err != nil {
21+
return -1
22+
}
23+
aname := strings.Trim(string(buf[0:16]), " ")
24+
if !strings.HasPrefix(aname, name) {
25+
return -1
26+
}
27+
asize := strings.Trim(string(buf[48:58]), " ")
28+
i, _ := strconv.Atoi(asize)
29+
return i
30+
}

0 commit comments

Comments
 (0)