Whamcloud - gitweb
Add new debugfs commands and arguments which make it easier to
[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 #ifdef HAVE_OPTRESET
25 extern int optreset;            /* defined by BSD, but not others */
26 #endif
27
28 #include "debugfs.h"
29
30 /*
31  * list directory
32  */
33
34 #define LONG_OPT        0x0001
35 #define DELETED_OPT     0x0002
36
37 struct list_dir_struct {
38         FILE    *f;
39         int     col;
40         int     options;
41 };
42
43 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
44                                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
45                                         
46 static int list_dir_proc(ext2_ino_t dir,
47                          int    entry,
48                          struct ext2_dir_entry *dirent,
49                          int    offset,
50                          int    blocksize,
51                          char   *buf,
52                          void   *private)
53 {
54         struct ext2_inode       inode;
55         ext2_ino_t              ino;
56         errcode_t               retval;
57         struct tm               *tm_p;
58         time_t                  modtime;
59         char                    name[EXT2_NAME_LEN];
60         char                    tmp[EXT2_NAME_LEN + 16];
61         char                    datestr[80];
62         char                    lbr, rbr;
63         int                     thislen;
64         struct list_dir_struct *ls = (struct list_dir_struct *) private;
65
66         thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
67                 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
68         strncpy(name, dirent->name, thislen);
69         name[thislen] = '\0';
70         ino = dirent->inode;
71
72         if (entry == DIRENT_DELETED_FILE) {
73                 lbr = '<';
74                 rbr = '>';
75                 ino = 0;
76         } else {
77                 lbr = rbr = ' ';
78         }
79         if (ls->options & LONG_OPT) {
80                 if (ino) {
81                         if (debugfs_read_inode(ino, &inode, name))
82                                 return;
83                         modtime = inode.i_mtime;
84                         tm_p = localtime(&modtime);
85                         sprintf(datestr, "%2d-%s-%4d %02d:%02d",
86                                 tm_p->tm_mday, monstr[tm_p->tm_mon],
87                                 1900 + tm_p->tm_year, tm_p->tm_hour,
88                                 tm_p->tm_min);
89                 } else {
90                         strcpy(datestr, "                 ");
91                         memset(&inode, 0, sizeof(struct ext2_inode));
92                 }
93                 fprintf(ls->f, "%c%6u%c %6o  %5d  %5d   ", lbr, ino, rbr,
94                         inode.i_mode, inode.i_uid, inode.i_gid);
95                 if (LINUX_S_ISDIR(inode.i_mode))
96                         fprintf(ls->f, "%5d", inode.i_size);
97                 else
98                         fprintf(ls->f, "%5lld", inode.i_size |
99                                 ((__u64)inode.i_size_high << 32));
100                 fprintf (ls->f, " %s %s\n", datestr, name);
101         } else {
102                 sprintf(tmp, "%c%u%c (%d) %s   ", lbr, dirent->inode, rbr,
103                         dirent->rec_len, name);
104                 thislen = strlen(tmp);
105
106                 if (ls->col + thislen > 80) {
107                         fprintf(ls->f, "\n");
108                         ls->col = 0;
109                 }
110                 fprintf(ls->f, "%s", tmp);
111                 ls->col += thislen;
112         }
113         return 0;
114 }
115
116 void do_list_dir(int argc, char *argv[])
117 {
118         ext2_ino_t      inode;
119         int             retval;
120         int             c;
121         int             flags;
122         struct list_dir_struct ls;
123         
124         ls.options = 0;
125         if (check_fs_open(argv[0]))
126                 return;
127
128         optind = 0;
129 #ifdef HAVE_OPTRESET
130         optreset = 1;           /* Makes BSD getopt happy */
131 #endif
132         while ((c = getopt (argc, argv, "dl")) != EOF) {
133                 switch (c) {
134                 case 'l':
135                         ls.options |= LONG_OPT;
136                         break;
137                 case 'd':
138                         ls.options |= DELETED_OPT;
139                         break;
140                 }
141         }
142
143         if (argc > optind+1) {
144                 com_err(0, 0, "Usage: ls [-l] [-d] file");
145                 return;
146         }
147
148         if (argc == optind)
149                 inode = cwd;
150         else
151                 inode = string_to_inode(argv[optind]);
152         if (!inode)
153                 return;
154
155         ls.f = open_pager();
156         ls.col = 0;
157         flags = DIRENT_FLAG_INCLUDE_EMPTY;
158         if (ls.options & DELETED_OPT)
159                 flags |= DIRENT_FLAG_INCLUDE_REMOVED;
160
161         retval = ext2fs_dir_iterate2(current_fs, inode, flags,
162                                     0, list_dir_proc, &ls);
163         fprintf(ls.f, "\n");
164         close_pager(ls.f);
165         if (retval)
166                 com_err(argv[1], retval, "");
167
168         return;
169 }
170
171