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