• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "config.h"
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <time.h>
15 #ifdef HAVE_ERRNO_H
16 #include <errno.h>
17 #endif
18 #include <sys/types.h>
19 #ifdef HAVE_GETOPT_H
20 #include <getopt.h>
21 #else
22 extern int optind;
23 extern char *optarg;
24 #endif
25 
26 #include "debugfs.h"
27 
28 /*
29  * list directory
30  */
31 
32 #define LONG_OPT	0x0001
33 #define PARSE_OPT	0x0002
34 #define RAW_OPT		0x0004
35 #define ENCRYPT_OPT	0x8000
36 
37 struct list_dir_struct {
38 	FILE	*f;
39 	int	col;
40 	int	options;
41 	int	state;
42 };
43 
44 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
45 				"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
46 
print_filename(FILE * f,struct ext2_dir_entry * dirent,int options)47 static int print_filename(FILE *f, struct ext2_dir_entry *dirent, int options)
48 {
49 	unsigned char	ch;
50 	const char *cp = dirent->name;
51 	int len = ext2fs_dirent_name_len(dirent);
52 	int retlen = 0;
53 
54 	if ((options & ENCRYPT_OPT) && !(options & RAW_OPT)) {
55 		if (f)
56 			return fprintf(f, "<encrypted (%d)>", len);
57 		else
58 			return snprintf(NULL, 0, "<encrypted (%d)>", len);
59 	}
60 	while (len--) {
61 		ch = *cp++;
62 		if (ch < 32 || ch >= 127 || ch == '\\') {
63 			if (f)
64 				fprintf(f, "\\x%02x", ch);
65 			retlen += 4;
66 		} else {
67 			if (f)
68 				fputc(ch, f);
69 			retlen++;
70 		}
71 	}
72 	return retlen;
73 }
74 
list_dir_proc(ext2_ino_t dir EXT2FS_ATTR ((unused)),int entry,struct ext2_dir_entry * dirent,int offset EXT2FS_ATTR ((unused)),int blocksize EXT2FS_ATTR ((unused)),char * buf EXT2FS_ATTR ((unused)),void * private)75 static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
76 			 int	entry,
77 			 struct ext2_dir_entry *dirent,
78 			 int	offset EXT2FS_ATTR((unused)),
79 			 int	blocksize EXT2FS_ATTR((unused)),
80 			 char	*buf EXT2FS_ATTR((unused)),
81 			 void	*private)
82 {
83 	struct ext2_inode	inode;
84 	ext2_ino_t		ino;
85 	struct tm		*tm_p;
86 	time_t			modtime;
87 	char			tmp[EXT2_NAME_LEN + 16];
88 	char			datestr[80];
89 	char			lbr, rbr;
90 	int			thislen;
91 	int			options;
92 	struct list_dir_struct *ls = (struct list_dir_struct *) private;
93 	struct ext2_dir_entry_tail *t = (struct ext2_dir_entry_tail *) dirent;
94 
95 	thislen = ext2fs_dirent_name_len(dirent);
96 	ino = dirent->inode;
97 	options = ls->options;
98 	if (ls->state < 2) {
99 		ls->state++;
100 		options |= RAW_OPT;
101 	}
102 
103 	if (entry == DIRENT_DELETED_FILE) {
104 		lbr = '<';
105 		rbr = '>';
106 		ino = 0;
107 	} else {
108 		lbr = rbr = ' ';
109 	}
110 	if (options & PARSE_OPT) {
111 		if (ino) {
112 			if (debugfs_read_inode(ino, &inode, "ls"))
113 				return 0;
114 		} else
115 			memset(&inode, 0, sizeof(struct ext2_inode));
116 		fprintf(ls->f,"/%u/%06o/%d/%d/%.*s/", ino, inode.i_mode,
117 			inode.i_uid, inode.i_gid, thislen, dirent->name);
118 		if (LINUX_S_ISDIR(inode.i_mode))
119 			fprintf(ls->f, "/");
120 		else
121 			fprintf(ls->f, "%lld/", EXT2_I_SIZE(&inode));
122 		fprintf(ls->f, "\n");
123 	} else if (options & LONG_OPT) {
124 		if (ino) {
125 			if (debugfs_read_inode(ino, &inode, "ls"))
126 				return 0;
127 			modtime = inode.i_mtime;
128 			tm_p = localtime(&modtime);
129 			sprintf(datestr, "%2d-%s-%4d %02d:%02d",
130 				tm_p->tm_mday, monstr[tm_p->tm_mon],
131 				1900 + tm_p->tm_year, tm_p->tm_hour,
132 				tm_p->tm_min);
133 		} else {
134 			strcpy(datestr, "                 ");
135 			memset(&inode, 0, sizeof(struct ext2_inode));
136 		}
137 		fprintf(ls->f, "%c%6u%c %6o ", lbr, ino, rbr, inode.i_mode);
138 		if (entry == DIRENT_CHECKSUM) {
139 			fprintf(ls->f, "(dirblock checksum: 0x%08x)\n",
140 				t->det_checksum);
141 			return 0;
142 		}
143 		fprintf(ls->f, "(%d)  %5d  %5d   ",
144 			ext2fs_dirent_file_type(dirent),
145 			inode_uid(inode), inode_gid(inode));
146 			fprintf(ls->f, "%5llu", EXT2_I_SIZE(&inode));
147 		fprintf(ls->f, " %s ", datestr);
148 		print_filename(ls->f, dirent, options);
149 		fputc('\n', ls->f);
150 	} else {
151 		if (entry == DIRENT_CHECKSUM) {
152 			sprintf(tmp, "%c%u%c (dirblock checksum: 0x%08x)   ",
153 				lbr, dirent->inode, rbr, t->det_checksum);
154 			thislen = strlen(tmp);
155 			if (ls->col + thislen > 80) {
156 				fputc('\n', ls->f);
157 				ls->col = 0;
158 			}
159 			fprintf(ls->f, "%s", tmp);
160 			ls->col += thislen;
161 			return 0;
162 		}
163 		sprintf(tmp, "%c%u%c (%d) ", lbr, dirent->inode, rbr,
164 			dirent->rec_len);
165 		thislen = strlen(tmp) + 3;
166 		thislen += print_filename(NULL, dirent, options);
167 
168 		if (ls->col + thislen > 80) {
169 			fputc('\n', ls->f);
170 			ls->col = 0;
171 		}
172 		fprintf(ls->f, "%s", tmp);
173 		print_filename(ls->f, dirent, options);
174 		fputs("   ", ls->f);
175 		ls->col += thislen;
176 	}
177 	return 0;
178 }
179 
do_list_dir(int argc,char * argv[],int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))180 void do_list_dir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
181 		 void *infop EXT2FS_ATTR((unused)))
182 {
183 	struct ext2_inode inode;
184 	ext2_ino_t	ino;
185 	int		retval;
186 	int		c;
187 	int		flags = DIRENT_FLAG_INCLUDE_EMPTY;
188 	struct list_dir_struct ls;
189 
190 	ls.options = 0;
191 	ls.state = 0;
192 	if (check_fs_open(argv[0]))
193 		return;
194 
195 	reset_getopt();
196 	while ((c = getopt (argc, argv, "cdlpr")) != EOF) {
197 		switch (c) {
198 		case 'c':
199 			flags |= DIRENT_FLAG_INCLUDE_CSUM;
200 			break;
201 		case 'l':
202 			ls.options |= LONG_OPT;
203 			break;
204 		case 'd':
205 			flags |= DIRENT_FLAG_INCLUDE_REMOVED;
206 			break;
207 		case 'p':
208 			ls.options |= PARSE_OPT;
209 			break;
210 		case 'r':
211 			ls.options |= RAW_OPT;
212 			break;
213 		default:
214 			goto print_usage;
215 		}
216 	}
217 
218 	if (argc > optind+1) {
219 	print_usage:
220 		com_err(0, 0, "Usage: ls [-c] [-d] [-l] [-p] [-r] file");
221 		return;
222 	}
223 
224 	if (argc == optind)
225 		ino = cwd;
226 	else
227 		ino = string_to_inode(argv[optind]);
228 	if (!ino)
229 		return;
230 
231 	ls.f = open_pager();
232 	ls.col = 0;
233 
234 	if (debugfs_read_inode(ino, &inode, argv[0]))
235 		return;
236 
237 	if (inode.i_flags & EXT4_ENCRYPT_FL)
238 		ls.options |= ENCRYPT_OPT;
239 
240 	retval = ext2fs_dir_iterate2(current_fs, ino, flags,
241 				    0, list_dir_proc, &ls);
242 	fprintf(ls.f, "\n");
243 	close_pager(ls.f);
244 	if (retval)
245 		com_err(argv[1], retval, 0);
246 
247 	return;
248 }
249 
250 
251