]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/type.c
xfsprogs: call libxfs_destroy from other utilities
[thirdparty/xfsprogs-dev.git] / db / type.c
1 /*
2 * Copyright (c) 2000-2001,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 "block.h"
21 #include "command.h"
22 #include "type.h"
23 #include "faddr.h"
24 #include "fprint.h"
25 #include "field.h"
26 #include "print.h"
27 #include "sb.h"
28 #include "inode.h"
29 #include "btblock.h"
30 #include "bmroot.h"
31 #include "agf.h"
32 #include "agfl.h"
33 #include "agi.h"
34 #include "io.h"
35 #include "output.h"
36 #include "write.h"
37 #include "attr.h"
38 #include "dquot.h"
39 #include "dir2.h"
40 #include "text.h"
41 #include "symlink.h"
42 #include "fuzz.h"
43
44 static const typ_t *findtyp(char *name);
45 static int type_f(int argc, char **argv);
46
47 const typ_t *cur_typ;
48
49 static const cmdinfo_t type_cmd =
50 { "type", NULL, type_f, 0, 1, 1, N_("[newtype]"),
51 N_("set/show current data type"), NULL };
52
53 static const typ_t __typtab[] = {
54 { TYP_AGF, "agf", handle_struct, agf_hfld, NULL, TYP_F_NO_CRC_OFF },
55 { TYP_AGFL, "agfl", handle_struct, agfl_hfld, NULL, TYP_F_NO_CRC_OFF },
56 { TYP_AGI, "agi", handle_struct, agi_hfld, NULL, TYP_F_NO_CRC_OFF },
57 { TYP_ATTR, "attr", handle_struct, attr_hfld, NULL, TYP_F_NO_CRC_OFF },
58 { TYP_BMAPBTA, "bmapbta", handle_struct, bmapbta_hfld, NULL,
59 TYP_F_NO_CRC_OFF },
60 { TYP_BMAPBTD, "bmapbtd", handle_struct, bmapbtd_hfld, NULL,
61 TYP_F_NO_CRC_OFF },
62 { TYP_BNOBT, "bnobt", handle_struct, bnobt_hfld, NULL, TYP_F_NO_CRC_OFF },
63 { TYP_CNTBT, "cntbt", handle_struct, cntbt_hfld, NULL, TYP_F_NO_CRC_OFF },
64 { TYP_RMAPBT, NULL },
65 { TYP_REFCBT, NULL },
66 { TYP_DATA, "data", handle_block, NULL, NULL, TYP_F_NO_CRC_OFF },
67 { TYP_DIR2, "dir2", handle_struct, dir2_hfld, NULL, TYP_F_NO_CRC_OFF },
68 { TYP_DQBLK, "dqblk", handle_struct, dqblk_hfld, NULL, TYP_F_NO_CRC_OFF },
69 { TYP_INOBT, "inobt", handle_struct, inobt_hfld, NULL, TYP_F_NO_CRC_OFF },
70 { TYP_INODATA, "inodata", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
71 { TYP_INODE, "inode", handle_struct, inode_hfld, NULL, TYP_F_NO_CRC_OFF },
72 { TYP_LOG, "log", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
73 { TYP_RTBITMAP, "rtbitmap", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
74 { TYP_RTSUMMARY, "rtsummary", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
75 { TYP_SB, "sb", handle_struct, sb_hfld, NULL, TYP_F_NO_CRC_OFF },
76 { TYP_SYMLINK, "symlink", handle_string, NULL, NULL, TYP_F_NO_CRC_OFF },
77 { TYP_TEXT, "text", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
78 { TYP_FINOBT, "finobt", handle_struct, inobt_hfld, NULL,
79 TYP_F_NO_CRC_OFF },
80 { TYP_NONE, NULL }
81 };
82
83 static const typ_t __typtab_crc[] = {
84 { TYP_AGF, "agf", handle_struct, agf_hfld, &xfs_agf_buf_ops,
85 XFS_AGF_CRC_OFF },
86 { TYP_AGFL, "agfl", handle_struct, agfl_crc_hfld, &xfs_agfl_buf_ops,
87 XFS_AGFL_CRC_OFF },
88 { TYP_AGI, "agi", handle_struct, agi_hfld, &xfs_agi_buf_ops,
89 XFS_AGI_CRC_OFF },
90 { TYP_ATTR, "attr3", handle_struct, attr3_hfld,
91 &xfs_attr3_db_buf_ops, TYP_F_CRC_FUNC, xfs_attr3_set_crc },
92 { TYP_BMAPBTA, "bmapbta", handle_struct, bmapbta_crc_hfld,
93 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
94 { TYP_BMAPBTD, "bmapbtd", handle_struct, bmapbtd_crc_hfld,
95 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
96 { TYP_BNOBT, "bnobt", handle_struct, bnobt_crc_hfld,
97 &xfs_allocbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
98 { TYP_CNTBT, "cntbt", handle_struct, cntbt_crc_hfld,
99 &xfs_allocbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
100 { TYP_RMAPBT, "rmapbt", handle_struct, rmapbt_crc_hfld,
101 &xfs_rmapbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
102 { TYP_REFCBT, "refcntbt", handle_struct, refcbt_crc_hfld,
103 &xfs_refcountbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
104 { TYP_DATA, "data", handle_block, NULL, NULL, TYP_F_NO_CRC_OFF },
105 { TYP_DIR2, "dir3", handle_struct, dir3_hfld,
106 &xfs_dir3_db_buf_ops, TYP_F_CRC_FUNC, xfs_dir3_set_crc },
107 { TYP_DQBLK, "dqblk", handle_struct, dqblk_hfld,
108 &xfs_dquot_buf_ops, TYP_F_CRC_FUNC, xfs_dquot_set_crc },
109 { TYP_INOBT, "inobt", handle_struct, inobt_crc_hfld,
110 &xfs_inobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
111 { TYP_INODATA, "inodata", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
112 { TYP_INODE, "inode", handle_struct, inode_crc_hfld,
113 &xfs_inode_buf_ops, TYP_F_CRC_FUNC, xfs_inode_set_crc },
114 { TYP_LOG, "log", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
115 { TYP_RTBITMAP, "rtbitmap", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
116 { TYP_RTSUMMARY, "rtsummary", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
117 { TYP_SB, "sb", handle_struct, sb_hfld, &xfs_sb_buf_ops,
118 XFS_SB_CRC_OFF },
119 { TYP_SYMLINK, "symlink", handle_struct, symlink_crc_hfld,
120 &xfs_symlink_buf_ops, XFS_SYMLINK_CRC_OFF },
121 { TYP_TEXT, "text", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
122 { TYP_FINOBT, "finobt", handle_struct, inobt_crc_hfld,
123 &xfs_inobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
124 { TYP_NONE, NULL }
125 };
126
127 static const typ_t __typtab_spcrc[] = {
128 { TYP_AGF, "agf", handle_struct, agf_hfld, &xfs_agf_buf_ops,
129 XFS_AGF_CRC_OFF },
130 { TYP_AGFL, "agfl", handle_struct, agfl_crc_hfld, &xfs_agfl_buf_ops ,
131 XFS_AGFL_CRC_OFF },
132 { TYP_AGI, "agi", handle_struct, agi_hfld, &xfs_agi_buf_ops ,
133 XFS_AGI_CRC_OFF },
134 { TYP_ATTR, "attr3", handle_struct, attr3_hfld,
135 &xfs_attr3_db_buf_ops, TYP_F_CRC_FUNC, xfs_attr3_set_crc },
136 { TYP_BMAPBTA, "bmapbta", handle_struct, bmapbta_crc_hfld,
137 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
138 { TYP_BMAPBTD, "bmapbtd", handle_struct, bmapbtd_crc_hfld,
139 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
140 { TYP_BNOBT, "bnobt", handle_struct, bnobt_crc_hfld,
141 &xfs_allocbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
142 { TYP_CNTBT, "cntbt", handle_struct, cntbt_crc_hfld,
143 &xfs_allocbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
144 { TYP_RMAPBT, "rmapbt", handle_struct, rmapbt_crc_hfld,
145 &xfs_rmapbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
146 { TYP_REFCBT, "refcntbt", handle_struct, refcbt_crc_hfld,
147 &xfs_refcountbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
148 { TYP_DATA, "data", handle_block, NULL, NULL, TYP_F_NO_CRC_OFF },
149 { TYP_DIR2, "dir3", handle_struct, dir3_hfld,
150 &xfs_dir3_db_buf_ops, TYP_F_CRC_FUNC, xfs_dir3_set_crc },
151 { TYP_DQBLK, "dqblk", handle_struct, dqblk_hfld,
152 &xfs_dquot_buf_ops, TYP_F_CRC_FUNC, xfs_dquot_set_crc },
153 { TYP_INOBT, "inobt", handle_struct, inobt_spcrc_hfld,
154 &xfs_inobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
155 { TYP_INODATA, "inodata", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
156 { TYP_INODE, "inode", handle_struct, inode_crc_hfld,
157 &xfs_inode_buf_ops, TYP_F_CRC_FUNC, xfs_inode_set_crc },
158 { TYP_LOG, "log", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
159 { TYP_RTBITMAP, "rtbitmap", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
160 { TYP_RTSUMMARY, "rtsummary", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
161 { TYP_SB, "sb", handle_struct, sb_hfld, &xfs_sb_buf_ops,
162 XFS_SB_CRC_OFF },
163 { TYP_SYMLINK, "symlink", handle_struct, symlink_crc_hfld,
164 &xfs_symlink_buf_ops, XFS_SYMLINK_CRC_OFF },
165 { TYP_TEXT, "text", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
166 { TYP_FINOBT, "finobt", handle_struct, inobt_crc_hfld,
167 &xfs_inobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
168 { TYP_NONE, NULL }
169 };
170
171 const typ_t *typtab = __typtab;
172
173 void
174 type_set_tab_crc(void)
175 {
176 typtab = __typtab_crc;
177 }
178
179 void
180 type_set_tab_spcrc(void)
181 {
182 typtab = __typtab_spcrc;
183 }
184
185 static const typ_t *
186 findtyp(
187 char *name)
188 {
189 const typ_t *tt;
190
191 for (tt = typtab; tt->typnm != TYP_NONE; tt++) {
192 ASSERT(tt->typnm == (typnm_t)(tt - typtab));
193 if (tt->name && strcmp(tt->name, name) == 0)
194 return tt;
195 }
196 return NULL;
197 }
198
199 static int
200 type_f(
201 int argc,
202 char **argv)
203 {
204 const typ_t *tt;
205 int count = 0;
206
207 if (argc == 1) {
208 if (cur_typ == NULL)
209 dbprintf(_("no current type\n"));
210 else
211 dbprintf(_("current type is \"%s\"\n"), cur_typ->name);
212
213 dbprintf(_("\n supported types are:\n "));
214 for (tt = typtab, count = 0; tt->typnm != TYP_NONE; tt++) {
215 if (tt->name == NULL)
216 continue;
217 if ((tt+1)->name != NULL) {
218 dbprintf("%s, ", tt->name);
219 if ((++count % 8) == 0)
220 dbprintf("\n ");
221 } else if ((tt+1)->typnm == TYP_NONE) {
222 dbprintf("%s\n", tt->name);
223 }
224 }
225
226
227 } else {
228 tt = findtyp(argv[1]);
229 if (tt == NULL) {
230 dbprintf(_("no such type %s\n"), argv[1]);
231 } else {
232 if (iocur_top->typ == NULL)
233 dbprintf(_("no current object\n"));
234 else {
235 cur_typ = tt;
236 set_iocur_type(tt);
237 }
238 }
239 }
240 return 0;
241 }
242
243 void
244 type_init(void)
245 {
246 add_command(&type_cmd);
247 }
248
249 /* read/write selectors for each major data type */
250
251 void
252 handle_struct(
253 int action,
254 const field_t *fields,
255 int argc,
256 char **argv)
257 {
258 switch (action) {
259 case DB_FUZZ:
260 fuzz_struct(fields, argc, argv);
261 break;
262 case DB_WRITE:
263 write_struct(fields, argc, argv);
264 break;
265 case DB_READ:
266 print_struct(fields, argc, argv);
267 break;
268 }
269 }
270
271 void
272 handle_string(
273 int action,
274 const field_t *fields,
275 int argc,
276 char **argv)
277 {
278 switch (action) {
279 case DB_WRITE:
280 write_string(fields, argc, argv);
281 break;
282 case DB_READ:
283 print_string(fields, argc, argv);
284 break;
285 case DB_FUZZ:
286 dbprintf(_("string fuzzing not supported.\n"));
287 break;
288 }
289 }
290
291 void
292 handle_block(
293 int action,
294 const field_t *fields,
295 int argc,
296 char **argv)
297 {
298 switch (action) {
299 case DB_WRITE:
300 write_block(fields, argc, argv);
301 break;
302 case DB_READ:
303 print_block(fields, argc, argv);
304 break;
305 case DB_FUZZ:
306 dbprintf(_("use 'blocktrash' or 'write' to fuzz a block.\n"));
307 break;
308 }
309 }
310
311 void
312 handle_text(
313 int action,
314 const field_t *fields,
315 int argc,
316 char **argv)
317 {
318 switch (action) {
319 case DB_FUZZ:
320 /* fall through */
321 case DB_WRITE:
322 dbprintf(_("text writing/fuzzing not supported.\n"));
323 break;
324 case DB_READ:
325 print_text(fields, argc, argv);
326 break;
327 }
328 }