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