/
multitape_stats.c
310 lines (257 loc) · 5.89 KB
/
multitape_stats.c
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
#include "bsdtar_platform.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "chunks.h"
#include "ctassert.h"
#include "multitape_internal.h"
#include "storage.h"
#include "sysendian.h"
#include "warnp.h"
#include "multitape.h"
/* Buffer length for printing dates. */
#define DATEBUFLEN 100
/*
* "Cookie" structure created by statstape_open and passed to other functions.
*/
struct multitape_stats_internal {
uint64_t machinenum;
CHUNKS_S * C;
STORAGE_R * SR;
};
static int callback_print(void *, struct chunkheader *);
/**
* callback_print(cookie, ch):
* Call chunks_stats_addchunk on the chunk stats cookie ${cookie} and the
* chunk header ${ch}.
*/
static int
callback_print(void * cookie, struct chunkheader * ch)
{
CHUNKS_S * C = cookie;
size_t len, zlen;
int rc;
/* Decode chunk header. */
len = le32dec(ch->len);
zlen = le32dec(ch->zlen);
if ((rc = chunks_stats_addchunk(C, ch->hash, len, zlen)) == 1) {
warn0("Directory is not consistent with archive: Run --fsck");
rc = -1;
}
/* Return status. */
return (rc);
}
/**
* statstape_open(machinenum, cachedir):
* Open the archive set in preparation for calls to _printglobal, _printall,
* and _print.
*/
TAPE_S *
statstape_open(uint64_t machinenum, const char * cachedir)
{
struct multitape_stats_internal * d;
/* Allocate memory. */
if ((d = malloc(sizeof(struct multitape_stats_internal))) == NULL)
goto err0;
d->machinenum = machinenum;
/* Obtain storage layer cookie. */
if ((d->SR = storage_read_init(machinenum)) == NULL)
goto err1;
/* Obtain chunk layer cookies. */
if (cachedir != NULL) {
if ((d->C = chunks_stats_init(cachedir)) == NULL)
goto err2;
} else {
d->C = NULL;
}
/* Success! */
return (d);
err2:
storage_read_free(d->SR);
err1:
free(d);
err0:
/* Failure! */
return (NULL);
}
/**
* statstape_printglobal(d):
* Print global statistics relating to a set of archives.
*/
int
statstape_printglobal(TAPE_S * d)
{
/* Ask the chunk storage layer to do this. */
if (chunks_stats_printglobal(stdout, d->C))
goto err0;
/* Success! */
return (0);
err0:
/* Failure! */
return (-1);
}
/**
* statstape_printall(d):
* Print statistics relating to each of the archives in a set.
*/
int
statstape_printall(TAPE_S * d)
{
struct tapemetadata tmd;
uint8_t * flist;
size_t nfiles;
size_t file;
/* Get a list of the metadata files. */
if (storage_directory_read(d->machinenum, 'm', &flist, &nfiles))
goto err0;
/* Iterate through the files. */
for (file = 0; file < nfiles; file++) {
/* Zero archive statistics. */
chunks_stats_zeroarchive(d->C);
/* Read the tape metadata. */
if (multitape_metadata_get_byhash(d->SR, d->C, &tmd,
&flist[file * 32], 0))
goto err1;
/* Compute statistics. */
if (multitape_chunkiter_tmd(d->SR, d->C, &tmd,
callback_print, d->C, 0))
goto err2;
/* Print the statistics. */
if (chunks_stats_printarchive(stdout, d->C, tmd.name))
goto err2;
/* Free parsed metadata. */
multitape_metadata_free(&tmd);
};
/* Free the list of files. */
free(flist);
/* Success! */
return (0);
err2:
multitape_metadata_free(&tmd);
err1:
free(flist);
err0:
/* Failure! */
return (-1);
}
/**
* statstape_printlist(d, verbose):
* Print the names of each of the archives in a set. If verbose > 0, print
* the creation times; if verbose > 1, print the argument vector of the
* program invocation which created the archive.
*/
int
statstape_printlist(TAPE_S * d, int verbose)
{
struct tapemetadata tmd;
uint8_t * flist;
size_t nfiles;
size_t file;
struct tm * ltime;
char datebuf[DATEBUFLEN];
int arg;
/* Get a list of the metadata files. */
if (storage_directory_read(d->machinenum, 'm', &flist, &nfiles))
goto err0;
/* Iterate through the files. */
for (file = 0; file < nfiles; file++) {
/* Read the tape metadata. */
if (multitape_metadata_get_byhash(d->SR, NULL, &tmd,
&flist[file * 32], 0))
goto err1;
/* Print name. */
if (fprintf(stdout, "%s", tmd.name) < 0) {
warnp("fprintf");
goto err2;
}
/* Print creation time. */
if (verbose > 0 && tmd.ctime != -1) {
if ((ltime = localtime(&tmd.ctime)) == NULL) {
warnp("localtime");
goto err2;
}
if (strftime(datebuf, DATEBUFLEN, "%F %T",
ltime) == 0) {
warnp("Cannot format date");
goto err2;
}
if (fprintf(stdout, "\t%s", datebuf) < 0) {
warnp("fprintf");
goto err2;
}
}
/* Print command line. */
if (verbose > 1) {
for (arg = 0; arg < tmd.argc; arg++) {
if (fprintf(stdout, arg ? " %s" : "\t%s",
tmd.argv[arg]) < 0) {
warnp("fprintf");
goto err2;
}
}
}
/* End line. */
if (fprintf(stdout, "\n") < 0) {
warnp("fprintf");
goto err2;
}
/* Free parsed metadata. */
multitape_metadata_free(&tmd);
};
/* Free the list of files. */
free(flist);
/* Success! */
return (0);
err2:
multitape_metadata_free(&tmd);
err1:
free(flist);
err0:
/* Failure! */
return (-1);
}
/**
* statstape_print(d, tapename):
* Print statistics relating to a specific archive in a set.
*/
int
statstape_print(TAPE_S * d, const char * tapename)
{
struct tapemetadata tmd;
/* Zero archive statistics. */
chunks_stats_zeroarchive(d->C);
/* Read the tape metadata. */
if (multitape_metadata_get_byname(d->SR, d->C, &tmd, tapename, 0))
goto err0;
if (multitape_chunkiter_tmd(d->SR, d->C, &tmd,
callback_print, d->C, 0))
goto err1;
/* Free parsed metadata. */
multitape_metadata_free(&tmd);
/* Print the statistics. */
if (chunks_stats_printarchive(stdout, d->C, tapename))
goto err0;
/* Success! */
return (0);
err1:
multitape_metadata_free(&tmd);
err0:
/* Failure! */
return (-1);
}
/**
* statstape_close(d):
* Close the given archive set.
*/
int
statstape_close(TAPE_S * d)
{
/* Free chunk layer cookies. */
chunks_stats_free(d->C);
/* Free storage layer cookie. */
storage_read_free(d->SR);
/* Free multitape cookie. */
free(d);
/* Success! */
return (0);
}