Whamcloud - gitweb
debugfs: Add -p option to ls subcommand.
[tools/e2fsprogs.git] / debugfs / ls.c
1 /*
2  * ls.c --- list directories
3  * 
4  * Copyright (C) 1997 Theodore Ts'o.  This file may be redistributed
5  * under the terms of the GNU Public License.
6  */
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <time.h>
14 #ifdef HAVE_ERRNO_H
15 #include <errno.h>
16 #endif
17 #include <sys/types.h>
18 #ifdef HAVE_GETOPT_H
19 #include <getopt.h>
20 #else 
21 extern int optind;
22 extern char *optarg;
23 #endif
24
25 #include "debugfs.h"
26
27 /*
28  * list directory
29  */
30
31 #define LONG_OPT        0x0001
32 #define DELETED_OPT     0x0002
33 #define PARSE_OPT       0x0004
34
35 struct list_dir_struct {
36         FILE    *f;
37         int     col;
38         int     options;
39 };
40
41 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
42                                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
43                                         
44 static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
45                          int    entry,
46                          struct ext2_dir_entry *dirent,
47                          int    offset EXT2FS_ATTR((unused)),
48                          int    blocksize EXT2FS_ATTR((unused)),
49                          char   *buf EXT2FS_ATTR((unused)),
50                          void   *private)
51 {
52         struct ext2_inode       inode;
53         ext2_ino_t              ino;
54         struct tm               *tm_p;
55         time_t                  modtime;
56         char                    name[EXT2_NAME_LEN + 1];
57         char                    tmp[EXT2_NAME_LEN + 16];
58         char                    datestr[80];
59         char                    lbr, rbr;
60         int                     thislen;
61         struct list_dir_struct *ls = (struct list_dir_struct *) private;
62
63         thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
64                 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
65         strncpy(name, dirent->name, thislen);
66         name[thislen] = '\0';
67         ino = dirent->inode;
68
69         if (entry == DIRENT_DELETED_FILE) {
70                 lbr = '<';
71                 rbr = '>';
72                 ino = 0;
73         } else {
74                 lbr = rbr = ' ';
75         }
76         if (ls->options & PARSE_OPT) {
77                 if (ino && debugfs_read_inode(ino, &inode, name)) return 0;
78                 fprintf(ls->f,"/%u/%06o/%d/%d/%s/",ino,inode.i_mode,inode.i_uid, inode.i_gid,name);
79                 if (LINUX_S_ISDIR(inode.i_mode))
80                         fprintf(ls->f, "/");
81                 else
82                         fprintf(ls->f, "%lld/", inode.i_size | ((__u64)inode.i_size_high << 32));
83                 fprintf(ls->f, "\n");
84         }
85         else if (ls->options & LONG_OPT) {
86                 if (ino) {
87                         if (debugfs_read_inode(ino, &inode, name))
88                                 return 0;
89                         modtime = inode.i_mtime;
90                         tm_p = localtime(&modtime);
91                         sprintf(datestr, "%2d-%s-%4d %02d:%02d",
92                                 tm_p->tm_mday, monstr[tm_p->tm_mon],
93                                 1900 + tm_p->tm_year, tm_p->tm_hour,
94                                 tm_p->tm_min);
95                 } else {
96                         strcpy(datestr, "                 ");
97                         memset(&inode, 0, sizeof(struct ext2_inode));
98                 }
99                 fprintf(ls->f, "%c%6u%c %6o (%d)  %5d  %5d   ", lbr, ino, rbr,
100                         inode.i_mode, dirent->name_len >> 8,
101                         inode_uid(inode), inode_gid(inode));
102                 if (LINUX_S_ISDIR(inode.i_mode))
103                         fprintf(ls->f, "%5d", inode.i_size);
104                 else
105                         fprintf(ls->f, "%5llu", inode.i_size |
106                                 ((unsigned long long) inode.i_size_high << 32));
107                 fprintf (ls->f, " %s %s\n", datestr, name);
108         } else {
109                 sprintf(tmp, "%c%u%c (%d) %s   ", lbr, dirent->inode, rbr,
110                         dirent->rec_len, name);
111                 thislen = strlen(tmp);
112
113                 if (ls->col + thislen > 80) {
114                         fprintf(ls->f, "\n");
115                         ls->col = 0;
116                 }
117                 fprintf(ls->f, "%s", tmp);
118                 ls->col += thislen;
119         }
120         return 0;
121 }
122
123 void do_list_dir(int argc, char *argv[])
124 {
125         ext2_ino_t      inode;
126         int             retval;
127         int             c;
128         int             flags;
129         struct list_dir_struct ls;
130         
131         ls.options = 0;
132         if (check_fs_open(argv[0]))
133                 return;
134
135         reset_getopt();
136         while ((c = getopt (argc, argv, "dlp")) != EOF) {
137                 switch (c) {
138                 case 'l':
139                         ls.options |= LONG_OPT;
140                         break;
141                 case 'd':
142                         ls.options |= DELETED_OPT;
143                         break;
144                 case 'p':
145                         ls.options |= PARSE_OPT;
146                         break;
147                 default:
148                         goto print_usage;
149                 }
150         }
151
152         if (argc > optind+1) {
153         print_usage:
154                 com_err(0, 0, "Usage: ls [-l] [-d] [-p] file");
155                 return;
156         }
157
158         if (argc == optind)
159                 inode = cwd;
160         else
161                 inode = string_to_inode(argv[optind]);
162         if (!inode)
163                 return;
164
165         ls.f = open_pager();
166         ls.col = 0;
167         flags = DIRENT_FLAG_INCLUDE_EMPTY;
168         if (ls.options & DELETED_OPT)
169                 flags |= DIRENT_FLAG_INCLUDE_REMOVED;
170
171         retval = ext2fs_dir_iterate2(current_fs, inode, flags,
172                                     0, list_dir_proc, &ls);
173         fprintf(ls.f, "\n");
174         close_pager(ls.f);
175         if (retval)
176                 com_err(argv[1], retval, 0);
177
178         return;
179 }
180
181