summaryrefslogtreecommitdiff
path: root/internal/app/siftool/info.go
blob: f1f6f32fbc5411c74d3f5aab5dad970c42b0e575 (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
// Copyright (c) 2018-2021, Sylabs Inc. All rights reserved.
// Copyright (c) 2018, Divya Cote <divya.cote@gmail.com> All rights reserved.
// Copyright (c) 2017, SingularityWare, LLC. All rights reserved.
// Copyright (c) 2017, Yannick Cote <yhcote@gmail.com> All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package siftool

import (
	"fmt"
	"io"
	"math"
	"strings"
	"text/tabwriter"

	"github.com/google/uuid"
	"github.com/sylabs/sif/v2/pkg/sif"
)

// readableSize returns the size in human readable format.
func readableSize(size int64) string {
	if -1024 < size && size < 1024 {
		return fmt.Sprintf("%d B", size)
	}

	units := "KMGTPE"

	div, exp := uint64(1024), 0
	for n := size / 1024; (n <= -1024) || (1024 <= n); n /= 1024 {
		div *= 1024
		exp++
	}

	return fmt.Sprintf("%.0f %ciB", math.Round(float64(size)/float64(div)), units[exp])
}

// writeHeader writes header information in f to w.
func writeHeader(w io.Writer, f *sif.FileImage) error {
	tw := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)

	if s := f.LaunchScript(); s != "" {
		fmt.Fprintf(tw, "Launch Script:\t%v\n", strings.TrimSuffix(s, "\n"))
	}

	fmt.Fprintf(tw, "Version:\t%v\n", f.Version())

	if arch := f.PrimaryArch(); arch != "unknown" {
		fmt.Fprintf(tw, "Primary Architecture:\t%v\n", arch)
	}

	if id := f.ID(); id != uuid.Nil.String() {
		fmt.Fprintf(tw, "ID:\t%v\n", id)
	}

	if t := f.CreatedAt(); !t.IsZero() {
		fmt.Fprintf(tw, "Created At:\t%v\n", t.UTC())
	}

	if t := f.ModifiedAt(); !t.IsZero() {
		fmt.Fprintf(tw, "Modified At:\t%v\n", t.UTC())
	}

	fmt.Fprintf(tw, "Descriptors Free:\t%v\n", f.DescriptorsFree())
	fmt.Fprintf(tw, "Descriptors Total:\t%v\n", f.DescriptorsTotal())
	fmt.Fprintf(tw, "Descriptors Offset:\t%v\n", f.DescriptorsOffset())
	fmt.Fprintf(tw, "Descriptors Size:\t%v\n", readableSize(f.DescriptorsSize()))
	fmt.Fprintf(tw, "Data Offset:\t%v\n", f.DataOffset())
	fmt.Fprintf(tw, "Data Size:\t%v\n", readableSize(f.DataSize()))

	return tw.Flush()
}

// Header displays a SIF file global header.
func (a *App) Header(path string) error {
	return withFileImage(path, false, func(f *sif.FileImage) error {
		return writeHeader(a.opts.out, f)
	})
}

// writeList writes the list of descriptors in f to w.
func writeList(w io.Writer, f *sif.FileImage) error {
	fmt.Fprintln(w, ("------------------------------------------------------------------------------"))
	fmt.Fprintf(w, "%-4s %-8s %-8s %-26s %s\n", "ID", "|GROUP", "|LINK", "|SIF POSITION (start-end)", "|TYPE")
	fmt.Fprintln(w, ("------------------------------------------------------------------------------"))

	f.WithDescriptors(func(d sif.Descriptor) bool {
		fmt.Fprintf(w, "%-4d ", d.ID())

		if id := d.GroupID(); id == 0 {
			fmt.Fprintf(w, "|%-7s ", "NONE")
		} else {
			fmt.Fprintf(w, "|%-7d ", id)
		}

		switch id, isGroup := d.LinkedID(); {
		case id == 0:
			fmt.Fprintf(w, "|%-7s ", "NONE")
		case isGroup:
			fmt.Fprintf(w, "|%-3d (G) ", id)
		default:
			fmt.Fprintf(w, "|%-7d ", id)
		}

		fmt.Fprintf(w, "%-26s ", fmt.Sprintf("|%d-%d ", d.Offset(), d.Offset()+d.Size()))

		switch dt := d.DataType(); dt {
		case sif.DataPartition:
			fs, pt, arch, err := d.PartitionMetadata()
			if err == nil {
				fmt.Fprintf(w, "|%s (%s/%s/%s)\n", dt, fs, pt, arch)
			}
		case sif.DataSignature:
			ht, _, err := d.SignatureMetadata()
			if err == nil {
				fmt.Fprintf(w, "|%s (%s)\n", dt, ht)
			}
		case sif.DataCryptoMessage:
			ft, mt, err := d.CryptoMessageMetadata()
			if err == nil {
				fmt.Fprintf(w, "|%s (%s/%s)\n", dt, ft, mt)
			}
		default:
			fmt.Fprintf(w, "|%s\n", dt)
		}

		return false
	})

	return nil
}

