ls.c 6.5 KB

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