ar.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 tarball for details.
  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. #include "libbb.h"
  20. #include "unarchive.h"
  21. #include "ar.h"
  22. #if ENABLE_FEATURE_AR_CREATE
  23. /* filter out entries with same names as specified on the command line */
  24. static char FAST_FUNC filter_replaceable(archive_handle_t *handle)
  25. {
  26. if (find_list_entry(handle->accept, handle->file_header->name))
  27. return EXIT_FAILURE;
  28. return EXIT_SUCCESS;
  29. }
  30. static void output_ar_header(archive_handle_t *handle)
  31. {
  32. /* GNU ar 2.19.51.0.14 creates malformed archives
  33. * if input files are >10G. It also truncates files >4GB
  34. * (uses "size mod 4G"). We abort in this case:
  35. * We could add support for up to 10G files, but this is unlikely to be useful.
  36. * Note that unpacking side limits all fields to "unsigned int" data type,
  37. * and treats "all ones" as an error indicator. Thus max we allow here is UINT_MAX-1.
  38. */
  39. enum {
  40. /* for 2nd field: mtime */
  41. MAX11CHARS = UINT_MAX > 0xffffffff ? (unsigned)99999999999 : UINT_MAX-1,
  42. /* for last field: filesize */
  43. MAX10CHARS = UINT_MAX > 0xffffffff ? (unsigned)9999999999 : UINT_MAX-1,
  44. };
  45. struct file_header_t *fh = handle->file_header;
  46. if (handle->offset & 1) {
  47. xwrite(handle->src_fd, "\n", 1);
  48. handle->offset++;
  49. }
  50. /* Careful! The widths should be exact. Fields must be separated */
  51. if (sizeof(off_t) > 4 && fh->size > (off_t)MAX10CHARS) {
  52. bb_error_msg_and_die("'%s' is bigger than ar can handle", fh->name);
  53. }
  54. fdprintf(handle->src_fd, "%-16.16s%-12lu%-6u%-6u%-8o%-10"OFF_FMT"u`\n",
  55. fh->name,
  56. (sizeof(time_t) > 4 && fh->mtime > MAX11CHARS) ? (long)0 : (long)fh->mtime,
  57. fh->uid > 99999 ? 0 : (int)fh->uid,
  58. fh->gid > 99999 ? 0 : (int)fh->gid,
  59. (int)fh->mode & 07777777,
  60. fh->size
  61. );
  62. handle->offset += AR_HEADER_LEN;
  63. }
  64. /*
  65. * when replacing files in an existing archive, copy from the the
  66. * original archive those files that are to be left intact
  67. */
  68. static void FAST_FUNC copy_data(archive_handle_t *handle)
  69. {
  70. archive_handle_t *out_handle = handle->ar__out;
  71. struct file_header_t *fh = handle->file_header;
  72. out_handle->file_header = fh;
  73. output_ar_header(out_handle);
  74. bb_copyfd_exact_size(handle->src_fd, out_handle->src_fd, fh->size);
  75. out_handle->offset += fh->size;
  76. }
  77. static int write_ar_header(archive_handle_t *handle)
  78. {
  79. char *fn;
  80. char fn_h[17]; /* 15 + "/" + NUL */
  81. struct stat st;
  82. int fd;
  83. fn = llist_pop(&handle->accept);
  84. if (!fn)
  85. return -1;
  86. xstat(fn, &st);
  87. handle->file_header->mtime = st.st_mtime;
  88. handle->file_header->uid = st.st_uid;
  89. handle->file_header->gid = st.st_gid;
  90. handle->file_header->mode = st.st_mode;
  91. handle->file_header->size = st.st_size;
  92. handle->file_header->name = fn_h;
  93. //TODO: if ENABLE_FEATURE_AR_LONG_FILENAMES...
  94. sprintf(fn_h, "%.15s/", bb_basename(fn));
  95. output_ar_header(handle);
  96. fd = xopen(fn, O_RDONLY);
  97. bb_copyfd_exact_size(fd, handle->src_fd, st.st_size);
  98. close(fd);
  99. handle->offset += st.st_size;
  100. return 0;
  101. }
  102. static int write_ar_archive(archive_handle_t *handle)
  103. {
  104. struct stat st;
  105. archive_handle_t *out_handle;
  106. if (fstat(handle->src_fd, &st) == -1)
  107. bb_simple_perror_msg_and_die(handle->ar__name);
  108. /* if archive exists, create a new handle for output.
  109. * we create it in place of the old one.
  110. */
  111. if (st.st_size != 0) {
  112. out_handle = init_handle();
  113. xunlink(handle->ar__name);
  114. out_handle->src_fd = xopen(handle->ar__name, O_WRONLY | O_CREAT | O_TRUNC);
  115. out_handle->accept = handle->accept;
  116. } else {
  117. out_handle = handle;
  118. }
  119. handle->ar__out = out_handle;
  120. xwrite(out_handle->src_fd, AR_MAGIC "\n", AR_MAGIC_LEN + 1);
  121. out_handle->offset += AR_MAGIC_LEN + 1;
  122. /* skip to the end of the archive if we have to append stuff */
  123. if (st.st_size != 0) {
  124. handle->filter = filter_replaceable;
  125. handle->action_data = copy_data;
  126. unpack_ar_archive(handle);
  127. }
  128. while (write_ar_header(out_handle) == 0)
  129. continue;
  130. /* optional, since we exit right after we return */
  131. if (ENABLE_FEATURE_CLEAN_UP) {
  132. close(handle->src_fd);
  133. if (out_handle->src_fd != handle->src_fd)
  134. close(out_handle->src_fd);
  135. }
  136. return EXIT_SUCCESS;
  137. }
  138. #endif /* FEATURE_AR_CREATE */
  139. static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
  140. {
  141. const char *mode = bb_mode_string(file_header->mode);
  142. char *mtime;
  143. mtime = ctime(&file_header->mtime);
  144. mtime[16] = ' ';
  145. memmove(&mtime[17], &mtime[20], 4);
  146. mtime[21] = '\0';
  147. printf("%s %u/%u%7"OFF_FMT"u %s %s\n", &mode[1],
  148. (int)file_header->uid, (int)file_header->gid,
  149. file_header->size,
  150. &mtime[4], file_header->name
  151. );
  152. }
  153. #define AR_OPT_VERBOSE (1 << 0)
  154. #define AR_OPT_PRESERVE_DATE (1 << 1)
  155. /* "ar r" implies create, but warns about it. c suppresses warning.
  156. * bbox accepts but ignores it: */
  157. #define AR_OPT_CREATE (1 << 2)
  158. #define AR_CMD_PRINT (1 << 3)
  159. #define FIRST_CMD AR_CMD_PRINT
  160. #define AR_CMD_LIST (1 << 4)
  161. #define AR_CMD_EXTRACT (1 << 5)
  162. #define AR_CMD_INSERT (1 << 6)
  163. int ar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  164. int ar_main(int argc UNUSED_PARAM, char **argv)
  165. {
  166. archive_handle_t *archive_handle;
  167. unsigned opt, t;
  168. archive_handle = init_handle();
  169. /* --: prepend '-' to the first argument if required */
  170. /* -1: at least one param is reqd */
  171. /* one of p,t,x[,r] is required */
  172. opt_complementary = "--:-1:p:t:x"IF_FEATURE_AR_CREATE(":r");
  173. opt = getopt32(argv, "voc""ptx"IF_FEATURE_AR_CREATE("r"));
  174. argv += optind;
  175. t = opt / FIRST_CMD;
  176. if (t & (t-1)) /* more than one of p,t,x[,r] are specified */
  177. bb_show_usage();
  178. if (opt & AR_CMD_PRINT) {
  179. archive_handle->action_data = data_extract_to_stdout;
  180. }
  181. if (opt & AR_CMD_LIST) {
  182. archive_handle->action_header = header_list;
  183. }
  184. if (opt & AR_CMD_EXTRACT) {
  185. archive_handle->action_data = data_extract_all;
  186. }
  187. if (opt & AR_OPT_PRESERVE_DATE) {
  188. archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
  189. }
  190. if (opt & AR_OPT_VERBOSE) {
  191. archive_handle->action_header = header_verbose_list_ar;
  192. }
  193. #if ENABLE_FEATURE_AR_CREATE
  194. archive_handle->ar__name = *argv;
  195. #endif
  196. archive_handle->src_fd = xopen(*argv++,
  197. (opt & AR_CMD_INSERT)
  198. ? O_RDWR | O_CREAT
  199. : O_RDONLY
  200. );
  201. if (*argv)
  202. archive_handle->filter = filter_accept_list;
  203. while (*argv) {
  204. llist_add_to_end(&archive_handle->accept, *argv++);
  205. }
  206. #if ENABLE_FEATURE_AR_CREATE
  207. if (opt & AR_CMD_INSERT)
  208. return write_ar_archive(archive_handle);
  209. #endif
  210. unpack_ar_archive(archive_handle);
  211. return EXIT_SUCCESS;
  212. }