]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - debugfs/util.c
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 */
26 extern int optreset
; /* defined by BSD, but not others */
33 * This function resets the libc getopt() function, which keeps
34 * internal state. Bad design! Stupid libc API designers! No
37 * BSD-derived getopt() functions require that optind be reset to 1 in
38 * order to reset getopt() state. This used to be generally accepted
39 * way of resetting getopt(). However, glibc's getopt()
40 * has additional getopt() state beyond optind, and requires that
41 * optind be set zero to reset its state. So the unfortunate state of
42 * affairs is that BSD-derived versions of getopt() misbehave if
43 * optind is set to 0 in order to reset getopt(), and glibc's getopt()
44 * will core dump if optind is set 1 in order to reset getopt().
46 * More modern versions of BSD require that optreset be set to 1 in
47 * order to reset getopt(). Sigh. Standards, anyone?
49 * We hide the hair here.
51 void reset_getopt(void)
53 #if defined(__GLIBC__) || defined(__linux__)
59 optreset
= 1; /* Makes BSD getopt happy */
63 static const char *pager_search_list
[] = { "pager", "more", "less", 0 };
64 static const char *pager_dir_list
[] = { "/usr/bin", "/bin", 0 };
66 static const char *find_pager(char *buf
)
70 for (i
= pager_search_list
; *i
; i
++) {
71 for (j
= pager_dir_list
; *j
; j
++) {
72 sprintf(buf
, "%s/%s", *j
, *i
);
73 if (access(buf
, X_OK
) == 0)
80 FILE *open_pager(void)
83 const char *pager
= ss_safe_getenv("DEBUGFS_PAGER");
86 signal(SIGPIPE
, SIG_IGN
);
90 pager
= ss_safe_getenv("PAGER");
92 pager
= find_pager(buf
);
94 (strcmp(pager
, "__none__") == 0) ||
95 ((outfile
= popen(pager
, "w")) == 0))
100 void close_pager(FILE *stream
)
102 if (stream
&& stream
!= stdout
) pclose(stream
);
106 * This routine is used whenever a command needs to turn a string into
109 ext2_ino_t
string_to_inode(char *str
)
112 int len
= strlen(str
);
117 * If the string is of the form <ino>, then treat it as an
120 if ((len
> 2) && (str
[0] == '<') && (str
[len
-1] == '>')) {
121 ino
= strtoul(str
+1, &end
, 0);
126 retval
= ext2fs_namei(current_fs
, root
, cwd
, str
, &ino
);
128 com_err(str
, retval
, 0);
135 * This routine returns 1 if the filesystem is not open, and prints an
136 * error message to that effect.
138 int check_fs_open(char *name
)
141 com_err(name
, 0, "Filesystem not open");
148 * This routine returns 1 if a filesystem is open, and prints an
149 * error message to that effect.
151 int check_fs_not_open(char *name
)
155 "Filesystem %s is still open. Close it first.\n",
156 current_fs
->device_name
);
163 * This routine returns 1 if a filesystem is not opened read/write,
164 * and prints an error message to that effect.
166 int check_fs_read_write(char *name
)
168 if (!(current_fs
->flags
& EXT2_FLAG_RW
)) {
169 com_err(name
, 0, "Filesystem opened read/only");
176 * This routine returns 1 if a filesystem is doesn't have its inode
177 * and block bitmaps loaded, and prints an error message to that
180 int check_fs_bitmaps(char *name
)
182 if (!current_fs
->block_map
|| !current_fs
->inode_map
) {
183 com_err(name
, 0, "Filesystem bitmaps not loaded");
189 char *inode_time_to_string(__u32 xtime
, __u32 xtime_extra
)
191 __s64 t
= (__s32
) xtime
;
193 t
+= (__s64
) (xtime_extra
& EXT4_EPOCH_MASK
) << 32;
194 return time_to_string(t
);
198 * This function takes a __s64 time value and converts it to a string,
201 char *time_to_string(__s64 cl
)
203 static int do_gmt
= -1;
204 time_t t
= (time_t) cl
;
208 /* The diet libc doesn't respect the TZ environemnt variable */
209 tz
= ss_safe_getenv("TZ");
212 do_gmt
= !strcmp(tz
, "GMT") || !strcmp(tz
, "GMT0");
215 return asctime((do_gmt
) ? gmtime(&t
) : localtime(&t
));
219 * Parse a string as a time. Return ((time_t)-1) if the string
220 * doesn't appear to be a sane time.
222 extern __s64
string_to_time(const char *arg
)
228 if (strcmp(arg
, "now") == 0) {
232 /* interpret it as an integer */
235 ret
= strtoll(arg
+1, &tmp
, 0);
240 memset(&ts
, 0, sizeof(ts
));
242 tmp
= strptime(arg
, "%Y%m%d%H%M%S", &ts
);
244 tmp
= strptime(arg
, "%Y%m%d%H%M", &ts
);
246 tmp
= strptime(arg
, "%Y%m%d", &ts
);
250 sscanf(arg
, "%4d%2d%2d%2d%2d%2d", &ts
.tm_year
, &ts
.tm_mon
,
251 &ts
.tm_mday
, &ts
.tm_hour
, &ts
.tm_min
, &ts
.tm_sec
);
254 if (ts
.tm_year
< 0 || ts
.tm_mon
< 0 || ts
.tm_mon
> 11 ||
255 ts
.tm_mday
<= 0 || ts
.tm_mday
> 31 || ts
.tm_hour
> 23 ||
256 ts
.tm_min
> 59 || ts
.tm_sec
> 61)
260 /* strptime() may only update the specified fields, which does not
261 * necessarily include ts.tm_yday (%j). Calculate this if unset:
263 * Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
264 * 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
266 * Start with 31 days per month. Even months have only 30 days, but
267 * reverse in August, subtract one day for those months. February has
268 * only 28 days, not 30, subtract two days. Add day of month, minus
269 * one, since day is not finished yet. Leap years handled afterward. */
271 ts
.tm_yday
= (ts
.tm_mon
* 31) -
272 ((ts
.tm_mon
- (ts
.tm_mon
> 7)) / 2) -
273 2 * (ts
.tm_mon
> 1) + ts
.tm_mday
- 1;
274 ret
= ts
.tm_sec
+ ts
.tm_min
*60 + ts
.tm_hour
*3600 + ts
.tm_yday
*86400 +
275 ((__s64
) ts
.tm_year
-70)*31536000 +
276 (((__s64
) ts
.tm_year
-69)/4)*86400 -
277 (((__s64
) ts
.tm_year
-1)/100)*86400 +
278 (((__s64
) ts
.tm_year
+299)/400)*86400;
283 * This function will convert a string to an unsigned long, printing
284 * an error message if it fails, and returning success or failure in err.
286 unsigned long parse_ulong(const char *str
, const char *cmd
,
287 const char *descr
, int *err
)
292 ret
= strtoul(str
, &tmp
, 0);
298 com_err(cmd
, 0, "Bad %s - %s", descr
, str
);
307 * This function will convert a string to an unsigned long long, printing
308 * an error message if it fails, and returning success or failure in err.
310 unsigned long long parse_ulonglong(const char *str
, const char *cmd
,
311 const char *descr
, int *err
)
314 unsigned long long ret
;
316 ret
= strtoull(str
, &tmp
, 0);
322 com_err(cmd
, 0, "Bad %s - %s", descr
, str
);
331 * This function will convert a string to a block number. It returns
332 * 0 on success, 1 on failure. On failure, it outputs either an optionally
333 * specified error message or a default.
335 int strtoblk(const char *cmd
, const char *str
, const char *errmsg
,
342 blk
= parse_ulonglong(str
, cmd
, "block number", &err
);
344 blk
= parse_ulonglong(str
, cmd
, errmsg
, &err
);
350 * This is a common helper function used by the command processing
353 int common_args_process(int argc
, char *argv
[], int min_argc
, int max_argc
,
354 const char *cmd
, const char *usage
, int flags
)
356 if (argc
< min_argc
|| argc
> max_argc
) {
357 com_err(argv
[0], 0, "Usage: %s %s", cmd
, usage
);
360 if (flags
& CHECK_FS_NOTOPEN
) {
361 if (check_fs_not_open(argv
[0]))
364 if (check_fs_open(argv
[0]))
367 if ((flags
& CHECK_FS_RW
) && check_fs_read_write(argv
[0]))
369 if ((flags
& CHECK_FS_BITMAPS
) && check_fs_bitmaps(argv
[0]))
375 * This is a helper function used by do_stat, do_freei, do_seti, and
376 * do_testi, etc. Basically, any command which takes a single
377 * argument which is a file/inode number specifier.
379 int common_inode_args_process(int argc
, char *argv
[],
380 ext2_ino_t
*inode
, int flags
)
382 if (common_args_process(argc
, argv
, 2, 2, argv
[0], "<file>", flags
))
385 *inode
= string_to_inode(argv
[1]);
392 * This is a helper function used by do_freeb, do_setb, and do_testb
394 int common_block_args_process(int argc
, char *argv
[],
395 blk64_t
*block
, blk64_t
*count
)
399 if (common_args_process(argc
, argv
, 2, 3, argv
[0],
400 "<block> [count]", CHECK_FS_BITMAPS
))
403 if (strtoblk(argv
[0], argv
[1], NULL
, block
))
406 com_err(argv
[0], 0, "Invalid block number 0");
411 err
= strtoblk(argv
[0], argv
[2], "count", count
);
418 int debugfs_read_inode_full(ext2_ino_t ino
, struct ext2_inode
* inode
,
419 const char *cmd
, int bufsize
)
423 retval
= ext2fs_read_inode_full(current_fs
, ino
, inode
, bufsize
);
425 com_err(cmd
, retval
, "while reading inode %u", ino
);
431 int debugfs_read_inode(ext2_ino_t ino
, struct ext2_inode
* inode
,
436 retval
= ext2fs_read_inode(current_fs
, ino
, inode
);
438 com_err(cmd
, retval
, "while reading inode %u", ino
);
444 int debugfs_write_inode_full(ext2_ino_t ino
,
445 struct ext2_inode
*inode
,
451 retval
= ext2fs_write_inode_full(current_fs
, ino
,
454 com_err(cmd
, retval
, "while writing inode %u", ino
);
460 int debugfs_write_inode(ext2_ino_t ino
, struct ext2_inode
* inode
,
465 retval
= ext2fs_write_inode(current_fs
, ino
, inode
);
467 com_err(cmd
, retval
, "while writing inode %u", ino
);
473 int debugfs_write_new_inode(ext2_ino_t ino
, struct ext2_inode
* inode
,
478 retval
= ext2fs_write_new_inode(current_fs
, ino
, inode
);
480 com_err(cmd
, retval
, "while creating inode %u", ino
);
487 * Given a mode, return the ext2 file type
489 int ext2_file_type(unsigned int mode
)
491 if (LINUX_S_ISREG(mode
))
492 return EXT2_FT_REG_FILE
;
494 if (LINUX_S_ISDIR(mode
))
497 if (LINUX_S_ISCHR(mode
))
498 return EXT2_FT_CHRDEV
;
500 if (LINUX_S_ISBLK(mode
))
501 return EXT2_FT_BLKDEV
;
503 if (LINUX_S_ISLNK(mode
))
504 return EXT2_FT_SYMLINK
;
506 if (LINUX_S_ISFIFO(mode
))
509 if (LINUX_S_ISSOCK(mode
))
515 errcode_t
read_list(char *str
, blk64_t
**list
, size_t *len
)
517 blk64_t
*lst
= *list
;
522 while ((tok
= strtok(p
, ","))) {
528 y
= x
= strtoull(tok
, &e
, 0);
532 y
= strtoull(e
+ 1, NULL
, 0);
535 } else if (*e
!= 0) {
543 l
= realloc(lst
, sizeof(blk64_t
) * (ln
+ y
- x
+ 1));