X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;ds=sidebyside;f=debugfs%2Fls.c;h=057e3f6f69b929149005b2ddb3490c5e994d2d80;hb=edc7b0988ababf0e8f2c86281fc4d14503460eba;hp=ffb2310e96c1f71e1ce08056889674d2a859df9f;hpb=b772900b4bcff407c883b7bd8419af56be0a9823;p=tools%2Fe2fsprogs.git diff --git a/debugfs/ls.c b/debugfs/ls.c index ffb2310..057e3f6 100644 --- a/debugfs/ls.c +++ b/debugfs/ls.c @@ -1,10 +1,11 @@ /* * ls.c --- list directories - * + * * Copyright (C) 1997 Theodore Ts'o. This file may be redistributed * under the terms of the GNU Public License. */ +#include "config.h" #include #include #include @@ -17,7 +18,7 @@ #include #ifdef HAVE_GETOPT_H #include -#else +#else extern int optind; extern char *optarg; #endif @@ -29,17 +30,57 @@ extern char *optarg; */ #define LONG_OPT 0x0001 -#define DELETED_OPT 0x0002 +#define PARSE_OPT 0x0002 +#define RAW_OPT 0x0004 +#define ENCRYPT_OPT 0x8000 struct list_dir_struct { FILE *f; int col; int options; + int state; }; static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; - + +static int print_filename(FILE *f, struct ext2_dir_entry *dirent, int options) +{ + unsigned char ch; + const char *cp = dirent->name; + int len = ext2fs_dirent_name_len(dirent); + int retlen = 0; + + if ((options & ENCRYPT_OPT) && !(options & RAW_OPT)) { + if (f) + return fprintf(f, "", len); + else { + char tmp[1]; + return snprintf(tmp, sizeof(tmp), + "", len); + } + } + while (len--) { + ch = *cp++; + if (ch > 128) { + if (f) + fputs("M-", f); + ch -= 128; + retlen += 2; + } + if ((ch < 32) || (ch == 0x7f)) { + if (f) + fputc('^', f); + ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */ + retlen++; + } + if (f) + fputc(ch, f); + retlen++; + } + return retlen; +} + static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), int entry, struct ext2_dir_entry *dirent, @@ -52,18 +93,21 @@ static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), ext2_ino_t ino; struct tm *tm_p; time_t modtime; - char name[EXT2_NAME_LEN + 1]; char tmp[EXT2_NAME_LEN + 16]; char datestr[80]; char lbr, rbr; int thislen; + int options; struct list_dir_struct *ls = (struct list_dir_struct *) private; + struct ext2_dir_entry_tail *t = (struct ext2_dir_entry_tail *) dirent; - thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ? - (dirent->name_len & 0xFF) : EXT2_NAME_LEN; - strncpy(name, dirent->name, thislen); - name[thislen] = '\0'; + thislen = ext2fs_dirent_name_len(dirent); ino = dirent->inode; + options = ls->options; + if (ls->state < 2) { + ls->state++; + options |= RAW_OPT; + } if (entry == DIRENT_DELETED_FILE) { lbr = '<'; @@ -72,9 +116,22 @@ static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), } else { lbr = rbr = ' '; } - if (ls->options & LONG_OPT) { + if (options & PARSE_OPT) { if (ino) { - if (debugfs_read_inode(ino, &inode, name)) + if (debugfs_read_inode(ino, &inode, "ls")) + return 0; + } else + memset(&inode, 0, sizeof(struct ext2_inode)); + fprintf(ls->f,"/%u/%06o/%d/%d/%*s/", ino, inode.i_mode, + inode.i_uid, inode.i_gid, thislen, dirent->name); + if (LINUX_S_ISDIR(inode.i_mode)) + fprintf(ls->f, "/"); + else + fprintf(ls->f, "%lld/", EXT2_I_SIZE(&inode)); + fprintf(ls->f, "\n"); + } else if (options & LONG_OPT) { + if (ino) { + if (debugfs_read_inode(ino, &inode, "ls")) return 0; modtime = inode.i_mtime; tm_p = localtime(&modtime); @@ -86,25 +143,48 @@ static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), strcpy(datestr, " "); memset(&inode, 0, sizeof(struct ext2_inode)); } - fprintf(ls->f, "%c%6u%c %6o (%d) %5d %5d ", lbr, ino, rbr, - inode.i_mode, dirent->name_len >> 8, - inode.i_uid, inode.i_gid); + fprintf(ls->f, "%c%6u%c %6o ", lbr, ino, rbr, inode.i_mode); + if (entry == DIRENT_CHECKSUM) { + fprintf(ls->f, "(dirblock checksum: 0x%08x)\n", + t->det_checksum); + return 0; + } + fprintf(ls->f, "(%d) %5d %5d ", + ext2fs_dirent_file_type(dirent), + inode_uid(inode), inode_gid(inode)); if (LINUX_S_ISDIR(inode.i_mode)) fprintf(ls->f, "%5d", inode.i_size); else - fprintf(ls->f, "%5lld", inode.i_size | - ((__u64)inode.i_size_high << 32)); - fprintf (ls->f, " %s %s\n", datestr, name); + fprintf(ls->f, "%5llu", EXT2_I_SIZE(&inode)); + fputs(datestr, ls->f); + fputc(' ', ls->f); + print_filename(ls->f, dirent, options); + fputc('\n', ls->f); } else { - sprintf(tmp, "%c%u%c (%d) %s ", lbr, dirent->inode, rbr, - dirent->rec_len, name); - thislen = strlen(tmp); + if (entry == DIRENT_CHECKSUM) { + sprintf(tmp, "%c%u%c (dirblock checksum: 0x%08x) ", + lbr, dirent->inode, rbr, t->det_checksum); + thislen = strlen(tmp); + if (ls->col + thislen > 80) { + fputc('\n', ls->f); + ls->col = 0; + } + fprintf(ls->f, "%s", tmp); + ls->col += thislen; + return 0; + } + sprintf(tmp, "%c%u%c (%d) ", lbr, dirent->inode, rbr, + dirent->rec_len); + thislen = strlen(tmp) + 3; + thislen += print_filename(NULL, dirent, options); if (ls->col + thislen > 80) { - fprintf(ls->f, "\n"); + fputc('\n', ls->f); ls->col = 0; } fprintf(ls->f, "%s", tmp); + print_filename(ls->f, dirent, options); + fputs(" ", ls->f); ls->col += thislen; } return 0; @@ -112,24 +192,35 @@ static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), void do_list_dir(int argc, char *argv[]) { - ext2_ino_t inode; + struct ext2_inode inode; + ext2_ino_t ino; int retval; int c; - int flags; + int flags = DIRENT_FLAG_INCLUDE_EMPTY; struct list_dir_struct ls; - + ls.options = 0; + ls.state = 0; if (check_fs_open(argv[0])) return; reset_getopt(); - while ((c = getopt (argc, argv, "dl")) != EOF) { + while ((c = getopt (argc, argv, "cdlpr")) != EOF) { switch (c) { + case 'c': + flags |= DIRENT_FLAG_INCLUDE_CSUM; + break; case 'l': ls.options |= LONG_OPT; break; case 'd': - ls.options |= DELETED_OPT; + flags |= DIRENT_FLAG_INCLUDE_REMOVED; + break; + case 'p': + ls.options |= PARSE_OPT; + break; + case 'r': + ls.options |= RAW_OPT; break; default: goto print_usage; @@ -138,24 +229,27 @@ void do_list_dir(int argc, char *argv[]) if (argc > optind+1) { print_usage: - com_err(0, 0, "Usage: ls [-l] [-d] file"); + com_err(0, 0, "Usage: ls [-l] [-d] [-p] file"); return; } if (argc == optind) - inode = cwd; + ino = cwd; else - inode = string_to_inode(argv[optind]); - if (!inode) + ino = string_to_inode(argv[optind]); + if (!ino) return; ls.f = open_pager(); ls.col = 0; - flags = DIRENT_FLAG_INCLUDE_EMPTY; - if (ls.options & DELETED_OPT) - flags |= DIRENT_FLAG_INCLUDE_REMOVED; - retval = ext2fs_dir_iterate2(current_fs, inode, flags, + if (debugfs_read_inode(ino, &inode, argv[0])) + return; + + if (inode.i_flags & EXT4_ENCRYPT_FL) + ls.options |= ENCRYPT_OPT; + + retval = ext2fs_dir_iterate2(current_fs, ino, flags, 0, list_dir_proc, &ls); fprintf(ls.f, "\n"); close_pager(ls.f);