2 * ls.c --- list directories
4 * Copyright (C) 1997 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
18 #include <sys/types.h>
27 #include "ext2fs/lfsck.h"
33 #define LONG_OPT 0x0001
34 #define PARSE_OPT 0x0002
35 #define RAW_OPT 0x0004
36 #define DIRDATA_OPT 0x0008
37 #define ENCRYPT_OPT 0x8000
39 struct list_dir_struct {
46 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
47 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
49 static void list_dirdata(struct list_dir_struct *ls,
50 struct ext2_dir_entry *dirent)
55 __u8 file_type = dirent->name_len >> 8;
57 data = (unsigned char *)dirent->name +
58 (dirent->name_len & EXT2_NAME_LEN) + 1;
60 for (dirdata_mask = EXT2_FT_MASK + 1;
61 dirdata_mask != 0; dirdata_mask <<= 1) {
62 if ((dirdata_mask & file_type) == 0)
67 if (dirdata_mask == EXT2_DIRENT_LUFID) {
68 struct lu_fid *fid = (struct lu_fid *)(data + 1);
70 fid_be_to_cpu(fid, fid);
71 fprintf(ls->f, "fid:"DFID, PFID(fid));
75 for (i = 1; i < dlen; i++)
76 fprintf(ls->f, "%02x", data[i]);
84 static int print_filename(FILE *f, struct ext2_dir_entry *dirent, int options)
87 const char *cp = dirent->name;
88 int len = ext2fs_dirent_name_len(dirent);
91 if ((options & ENCRYPT_OPT) && !(options & RAW_OPT)) {
93 return fprintf(f, "<encrypted (%d)>", len);
95 return snprintf(NULL, 0, "<encrypted (%d)>", len);
99 if (ch < 32 || ch >= 127 || ch == '\\') {
101 fprintf(f, "\\x%02x", ch);
112 static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
114 struct ext2_dir_entry *dirent,
115 int offset EXT2FS_ATTR((unused)),
116 int blocksize EXT2FS_ATTR((unused)),
117 char *buf EXT2FS_ATTR((unused)),
120 struct ext2_inode inode;
124 char tmp[EXT2_NAME_LEN + 16];
129 struct list_dir_struct *ls = (struct list_dir_struct *) private;
130 struct ext2_dir_entry_tail *t = (struct ext2_dir_entry_tail *) dirent;
132 thislen = ext2fs_dirent_name_len(dirent);
134 options = ls->options;
140 if (entry == DIRENT_DELETED_FILE) {
147 if (options & PARSE_OPT) {
149 if (debugfs_read_inode(ino, &inode, "ls"))
152 memset(&inode, 0, sizeof(struct ext2_inode));
153 fprintf(ls->f,"/%u/%06o/%d/%d/%.*s/", ino, inode.i_mode,
154 inode_uid(inode), inode_gid(inode), thislen, dirent->name);
155 if (LINUX_S_ISDIR(inode.i_mode))
158 fprintf(ls->f, "%llu/",
159 (unsigned long long) EXT2_I_SIZE(&inode));
160 fprintf(ls->f, "\n");
161 } else if (options & LONG_OPT) {
163 if (debugfs_read_inode(ino, &inode, "ls"))
165 modtime = inode.i_mtime;
166 tm_p = localtime(&modtime);
167 sprintf(datestr, "%2d-%s-%4d %02d:%02d",
168 tm_p->tm_mday, monstr[tm_p->tm_mon],
169 1900 + tm_p->tm_year, tm_p->tm_hour,
172 strcpy(datestr, " ");
173 memset(&inode, 0, sizeof(struct ext2_inode));
175 fprintf(ls->f, "%c%6u%c %6o ", lbr, ino, rbr, inode.i_mode);
176 if (entry == DIRENT_CHECKSUM) {
177 fprintf(ls->f, "(dirblock checksum: 0x%08x)\n",
181 fprintf(ls->f, "(%d) %5d %5d ",
182 ext2fs_dirent_file_type(dirent),
183 inode_uid(inode), inode_gid(inode));
184 fprintf(ls->f, "%5llu",
185 (unsigned long long) EXT2_I_SIZE(&inode));
186 fprintf(ls->f, " %s ", datestr);
187 if ((ls->options & DIRDATA_OPT) != 0)
188 list_dirdata(ls, dirent);
189 print_filename(ls->f, dirent, options);
192 if (entry == DIRENT_CHECKSUM) {
193 sprintf(tmp, "%c%u%c (dirblock checksum: 0x%08x) ",
194 lbr, dirent->inode, rbr, t->det_checksum);
195 thislen = strlen(tmp);
196 if (ls->col + thislen > 80) {
200 fprintf(ls->f, "%s", tmp);
204 sprintf(tmp, "%c%u%c (%d) ", lbr, dirent->inode, rbr,
206 thislen = strlen(tmp) + 3;
207 thislen += print_filename(NULL, dirent, options);
209 if (ls->col + thislen > 80) {
213 fprintf(ls->f, "%s", tmp);
214 print_filename(ls->f, dirent, options);
221 void do_list_dir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
222 void *infop EXT2FS_ATTR((unused)))
224 struct ext2_inode inode;
228 int flags = DIRENT_FLAG_INCLUDE_EMPTY;
229 struct list_dir_struct ls;
233 if (check_fs_open(argv[0]))
237 while ((c = getopt(argc, argv, "cdDlpr")) != EOF) {
240 flags |= DIRENT_FLAG_INCLUDE_CSUM;
243 ls.options |= LONG_OPT;
246 ls.options |= DIRDATA_OPT;
249 flags |= DIRENT_FLAG_INCLUDE_REMOVED;
252 ls.options |= PARSE_OPT;
255 ls.options |= RAW_OPT;
262 if (argc > optind+1) {
264 com_err(0, 0, "Usage: ls [-c] [-d] [-l] [-p] [-r] [-D] file");
271 ino = string_to_inode(argv[optind]);
278 if (debugfs_read_inode(ino, &inode, argv[0]))
281 if (inode.i_flags & EXT4_ENCRYPT_FL)
282 ls.options |= ENCRYPT_OPT;
284 retval = ext2fs_dir_iterate2(current_fs, ino, flags,
285 0, list_dir_proc, &ls);
289 com_err(argv[1], retval, 0);