]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/print.c
xfs_repair: invalidate dirty dir buffers when we zap a directory
[thirdparty/xfsprogs-dev.git] / db / print.c
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libxfs.h"
20 #include "command.h"
21 #include "type.h"
22 #include "faddr.h"
23 #include "fprint.h"
24 #include "field.h"
25 #include "io.h"
26 #include "print.h"
27 #include "bit.h"
28 #include "flist.h"
29 #include "strvec.h"
30 #include "output.h"
31 #include "sig.h"
32 #include "write.h"
33
34 static void print_allfields(const struct field *fields);
35 static int print_f(int argc, char **argv);
36 static void print_flist_1(struct flist *flist, char **pfx, int parentoff);
37 static void print_somefields(const struct field *fields, int argc,
38 char **argv);
39
40 static const cmdinfo_t print_cmd =
41 { "print", "p", print_f, 0, -1, 0, N_("[value]..."),
42 N_("print field values"), NULL };
43
44 static void
45 print_allfields(
46 const field_t *fields)
47 {
48 flist_t *flist;
49 #ifdef DEBUG
50 int i;
51 #endif
52
53 flist = flist_make("");
54 flist->fld = fields;
55 #ifndef DEBUG
56 (void)flist_parse(fields, flist, iocur_top->data, 0);
57 #else
58 i = flist_parse(fields, flist, iocur_top->data, 0);
59 ASSERT(i == 1);
60 #endif
61 flist_print(flist);
62 print_flist(flist);
63 flist_free(flist);
64 }
65
66 static int
67 print_f(
68 int argc,
69 char **argv)
70 {
71 pfunc_t pf;
72
73 if (cur_typ == NULL) {
74 dbprintf(_("no current type\n"));
75 return 0;
76 }
77 pf = cur_typ->pfunc;
78 if (pf == NULL) {
79 dbprintf(_("no print function for type %s\n"), cur_typ->name);
80 return 0;
81 }
82 argc--;
83 argv++;
84 (*pf)(DB_READ, cur_typ->fields, argc, argv);
85 return 0;
86 }
87
88 void
89 print_flist(
90 flist_t *flist)
91 {
92 char **pfx;
93
94 pfx = new_strvec(0);
95 print_flist_1(flist, pfx, 0);
96 free_strvec(pfx);
97 }
98
99 static void
100 print_flist_1(
101 flist_t *flist,
102 char **ppfx,
103 int parentoff)
104 {
105 char buf[16];
106 const field_t *f;
107 const ftattr_t *fa;
108 flist_t *fl;
109 int low;
110 int count;
111 int neednl;
112 char **pfx;
113
114 for (fl = flist; fl && !seenint(); fl = fl->sibling) {
115 pfx = copy_strvec(ppfx);
116 if (fl->name[0])
117 add_strvec(&pfx, fl->name);
118 if (fl->flags & FL_OKLOW) {
119 add_strvec(&pfx, "[");
120 snprintf(buf, sizeof(buf), "%d", fl->low);
121 add_strvec(&pfx, buf);
122 if (fl->low != fl->high) {
123 add_strvec(&pfx, "-");
124 snprintf(buf, sizeof(buf), "%d", fl->high);
125 add_strvec(&pfx, buf);
126 }
127 add_strvec(&pfx, "]");
128 }
129 if (fl->child) {
130 if (fl->name[0])
131 add_strvec(&pfx, ".");
132 print_flist_1(fl->child, pfx, fl->offset);
133 } else {
134 f = fl->fld;
135 fa = &ftattrtab[f->ftyp];
136 ASSERT(fa->ftyp == f->ftyp);
137 print_strvec(pfx);
138 dbprintf(" = ");
139 if (fl->flags & FL_OKLOW)
140 low = fl->low;
141 else
142 low = 0;
143 count = fcount(f, iocur_top->data, parentoff);
144 if (fl->flags & FL_OKHIGH)
145 count = min(count, fl->high - low + 1);
146 if (fa->prfunc) {
147 int fsz;
148 int bitlen;
149
150 /* Don't read an array off the end of the buffer */
151 fsz = fsize(f, iocur_top->data, parentoff, 0);
152 bitlen = iocur_top->len * NBBY;
153 if ((f->flags & FLD_ARRAY) &&
154 fl->offset + (count * fsz) > bitlen) {
155 count = (bitlen - fl->offset) / fsz;
156 }
157
158 neednl = fa->prfunc(iocur_top->data, fl->offset,
159 count, fa->fmtstr, fsz, fa->arg, low,
160 (f->flags & FLD_ARRAY) != 0);
161 if (neednl)
162 dbprintf("\n");
163 } else {
164 ASSERT(fa->arg & FTARG_OKEMPTY);
165 dbprintf(_("(empty)\n"));
166 }
167 }
168 free_strvec(pfx);
169 }
170 }
171
172 void
173 print_init(void)
174 {
175 add_command(&print_cmd);
176 }
177
178 void
179 print_sarray(
180 void *obj,
181 int bit,
182 int count,
183 int size,
184 int base,
185 int array,
186 const field_t *flds,
187 int skipnms)
188 {
189 int bitoff;
190 const field_t *f;
191 const ftattr_t *fa;
192 int first;
193 int i;
194
195 ASSERT(bitoffs(bit) == 0);
196 if (skipnms == 0) {
197 for (f = flds, first = 1; f->name; f++) {
198 if (f->flags & FLD_SKIPALL)
199 continue;
200 dbprintf("%c%s", first ? '[' : ',', f->name);
201 first = 0;
202 }
203 dbprintf("] ");
204 }
205 for (i = 0, bitoff = bit;
206 i < count && !seenint();
207 i++, bitoff += size) {
208 if (array)
209 dbprintf("\n%d:", i + base);
210 for (f = flds, first = 1; f->name; f++) {
211 if (f->flags & FLD_SKIPALL)
212 continue;
213 fa = &ftattrtab[f->ftyp];
214 ASSERT(fa->ftyp == f->ftyp);
215 dbprintf("%c", first ? '[' : ',');
216 first = 0;
217 if (fa->prfunc)
218 fa->prfunc(obj,
219 bitoff +
220 bitoffset(f, obj, bitoff, i + base),
221 fcount(f, obj, bitoff), fa->fmtstr,
222 fsize(f, obj, bitoff, i + base),
223 fa->arg, (f->flags & FLD_ABASE1) != 0,
224 f->flags & FLD_ARRAY);
225 else {
226 ASSERT(fa->arg & FTARG_OKEMPTY);
227 dbprintf(_("(empty)"));
228 }
229 }
230 dbprintf("]");
231 if (i < count - 1)
232 dbprintf(" ");
233 }
234 }
235
236 static void
237 print_somefields(
238 const field_t *fields,
239 int argc,
240 char **argv)
241 {
242 const ftattr_t *fa;
243 flist_t *fl;
244 flist_t *lfl;
245 flist_t *nfl;
246
247 fl = lfl = NULL;
248 while (argc > 0) {
249 nfl = flist_scan(*argv);
250 if (!nfl) {
251 if (fl)
252 flist_free(fl);
253 return;
254 }
255 if (lfl)
256 lfl->sibling = nfl;
257 else
258 fl = nfl;
259 lfl = nfl;
260 argc--;
261 argv++;
262 }
263 if (fields->name[0] == '\0') {
264 fa = &ftattrtab[fields->ftyp];
265 ASSERT(fa->ftyp == fields->ftyp);
266 fields = fa->subfld;
267 }
268 if (!flist_parse(fields, fl, iocur_top->data, 0)) {
269 flist_free(fl);
270 return;
271 }
272 flist_print(fl);
273 print_flist(fl);
274 flist_free(fl);
275 }
276
277 /*ARGSUSED*/
278 void
279 print_string(
280 const field_t *fields,
281 int argc,
282 char **argv)
283 {
284 char *cp;
285
286 if (argc != 0)
287 dbprintf(_("no arguments allowed\n"));
288 dbprintf("\"");
289 for (cp = iocur_top->data;
290 cp < (char *)iocur_top->data + iocur_top->len && *cp &&
291 !seenint();
292 cp++)
293 dbprintf("%c", *cp);
294 dbprintf("\"\n");
295 }
296
297 void
298 print_struct(
299 const field_t *fields,
300 int argc,
301 char **argv)
302 {
303 if (argc == 0)
304 print_allfields(fields);
305 else
306 print_somefields(fields, argc, argv);
307 }