ls.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. GRUB_MOD_LICENSE ("GPLv3+");
  34. static const struct grub_arg_option options[] =
  35. {
  36. {"long", 'l', 0, N_("Show a long list with more detailed information."), 0, 0},
  37. {"human-readable", 'h', 0, N_("Print sizes in a human readable format."), 0, 0},
  38. {"all", 'a', 0, N_("List all files."), 0, 0},
  39. {0, 0, 0, 0, 0, 0}
  40. };
  41. static const char grub_human_sizes[] = {' ', 'K', 'M', 'G', 'T'};
  42. static grub_err_t
  43. grub_ls_list_devices (int longlist)
  44. {
  45. auto int grub_ls_print_devices (const char *name);
  46. int grub_ls_print_devices (const char *name)
  47. {
  48. if (longlist)
  49. grub_normal_print_device_info (name);
  50. else
  51. grub_printf ("(%s) ", name);
  52. return 0;
  53. }
  54. grub_device_iterate (grub_ls_print_devices);
  55. grub_xputs ("\n");
  56. grub_refresh ();
  57. return 0;
  58. }
  59. static grub_err_t
  60. grub_ls_list_files (char *dirname, int longlist, int all, int human)
  61. {
  62. char *device_name;
  63. grub_fs_t fs;
  64. const char *path;
  65. grub_device_t dev;
  66. auto int print_files (const char *filename,
  67. const struct grub_dirhook_info *info);
  68. auto int print_files_long (const char *filename,
  69. const struct grub_dirhook_info *info);
  70. int print_files (const char *filename, const struct grub_dirhook_info *info)
  71. {
  72. if (all || filename[0] != '.')
  73. grub_printf ("%s%s ", filename, info->dir ? "/" : "");
  74. return 0;
  75. }
  76. int print_files_long (const char *filename,
  77. const struct grub_dirhook_info *info)
  78. {
  79. if ((! all) && (filename[0] == '.'))
  80. return 0;
  81. if (! info->dir)
  82. {
  83. grub_file_t file;
  84. char *pathname;
  85. if (dirname[grub_strlen (dirname) - 1] == '/')
  86. pathname = grub_xasprintf ("%s%s", dirname, filename);
  87. else
  88. pathname = grub_xasprintf ("%s/%s", dirname, filename);
  89. if (!pathname)
  90. return 1;
  91. /* XXX: For ext2fs symlinks are detected as files while they
  92. should be reported as directories. */
  93. grub_file_filter_disable_compression ();
  94. file = grub_file_open (pathname);
  95. if (! file)
  96. {
  97. grub_errno = 0;
  98. grub_free (pathname);
  99. return 0;
  100. }
  101. if (! 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 (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. device_name = grub_file_get_device_name (dirname);
  152. dev = grub_device_open (device_name);
  153. if (! dev)
  154. goto fail;
  155. fs = grub_fs_probe (dev);
  156. path = grub_strchr (dirname, ')');
  157. if (! path)
  158. path = dirname;
  159. else
  160. path++;
  161. if (! path && ! device_name)
  162. {
  163. grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
  164. goto fail;
  165. }
  166. if (! *path)
  167. {
  168. if (grub_errno == GRUB_ERR_UNKNOWN_FS)
  169. grub_errno = GRUB_ERR_NONE;
  170. grub_normal_print_device_info (device_name);
  171. }
  172. else if (fs)
  173. {
  174. if (longlist)
  175. (fs->dir) (dev, path, print_files_long);
  176. else
  177. (fs->dir) (dev, path, print_files);
  178. if (grub_errno == GRUB_ERR_BAD_FILE_TYPE
  179. && path[grub_strlen (path) - 1] != '/')
  180. {
  181. /* PATH might be a regular file. */
  182. char *p;
  183. grub_file_t file;
  184. struct grub_dirhook_info info;
  185. grub_errno = 0;
  186. grub_file_filter_disable_compression ();
  187. file = grub_file_open (dirname);
  188. if (! file)
  189. goto fail;
  190. grub_file_close (file);
  191. p = grub_strrchr (dirname, '/') + 1;
  192. dirname = grub_strndup (dirname, p - dirname);
  193. if (! dirname)
  194. goto fail;
  195. all = 1;
  196. grub_memset (&info, 0, sizeof (info));
  197. if (longlist)
  198. print_files_long (p, &info);
  199. else
  200. print_files (p, &info);
  201. grub_free (dirname);
  202. }
  203. if (grub_errno == GRUB_ERR_NONE)
  204. grub_xputs ("\n");
  205. grub_refresh ();
  206. }
  207. fail:
  208. if (dev)
  209. grub_device_close (dev);
  210. grub_free (device_name);
  211. return 0;
  212. }
  213. static grub_err_t
  214. grub_cmd_ls (grub_extcmd_context_t ctxt, int argc, char **args)
  215. {
  216. struct grub_arg_list *state = ctxt->state;
  217. int i;
  218. if (argc == 0)
  219. grub_ls_list_devices (state[0].set);
  220. else
  221. for (i = 0; i < argc; i++)
  222. grub_ls_list_files (args[i], state[0].set, state[2].set,
  223. state[1].set);
  224. return 0;
  225. }
  226. static grub_extcmd_t cmd;
  227. GRUB_MOD_INIT(ls)
  228. {
  229. cmd = grub_register_extcmd ("ls", grub_cmd_ls, 0,
  230. N_("[-l|-h|-a] [FILE ...]"),
  231. N_("List devices and files."), options);
  232. }
  233. GRUB_MOD_FINI(ls)
  234. {
  235. grub_unregister_extcmd (cmd);
  236. }