]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/init.c
Merge back kernel changes into libxfs, mainly endian stuff.
[thirdparty/xfsprogs-dev.git] / db / init.c
1 /*
2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33 #include <xfs/libxfs.h>
34 #include <signal.h>
35 #include "command.h"
36 #include "init.h"
37 #include "input.h"
38 #include "io.h"
39 #include "init.h"
40 #include "sig.h"
41 #include "output.h"
42 #include "malloc.h"
43
44 static char **cmdline;
45 static int ncmdline;
46 char *fsdevice;
47 int blkbb;
48 int exitcode;
49 int expert_mode;
50 xfs_mount_t xmount;
51 xfs_mount_t *mp;
52 libxfs_init_t x;
53 xfs_agnumber_t cur_agno = NULLAGNUMBER;
54
55 static void
56 usage(void)
57 {
58 fprintf(stderr, _(
59 "Usage: %s [-frxV] [-p prog] [-l logdev] [-c cmd]... device\n"),
60 progname);
61 exit(1);
62 }
63
64 void
65 init(
66 int argc,
67 char **argv)
68 {
69 xfs_sb_t *sbp;
70 void *bufp = NULL;
71 int c;
72
73 progname = basename(argv[0]);
74 while ((c = getopt(argc, argv, "c:fip:rxVl:")) != EOF) {
75 switch (c) {
76 case 'c':
77 cmdline = xrealloc(cmdline, (ncmdline+1)*sizeof(char*));
78 cmdline[ncmdline++] = optarg;
79 break;
80 case 'f':
81 x.disfile = 1;
82 break;
83 case 'i':
84 x.isreadonly = (LIBXFS_ISREADONLY|LIBXFS_ISINACTIVE);
85 break;
86 case 'p':
87 progname = optarg;
88 break;
89 case 'r':
90 x.isreadonly = LIBXFS_ISREADONLY;
91 break;
92 case 'l':
93 x.logname = optarg;
94 break;
95 case 'x':
96 expert_mode = 1;
97 break;
98 case 'V':
99 printf("%s version %s\n", progname, VERSION);
100 exit(0);
101 case '?':
102 usage();
103 /*NOTREACHED*/
104 }
105 }
106 if (optind + 1 != argc) {
107 usage();
108 /*NOTREACHED*/
109 }
110
111 fsdevice = argv[optind];
112 if (!x.disfile)
113 x.volname = fsdevice;
114 else
115 x.dname = fsdevice;
116 x.notvolok = 1;
117
118 if (!libxfs_init(&x)) {
119 fputs(_("\nfatal error -- couldn't initialize XFS library\n"),
120 stderr);
121 exit(1);
122 }
123
124 if (read_bbs(XFS_SB_DADDR, 1, &bufp, NULL)) {
125 dbprintf(_("%s: %s is invalid (cannot read first 512 bytes)\n"),
126 progname, fsdevice);
127 exit(1);
128 }
129
130 /* copy SB from buffer to in-core, converting architecture as we go */
131 libxfs_xlate_sb(bufp, &xmount.m_sb, 1, XFS_SB_ALL_BITS);
132 xfree(bufp);
133
134 sbp = &xmount.m_sb;
135 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
136 dbprintf(_("%s: unexpected XFS SB magic number 0x%08x\n"),
137 progname, sbp->sb_magicnum);
138 }
139
140 mp = libxfs_mount(&xmount, sbp, x.ddev, x.logdev, x.rtdev,
141 LIBXFS_MOUNT_ROOTINOS | LIBXFS_MOUNT_DEBUGGER);
142
143 blkbb = 1 << mp->m_blkbb_log;
144
145 push_cur();
146 init_commands();
147 init_sig();
148 }
149
150 int
151 main(
152 int argc,
153 char **argv)
154 {
155 int c, i, done = 0;
156 char *input;
157 char **v;
158
159 pushfile(stdin);
160 init(argc, argv);
161
162 for (i = 0; !done && i < ncmdline; i++) {
163 v = breakline(cmdline[i], &c);
164 if (c)
165 done = command(c, v);
166 xfree(v);
167 }
168 if (cmdline) {
169 xfree(cmdline);
170 return exitcode;
171 }
172
173 while (!done) {
174 if ((input = fetchline()) == NULL)
175 break;
176 v = breakline(input, &c);
177 if (c)
178 done = command(c, v);
179 doneline(input, v);
180 }
181 return exitcode;
182 }