]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/init.c
Update copyright/license notices to match SGI legal prefered boilerplate.
[thirdparty/xfsprogs-dev.git] / db / init.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 <xfs/libxfs.h>
20 #include <signal.h>
21 #include "command.h"
22 #include "init.h"
23 #include "input.h"
24 #include "io.h"
25 #include "init.h"
26 #include "sig.h"
27 #include "output.h"
28 #include "malloc.h"
29
30 static char **cmdline;
31 static int ncmdline;
32 char *fsdevice;
33 int blkbb;
34 int exitcode;
35 int expert_mode;
36 xfs_mount_t xmount;
37 xfs_mount_t *mp;
38 libxfs_init_t x;
39 xfs_agnumber_t cur_agno = NULLAGNUMBER;
40
41 static void
42 usage(void)
43 {
44 fprintf(stderr, _(
45 "Usage: %s [-frxV] [-p prog] [-l logdev] [-c cmd]... device\n"),
46 progname);
47 exit(1);
48 }
49
50 void
51 init(
52 int argc,
53 char **argv)
54 {
55 xfs_sb_t *sbp;
56 void *bufp = NULL;
57 int c;
58
59 progname = basename(argv[0]);
60 while ((c = getopt(argc, argv, "c:fip:rxVl:")) != EOF) {
61 switch (c) {
62 case 'c':
63 cmdline = xrealloc(cmdline, (ncmdline+1)*sizeof(char*));
64 cmdline[ncmdline++] = optarg;
65 break;
66 case 'f':
67 x.disfile = 1;
68 break;
69 case 'i':
70 x.isreadonly = (LIBXFS_ISREADONLY|LIBXFS_ISINACTIVE);
71 break;
72 case 'p':
73 progname = optarg;
74 break;
75 case 'r':
76 x.isreadonly = LIBXFS_ISREADONLY;
77 break;
78 case 'l':
79 x.logname = optarg;
80 break;
81 case 'x':
82 expert_mode = 1;
83 break;
84 case 'V':
85 printf("%s version %s\n", progname, VERSION);
86 exit(0);
87 case '?':
88 usage();
89 /*NOTREACHED*/
90 }
91 }
92 if (optind + 1 != argc) {
93 usage();
94 /*NOTREACHED*/
95 }
96
97 fsdevice = argv[optind];
98 if (!x.disfile)
99 x.volname = fsdevice;
100 else
101 x.dname = fsdevice;
102 x.notvolok = 1;
103
104 if (!libxfs_init(&x)) {
105 fputs(_("\nfatal error -- couldn't initialize XFS library\n"),
106 stderr);
107 exit(1);
108 }
109
110 if (read_bbs(XFS_SB_DADDR, 1, &bufp, NULL)) {
111 dbprintf(_("%s: %s is invalid (cannot read first 512 bytes)\n"),
112 progname, fsdevice);
113 exit(1);
114 }
115
116 /* copy SB from buffer to in-core, converting architecture as we go */
117 libxfs_xlate_sb(bufp, &xmount.m_sb, 1, XFS_SB_ALL_BITS);
118 xfree(bufp);
119
120 sbp = &xmount.m_sb;
121 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
122 dbprintf(_("%s: unexpected XFS SB magic number 0x%08x\n"),
123 progname, sbp->sb_magicnum);
124 }
125
126 mp = libxfs_mount(&xmount, sbp, x.ddev, x.logdev, x.rtdev,
127 LIBXFS_MOUNT_ROOTINOS | LIBXFS_MOUNT_DEBUGGER);
128 if (!mp) {
129 dbprintf(_("%s: device %s unusable (not an XFS filesystem?)\n"),
130 progname, fsdevice);
131 exit(1);
132 }
133 blkbb = 1 << mp->m_blkbb_log;
134
135 push_cur();
136 init_commands();
137 init_sig();
138 }
139
140 int
141 main(
142 int argc,
143 char **argv)
144 {
145 int c, i, done = 0;
146 char *input;
147 char **v;
148
149 pushfile(stdin);
150 init(argc, argv);
151
152 for (i = 0; !done && i < ncmdline; i++) {
153 v = breakline(cmdline[i], &c);
154 if (c)
155 done = command(c, v);
156 xfree(v);
157 }
158 if (cmdline) {
159 xfree(cmdline);
160 return exitcode;
161 }
162
163 while (!done) {
164 if ((input = fetchline()) == NULL)
165 break;
166 v = breakline(input, &c);
167 if (c)
168 done = command(c, v);
169 doneline(input, v);
170 }
171 return exitcode;
172 }