2 * util.c --- utilities for the debugfs program
4 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
9 #define _XOPEN_SOURCE 600 /* needed for strptime */
25 extern int optreset; /* defined by BSD, but not others */
32 * This function resets the libc getopt() function, which keeps
33 * internal state. Bad design! Stupid libc API designers! No
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().
45 * More modern versions of BSD require that optreset be set to 1 in
46 * order to reset getopt(). Sigh. Standards, anyone?
48 * We hide the hair here.
50 void reset_getopt(void)
52 #if defined(__GLIBC__) || defined(__linux__)
58 optreset = 1; /* Makes BSD getopt happy */
62 static const char *pager_search_list[] = { "pager", "more", "less", 0 };
63 static const char *pager_dir_list[] = { "/usr/bin", "/bin", 0 };
65 static const char *find_pager(char *buf)
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)
79 FILE *open_pager(void)
82 const char *pager = ss_safe_getenv("DEBUGFS_PAGER");
85 signal(SIGPIPE, SIG_IGN);
89 pager = ss_safe_getenv("PAGER");
91 pager = find_pager(buf);
93 (strcmp(pager, "__none__") == 0) ||
94 ((outfile = popen(pager, "w")) == 0))
99 void close_pager(FILE *stream)
101 if (stream && stream != stdout) pclose(stream);
105 * This routine is used whenever a command needs to turn a string into
108 ext2_ino_t string_to_inode(char *str)
111 int len = strlen(str);
116 * If the string is of the form <ino>, then treat it as an
119 if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
120 ino = strtoul(str+1, &end, 0);
125 retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
127 com_err(str, retval, 0);
134 * This routine returns 1 if the filesystem is not open, and prints an
135 * error message to that effect.
137 int check_fs_open(char *name)
140 com_err(name, 0, "Filesystem not open");
147 * This routine returns 1 if a filesystem is open, and prints an
148 * error message to that effect.
150 int check_fs_not_open(char *name)
154 "Filesystem %s is still open. Close it first.\n",
155 current_fs->device_name);
162 * This routine returns 1 if a filesystem is not opened read/write,
163 * and prints an error message to that effect.
165 int check_fs_read_write(char *name)
167 if (!(current_fs->flags & EXT2_FLAG_RW)) {
168 com_err(name, 0, "Filesystem opened read/only");
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
179 int check_fs_bitmaps(char *name)
181 if (!current_fs->block_map || !current_fs->inode_map) {
182 com_err(name, 0, "Filesystem bitmaps not loaded");
189 * This function takes a __u32 time value and converts it to a string,
192 char *time_to_string(__u32 cl)
194 static int do_gmt = -1;
195 time_t t = (time_t) cl;
199 /* The diet libc doesn't respect the TZ environemnt variable */
200 tz = ss_safe_getenv("TZ");
203 do_gmt = !strcmp(tz, "GMT");
206 return asctime((do_gmt) ? gmtime(&t) : localtime(&t));
210 * Parse a string as a time. Return ((time_t)-1) if the string
211 * doesn't appear to be a sane time.
213 time_t string_to_time(const char *arg)
219 if (strcmp(arg, "now") == 0) {
223 /* interpret it as an integer */
224 ret = strtoul(arg+1, &tmp, 0);
226 return ((time_t) -1);
229 memset(&ts, 0, sizeof(ts));
231 strptime(arg, "%Y%m%d%H%M%S", &ts);
233 sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
234 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
237 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
238 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
239 ts.tm_min > 59 || ts.tm_sec > 61)
244 if (ts.tm_mday == 0 || ret == ((time_t) -1)) {
245 /* Try it as an integer... */
246 ret = strtoul(arg, &tmp, 0);
248 return ((time_t) -1);
254 * This function will convert a string to an unsigned long, printing
255 * an error message if it fails, and returning success or failure in err.
257 unsigned long parse_ulong(const char *str, const char *cmd,
258 const char *descr, int *err)
263 ret = strtoul(str, &tmp, 0);
269 com_err(cmd, 0, "Bad %s - %s", descr, str);
278 * This function will convert a string to an unsigned long long, printing
279 * an error message if it fails, and returning success or failure in err.
281 unsigned long long parse_ulonglong(const char *str, const char *cmd,
282 const char *descr, int *err)
285 unsigned long long ret;
287 ret = strtoull(str, &tmp, 0);
293 com_err(cmd, 0, "Bad %s - %s", descr, str);
302 * This function will convert a string to a block number. It returns
303 * 0 on success, 1 on failure.
305 int strtoblk(const char *cmd, const char *str, blk64_t *ret)
310 blk = parse_ulonglong(str, cmd, "block number", &err);
313 com_err(cmd, 0, "Invalid block number: %s", str);
318 * This is a common helper function used by the command processing
321 int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
322 const char *cmd, const char *usage, int flags)
324 if (argc < min_argc || argc > max_argc) {
325 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
328 if (flags & CHECK_FS_NOTOPEN) {
329 if (check_fs_not_open(argv[0]))
332 if (check_fs_open(argv[0]))
335 if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
337 if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
343 * This is a helper function used by do_stat, do_freei, do_seti, and
344 * do_testi, etc. Basically, any command which takes a single
345 * argument which is a file/inode number specifier.
347 int common_inode_args_process(int argc, char *argv[],
348 ext2_ino_t *inode, int flags)
350 if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
353 *inode = string_to_inode(argv[1]);
360 * This is a helper function used by do_freeb, do_setb, and do_testb
362 int common_block_args_process(int argc, char *argv[],
363 blk64_t *block, blk64_t *count)
367 if (common_args_process(argc, argv, 2, 3, argv[0],
368 "<block> [count]", CHECK_FS_BITMAPS))
371 if (strtoblk(argv[0], argv[1], block))
374 com_err(argv[0], 0, "Invalid block number 0");
379 err = strtoblk(argv[0], argv[2], count);
386 int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode,
387 const char *cmd, int bufsize)
391 retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize);
393 com_err(cmd, retval, "while reading inode %u", ino);
399 int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
404 retval = ext2fs_read_inode(current_fs, ino, inode);
406 com_err(cmd, retval, "while reading inode %u", ino);
412 int debugfs_write_inode_full(ext2_ino_t ino,
413 struct ext2_inode *inode,
419 retval = ext2fs_write_inode_full(current_fs, ino,
422 com_err(cmd, retval, "while writing inode %u", ino);
428 int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
433 retval = ext2fs_write_inode(current_fs, ino, inode);
435 com_err(cmd, retval, "while writing inode %u", ino);
441 int debugfs_write_new_inode(ext2_ino_t ino, struct ext2_inode * inode,
446 retval = ext2fs_write_new_inode(current_fs, ino, inode);
448 com_err(cmd, retval, "while creating inode %u", ino);
455 * Given a mode, return the ext2 file type
457 int ext2_file_type(unsigned int mode)
459 if (LINUX_S_ISREG(mode))
460 return EXT2_FT_REG_FILE;
462 if (LINUX_S_ISDIR(mode))
465 if (LINUX_S_ISCHR(mode))
466 return EXT2_FT_CHRDEV;
468 if (LINUX_S_ISBLK(mode))
469 return EXT2_FT_BLKDEV;
471 if (LINUX_S_ISLNK(mode))
472 return EXT2_FT_SYMLINK;
474 if (LINUX_S_ISFIFO(mode))
477 if (LINUX_S_ISSOCK(mode))