-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.c
262 lines (216 loc) · 7.14 KB
/
table.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
// table.c - drawing a custom table of file extents (implementation)
//
// This file is part of extents, tools for querying and accessing file extents.
//
// Written in 2019 by Eliah Kagan <[email protected]>.
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// <http://creativecommons.org/publicdomain/zero/1.0/>.
#include "table.h"
#include "attribute.h"
#include "constants.h"
#include "util.h"
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/fiemap.h>
#include <linux/types.h>
#include <sys/ioctl.h>
ATTRIBUTE((malloc, returns_nonnull))
static struct tablespec *alloc_tablespec(const int col_count)
{
ASSERT_NONNEGATIVE_INT_FITS_IN_SIZE_T();
assert(col_count >= 0);
struct tablespec *const tsp =
xcalloc(1u, sizeof(struct tablespec)
+ sizeof(struct colspec) * (size_t)col_count);
tsp->col_count = col_count;
return tsp;
}
ATTRIBUTE((nonnull))
static inline __u64 get_raw(const struct fiemap_extent *const fep,
const enum datum datum)
{
assert(fep);
switch (datum) {
case k_datum_logical: return fep->fe_logical;
case k_datum_physical: return fep->fe_physical;
case k_datum_length: return fep->fe_length;
case k_datum_physical_end: return fep->fe_physical + fep->fe_length;
}
die(BUG("unrecognized datum selection"));
}
ATTRIBUTE((nonnull))
static inline __u64
get(const struct fiemap *restrict const fmp,
const struct colspec *restrict const csp, const __u32 row_index)
{
assert(fmp);
assert(csp);
assert(row_index < fmp->fm_mapped_extents);
const __u64 raw = get_raw(&fmp->fm_extents[row_index], csp->datum);
return (raw + csp->offset) / csp->divisor;
}
ATTRIBUTE((nonnull))
static void set_widths_from_labels(struct tablespec *const tsp)
{
assert(tsp);
assert(tsp->col_count >= 0);
for (int col_index = 0; col_index < tsp->col_count; ++col_index) {
struct colspec *const csp = &tsp->cols[col_index];
assert(csp->label);
const size_t width = strlen(csp->label);
if (width > (size_t)INT_MAX) die(BUG("label too big to format"));
csp->width = (int)width;
}
}
ATTRIBUTE((nonnull))
static void update_widths_from_values(struct tablespec *const tsp)
{
assert(tsp);
assert(tsp->col_count >= 0);
const __u32 row_count = tsp->fmp->fm_mapped_extents;
for (__u32 row_index = 0u; row_index < row_count; ++row_index) {
for (int col_index = 0; col_index < tsp->col_count; ++col_index) {
struct colspec *const csp = &tsp->cols[col_index];
const __u64 value = get(tsp->fmp, csp, row_index);
csp->width = max(csp->width, snprintf(NULL, 0u, "%llu", value));
}
}
}
ATTRIBUTE((nonnull))
static void populate_widths(struct tablespec *const tsp)
{
assert(tsp);
set_widths_from_labels(tsp);
update_widths_from_values(tsp);
}
ATTRIBUTE((nonnull))
static void show_labels(const struct tablespec *const tsp)
{
assert(tsp);
assert(tsp->gap_width > 0 && tsp->col_count >= 0);
for (int col_index = 0; col_index < tsp->col_count; ++col_index) {
const struct colspec *const csp = &tsp->cols[col_index];
assert(csp->label);
printf("%*s", tsp->gap_width + csp->width, csp->label);
}
putchar('\n');
}
ATTRIBUTE((nonnull))
static void show_all_rows(const struct tablespec *const tsp)
{
assert(tsp);
assert(tsp->gap_width > 0 && tsp->col_count >= 0);
const __u32 row_count = tsp->fmp->fm_mapped_extents;
for (__u32 row_index = 0u; row_index < row_count; ++row_index) {
for (int col_index = 0; col_index < tsp->col_count; ++col_index) {
const struct colspec *const csp = &tsp->cols[col_index];
const __u64 value = get(tsp->fmp, csp, row_index);
printf("%*llu", tsp->gap_width + csp->width, value);
}
putchar('\n');
}
}
ATTRIBUTE((nonnull))
static void show_populated_table(const struct tablespec *const tsp)
{
assert(tsp);
show_labels(tsp);
show_all_rows(tsp);
}
// Currently this is just the length of the string, because the user specifies
// each column using a single character, with no extraneous characters allowed.
ATTRIBUTE((nonnull))
static int count_columns(const char *const columns)
{
ASSERT_NONNEGATIVE_INT_FITS_IN_SIZE_T();
assert(columns);
const size_t count = strlen(columns);
if (count > (size_t)INT_MAX)
die("you want more than %d columns?", INT_MAX);
return (int)count;
}
ATTRIBUTE((nonnull))
static void specify_column(struct colspec *const colp,
const __u64 offset, const char column)
{
assert(colp);
switch (column) {
case 'L':
colp->label = "LOGICAL (B)";
colp->datum = k_datum_logical;
colp->offset = 0uLL;
colp->divisor = 1uLL;
break;
case 'l':
colp->label = "LOGICAL (sec)";
colp->datum = k_datum_logical;
colp->offset = 0uLL;
colp->divisor = k_sector_size;
break;
case 'I':
colp->label = "INITIAL (B)";
colp->datum = k_datum_physical;
colp->offset = offset;
colp->divisor = 1uLL;
break;
case 'i':
colp->label = "INITIAL (sec)";
colp->datum = k_datum_physical;
colp->offset = offset;
colp->divisor = k_sector_size;
break;
case 'F':
colp->label = "FINAL (B)";
colp->datum = k_datum_physical_end;
colp->offset = offset - 1uLL;
colp->divisor = 1uLL;
break;
case 'f':
colp->label = "FINAL (sec)";
colp->datum = k_datum_physical_end;
colp->offset = offset - 1uLL;
colp->divisor = k_sector_size;
break;
case 'C':
colp->label = "COUNT (B)";
colp->datum = k_datum_length;
colp->offset = 0uLL;
colp->divisor = 1uLL;
break;
case 'c':
colp->label = "COUNT (sec)";
colp->datum = k_datum_length;
colp->offset = 0uLL;
colp->divisor = k_sector_size;
break;
default:
// FIXME: Do an earlier check when the column specifier comes from the
// user, and keep this check but make it a BUG message.
die("unrecognized column specifier \"%c\"", column);
}
}
void
show_extent_table(const struct fiemap *restrict const fmp, const __u64 offset,
const char *restrict const columns)
{
enum { gap_width = 3 }; // TODO: Let the user customize this.
assert(fmp);
assert(columns);
struct tablespec *const tsp = alloc_tablespec(count_columns(columns));
tsp->fmp = fmp;
tsp->gap_width = gap_width;
for (int i = 0; i < tsp->col_count; ++i)
specify_column(&tsp->cols[i], offset, columns[i]);
populate_widths(tsp);
show_populated_table(tsp);
free(tsp);
}