Whamcloud - gitweb
build: update version to 1.45.6.wc7
[tools/e2fsprogs.git] / debugfs / ls.c
index a1e8f4e..3193865 100644 (file)
@@ -24,6 +24,7 @@ extern char *optarg;
 #endif
 
 #include "debugfs.h"
+#include "ext2fs/lfsck.h"
 
 /*
  * list directory
@@ -32,6 +33,7 @@ extern char *optarg;
 #define LONG_OPT       0x0001
 #define PARSE_OPT      0x0002
 #define RAW_OPT                0x0004
+#define DIRDATA_OPT    0x0008
 #define ENCRYPT_OPT    0x8000
 
 struct list_dir_struct {
@@ -44,6 +46,41 @@ struct list_dir_struct {
 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
 
+static void list_dirdata(struct list_dir_struct *ls,
+                        struct ext2_dir_entry *dirent)
+{
+       unsigned char   *data;
+       int             dlen;
+       __u8            dirdata_mask;
+       __u8            file_type = dirent->name_len >> 8;
+
+       data = (unsigned char *)dirent->name +
+               (dirent->name_len & EXT2_NAME_LEN) + 1;
+
+       for (dirdata_mask = EXT2_FT_MASK + 1;
+            dirdata_mask != 0; dirdata_mask <<= 1) {
+               if ((dirdata_mask & file_type) == 0)
+                       continue;
+
+               dlen = data[0];
+
+               if (dirdata_mask == EXT2_DIRENT_LUFID) {
+                       struct lu_fid *fid = (struct lu_fid *)(data + 1);
+
+                       fid_be_to_cpu(fid, fid);
+                       fprintf(ls->f, "fid:"DFID, PFID(fid));
+               } else {
+                       int i;
+
+                       for (i = 1; i < dlen; i++)
+                               fprintf(ls->f, "%02x", data[i]);
+               }
+
+               fprintf(ls->f, " ");
+               data += dlen;
+       }
+}
+
 static int print_filename(FILE *f, struct ext2_dir_entry *dirent, int options)
 {
        unsigned char   ch;
@@ -54,29 +91,20 @@ static int print_filename(FILE *f, struct ext2_dir_entry *dirent, int options)
        if ((options & ENCRYPT_OPT) && !(options & RAW_OPT)) {
                if (f)
                        return fprintf(f, "<encrypted (%d)>", len);
-               else {
-                       char tmp[1];
-                       return snprintf(tmp, sizeof(tmp),
-                                       "<encrypted (%d)>", len);
-               }
+               else
+                       return snprintf(NULL, 0, "<encrypted (%d)>", len);
        }
        while (len--) {
                ch = *cp++;
-               if (ch > 128) {
+               if (ch < 32 || ch >= 127 || ch == '\\') {
                        if (f)
-                               fputs("M-", f);
-                       ch -= 128;
-                       retlen += 2;
-               }
-               if ((ch < 32) || (ch == 0x7f)) {
+                               fprintf(f, "\\x%02x", ch);
+                       retlen += 4;
+               } else {
                        if (f)
-                               fputc('^', f);
-                       ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
+                               fputc(ch, f);
                        retlen++;
                }
-               if (f)
-                       fputc(ch, f);
-               retlen++;
        }
        return retlen;
 }
@@ -152,11 +180,10 @@ static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
                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, "%5llu", EXT2_I_SIZE(&inode));
                fprintf(ls->f, " %s ", datestr);
+               if ((ls->options & DIRDATA_OPT) != 0)
+                       list_dirdata(ls, dirent);
                print_filename(ls->f, dirent, options);
                fputc('\n', ls->f);
        } else {
@@ -205,7 +232,7 @@ void do_list_dir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
                return;
 
        reset_getopt();
-       while ((c = getopt (argc, argv, "cdlpr")) != EOF) {
+       while ((c = getopt(argc, argv, "cdDlpr")) != EOF) {
                switch (c) {
                case 'c':
                        flags |= DIRENT_FLAG_INCLUDE_CSUM;
@@ -213,6 +240,9 @@ void do_list_dir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
                case 'l':
                        ls.options |= LONG_OPT;
                        break;
+               case 'D':
+                       ls.options |= DIRDATA_OPT;
+                       break;
                case 'd':
                        flags |= DIRENT_FLAG_INCLUDE_REMOVED;
                        break;
@@ -229,7 +259,7 @@ void do_list_dir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
 
        if (argc > optind+1) {
        print_usage:
-               com_err(0, 0, "Usage: ls [-c] [-d] [-l] [-p] [-r] file");
+               com_err(0, 0, "Usage: ls [-c] [-d] [-l] [-p] [-r] [-D] file");
                return;
        }