// List displays a list of all active descriptors from a SIF file.
func (a *App) List(path string) error {
	return withFileImage(path, false, func(f *sif.FileImage) error {
		return writeList(a.opts.out, f)
	})
}

// writeInfo writes info about d to w.
func writeInfo(w io.Writer, v sif.Descriptor) error {
	tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)

	fmt.Fprintf(tw, "\tData Type:\t%v\n", v.DataType())
	fmt.Fprintf(tw, "\tID:\t%v\n", v.ID())

	fmt.Fprint(tw, "\tGroup ID:\t")
	if id := v.GroupID(); id == 0 {
		fmt.Fprintln(tw, "NONE")
	} else {
		fmt.Fprintln(tw, id)
	}

	fmt.Fprint(tw, "\tLinked ID:\t")
	switch id, isGroup := v.LinkedID(); {
	case id == 0:
		fmt.Fprintln(tw, "NONE")
	case isGroup:
		fmt.Fprintln(tw, id, "(G)")
	default:
		fmt.Fprintln(tw, id)
	}

	fmt.Fprintf(tw, "\tOffset:\t%v\n", v.Offset())
	fmt.Fprintf(tw, "\tSize:\t%v\n", v.Size())

	if t := v.CreatedAt(); !t.IsZero() {
		fmt.Fprintf(tw, "\tCreated At:\t%v\n", t.UTC())
	}

	if t := v.ModifiedAt(); !t.IsZero() {
		fmt.Fprintf(tw, "\tModified At:\t%v\n", t.UTC())
	}

	if nm := v.Name(); nm != "" {
		fmt.Fprintf(tw, "\tName:\t%v\n", nm)
	}

	switch v.DataType() {
	case sif.DataPartition:
		fs, pt, arch, err := v.PartitionMetadata()
		if err != nil {
			return err
		}

		fmt.Fprintf(tw, "\tFilesystem Type:\t%v\n", fs)
		fmt.Fprintf(tw, "\tPartition Type:\t%v\n", pt)
		fmt.Fprintf(tw, "\tArchitecture:\t%v\n", arch)

	case sif.DataSignature:
		ht, fp, err := v.SignatureMetadata()
		if err != nil {
			return err
		}

		fmt.Fprintf(tw, "\tHash Type:\t%v\n", ht)
		fmt.Fprintf(tw, "\tEntity:\t%X\n", fp)

	case sif.DataCryptoMessage:
		ft, mt, err := v.CryptoMessageMetadata()
		if err != nil {
			return err
		}

		fmt.Fprintf(tw, "\tFormat Type:\t%v\n", ft)
		fmt.Fprintf(tw, "\tMessage Type:\t%v\n", mt)
	}

	return tw.Flush()
}

// Info displays detailed info about a descriptor from a SIF file.
func (a *App) Info(path string, id uint32) error {
	return withFileImage(path, false, func(f *sif.FileImage) error {
		d, err := f.GetDescriptor(sif.WithID(id))
		if err != nil {
			return err
		}

		return writeInfo(a.opts.out, d)
	})
}

// Dump extracts and outputs a data object from a SIF file.
func (a *App) Dump(path string, id uint32) error {
	return withFileImage(path, false, func(f *sif.FileImage) error {
		d, err := f.GetDescriptor(sif.WithID(id))
		if err != nil {
			return err
		}

		_, err = io.CopyN(a.opts.out, d.GetReader(), d.Size())
		return err
	})
}