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/normal.h>
  30. #include <grub/extcmd.h>
  31. #include <grub/datetime.h>
  32. #include <grub/i18n.h>
  33. #include <grub/net.h>
  34. GRUB_MOD_LICENSE ("GPLv3+");
  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. /* Helper for grub_ls_list_devices. */
  43. static int
  44. grub_ls_print_devices (const char *name, void *data)
  45. {
  46. int *longlist = data;
  47. if (*longlist)
  48. grub_normal_print_device_info (name);
  49. else
  50. grub_printf ("(%s) ", name);
  51. return 0;
  52. }
  53. static grub_err_t
  54. grub_ls_list_devices (int longlist)
  55. {
  56. grub_device_iterate (grub_ls_print_devices, &longlist);
  57. grub_xputs ("\n");
  58. #if 0
  59. {
  60. grub_net_app_level_t proto;
  61. int first = 1;
  62. FOR_NET_APP_LEVEL (proto)
  63. {
  64. if (first)
  65. grub_puts_ (N_ ("Network protocols:"));
  66. first = 0;
  67. grub_printf ("%s ", proto->name);
  68. }
  69. grub_xputs ("\n");
  70. }
  71. #endif
  72. grub_refresh ();
  73. return 0;
  74. }
  75. /* Context for grub_ls_list_files. */
  76. struct grub_ls_list_files_ctx
  77. {
  78. char *dirname;
  79. int all;
  80. int human;
  81. };
  82. /* Helper for grub_ls_list_files. */
  83. static int
  84. print_files (const char *filename, const struct grub_dirhook_info *info,
  85. void *data)
  86. {
  87. struct grub_ls_list_files_ctx *ctx = data;
  88. if (ctx->all || filename[0] != '.')
  89. grub_printf ("%s%s ", filename, info->dir ? "/" : "");
  90. return 0;
  91. }
  92. /* Helper for grub_ls_list_files. */
  93. static int
  94. print_files_long (const char *filename, const struct grub_dirhook_info *info,
  95. void *data)
  96. {
  97. struct grub_ls_list_files_ctx *ctx = data;
  98. if ((! ctx->all) && (filename[0] == '.'))
  99. return 0;
  100. if (! info->dir)
  101. {
  102. grub_file_t file;
  103. char *pathname;
  104. if (ctx->dirname[grub_strlen (ctx->dirname) - 1] == '/')
  105. pathname = grub_xasprintf ("%s%s", ctx->dirname, filename);
  106. else
  107. pathname = grub_xasprintf ("%s/%s", ctx->dirname, filename);
  108. if (!pathname)
  109. return 1;
  110. /* XXX: For ext2fs symlinks are detected as files while they
  111. should be reported as directories. */
  112. grub_file_filter_disable_compression ();
  113. file = grub_file_open (pathname);
  114. if (! file)
  115. {
  116. grub_errno = 0;
  117. grub_free (pathname);
  118. return 0;
  119. }
  120. if (! ctx->human)
  121. grub_printf ("%-12llu", (unsigned long long) file->size);
  122. else
  123. grub_printf ("%-12s", grub_get_human_size (file->size,
  124. GRUB_HUMAN_SIZE_SHORT));
  125. grub_file_close (file);
  126. grub_free (pathname);
  127. }
  128. else
  129. grub_printf ("%-12s", _("DIR"));
  130. if (info->mtimeset)
  131. {
  132. struct grub_datetime datetime;
  133. grub_unixtime2datetime (info->mtime, &datetime);
  134. if (ctx->human)
  135. grub_printf (" %d-%02d-%02d %02d:%02d:%02d %-11s ",
  136. datetime.year, datetime.month, datetime.day,
  137. datetime.hour, datetime.minute,
  138. datetime.second,
  139. grub_get_weekday_name (&datetime));
  140. else
  141. grub_printf (" %04d%02d%02d%02d%02d%02d ",
  142. datetime.year, datetime.month,
  143. datetime.day, datetime.hour,
  144. datetime.minute, datetime.second);
  145. }
  146. grub_printf ("%s%s\n", filename, info->dir ? "/" : "");
  147. return 0;
  148. }
  149. static grub_err_t
  150. grub_ls_list_files (char *dirname, int longlist, int all, int human)
  151. {
  152. char *device_name;
  153. grub_fs_t fs;
  154. const char *path;
  155. grub_device_t dev;
  156. device_name = grub_file_get_device_name (dirname);
  157. dev = grub_device_open (device_name);
  158. if (! dev)
  159. goto fail;
  160. fs = grub_fs_probe (dev);
  161. path = grub_strchr (dirname, ')');
  162. if (! path)
  163. path = dirname;
  164. else
  165. path++;
  166. if (! path && ! device_name)
  167. {
  168. grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
  169. goto fail;
  170. }
  171. if (! *path)
  172. {
  173. if (grub_errno == GRUB_ERR_UNKNOWN_FS)
  174. grub_errno = GRUB_ERR_NONE;
  175. grub_normal_print_device_info (device_name);
  176. }
  177. else if (fs)
  178. {
  179. struct grub_ls_list_files_ctx ctx = {
  180. .dirname = dirname,
  181. .all = all,
  182. .human = human
  183. };
  184. if (longlist)
  185. (fs->dir) (dev, path, print_files_long, &ctx);
  186. else
  187. (fs->dir) (dev, path, print_files, &ctx);
  188. if (grub_errno == GRUB_ERR_BAD_FILE_TYPE
  189. && path[grub_strlen (path) - 1] != '/')
  190. {
  191. /* PATH might be a regular file. */
  192. char *p;
  193. grub_file_t file;
  194. struct grub_dirhook_info info;
  195. grub_errno = 0;
  196. grub_file_filter_disable_compression ();
  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, &ctx);
  209. else
  210. print_files (p, &info, &ctx);
  211. grub_free (dirname);
  212. }
  213. if (grub_errno == GRUB_ERR_NONE)
  214. grub_xputs ("\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_context_t ctxt, int argc, char **args)
  225. {
  226. struct grub_arg_list *state = ctxt->state;
  227. int i;
  228. if (argc == 0)
  229. grub_ls_list_devices (state[0].set);
  230. else
  231. for (i = 0; i < argc; i++)
  232. grub_ls_list_files (args[i], state[0].set, state[2].set,
  233. state[1].set);
  234. return 0;
  235. }
  236. static grub_extcmd_t cmd;
  237. GRUB_MOD_INIT(ls)
  238. {
  239. cmd = grub_register_extcmd ("ls", grub_cmd_ls, 0,
  240. N_("[-l|-h|-a] [FILE ...]"),
  241. N_("List devices and files."), options);
  242. }
  243. GRUB_MOD_FINI(ls)
  244. {
  245. grub_unregister_extcmd (cmd);
  246. }