]> git.proxmox.com Git - grub2.git/blob - grub-core/commands/ls.c
AHCI support.
[grub2.git] / grub-core / commands / ls.c
1 /* ls.c - command to list files and devices */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2007,2008,2009 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <grub/types.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/dl.h>
25 #include <grub/disk.h>
26 #include <grub/device.h>
27 #include <grub/term.h>
28 #include <grub/partition.h>
29 #include <grub/file.h>
30 #include <grub/normal.h>
31 #include <grub/extcmd.h>
32 #include <grub/datetime.h>
33 #include <grub/i18n.h>
34
35 GRUB_MOD_LICENSE ("GPLv3+");
36
37 static const struct grub_arg_option options[] =
38 {
39 {"long", 'l', 0, N_("Show a long list with more detailed information."), 0, 0},
40 {"human-readable", 'h', 0, N_("Print sizes in a human readable format."), 0, 0},
41 {"all", 'a', 0, N_("List all files."), 0, 0},
42 {0, 0, 0, 0, 0, 0}
43 };
44
45 static const char grub_human_sizes[] = {' ', 'K', 'M', 'G', 'T'};
46
47 static grub_err_t
48 grub_ls_list_devices (int longlist)
49 {
50 auto int grub_ls_print_devices (const char *name);
51 int grub_ls_print_devices (const char *name)
52 {
53 if (longlist)
54 grub_normal_print_device_info (name);
55 else
56 grub_printf ("(%s) ", name);
57
58 return 0;
59 }
60
61 grub_device_iterate (grub_ls_print_devices);
62 grub_xputs ("\n");
63 grub_refresh ();
64
65 return 0;
66 }
67
68 static grub_err_t
69 grub_ls_list_files (char *dirname, int longlist, int all, int human)
70 {
71 char *device_name;
72 grub_fs_t fs;
73 const char *path;
74 grub_device_t dev;
75
76 auto int print_files (const char *filename,
77 const struct grub_dirhook_info *info);
78 auto int print_files_long (const char *filename,
79 const struct grub_dirhook_info *info);
80
81 int print_files (const char *filename, const struct grub_dirhook_info *info)
82 {
83 if (all || filename[0] != '.')
84 grub_printf ("%s%s ", filename, info->dir ? "/" : "");
85
86 return 0;
87 }
88
89 int print_files_long (const char *filename,
90 const struct grub_dirhook_info *info)
91 {
92 if ((! all) && (filename[0] == '.'))
93 return 0;
94
95 if (! info->dir)
96 {
97 grub_file_t file;
98 char *pathname;
99
100 if (dirname[grub_strlen (dirname) - 1] == '/')
101 pathname = grub_xasprintf ("%s%s", dirname, filename);
102 else
103 pathname = grub_xasprintf ("%s/%s", dirname, filename);
104
105 if (!pathname)
106 return 1;
107
108 /* XXX: For ext2fs symlinks are detected as files while they
109 should be reported as directories. */
110 grub_file_filter_disable_compression ();
111 file = grub_file_open (pathname);
112 if (! file)
113 {
114 grub_errno = 0;
115 grub_free (pathname);
116 return 0;
117 }
118
119 if (! human)
120 grub_printf ("%-12llu", (unsigned long long) file->size);
121 else
122 {
123 grub_uint64_t fsize = file->size * 100ULL;
124 int fsz = file->size;
125 int units = 0;
126 char buf[20];
127
128 while (fsz / 1024)
129 {
130 fsize = (fsize + 512) / 1024;
131 fsz /= 1024;
132 units++;
133 }
134
135 if (units)
136 {
137 grub_uint64_t whole, fraction;
138
139 whole = grub_divmod64 (fsize, 100, &fraction);
140 grub_snprintf (buf, sizeof (buf),
141 "%" PRIuGRUB_UINT64_T
142 ".%02" PRIuGRUB_UINT64_T "%c", whole, fraction,
143 grub_human_sizes[units]);
144 grub_printf ("%-12s", buf);
145 }
146 else
147 grub_printf ("%-12llu", (unsigned long long) file->size);
148
149 }
150 grub_file_close (file);
151 grub_free (pathname);
152 }
153 else
154 grub_printf ("%-12s", "DIR");
155
156 if (info->mtimeset)
157 {
158 struct grub_datetime datetime;
159 grub_unixtime2datetime (info->mtime, &datetime);
160 if (human)
161 grub_printf (" %d-%02d-%02d %02d:%02d:%02d %-11s ",
162 datetime.year, datetime.month, datetime.day,
163 datetime.hour, datetime.minute,
164 datetime.second,
165 grub_get_weekday_name (&datetime));
166 else
167 grub_printf (" %04d%02d%02d%02d%02d%02d ",
168 datetime.year, datetime.month,
169 datetime.day, datetime.hour,
170 datetime.minute, datetime.second);
171 }
172 grub_printf ("%s%s\n", filename, info->dir ? "/" : "");
173
174 return 0;
175 }
176
177 device_name = grub_file_get_device_name (dirname);
178 dev = grub_device_open (device_name);
179 if (! dev)
180 goto fail;
181
182 fs = grub_fs_probe (dev);
183 path = grub_strchr (dirname, ')');
184 if (! path)
185 path = dirname;
186 else
187 path++;
188
189 if (! path && ! device_name)
190 {
191 grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
192 goto fail;
193 }
194
195 if (! *path)
196 {
197 if (grub_errno == GRUB_ERR_UNKNOWN_FS)
198 grub_errno = GRUB_ERR_NONE;
199
200 grub_normal_print_device_info (device_name);
201 }
202 else if (fs)
203 {
204 if (longlist)
205 (fs->dir) (dev, path, print_files_long);
206 else
207 (fs->dir) (dev, path, print_files);
208
209 if (grub_errno == GRUB_ERR_BAD_FILE_TYPE
210 && path[grub_strlen (path) - 1] != '/')
211 {
212 /* PATH might be a regular file. */
213 char *p;
214 grub_file_t file;
215 struct grub_dirhook_info info;
216 grub_errno = 0;
217
218 grub_file_filter_disable_compression ();
219 file = grub_file_open (dirname);
220 if (! file)
221 goto fail;
222
223 grub_file_close (file);
224
225 p = grub_strrchr (dirname, '/') + 1;
226 dirname = grub_strndup (dirname, p - dirname);
227 if (! dirname)
228 goto fail;
229
230 all = 1;
231 grub_memset (&info, 0, sizeof (info));
232 if (longlist)
233 print_files_long (p, &info);
234 else
235 print_files (p, &info);
236
237 grub_free (dirname);
238 }
239
240 if (grub_errno == GRUB_ERR_NONE)
241 grub_xputs ("\n");
242
243 grub_refresh ();
244 }
245
246 fail:
247 if (dev)
248 grub_device_close (dev);
249
250 grub_free (device_name);
251
252 return 0;
253 }
254
255 static grub_err_t
256 grub_cmd_ls (grub_extcmd_context_t ctxt, int argc, char **args)
257 {
258 struct grub_arg_list *state = ctxt->state;
259 int i;
260
261 if (argc == 0)
262 grub_ls_list_devices (state[0].set);
263 else
264 for (i = 0; i < argc; i++)
265 grub_ls_list_files (args[i], state[0].set, state[2].set,
266 state[1].set);
267
268 return 0;
269 }
270
271 static grub_extcmd_t cmd;
272
273 GRUB_MOD_INIT(ls)
274 {
275 cmd = grub_register_extcmd ("ls", grub_cmd_ls, 0,
276 N_("[-l|-h|-a] [FILE ...]"),
277 N_("List devices and files."), options);
278 }
279
280 GRUB_MOD_FINI(ls)
281 {
282 grub_unregister_extcmd (cmd);
283 }