3
0

ar.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini ar implementation for busybox
  4. *
  5. * Copyright (C) 2000 by Glenn McGrath
  6. *
  7. * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar.
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10. *
  11. * Archive creation support:
  12. * Copyright (C) 2010 Nokia Corporation. All rights reserved.
  13. * Written by Alexander Shishkin.
  14. *
  15. * There is no single standard to adhere to so ar may not portable
  16. * between different systems
  17. * http://www.unix-systems.org/single_unix_specification_v2/xcu/ar.html
  18. */
  19. //config:config AR
  20. //config: bool "ar (9.5 kb)"
  21. //config: default n # needs to be improved to be able to replace binutils ar
  22. //config: help
  23. //config: ar is an archival utility program used to create, modify, and
  24. //config: extract contents from archives. In practice, it is used exclusively
  25. //config: for object module archives used by compilers.
  26. //config:
  27. //config: Unless you have a specific application which requires ar, you should
  28. //config: probably say N here: most compilers come with their own ar utility.
  29. //config:
  30. //config:config FEATURE_AR_LONG_FILENAMES
  31. //config: bool "Support long filenames (not needed for debs)"
  32. //config: default y
  33. //config: depends on AR
  34. //config: help
  35. //config: By default the ar format can only store the first 15 characters
  36. //config: of the filename, this option removes that limitation.
  37. //config: It supports the GNU ar long filename method which moves multiple long
  38. //config: filenames into a the data section of a new ar entry.
  39. //config:
  40. //config:config FEATURE_AR_CREATE
  41. //config: bool "Support archive creation"
  42. //config: default y
  43. //config: depends on AR
  44. //config: help
  45. //config: This enables archive creation (-c and -r) with busybox ar.
  46. //applet:IF_AR(APPLET(ar, BB_DIR_USR_BIN, BB_SUID_DROP))
  47. //kbuild:lib-$(CONFIG_AR) += ar.o
  48. #include "libbb.h"
  49. #include "bb_archive.h"
  50. #include "ar_.h"
  51. #if ENABLE_FEATURE_AR_CREATE
  52. /* filter out entries with same names as specified on the command line */
  53. static char FAST_FUNC filter_replaceable(archive_handle_t *handle)
  54. {
  55. if (find_list_entry(handle->accept, handle->file_header->name))
  56. return EXIT_FAILURE;
  57. return EXIT_SUCCESS;
  58. }
  59. static void output_ar_header(archive_handle_t *handle)
  60. {
  61. /* GNU ar 2.19.51.0.14 creates malformed archives
  62. * if input files are >10G. It also truncates files >4GB
  63. * (uses "size mod 4G"). We abort in this case:
  64. * We could add support for up to 10G files, but this is unlikely to be useful.
  65. * Note that unpacking side limits all fields to "unsigned int" data type,
  66. * and treats "all ones" as an error indicator. Thus max we allow here is UINT_MAX-1.
  67. */
  68. enum {
  69. /* for 2nd field: mtime */
  70. MAX11CHARS = UINT_MAX > 0xffffffff ? (unsigned)99999999999 : UINT_MAX-1,
  71. /* for last field: filesize */
  72. MAX10CHARS = UINT_MAX > 0xffffffff ? (unsigned)9999999999 : UINT_MAX-1,
  73. };
  74. struct file_header_t *fh = handle->file_header;
  75. if (handle->offset & 1) {
  76. xwrite(handle->src_fd, "\n", 1);
  77. handle->offset++;
  78. }
  79. /* Careful! The widths should be exact. Fields must be separated */
  80. if (sizeof(off_t) > 4 && fh->size > (off_t)MAX10CHARS) {
  81. bb_error_msg_and_die("'%s' is bigger than ar can handle", fh->name);
  82. }
  83. fdprintf(handle->src_fd, "%-16.16s%-12lu%-6u%-6u%-8o%-10"OFF_FMT"u`\n",
  84. fh->name,
  85. (sizeof(time_t) > 4 && fh->mtime > MAX11CHARS) ? (long)0 : (long)fh->mtime,
  86. fh->uid > 99999 ? 0 : (int)fh->uid,
  87. fh->gid > 99999 ? 0 : (int)fh->gid,
  88. (int)fh->mode & 07777777,
  89. fh->size
  90. );
  91. handle->offset += AR_HEADER_LEN;
  92. }
  93. /*
  94. * when replacing files in an existing archive, copy from the
  95. * original archive those files that are to be left intact
  96. */
  97. static void FAST_FUNC copy_data(archive_handle_t *handle)
  98. {
  99. archive_handle_t *out_handle = handle->ar__out;
  100. struct file_header_t *fh = handle->file_header;
  101. out_handle->file_header = fh;
  102. output_ar_header(out_handle);
  103. bb_copyfd_exact_size(handle->src_fd, out_handle->src_fd, fh->size);
  104. out_handle->offset += fh->size;
  105. }
  106. static int write_ar_header(archive_handle_t *handle)
  107. {
  108. char *fn;
  109. char fn_h[17]; /* 15 + "/" + NUL */
  110. struct stat st;
  111. int fd;
  112. fn = llist_pop(&handle->accept);
  113. if (!fn)
  114. return -1;
  115. xstat(fn, &st);
  116. handle->file_header->mtime = st.st_mtime;
  117. handle->file_header->uid = st.st_uid;
  118. handle->file_header->gid = st.st_gid;
  119. handle->file_header->mode = st.st_mode;
  120. handle->file_header->size = st.st_size;
  121. handle->file_header->name = fn_h;
  122. //TODO: if ENABLE_FEATURE_AR_LONG_FILENAMES...
  123. sprintf(fn_h, "%.15s/", bb_basename(fn));
  124. output_ar_header(handle);
  125. fd = xopen(fn, O_RDONLY);
  126. bb_copyfd_exact_size(fd, handle->src_fd, st.st_size);
  127. close(fd);
  128. handle->offset += st.st_size;
  129. return 0;
  130. }
  131. static int write_ar_archive(archive_handle_t *handle)
  132. {
  133. struct stat st;
  134. archive_handle_t *out_handle;
  135. xfstat(handle->src_fd, &st, handle->ar__name);
  136. /* if archive exists, create a new handle for output.
  137. * we create it in place of the old one.
  138. */
  139. if (st.st_size != 0) {
  140. out_handle = init_handle();
  141. xunlink(handle->ar__name);
  142. out_handle->src_fd = xopen(handle->ar__name, O_WRONLY | O_CREAT | O_TRUNC);
  143. out_handle->accept = handle->accept;
  144. } else {
  145. out_handle = handle;
  146. }
  147. handle->ar__out = out_handle;
  148. xwrite(out_handle->src_fd, AR_MAGIC "\n", AR_MAGIC_LEN + 1);
  149. out_handle->offset += AR_MAGIC_LEN + 1;
  150. /* skip to the end of the archive if we have to append stuff */
  151. if (st.st_size != 0) {
  152. handle->filter = filter_replaceable;
  153. handle->action_data = copy_data;
  154. unpack_ar_archive(handle);
  155. }
  156. while (write_ar_header(out_handle) == 0)
  157. continue;
  158. /* optional, since we exit right after we return */
  159. if (ENABLE_FEATURE_CLEAN_UP) {
  160. close(handle->src_fd);
  161. if (out_handle->src_fd != handle->src_fd)
  162. close(out_handle->src_fd);
  163. }
  164. return EXIT_SUCCESS;
  165. }
  166. #endif /* FEATURE_AR_CREATE */
  167. static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
  168. {
  169. const char *mode = bb_mode_string(file_header->mode);
  170. char *mtime;
  171. mtime = ctime(&file_header->mtime);
  172. mtime[16] = ' ';
  173. memmove(&mtime[17], &mtime[20], 4);
  174. mtime[21] = '\0';
  175. printf("%s %u/%u%7"OFF_FMT"u %s %s\n", &mode[1],
  176. (int)file_header->uid, (int)file_header->gid,
  177. file_header->size,
  178. &mtime[4], file_header->name
  179. );
  180. }
  181. //usage:#define ar_trivial_usage
  182. //usage: "x|p|t"IF_FEATURE_AR_CREATE("|r")" [-ov] ARCHIVE [FILE]..."
  183. //usage:#define ar_full_usage "\n\n"
  184. //usage: "Extract or list FILEs from an ar archive"IF_FEATURE_AR_CREATE(", or create it")"\n"
  185. //usage: "\n x Extract"
  186. //usage: "\n p Extract to stdout"
  187. //usage: "\n t List"
  188. //usage: IF_FEATURE_AR_CREATE(
  189. //usage: "\n r Create"
  190. //usage: )
  191. //usage: "\n -o Restore mtime"
  192. //usage: "\n -v Verbose"
  193. int ar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  194. int ar_main(int argc UNUSED_PARAM, char **argv)
  195. {
  196. archive_handle_t *archive_handle;
  197. unsigned opt, t;
  198. enum {
  199. OPT_VERBOSE = (1 << 0),
  200. OPT_PRESERVE_DATE = (1 << 1),
  201. /* "ar r" implies create, but warns about it. c suppresses warning.
  202. * bbox accepts but ignores it: */
  203. OPT_CREATE = (1 << 2),
  204. CMD_PRINT = (1 << 3),
  205. FIRST_CMD = CMD_PRINT,
  206. CMD_LIST = (1 << 4),
  207. CMD_EXTRACT = (1 << 5),
  208. CMD_INSERT = ((1 << 6) * ENABLE_FEATURE_AR_CREATE),
  209. };
  210. archive_handle = init_handle();
  211. /* prepend '-' to the first argument if required */
  212. if (argv[1] && argv[1][0] != '-' && argv[1][0] != '\0')
  213. argv[1] = xasprintf("-%s", argv[1]);
  214. opt = getopt32(argv, "^"
  215. "voc""ptx"IF_FEATURE_AR_CREATE("r")
  216. "\0"
  217. /* -1: at least one arg is reqd */
  218. /* one of p,t,x[,r] is required */
  219. "-1:p:t:x"IF_FEATURE_AR_CREATE(":r")
  220. );
  221. argv += optind;
  222. t = opt / FIRST_CMD;
  223. if (t & (t-1)) /* more than one of p,t,x[,r] are specified */
  224. bb_show_usage();
  225. if (opt & CMD_PRINT) {
  226. archive_handle->action_data = data_extract_to_stdout;
  227. }
  228. if (opt & CMD_LIST) {
  229. archive_handle->action_header = header_list;
  230. }
  231. if (opt & CMD_EXTRACT) {
  232. archive_handle->action_data = data_extract_all;
  233. }
  234. if (opt & OPT_PRESERVE_DATE) {
  235. archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
  236. }
  237. if (opt & OPT_VERBOSE) {
  238. archive_handle->action_header = header_verbose_list_ar;
  239. }
  240. #if ENABLE_FEATURE_AR_CREATE
  241. archive_handle->ar__name = *argv;
  242. #endif
  243. archive_handle->src_fd = xopen(*argv++,
  244. (opt & CMD_INSERT)
  245. ? O_RDWR | O_CREAT
  246. : O_RDONLY
  247. );
  248. if (*argv)
  249. archive_handle->filter = filter_accept_list;
  250. while (*argv) {
  251. llist_add_to_end(&archive_handle->accept, *argv++);
  252. }
  253. #if ENABLE_FEATURE_AR_CREATE
  254. if (opt & CMD_INSERT)
  255. return write_ar_archive(archive_handle);
  256. #endif
  257. unpack_ar_archive(archive_handle);
  258. return EXIT_SUCCESS;
  259. }