X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=debugfs%2Futil.c;h=9e8805481b7bd2b9e0c65d2228d01be80562171a;hb=cdc9dbf348a99b94a9f750ffdb7e6191d8ac0f39;hp=20c6c61a3fed228ed1d82aa79b6649f833794359;hpb=90c5b4c9de8ea43e0994cc3a07cdc4c4c777623c;p=tools%2Fe2fsprogs.git diff --git a/debugfs/util.c b/debugfs/util.c index 20c6c61..9e88054 100644 --- a/debugfs/util.c +++ b/debugfs/util.c @@ -119,7 +119,7 @@ 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; } @@ -128,6 +128,11 @@ ext2_ino_t string_to_inode(char *str) 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; } @@ -186,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; const char *tz; if (do_gmt == -1) { - /* The diet libc doesn't respect the TZ environemnt variable */ + /* The diet libc doesn't respect the TZ environment variable */ tz = ss_safe_getenv("TZ"); if (!tz) tz = ""; - do_gmt = !strcmp(tz, "GMT") | !strcmp(tz, "GMT0"); + do_gmt = !strcmp(tz, "GMT") || !strcmp(tz, "GMT0"); } return asctime((do_gmt) ? gmtime(&t) : localtime(&t)); @@ -211,10 +224,10 @@ 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. */ -time_t string_to_time(const char *arg) +extern __s64 string_to_time(const char *arg) { struct tm ts; - time_t ret; + __s64 ret; char *tmp; if (strcmp(arg, "now") == 0) { @@ -224,15 +237,19 @@ time_t string_to_time(const char *arg) /* interpret it as an integer */ arg++; fallback: - ret = strtoul(arg, &tmp, 0); + ret = strtoll(arg, &tmp, 0); if (*tmp) - return ((time_t) -1); + return -1; return ret; } memset(&ts, 0, sizeof(ts)); #ifdef HAVE_STRPTIME 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, @@ -240,9 +257,9 @@ time_t string_to_time(const char *arg) 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 ts.tm_isdst = -1; /* strptime() may only update the specified fields, which does not @@ -260,8 +277,10 @@ time_t string_to_time(const char *arg) ((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 + - (ts.tm_year-70)*31536000 + ((ts.tm_year-69)/4)*86400 - - ((ts.tm_year-1)/100)*86400 + ((ts.tm_year+299)/400)*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; } @@ -390,7 +409,7 @@ int common_block_args_process(int argc, char *argv[], return 1; if (*block == 0) { com_err(argv[0], 0, "Invalid block number 0"); - err = 1; + return 1; } if (argc > 2) { @@ -401,12 +420,12 @@ int common_block_args_process(int argc, char *argv[], 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; @@ -427,15 +446,14 @@ int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode, return 0; } -int debugfs_write_inode_full(ext2_ino_t ino, - struct ext2_inode *inode, - const char *cmd, - int bufsize) +int debugfs_write_inode2(ext2_ino_t ino, + struct ext2_inode *inode, + const char *cmd, + int bufsize, int flags) { int retval; - retval = ext2fs_write_inode_full(current_fs, ino, - inode, bufsize); + retval = ext2fs_write_inode2(current_fs, ino, inode, bufsize, flags); if (retval) { com_err(cmd, retval, "while writing inode %u", ino); return 1; @@ -497,3 +515,86 @@ int ext2_file_type(unsigned int mode) 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"); +}