X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=debugfs%2Futil.c;h=9e8805481b7bd2b9e0c65d2228d01be80562171a;hb=cdc9dbf348a99b94a9f750ffdb7e6191d8ac0f39;hp=cbbc99b5c40e5c5dafa629a342e036f7689ff780;hpb=4efae606bf3159f2134fa29a5ad78fb9f6331f92;p=tools%2Fe2fsprogs.git diff --git a/debugfs/util.c b/debugfs/util.c index cbbc99b..9e88054 100644 --- a/debugfs/util.c +++ b/debugfs/util.c @@ -1,11 +1,14 @@ /* * util.c --- utilities for the debugfs program - * + * * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be * redistributed under the terms of the GNU Public License. * */ +#define _XOPEN_SOURCE 600 /* needed for strptime */ + +#include "config.h" #include #include #include @@ -15,7 +18,7 @@ #include #ifdef HAVE_GETOPT_H #include -#else +#else extern int optind; extern char *optarg; #endif @@ -23,6 +26,7 @@ extern char *optarg; extern int optreset; /* defined by BSD, but not others */ #endif +#include "ss/ss.h" #include "debugfs.h" /* @@ -37,8 +41,8 @@ extern int optreset; /* defined by BSD, but not others */ * optind be set zero to reset its state. So the unfortunate state of * affairs is that BSD-derived versions of getopt() misbehave if * optind is set to 0 in order to reset getopt(), and glibc's getopt() - * will core ump if optind is set 1 in order to reset getopt(). - * + * will core dump if optind is set 1 in order to reset getopt(). + * * More modern versions of BSD require that optreset be set to 1 in * order to reset getopt(). Sigh. Standards, anyone? * @@ -46,7 +50,7 @@ extern int optreset; /* defined by BSD, but not others */ */ void reset_getopt(void) { -#ifdef __GLIBC__ +#if defined(__GLIBC__) || defined(__linux__) optind = 0; #else optind = 1; @@ -76,15 +80,17 @@ static const char *find_pager(char *buf) FILE *open_pager(void) { FILE *outfile = 0; - const char *pager = getenv("DEBUGFS_PAGER"); + const char *pager = ss_safe_getenv("DEBUGFS_PAGER"); char buf[80]; signal(SIGPIPE, SIG_IGN); + if (!isatty(1)) + return stdout; if (!pager) - pager = getenv("PAGER"); + pager = ss_safe_getenv("PAGER"); if (!pager) pager = find_pager(buf); - if (!pager || + if (!pager || (strcmp(pager, "__none__") == 0) || ((outfile = popen(pager, "w")) == 0)) return stdout; @@ -113,13 +119,18 @@ ext2_ino_t string_to_inode(char *str) */ if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) { ino = strtoul(str+1, &end, 0); - if (*end=='>') + if (*end=='>' && (ino <= current_fs->super->s_inodes_count)) return ino; } retval = ext2fs_namei(current_fs, root, cwd, str, &ino); if (retval) { - com_err(str, retval, ""); + com_err(str, retval, 0); + return 0; + } + if (ino > current_fs->super->s_inodes_count) { + com_err(str, 0, "resolves to an illegal inode number: %u\n", + ino); return 0; } return ino; @@ -180,22 +191,30 @@ int check_fs_bitmaps(char *name) return 0; } +char *inode_time_to_string(__u32 xtime, __u32 xtime_extra) +{ + __s64 t = (__s32) xtime; + + t += (__s64) (xtime_extra & EXT4_EPOCH_MASK) << 32; + return time_to_string(t); +} + /* - * This function takes a __u32 time value and converts it to a string, + * This function takes a __s64 time value and converts it to a string, * using ctime */ -char *time_to_string(__u32 cl) +char *time_to_string(__s64 cl) { static int do_gmt = -1; time_t t = (time_t) cl; - char * tz; + const char *tz; if (do_gmt == -1) { - /* The diet libc doesn't respect the TZ environemnt variable */ - tz = getenv("TZ"); + /* The diet libc doesn't respect the TZ environment variable */ + tz = ss_safe_getenv("TZ"); if (!tz) tz = ""; - do_gmt = !strcmp(tz, "GMT"); + do_gmt = !strcmp(tz, "GMT") || !strcmp(tz, "GMT0"); } return asctime((do_gmt) ? gmtime(&t) : localtime(&t)); @@ -205,36 +224,64 @@ char *time_to_string(__u32 cl) * Parse a string as a time. Return ((time_t)-1) if the string * doesn't appear to be a sane time. */ -extern time_t string_to_time(const char *arg) +extern __s64 string_to_time(const char *arg) { struct tm ts; - unsigned long ret; + __s64 ret; char *tmp; if (strcmp(arg, "now") == 0) { return time(0); } + if (arg[0] == '@') { + /* interpret it as an integer */ + arg++; + fallback: + ret = strtoll(arg, &tmp, 0); + if (*tmp) + return -1; + return ret; + } memset(&ts, 0, sizeof(ts)); #ifdef HAVE_STRPTIME - strptime(arg, "%Y%m%d%H%M%S", &ts); + tmp = strptime(arg, "%Y%m%d%H%M%S", &ts); + if (tmp == NULL) + tmp = strptime(arg, "%Y%m%d%H%M", &ts); + if (tmp == NULL) + tmp = strptime(arg, "%Y%m%d", &ts); + if (tmp == NULL) + goto fallback; #else sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon, &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec); ts.tm_year -= 1900; ts.tm_mon -= 1; if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 || - ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 || + ts.tm_mday <= 0 || ts.tm_mday > 31 || ts.tm_hour > 23 || ts.tm_min > 59 || ts.tm_sec > 61) - ts.tm_mday = 0; + goto fallback; #endif - if (ts.tm_mday == 0) { - /* Try it as an integer... */ - - ret = strtoul(arg, &tmp, 0); - if (*tmp) - return ((time_t) -1); - } - return mktime(&ts); + ts.tm_isdst = -1; + /* strptime() may only update the specified fields, which does not + * necessarily include ts.tm_yday (%j). Calculate this if unset: + * + * Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec + * 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 + * + * Start with 31 days per month. Even months have only 30 days, but + * reverse in August, subtract one day for those months. February has + * only 28 days, not 30, subtract two days. Add day of month, minus + * one, since day is not finished yet. Leap years handled afterward. */ + if (ts.tm_yday == 0) + ts.tm_yday = (ts.tm_mon * 31) - + ((ts.tm_mon - (ts.tm_mon > 7)) / 2) - + 2 * (ts.tm_mon > 1) + ts.tm_mday - 1; + ret = ts.tm_sec + ts.tm_min*60 + ts.tm_hour*3600 + ts.tm_yday*86400 + + ((__s64) ts.tm_year-70)*31536000 + + (((__s64) ts.tm_year-69)/4)*86400 - + (((__s64) ts.tm_year-1)/100)*86400 + + (((__s64) ts.tm_year+299)/400)*86400; + return ret; } /* @@ -246,7 +293,7 @@ unsigned long parse_ulong(const char *str, const char *cmd, { char *tmp; unsigned long ret; - + ret = strtoul(str, &tmp, 0); if (*tmp == 0) { if (err) @@ -262,20 +309,45 @@ unsigned long parse_ulong(const char *str, const char *cmd, } /* + * This function will convert a string to an unsigned long long, printing + * an error message if it fails, and returning success or failure in err. + */ +unsigned long long parse_ulonglong(const char *str, const char *cmd, + const char *descr, int *err) +{ + char *tmp; + unsigned long long ret; + + ret = strtoull(str, &tmp, 0); + if (*tmp == 0) { + if (err) + *err = 0; + return ret; + } + com_err(cmd, 0, "Bad %s - %s", descr, str); + if (err) + *err = 1; + else + exit(1); + return 0; +} + +/* * This function will convert a string to a block number. It returns - * 0 on success, 1 on failure. + * 0 on success, 1 on failure. On failure, it outputs either an optionally + * specified error message or a default. */ -int strtoblk(const char *cmd, const char *str, blk_t *ret) +int strtoblk(const char *cmd, const char *str, const char *errmsg, + blk64_t *ret) { - blk_t blk; + blk64_t blk; int err; - blk = parse_ulong(str, cmd, "block number", &err); + if (errmsg == NULL) + blk = parse_ulonglong(str, cmd, "block number", &err); + else + blk = parse_ulonglong(str, cmd, errmsg, &err); *ret = blk; - if (err == 0 && blk == 0) { - com_err(cmd, 0, "Invalid block number 0"); - err = 1; - } return err; } @@ -314,9 +386,9 @@ int common_inode_args_process(int argc, char *argv[], { if (common_args_process(argc, argv, 2, 2, argv[0], "", flags)) return 1; - + *inode = string_to_inode(argv[1]); - if (!*inode) + if (!*inode) return 1; return 0; } @@ -325,7 +397,7 @@ int common_inode_args_process(int argc, char *argv[], * This is a helper function used by do_freeb, do_setb, and do_testb */ int common_block_args_process(int argc, char *argv[], - blk_t *block, int *count) + blk64_t *block, blk64_t *count) { int err; @@ -333,22 +405,27 @@ int common_block_args_process(int argc, char *argv[], " [count]", CHECK_FS_BITMAPS)) return 1; - if (strtoblk(argv[0], argv[1], block)) + if (strtoblk(argv[0], argv[1], NULL, block)) + return 1; + if (*block == 0) { + com_err(argv[0], 0, "Invalid block number 0"); return 1; + } + if (argc > 2) { - *count = parse_ulong(argv[2], argv[0], "count", &err); + err = strtoblk(argv[0], argv[2], "count", count); if (err) return 1; } return 0; } -int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode, - const char *cmd, int bufsize) +int debugfs_read_inode2(ext2_ino_t ino, struct ext2_inode * inode, + const char *cmd, int bufsize, int flags) { int retval; - retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize); + retval = ext2fs_read_inode2(current_fs, ino, inode, bufsize, flags); if (retval) { com_err(cmd, retval, "while reading inode %u", ino); return 1; @@ -369,6 +446,21 @@ int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode, return 0; } +int debugfs_write_inode2(ext2_ino_t ino, + struct ext2_inode *inode, + const char *cmd, + int bufsize, int flags) +{ + int retval; + + retval = ext2fs_write_inode2(current_fs, ino, inode, bufsize, flags); + if (retval) { + com_err(cmd, retval, "while writing inode %u", ino); + return 1; + } + return 0; +} + int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode, const char *cmd) { @@ -394,3 +486,115 @@ int debugfs_write_new_inode(ext2_ino_t ino, struct ext2_inode * inode, } return 0; } + +/* + * Given a mode, return the ext2 file type + */ +int ext2_file_type(unsigned int mode) +{ + if (LINUX_S_ISREG(mode)) + return EXT2_FT_REG_FILE; + + if (LINUX_S_ISDIR(mode)) + return EXT2_FT_DIR; + + if (LINUX_S_ISCHR(mode)) + return EXT2_FT_CHRDEV; + + if (LINUX_S_ISBLK(mode)) + return EXT2_FT_BLKDEV; + + if (LINUX_S_ISLNK(mode)) + return EXT2_FT_SYMLINK; + + if (LINUX_S_ISFIFO(mode)) + return EXT2_FT_FIFO; + + if (LINUX_S_ISSOCK(mode)) + return EXT2_FT_SOCK; + + return 0; +} + +errcode_t read_list(char *str, blk64_t **list, size_t *len) +{ + blk64_t *lst = *list; + size_t ln = *len; + char *tok, *p = str; + errcode_t retval = 0; + + while ((tok = strtok(p, ","))) { + blk64_t *l; + blk64_t x, y; + char *e; + + errno = 0; + y = x = strtoull(tok, &e, 0); + if (errno) { + retval = errno; + break; + } + if (*e == '-') { + y = strtoull(e + 1, NULL, 0); + if (errno) { + retval = errno; + break; + } + } else if (*e != 0) { + retval = EINVAL; + break; + } + if (y < x) { + retval = EINVAL; + break; + } + l = realloc(lst, sizeof(blk64_t) * (ln + y - x + 1)); + if (l == NULL) { + retval = ENOMEM; + break; + } + lst = l; + for (; x <= y; x++) + lst[ln++] = x; + p = NULL; + } + + *list = lst; + *len = ln; + return retval; +} + +void do_byte_hexdump(FILE *fp, unsigned char *buf, size_t bufsize) +{ + size_t i, j, max; + int suppress = -1; + + for (i = 0; i < bufsize; i += 16) { + max = (bufsize - i > 16) ? 16 : bufsize - i; + if (suppress < 0) { + if (i && memcmp(buf + i, buf + i - max, max) == 0) { + suppress = i; + fprintf(fp, "*\n"); + continue; + } + } else { + if (memcmp(buf + i, buf + suppress, max) == 0) + continue; + suppress = -1; + } + fprintf(fp, "%04o ", (unsigned int)i); + for (j = 0; j < 16; j++) { + if (j < max) + fprintf(fp, "%02x", buf[i+j]); + else + fprintf(fp, " "); + if ((j % 2) == 1) + fprintf(fp, " "); + } + fprintf(fp, " "); + for (j = 0; j < max; j++) + fprintf(fp, "%c", isprint(buf[i+j]) ? buf[i+j] : '.'); + fprintf(fp, "\n"); + } + fprintf(fp, "\n"); +}