forked from rsc/goversion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
289 lines (271 loc) · 6.58 KB
/
main.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
// Copyright 2017 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.
// Goversion scans a directory tree and, for every executable it finds,
// prints the Go version used to build that executable.
//
// Usage:
//
// goversion [-crypto] [-m | -mh] [-v] path...
//
// The list of paths can be individual files or directories; if the latter,
// goversion scans all files in the directory tree, not following symlinks.
//
// Goversion scans inside of tar or gzipped tar archives that it finds
// (named *.tar, *.tar.gz, or *.tgz), but not recursively.
//
// The -crypto flag causes goversion to print additional information
// about the crypto libraries linked into each executable.
//
// The -m flag causes goversion to print the list of modules
// found in the executable, along with version information.
//
// The -mh flag causes goversion to print the list of modules
// found in the executable, along with version and hash information.
//
// The -v flag causes goversion to print information about every
// file it considers.
//
// Example
//
// Scan /usr/bin for Go binaries and print their versions:
//
// $ goversion /usr/bin
// /usr/bin/containerd go1.7.4
// /usr/bin/containerd-shim go1.7.4
// /usr/bin/ctr go1.7.4
// /usr/bin/docker go1.7.4
// /usr/bin/docker-proxy go1.7.4
// /usr/bin/dockerd go1.7.4
// /usr/bin/kbfsfuse go1.8.3
// /usr/bin/kbnm go1.8.3
// /usr/bin/keybase go1.8.3
// /usr/bin/snap go1.7.4
// /usr/bin/snapctl go1.7.4
//
package main // import "rsc.io/goversion"
import (
"archive/tar"
"bufio"
"compress/gzip"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"unicode/utf8"
"rsc.io/goversion/version"
)
var exe_extension string
var (
crypto = flag.Bool("crypto", false, "check kind of crypto library")
modinfo = flag.Bool("m", false, "show module info")
modhash = flag.Bool("mh", false, "show module and hash info")
verbose = flag.Bool("v", false, "print verbose information")
)
func init() {
flag.BoolVar(&version.DebugMatch, "d", version.DebugMatch, "print debug information")
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: goversion [-crypto] [-v] path...\n")
flag.PrintDefaults()
os.Exit(2)
}
func main() {
log.SetFlags(0)
log.SetPrefix("goversion: ")
flag.Usage = usage
flag.Parse()
if flag.NArg() == 0 {
usage()
}
out, err := exec.Command("go", "env", "GOEXE").Output()
if err != nil {
log.Fatal(err)
}
exe_extension = strings.TrimRight(fmt.Sprintf("%s", out), "\r\n")
for _, file := range flag.Args() {
info, err := os.Stat(file)
if err != nil {
log.Print(err)
continue
}
if info.IsDir() {
scandir(file)
} else {
scanfile(file, file, info, true)
}
}
}
func scandir(dir string) {
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.Mode().IsRegular() || info.Mode()&os.ModeSymlink != 0 {
scanfile(path, path, info, *verbose)
}
return nil
})
}
func scanfile(file, diskFile string, info os.FileInfo, mustPrint bool) {
if strings.HasSuffix(file, ".tar") {
if file != diskFile {
fmt.Fprintf(os.Stderr, "%s: not scanning tar recursively\n", file)
return
}
if mustPrint {
fmt.Fprintf(os.Stderr, "%s: scanning tar archive\n", file)
}
scantar(file, info)
return
}
if strings.HasSuffix(file, ".tar.gz") || strings.HasSuffix(file, ".tgz") {
if file != diskFile {
fmt.Fprintf(os.Stderr, "%s: not scanning tgz recursively\n", file)
return
}
if mustPrint {
fmt.Fprintf(os.Stderr, "%s: scanning tgz archive\n", file)
}
scantar(file, info)
return
}
if info.Mode()&os.ModeSymlink != 0 {
// Accept file symlinks, but not dir symlinks, to avoid cycles.
i, err := os.Stat(diskFile)
if err != nil || !i.Mode().IsRegular() {
if mustPrint {
fmt.Fprintf(os.Stderr, "%s: symlink\n", file)
}
return
}
info = i
}
if file == diskFile && info.Mode()&0111 == 0 && (exe_extension == "" || ! strings.HasSuffix(file, exe_extension)) {
if mustPrint {
fmt.Fprintf(os.Stderr, "%s: not executable\n", file)
}
return
}
v, err := version.ReadExe(diskFile)
if err != nil {
if mustPrint {
fmt.Fprintf(os.Stderr, "%s: %v\n", file, err)
}
return
}
buildVersion := v.Release
if *crypto {
switch {
case v.BoringCrypto && v.StandardCrypto:
buildVersion += " (boring AND standard crypto!!!)"
case v.BoringCrypto:
buildVersion += " (boring crypto)"
case v.StandardCrypto:
buildVersion += " (standard crypto)"
}
if v.FIPSOnly {
buildVersion += " +crypto/tls/fipsonly"
}
}
fmt.Printf("%s %s\n", file, buildVersion)
if (*modinfo || *modhash) && v.ModuleInfo != "" {
var rows [][]string
for _, line := range strings.Split(strings.TrimSpace(v.ModuleInfo), "\n") {
row := strings.Split(line, "\t")
if !*modhash && len(row) > 3 {
row = row[:3]
}
rows = append(rows, row)
}
printTable(os.Stdout, "\t", rows)
}
}
type Version struct {
Release string
BoringCrypto bool
StandardCrypto bool
}
func scantar(file string, info os.FileInfo) {
f, err := os.Open(file)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", file, err)
return
}
defer f.Close()
var r io.Reader = f
if strings.HasSuffix(file, "z") {
z, err := gzip.NewReader(r)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", file, err)
return
}
defer z.Close()
r = z
}
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err != nil {
break
}
if hdr.Typeflag != tar.TypeReg {
if *verbose {
fmt.Fprintf(os.Stderr, "%s/%s: not regular file\n", file, hdr.Name)
}
continue
}
if hdr.Mode&0111 == 0 {
if *verbose {
fmt.Fprintf(os.Stderr, "%s/%s: not executable\n", file, hdr.Name)
}
continue
}
// executable but not special
tmp, err := ioutil.TempFile("", "goversion-")
if err != nil {
log.Fatal(err)
}
io.Copy(tmp, tr)
tmpName := tmp.Name()
info, err := tmp.Stat()
if err != nil {
log.Fatal(err)
}
tmp.Close()
scanfile(file+"/"+hdr.Name, tmpName, info, *verbose)
os.Remove(tmpName)
}
}
func printTable(w io.Writer, prefix string, rows [][]string) {
var max []int
for _, row := range rows {
for i, c := range row {
n := utf8.RuneCountInString(c)
if i >= len(max) {
max = append(max, n)
} else if max[i] < n {
max[i] = n
}
}
}
b := bufio.NewWriter(w)
for _, row := range rows {
b.WriteString(prefix)
for len(row) > 0 && row[len(row)-1] == "" {
row = row[:len(row)-1]
}
for i, c := range row {
b.WriteString(c)
if i+1 < len(row) {
for j := utf8.RuneCountInString(c); j < max[i]+2; j++ {
b.WriteRune(' ')
}
}
}
b.WriteRune('\n')
}
b.Flush()
}