]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - db/addr.c
xfs_db: don't crash in ablock if there's no inode
[thirdparty/xfsprogs-dev.git] / db / addr.c
CommitLineData
2bd0ea18 1/*
da23017d
NS
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
dfc130f3 4 *
da23017d
NS
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
2bd0ea18 7 * published by the Free Software Foundation.
dfc130f3 8 *
da23017d
NS
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.
dfc130f3 13 *
da23017d
NS
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
2bd0ea18
NS
17 */
18
6b803e5a 19#include "libxfs.h"
2bd0ea18
NS
20#include "addr.h"
21#include "command.h"
22#include "type.h"
23#include "faddr.h"
24#include "fprint.h"
25#include "field.h"
26#include "io.h"
27#include "flist.h"
28#include "inode.h"
29#include "output.h"
30
31static int addr_f(int argc, char **argv);
32static void addr_help(void);
33
34static const cmdinfo_t addr_cmd =
9ee7055c
AM
35 { "addr", "a", addr_f, 0, 1, 1, N_("[field-expression]"),
36 N_("set current address"), addr_help };
2bd0ea18
NS
37
38static void
39addr_help(void)
40{
9ee7055c 41 dbprintf(_(
2bd0ea18
NS
42"\n"
43" 'addr' uses the given field to set the filesystem address and type\n"
44"\n"
45" Examples:\n"
46"\n"
47" sb\n"
48" a rootino - set the type to inode and set position to the root inode\n"
49" a u.bmx[0].startblock (for inode with blockmap)\n"
50"\n"
9ee7055c 51));
2bd0ea18
NS
52
53}
54
55static int
56addr_f(
57 int argc,
58 char **argv)
59{
60 adfnc_t adf;
61 const ftattr_t *fa;
62 flist_t *fl;
63 const field_t *fld;
64 typnm_t next;
65 flist_t *tfl;
66
67 if (argc == 1) {
68 print_iocur("current", iocur_top);
69 return 0;
70 }
71 if (cur_typ == NULL) {
9ee7055c 72 dbprintf(_("no current type\n"));
2bd0ea18
NS
73 return 0;
74 }
75 fld = cur_typ->fields;
76 if (fld != NULL && fld->name[0] == '\0') {
77 fa = &ftattrtab[fld->ftyp];
78 ASSERT(fa->ftyp == fld->ftyp);
79 fld = fa->subfld;
80 }
81 if (fld == NULL) {
9ee7055c 82 dbprintf(_("no fields for type %s\n"), cur_typ->name);
2bd0ea18
NS
83 return 0;
84 }
85 fl = flist_scan(argv[1]);
86 if (fl == NULL)
87 return 0;
355ac015
ES
88 if (!flist_parse(fld, fl, iocur_top->data, 0))
89 goto out;
90
2bd0ea18
NS
91 flist_print(fl);
92 for (tfl = fl; tfl->child != NULL; tfl = tfl->child) {
93 if ((tfl->flags & FL_OKLOW) && tfl->low < tfl->high) {
9ee7055c 94 dbprintf(_("array not allowed for addr command\n"));
355ac015 95 goto out;
2bd0ea18
NS
96 }
97 }
98 fld = tfl->fld;
99 next = fld->next;
100 if (next == TYP_INODATA)
101 next = inode_next_type();
102 if (next == TYP_NONE) {
9ee7055c 103 dbprintf(_("no next type for field %s\n"), fld->name);
355ac015 104 goto out;
2bd0ea18
NS
105 }
106 fa = &ftattrtab[fld->ftyp];
107 ASSERT(fa->ftyp == fld->ftyp);
108 adf = fa->adfunc;
109 if (adf == NULL) {
9ee7055c 110 dbprintf(_("no addr function for field %s (type %s)\n"),
2bd0ea18 111 fld->name, fa->name);
355ac015 112 goto out;
2bd0ea18
NS
113 }
114 (*adf)(iocur_top->data, tfl->offset, next);
355ac015 115out:
2bd0ea18
NS
116 flist_free(fl);
117 return 0;
118}
119
120void
121addr_init(void)
122{
123 add_command(&addr_cmd);
124}