~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to commands/ls.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
static const struct grub_arg_option options[] =
36
 
  {
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},
40
 
    {0, 0, 0, 0, 0, 0}
41
 
  };
42
 
 
43
 
static const char grub_human_sizes[] = {' ', 'K', 'M', 'G', 'T'};
44
 
 
45
 
static grub_err_t
46
 
grub_ls_list_devices (int longlist)
47
 
{
48
 
  auto int grub_ls_print_devices (const char *name);
49
 
  int grub_ls_print_devices (const char *name)
50
 
    {
51
 
      if (longlist)
52
 
        grub_normal_print_device_info (name);
53
 
      else
54
 
        grub_printf ("(%s) ", name);
55
 
 
56
 
      return 0;
57
 
    }
58
 
 
59
 
  grub_device_iterate (grub_ls_print_devices);
60
 
  grub_putchar ('\n');
61
 
  grub_refresh ();
62
 
 
63
 
  return 0;
64
 
}
65
 
 
66
 
static grub_err_t
67
 
grub_ls_list_files (char *dirname, int longlist, int all, int human)
68
 
{
69
 
  char *device_name;
70
 
  grub_fs_t fs;
71
 
  const char *path;
72
 
  grub_device_t dev;
73
 
 
74
 
  auto int print_files (const char *filename,
75
 
                        const struct grub_dirhook_info *info);
76
 
  auto int print_files_long (const char *filename,
77
 
                             const struct grub_dirhook_info *info);
78
 
 
79
 
  int print_files (const char *filename, const struct grub_dirhook_info *info)
80
 
    {
81
 
      if (all || filename[0] != '.')
82
 
        grub_printf ("%s%s ", filename, info->dir ? "/" : "");
83
 
 
84
 
      return 0;
85
 
    }
86
 
 
87
 
  int print_files_long (const char *filename,
88
 
                        const struct grub_dirhook_info *info)
89
 
    {
90
 
      char *pathname;
91
 
 
92
 
      if ((! all) && (filename[0] == '.'))
93
 
        return 0;
94
 
 
95
 
      if (! info->dir)
96
 
        {
97
 
          grub_file_t file;
98
 
 
99
 
          if (dirname[grub_strlen (dirname) - 1] == '/')
100
 
            pathname = grub_xasprintf ("%s%s", dirname, filename);
101
 
          else
102
 
            pathname = grub_xasprintf ("%s/%s", dirname, filename);
103
 
 
104
 
          if (!pathname)
105
 
            return 1;
106
 
 
107
 
          /* XXX: For ext2fs symlinks are detected as files while they
108
 
             should be reported as directories.  */
109
 
          file = grub_file_open (pathname);
110
 
          if (! file)
111
 
            {
112
 
              grub_errno = 0;
113
 
              return 0;
114
 
            }
115
 
 
116
 
          if (! human)
117
 
            grub_printf ("%-12llu", (unsigned long long) file->size);
118
 
          else
119
 
            {
120
 
              grub_uint64_t fsize = file->size * 100ULL;
121
 
              int fsz = file->size;
122
 
              int units = 0;
123
 
              char buf[20];
124
 
 
125
 
              while (fsz / 1024)
126
 
                {
127
 
                  fsize = (fsize + 512) / 1024;
128
 
                  fsz /= 1024;
129
 
                  units++;
130
 
                }
131
 
 
132
 
              if (units)
133
 
                {
134
 
                  grub_uint32_t whole, fraction;
135
 
 
136
 
                  whole = grub_divmod64 (fsize, 100, &fraction);
137
 
                  grub_snprintf (buf, sizeof (buf),
138
 
                                 "%u.%02u%c", whole, fraction,
139
 
                                 grub_human_sizes[units]);
140
 
                  grub_printf ("%-12s", buf);
141
 
                }
142
 
              else
143
 
                grub_printf ("%-12llu", (unsigned long long) file->size);
144
 
 
145
 
            }
146
 
          grub_file_close (file);
147
 
        }
148
 
      else
149
 
        grub_printf ("%-12s", "DIR");
150
 
 
151
 
      if (info->mtimeset)
152
 
        {
153
 
          struct grub_datetime datetime;
154
 
          grub_unixtime2datetime (info->mtime, &datetime);
155
 
          if (human)
156
 
            grub_printf (" %d-%02d-%02d %02d:%02d:%02d %-11s ",
157
 
                         datetime.year, datetime.month, datetime.day,
158
 
                         datetime.hour, datetime.minute,
159
 
                         datetime.second,
160
 
                         grub_get_weekday_name (&datetime));
161
 
          else
162
 
            grub_printf (" %04d%02d%02d%02d%02d%02d ",
163
 
                         datetime.year, datetime.month,
164
 
                         datetime.day, datetime.hour,
165
 
                         datetime.minute, datetime.second);
166
 
        }
167
 
      grub_printf ("%s%s\n", filename, info->dir ? "/" : "");
168
 
 
169
 
      return 0;
170
 
    }
171
 
 
172
 
  device_name = grub_file_get_device_name (dirname);
173
 
  dev = grub_device_open (device_name);
174
 
  if (! dev)
175
 
    goto fail;
176
 
 
177
 
  fs = grub_fs_probe (dev);
178
 
  path = grub_strchr (dirname, ')');
179
 
  if (! path)
180
 
    path = dirname;
181
 
  else
182
 
    path++;
183
 
 
184
 
  if (! path && ! device_name)
185
 
    {
186
 
      grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
187
 
      goto fail;
188
 
    }
189
 
 
190
 
  if (! *path)
191
 
    {
192
 
      if (grub_errno == GRUB_ERR_UNKNOWN_FS)
193
 
        grub_errno = GRUB_ERR_NONE;
194
 
 
195
 
      grub_normal_print_device_info (device_name);
196
 
    }
197
 
  else if (fs)
198
 
    {
199
 
      if (longlist)
200
 
        (fs->dir) (dev, path, print_files_long);
201
 
      else
202
 
        (fs->dir) (dev, path, print_files);
203
 
 
204
 
      if (grub_errno == GRUB_ERR_BAD_FILE_TYPE
205
 
          && path[grub_strlen (path) - 1] != '/')
206
 
        {
207
 
          /* PATH might be a regular file.  */
208
 
          char *p;
209
 
          grub_file_t file;
210
 
          struct grub_dirhook_info info;
211
 
          grub_errno = 0;
212
 
 
213
 
          file = grub_file_open (dirname);
214
 
          if (! file)
215
 
            goto fail;
216
 
 
217
 
          grub_file_close (file);
218
 
 
219
 
          p = grub_strrchr (dirname, '/') + 1;
220
 
          dirname = grub_strndup (dirname, p - dirname);
221
 
          if (! dirname)
222
 
            goto fail;
223
 
 
224
 
          all = 1;
225
 
          grub_memset (&info, 0, sizeof (info));
226
 
          if (longlist)
227
 
            print_files_long (p, &info);
228
 
          else
229
 
            print_files (p, &info);
230
 
 
231
 
          grub_free (dirname);
232
 
        }
233
 
 
234
 
      if (grub_errno == GRUB_ERR_NONE)
235
 
        grub_putchar ('\n');
236
 
 
237
 
      grub_refresh ();
238
 
    }
239
 
 
240
 
 fail:
241
 
  if (dev)
242
 
    grub_device_close (dev);
243
 
 
244
 
  grub_free (device_name);
245
 
 
246
 
  return 0;
247
 
}
248
 
 
249
 
static grub_err_t
250
 
grub_cmd_ls (grub_extcmd_t cmd, int argc, char **args)
251
 
{
252
 
  struct grub_arg_list *state = cmd->state;
253
 
 
254
 
  if (argc == 0)
255
 
    grub_ls_list_devices (state[0].set);
256
 
  else
257
 
    grub_ls_list_files (args[0], state[0].set, state[2].set,
258
 
                        state[1].set);
259
 
 
260
 
  return 0;
261
 
}
262
 
 
263
 
static grub_extcmd_t cmd;
264
 
 
265
 
GRUB_MOD_INIT(ls)
266
 
{
267
 
  cmd = grub_register_extcmd ("ls", grub_cmd_ls, GRUB_COMMAND_FLAG_BOTH,
268
 
                              N_("[-l|-h|-a] [FILE]"),
269
 
                              N_("List devices and files."), options);
270
 
}
271
 
 
272
 
GRUB_MOD_FINI(ls)
273
 
{
274
 
  grub_unregister_extcmd (cmd);
275
 
}