]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - db/fprint.c
xfsprogs: Release v6.7.0
[thirdparty/xfsprogs-dev.git] / db / fprint.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
2bd0ea18 2/*
da23017d
NS
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
2bd0ea18
NS
5 */
6
6b803e5a 7#include "libxfs.h"
2bd0ea18
NS
8#include <ctype.h>
9#include <time.h>
10#include "type.h"
11#include "faddr.h"
12#include "fprint.h"
13#include "field.h"
14#include "inode.h"
49b31417 15#include "btblock.h"
2bd0ea18
NS
16#include "bit.h"
17#include "print.h"
18#include "output.h"
19#include "sig.h"
20#include "malloc.h"
0522f1cc 21#include "io.h"
2bd0ea18
NS
22
23int
24fp_charns(
25 void *obj,
26 int bit,
27 int count,
28 char *fmtstr,
29 int size,
30 int arg,
31 int base,
32 int array)
33{
34 int i;
35 char *p;
36
37 ASSERT(bitoffs(bit) == 0);
38 ASSERT(size == bitsz(char));
39 dbprintf("\"");
40 for (i = 0, p = (char *)obj + byteize(bit);
41 i < count && !seenint();
42 i++, p++) {
43 if (*p == '\\' || *p == '\'' || *p == '"' || *p == '\?')
44 dbprintf("\\%c", *p);
93d9f139 45 else if (isgraph((int)*p) || *p == ' ')
2bd0ea18
NS
46 dbprintf("%c", *p);
47 else if (*p == '\a' || *p == '\b' || *p == '\f' || *p == '\n' ||
48 *p == '\r' || *p == '\t' || *p == '\v')
49 dbprintf("\\%c", *p + ('a' - '\a'));
50 else
51 dbprintf("\\%03o", *p & 0xff);
52 }
53 dbprintf("\"");
54 return 1;
55}
56
57int
58fp_num(
59 void *obj,
60 int bit,
61 int count,
62 char *fmtstr,
63 int size,
64 int arg,
65 int base,
66 int array)
67{
68 int bitpos;
69 int i;
70 int isnull;
14f8b681 71 int64_t val;
2bd0ea18
NS
72
73 for (i = 0, bitpos = bit;
74 i < count && !seenint();
75 i++, bitpos += size) {
76 val = getbitval(obj, bitpos, size,
77 (arg & FTARG_SIGNED) ? BVSIGNED : BVUNSIGNED);
78 if ((arg & FTARG_SKIPZERO) && val == 0)
79 continue;
80 isnull = (arg & FTARG_SIGNED) || size == 64 ?
81 val == -1LL : val == ((1LL << size) - 1LL);
82 if ((arg & FTARG_SKIPNULL) && isnull)
83 continue;
0ebbf1d5 84 if (array && count > 1)
2bd0ea18
NS
85 dbprintf("%d:", i + base);
86 if ((arg & FTARG_DONULL) && isnull)
9ee7055c 87 dbprintf(_("null"));
2bd0ea18
NS
88 else if (size > 32)
89 dbprintf(fmtstr, val);
90 else
14f8b681 91 dbprintf(fmtstr, (int32_t)val);
2bd0ea18
NS
92 if (i < count - 1)
93 dbprintf(" ");
94 }
95 return 1;
96}
97
98/*ARGSUSED*/
99int
100fp_sarray(
101 void *obj,
102 int bit,
103 int count,
104 char *fmtstr,
105 int size,
106 int arg,
107 int base,
108 int array)
109{
110 print_sarray(obj, bit, count, size, base, array,
111 (const field_t *)fmtstr, (arg & FTARG_SKIPNMS) != 0);
112 return 1;
113}
114
344f38a9
DW
115static void
116fp_time64(
117 time64_t sec)
118{
119 time_t tt = sec;
120 time64_t tt_sec = tt;
121 char *c;
122
123 /*
124 * Stupid time_t shenanigans -- POSIX.1-2017 only requires that this
125 * type represent a time in seconds. Since we have no idea if our
126 * time64_t filesystem timestamps can actually be represented by the C
127 * library, we resort to converting the input value from time64_t to
128 * time_t and back to time64_t to check for information loss. If so,
129 * we print the raw value; otherwise we print a human-readable value.
130 */
131 if (tt_sec != sec)
132 goto raw;
133
134 c = ctime(&tt);
135 if (!c)
136 goto raw;
137
138 dbprintf("%24.24s", c);
139 return;
140raw:
141 dbprintf("%lld", sec);
142}
143
2bd0ea18
NS
144int
145fp_time(
30042222
DW
146 void *obj,
147 int bit,
148 int count,
149 char *fmtstr,
150 int size,
151 int arg,
152 int base,
153 int array)
2bd0ea18 154{
30042222
DW
155 struct timespec64 tv;
156 xfs_timestamp_t *ts;
157 int bitpos;
158 int i;
2bd0ea18
NS
159
160 ASSERT(bitoffs(bit) == 0);
161 for (i = 0, bitpos = bit;
162 i < count && !seenint();
163 i++, bitpos += size) {
164 if (array)
165 dbprintf("%d:", i + base);
30042222
DW
166
167 ts = obj + byteize(bitpos);
168 tv = libxfs_inode_from_disk_ts(obj, *ts);
169
344f38a9 170 fp_time64(tv.tv_sec);
30042222
DW
171
172 if (i < count - 1)
173 dbprintf(" ");
174 }
175 return 1;
176}
177
178int
179fp_nsec(
180 void *obj,
181 int bit,
182 int count,
183 char *fmtstr,
184 int size,
185 int arg,
186 int base,
187 int array)
188{
189 struct timespec64 tv;
190 xfs_timestamp_t *ts;
191 int bitpos;
192 int i;
193
194 ASSERT(bitoffs(bit) == 0);
195 for (i = 0, bitpos = bit;
196 i < count && !seenint();
197 i++, bitpos += size) {
198 if (array)
199 dbprintf("%d:", i + base);
200
201 ts = obj + byteize(bitpos);
202 tv = libxfs_inode_from_disk_ts(obj, *ts);
203
204 dbprintf("%u", tv.tv_nsec);
205
2bd0ea18
NS
206 if (i < count - 1)
207 dbprintf(" ");
208 }
209 return 1;
210}
211
a9a32fcb
DW
212int
213fp_qtimer(
214 void *obj,
215 int bit,
216 int count,
217 char *fmtstr,
218 int size,
219 int arg,
220 int base,
221 int array)
222{
344f38a9
DW
223 struct xfs_disk_dquot *ddq = obj;
224 time64_t sec;
a9a32fcb
DW
225 __be32 *t;
226 int bitpos;
227 int i;
228
229 ASSERT(bitoffs(bit) == 0);
230 for (i = 0, bitpos = bit;
231 i < count && !seenint();
232 i++, bitpos += size) {
233 if (array)
234 dbprintf("%d:", i + base);
235
236 t = obj + byteize(bitpos);
344f38a9 237 sec = libxfs_dquot_from_disk_ts(ddq, *t);
a9a32fcb 238
344f38a9
DW
239 /*
240 * Display the raw value if it's the default grace expiration
241 * period (root dquot) or if the quota has not expired.
242 */
243 if (ddq->d_id == 0 || sec == 0)
244 dbprintf("%lld", sec);
245 else
246 fp_time64(sec);
a9a32fcb
DW
247
248 if (i < count - 1)
249 dbprintf(" ");
250 }
251 return 1;
252}
253
2bd0ea18
NS
254/*ARGSUSED*/
255int
256fp_uuid(
257 void *obj,
258 int bit,
259 int count,
260 char *fmtstr,
261 int size,
262 int arg,
263 int base,
264 int array)
265{
266 char bp[40]; /* UUID string is 36 chars + trailing '\0' */
267 int i;
268 uuid_t *p;
269
270 ASSERT(bitoffs(bit) == 0);
271 for (p = (uuid_t *)((char *)obj + byteize(bit)), i = 0;
272 i < count && !seenint();
273 i++, p++) {
274 if (array)
275 dbprintf("%d:", i + base);
4d32d744 276 platform_uuid_unparse(p, bp);
2bd0ea18
NS
277 dbprintf("%s", bp);
278 if (i < count - 1)
279 dbprintf(" ");
280 }
281 return 1;
282}
0522f1cc
DC
283
284/*
285 * CRC is correct is the current buffer it is being pulled out
286 * of is not marked with a EFSCORRUPTED error.
287 */
288int
289fp_crc(
290 void *obj,
291 int bit,
292 int count,
293 char *fmtstr,
294 int size,
295 int arg,
296 int base,
297 int array)
298{
299 int bitpos;
300 int i;
14f8b681 301 int64_t val;
0522f1cc
DC
302 char *ok;
303
b511ff41
DC
304 switch (iocur_crc_valid()) {
305 case -1:
306 ok = "unchecked";
307 break;
308 case 0:
309 ok = "bad";
310 break;
311 case 1:
312 ok = "correct";
313 break;
314 default:
315 ok = "unknown state";
316 break;
317 }
0522f1cc
DC
318
319 for (i = 0, bitpos = bit;
320 i < count && !seenint();
321 i++, bitpos += size) {
322 if (array)
323 dbprintf("%d:", i + base);
324 val = getbitval(obj, bitpos, size, BVUNSIGNED);
325 if (size > 32)
326 dbprintf(fmtstr, val, ok);
327 else
14f8b681 328 dbprintf(fmtstr, (int32_t)val, ok);
0522f1cc
DC
329 if (i < count - 1)
330 dbprintf(" ");
331 }
332 return 1;
333}