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