]>
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 */
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
= getenv("DEBUGFS_PAGER");
85 signal(SIGPIPE
, SIG_IGN
);
89 pager
= 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 */
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 extern time_t string_to_time(const char *arg
)
219 if (strcmp(arg
, "now") == 0) {
222 memset(&ts
, 0, sizeof(ts
));
224 strptime(arg
, "%Y%m%d%H%M%S", &ts
);
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
);
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)
237 if (ts
.tm_mday
== 0 || ret
== ((time_t) -1)) {
238 /* Try it as an integer... */
239 ret
= strtoul(arg
, &tmp
, 0);
241 return ((time_t) -1);
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.
250 unsigned long parse_ulong(const char *str
, const char *cmd
,
251 const char *descr
, int *err
)
256 ret
= strtoul(str
, &tmp
, 0);
262 com_err(cmd
, 0, "Bad %s - %s", descr
, str
);
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.
274 unsigned long long parse_ulonglong(const char *str
, const char *cmd
,
275 const char *descr
, int *err
)
278 unsigned long long ret
;
280 ret
= strtoull(str
, &tmp
, 0);
286 com_err(cmd
, 0, "Bad %s - %s", descr
, str
);
295 * This function will convert a string to a block number. It returns
296 * 0 on success, 1 on failure.
298 int strtoblk(const char *cmd
, const char *str
, blk64_t
*ret
)
303 blk
= parse_ulonglong(str
, cmd
, "block number", &err
);
306 com_err(cmd
, 0, "Invalid block number: %s", str
);
311 * This is a common helper function used by the command processing
314 int common_args_process(int argc
, char *argv
[], int min_argc
, int max_argc
,
315 const char *cmd
, const char *usage
, int flags
)
317 if (argc
< min_argc
|| argc
> max_argc
) {
318 com_err(argv
[0], 0, "Usage: %s %s", cmd
, usage
);
321 if (flags
& CHECK_FS_NOTOPEN
) {
322 if (check_fs_not_open(argv
[0]))
325 if (check_fs_open(argv
[0]))
328 if ((flags
& CHECK_FS_RW
) && check_fs_read_write(argv
[0]))
330 if ((flags
& CHECK_FS_BITMAPS
) && check_fs_bitmaps(argv
[0]))
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.
340 int common_inode_args_process(int argc
, char *argv
[],
341 ext2_ino_t
*inode
, int flags
)
343 if (common_args_process(argc
, argv
, 2, 2, argv
[0], "<file>", flags
))
346 *inode
= string_to_inode(argv
[1]);
353 * This is a helper function used by do_freeb, do_setb, and do_testb
355 int common_block_args_process(int argc
, char *argv
[],
356 blk64_t
*block
, blk64_t
*count
)
360 if (common_args_process(argc
, argv
, 2, 3, argv
[0],
361 "<block> [count]", CHECK_FS_BITMAPS
))
364 if (strtoblk(argv
[0], argv
[1], block
))
367 com_err(argv
[0], 0, "Invalid block number 0");
372 *count
= parse_ulong(argv
[2], argv
[0], "count", &err
);
379 int debugfs_read_inode_full(ext2_ino_t ino
, struct ext2_inode
* inode
,
380 const char *cmd
, int bufsize
)
384 retval
= ext2fs_read_inode_full(current_fs
, ino
, inode
, bufsize
);
386 com_err(cmd
, retval
, "while reading inode %u", ino
);
392 int debugfs_read_inode(ext2_ino_t ino
, struct ext2_inode
* inode
,
397 retval
= ext2fs_read_inode(current_fs
, ino
, inode
);
399 com_err(cmd
, retval
, "while reading inode %u", ino
);
405 int debugfs_write_inode_full(ext2_ino_t ino
,
406 struct ext2_inode
*inode
,
412 retval
= ext2fs_write_inode_full(current_fs
, ino
,
415 com_err(cmd
, retval
, "while writing inode %u", ino
);
421 int debugfs_write_inode(ext2_ino_t ino
, struct ext2_inode
* inode
,
426 retval
= ext2fs_write_inode(current_fs
, ino
, inode
);
428 com_err(cmd
, retval
, "while writing inode %u", ino
);
434 int debugfs_write_new_inode(ext2_ino_t ino
, struct ext2_inode
* inode
,
439 retval
= ext2fs_write_new_inode(current_fs
, ino
, inode
);
441 com_err(cmd
, retval
, "while creating inode %u", ino
);