]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - debugfs/util.c
Shorten compile commands run by the build system
[thirdparty/e2fsprogs.git] / debugfs / util.c
1 /*
2 * util.c --- utilities for the debugfs program
3 *
4 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
6 *
7 */
8
9 #define _XOPEN_SOURCE 600 /* needed for strptime */
10
11 #include "config.h"
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include <time.h>
18 #include <signal.h>
19 #ifdef HAVE_GETOPT_H
20 #include <getopt.h>
21 #else
22 extern int optind;
23 extern char *optarg;
24 #endif
25 #ifdef HAVE_OPTRESET
26 extern int optreset; /* defined by BSD, but not others */
27 #endif
28
29 #include "debugfs.h"
30
31 /*
32 * This function resets the libc getopt() function, which keeps
33 * internal state. Bad design! Stupid libc API designers! No
34 * biscuit!
35 *
36 * BSD-derived getopt() functions require that optind be reset to 1 in
37 * order to reset getopt() state. This used to be generally accepted
38 * way of resetting getopt(). However, glibc's getopt()
39 * has additional getopt() state beyond optind, and requires that
40 * optind be set zero to reset its state. So the unfortunate state of
41 * affairs is that BSD-derived versions of getopt() misbehave if
42 * optind is set to 0 in order to reset getopt(), and glibc's getopt()
43 * will core dump if optind is set 1 in order to reset getopt().
44 *
45 * More modern versions of BSD require that optreset be set to 1 in
46 * order to reset getopt(). Sigh. Standards, anyone?
47 *
48 * We hide the hair here.
49 */
50 void reset_getopt(void)
51 {
52 #if defined(__GLIBC__) || defined(__linux__)
53 optind = 0;
54 #else
55 optind = 1;
56 #endif
57 #ifdef HAVE_OPTRESET
58 optreset = 1; /* Makes BSD getopt happy */
59 #endif
60 }
61
62 static const char *pager_search_list[] = { "pager", "more", "less", 0 };
63 static const char *pager_dir_list[] = { "/usr/bin", "/bin", 0 };
64
65 static const char *find_pager(char *buf)
66 {
67 const char **i, **j;
68
69 for (i = pager_search_list; *i; i++) {
70 for (j = pager_dir_list; *j; j++) {
71 sprintf(buf, "%s/%s", *j, *i);
72 if (access(buf, X_OK) == 0)
73 return(buf);
74 }
75 }
76 return 0;
77 }
78
79 FILE *open_pager(void)
80 {
81 FILE *outfile = 0;
82 const char *pager = getenv("DEBUGFS_PAGER");
83 char buf[80];
84
85 signal(SIGPIPE, SIG_IGN);
86 if (!isatty(1))
87 return stdout;
88 if (!pager)
89 pager = getenv("PAGER");
90 if (!pager)
91 pager = find_pager(buf);
92 if (!pager ||
93 (strcmp(pager, "__none__") == 0) ||
94 ((outfile = popen(pager, "w")) == 0))
95 return stdout;
96 return outfile;
97 }
98
99 void close_pager(FILE *stream)
100 {
101 if (stream && stream != stdout) pclose(stream);
102 }
103
104 /*
105 * This routine is used whenever a command needs to turn a string into
106 * an inode.
107 */
108 ext2_ino_t string_to_inode(char *str)
109 {
110 ext2_ino_t ino;
111 int len = strlen(str);
112 char *end;
113 int retval;
114
115 /*
116 * If the string is of the form <ino>, then treat it as an
117 * inode number.
118 */
119 if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
120 ino = strtoul(str+1, &end, 0);
121 if (*end=='>')
122 return ino;
123 }
124
125 retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
126 if (retval) {
127 com_err(str, retval, 0);
128 return 0;
129 }
130 return ino;
131 }
132
133 /*
134 * This routine returns 1 if the filesystem is not open, and prints an
135 * error message to that effect.
136 */
137 int check_fs_open(char *name)
138 {
139 if (!current_fs) {
140 com_err(name, 0, "Filesystem not open");
141 return 1;
142 }
143 return 0;
144 }
145
146 /*
147 * This routine returns 1 if a filesystem is open, and prints an
148 * error message to that effect.
149 */
150 int check_fs_not_open(char *name)
151 {
152 if (current_fs) {
153 com_err(name, 0,
154 "Filesystem %s is still open. Close it first.\n",
155 current_fs->device_name);
156 return 1;
157 }
158 return 0;
159 }
160
161 /*
162 * This routine returns 1 if a filesystem is not opened read/write,
163 * and prints an error message to that effect.
164 */
165 int check_fs_read_write(char *name)
166 {
167 if (!(current_fs->flags & EXT2_FLAG_RW)) {
168 com_err(name, 0, "Filesystem opened read/only");
169 return 1;
170 }
171 return 0;
172 }
173
174 /*
175 * This routine returns 1 if a filesystem is doesn't have its inode
176 * and block bitmaps loaded, and prints an error message to that
177 * effect.
178 */
179 int check_fs_bitmaps(char *name)
180 {
181 if (!current_fs->block_map || !current_fs->inode_map) {
182 com_err(name, 0, "Filesystem bitmaps not loaded");
183 return 1;
184 }
185 return 0;
186 }
187
188 /*
189 * This function takes a __u32 time value and converts it to a string,
190 * using ctime
191 */
192 char *time_to_string(__u32 cl)
193 {
194 static int do_gmt = -1;
195 time_t t = (time_t) cl;
196 const char *tz;
197
198 if (do_gmt == -1) {
199 /* The diet libc doesn't respect the TZ environemnt variable */
200 tz = getenv("TZ");
201 if (!tz)
202 tz = "";
203 do_gmt = !strcmp(tz, "GMT");
204 }
205
206 return asctime((do_gmt) ? gmtime(&t) : localtime(&t));
207 }
208
209 /*
210 * Parse a string as a time. Return ((time_t)-1) if the string
211 * doesn't appear to be a sane time.
212 */
213 extern time_t string_to_time(const char *arg)
214 {
215 struct tm ts;
216 time_t ret;
217 char *tmp;
218
219 if (strcmp(arg, "now") == 0) {
220 return time(0);
221 }
222 memset(&ts, 0, sizeof(ts));
223 #ifdef HAVE_STRPTIME
224 strptime(arg, "%Y%m%d%H%M%S", &ts);
225 #else
226 sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
227 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
228 ts.tm_year -= 1900;
229 ts.tm_mon -= 1;
230 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
231 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
232 ts.tm_min > 59 || ts.tm_sec > 61)
233 ts.tm_mday = 0;
234 #endif
235 ts.tm_isdst = -1;
236 ret = mktime(&ts);
237 if (ts.tm_mday == 0 || ret == ((time_t) -1)) {
238 /* Try it as an integer... */
239 ret = strtoul(arg, &tmp, 0);
240 if (*tmp)
241 return ((time_t) -1);
242 }
243 return ret;
244 }
245
246 /*
247 * This function will convert a string to an unsigned long, printing
248 * an error message if it fails, and returning success or failure in err.
249 */
250 unsigned long parse_ulong(const char *str, const char *cmd,
251 const char *descr, int *err)
252 {
253 char *tmp;
254 unsigned long ret;
255
256 ret = strtoul(str, &tmp, 0);
257 if (*tmp == 0) {
258 if (err)
259 *err = 0;
260 return ret;
261 }
262 com_err(cmd, 0, "Bad %s - %s", descr, str);
263 if (err)
264 *err = 1;
265 else
266 exit(1);
267 return 0;
268 }
269
270 /*
271 * This function will convert a string to an unsigned long long, printing
272 * an error message if it fails, and returning success or failure in err.
273 */
274 unsigned long long parse_ulonglong(const char *str, const char *cmd,
275 const char *descr, int *err)
276 {
277 char *tmp;
278 unsigned long long ret;
279
280 ret = strtoull(str, &tmp, 0);
281 if (*tmp == 0) {
282 if (err)
283 *err = 0;
284 return ret;
285 }
286 com_err(cmd, 0, "Bad %s - %s", descr, str);
287 if (err)
288 *err = 1;
289 else
290 exit(1);
291 return 0;
292 }
293
294 /*
295 * This function will convert a string to a block number. It returns
296 * 0 on success, 1 on failure.
297 */
298 int strtoblk(const char *cmd, const char *str, blk64_t *ret)
299 {
300 blk_t blk;
301 int err;
302
303 blk = parse_ulonglong(str, cmd, "block number", &err);
304 *ret = blk;
305 if (err)
306 com_err(cmd, 0, "Invalid block number: %s", str);
307 return err;
308 }
309
310 /*
311 * This is a common helper function used by the command processing
312 * routines
313 */
314 int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
315 const char *cmd, const char *usage, int flags)
316 {
317 if (argc < min_argc || argc > max_argc) {
318 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
319 return 1;
320 }
321 if (flags & CHECK_FS_NOTOPEN) {
322 if (check_fs_not_open(argv[0]))
323 return 1;
324 } else {
325 if (check_fs_open(argv[0]))
326 return 1;
327 }
328 if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
329 return 1;
330 if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
331 return 1;
332 return 0;
333 }
334
335 /*
336 * This is a helper function used by do_stat, do_freei, do_seti, and
337 * do_testi, etc. Basically, any command which takes a single
338 * argument which is a file/inode number specifier.
339 */
340 int common_inode_args_process(int argc, char *argv[],
341 ext2_ino_t *inode, int flags)
342 {
343 if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
344 return 1;
345
346 *inode = string_to_inode(argv[1]);
347 if (!*inode)
348 return 1;
349 return 0;
350 }
351
352 /*
353 * This is a helper function used by do_freeb, do_setb, and do_testb
354 */
355 int common_block_args_process(int argc, char *argv[],
356 blk64_t *block, blk64_t *count)
357 {
358 int err;
359
360 if (common_args_process(argc, argv, 2, 3, argv[0],
361 "<block> [count]", CHECK_FS_BITMAPS))
362 return 1;
363
364 if (strtoblk(argv[0], argv[1], block))
365 return 1;
366 if (*block == 0) {
367 com_err(argv[0], 0, "Invalid block number 0");
368 err = 1;
369 }
370
371 if (argc > 2) {
372 *count = parse_ulong(argv[2], argv[0], "count", &err);
373 if (err)
374 return 1;
375 }
376 return 0;
377 }
378
379 int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode,
380 const char *cmd, int bufsize)
381 {
382 int retval;
383
384 retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize);
385 if (retval) {
386 com_err(cmd, retval, "while reading inode %u", ino);
387 return 1;
388 }
389 return 0;
390 }
391
392 int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
393 const char *cmd)
394 {
395 int retval;
396
397 retval = ext2fs_read_inode(current_fs, ino, inode);
398 if (retval) {
399 com_err(cmd, retval, "while reading inode %u", ino);
400 return 1;
401 }
402 return 0;
403 }
404
405 int debugfs_write_inode_full(ext2_ino_t ino,
406 struct ext2_inode *inode,
407 const char *cmd,
408 int bufsize)
409 {
410 int retval;
411
412 retval = ext2fs_write_inode_full(current_fs, ino,
413 inode, bufsize);
414 if (retval) {
415 com_err(cmd, retval, "while writing inode %u", ino);
416 return 1;
417 }
418 return 0;
419 }
420
421 int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
422 const char *cmd)
423 {
424 int retval;
425
426 retval = ext2fs_write_inode(current_fs, ino, inode);
427 if (retval) {
428 com_err(cmd, retval, "while writing inode %u", ino);
429 return 1;
430 }
431 return 0;
432 }
433
434 int debugfs_write_new_inode(ext2_ino_t ino, struct ext2_inode * inode,
435 const char *cmd)
436 {
437 int retval;
438
439 retval = ext2fs_write_new_inode(current_fs, ino, inode);
440 if (retval) {
441 com_err(cmd, retval, "while creating inode %u", ino);
442 return 1;
443 }
444 return 0;
445 }