ar.c 8.4 KB

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