From d056b99191c87a68dc73ef01817169fb8f50afbd Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Fri, 4 Jan 2008 15:28:51 -0500 Subject: [PATCH] debugfs: Add -p option to ls subcommand. Originally submitted by Jason Pyeron: While working with a compromized system (suckit root kit) hidden files were viewable by debugfs but not any other utility. When spaces and tabs were put into the directory names defugfs did not "show" them. "ls -p" quotes the output so is can be parsed easily. Addresses-Red-Hat-Bugzilla: #149480 Addresses-Sourceforge-Feature-Request: #1201667 Signed-off-by: "Theodore Ts'o" --- debugfs/debugfs.8.in | 7 ++++++- debugfs/ls.c | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/debugfs/debugfs.8.in b/debugfs/debugfs.8.in index b9de234..74d8756 100644 --- a/debugfs/debugfs.8.in +++ b/debugfs/debugfs.8.in @@ -306,7 +306,7 @@ and .I \-b options. .TP -.I ls [-l] [-d] filespec +.I ls [-l] [-d] [-p] filespec Print a listing of the files in the directory .IR filespec . The @@ -315,6 +315,11 @@ flag will list files using a more verbose format. The .I \-d flag will list deleted entries in the directory. +The +.I \-p +flag will list the files in a format which is more easily parsable by +scripts, as well as making it more clear when there are spaces or other +non-prinitng characters at the end of filenames. .TP .I modify_inode filespec Modify the contents of the inode structure in the inode diff --git a/debugfs/ls.c b/debugfs/ls.c index 52c7e34..1ceeb1b 100644 --- a/debugfs/ls.c +++ b/debugfs/ls.c @@ -30,6 +30,7 @@ extern char *optarg; #define LONG_OPT 0x0001 #define DELETED_OPT 0x0002 +#define PARSE_OPT 0x0004 struct list_dir_struct { FILE *f; @@ -72,7 +73,16 @@ static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), } else { lbr = rbr = ' '; } - if (ls->options & LONG_OPT) { + if (ls->options & PARSE_OPT) { + if (ino && debugfs_read_inode(ino, &inode, name)) return 0; + fprintf(ls->f,"/%u/%06o/%d/%d/%s/",ino,inode.i_mode,inode.i_uid, inode.i_gid,name); + if (LINUX_S_ISDIR(inode.i_mode)) + fprintf(ls->f, "/"); + else + fprintf(ls->f, "%lld/", inode.i_size | ((__u64)inode.i_size_high << 32)); + fprintf(ls->f, "\n"); + } + else if (ls->options & LONG_OPT) { if (ino) { if (debugfs_read_inode(ino, &inode, name)) return 0; @@ -123,7 +133,7 @@ void do_list_dir(int argc, char *argv[]) return; reset_getopt(); - while ((c = getopt (argc, argv, "dl")) != EOF) { + while ((c = getopt (argc, argv, "dlp")) != EOF) { switch (c) { case 'l': ls.options |= LONG_OPT; @@ -131,6 +141,9 @@ void do_list_dir(int argc, char *argv[]) case 'd': ls.options |= DELETED_OPT; break; + case 'p': + ls.options |= PARSE_OPT; + break; default: goto print_usage; } @@ -138,7 +151,7 @@ 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; } -- 1.8.3.1