untar.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * This file is in the public domain.
  3. * Use it as you wish.
  4. */
  5. /*
  6. * This is a compact tar extraction program using libarchive whose
  7. * primary goal is small executable size. Statically linked, it can
  8. * be very small, depending in large part on how cleanly factored your
  9. * system libraries are. Note that this uses the standard libarchive,
  10. * without any special recompilation. The only functional concession
  11. * is that this program uses the uid/gid from the archive instead of
  12. * doing uname/gname lookups. (Add a call to
  13. * archive_write_disk_set_standard_lookup() to enable uname/gname
  14. * lookups, but be aware that this can add 500k or more to a static
  15. * executable, depending on the system libraries, since user/group
  16. * lookups frequently pull in password, YP/LDAP, networking, and DNS
  17. * resolver libraries.)
  18. *
  19. * To build:
  20. * $ gcc -static -Wall -o untar untar.c -larchive
  21. * $ strip untar
  22. *
  23. * NOTE: On some systems, you may need to add additional flags
  24. * to ensure that untar.c is compiled the same way as libarchive
  25. * was compiled. In particular, Linux users will probably
  26. * have to add -D_FILE_OFFSET_BITS=64 to the command line above.
  27. *
  28. * For fun, statically compile the following simple hello.c program
  29. * using the same flags as for untar and compare the size:
  30. *
  31. * #include <stdio.h>
  32. * int main(int argc, char **argv) {
  33. * printf("hello, world\n");
  34. * return(0);
  35. * }
  36. *
  37. * You may be even more surprised by the compiled size of true.c listed here:
  38. *
  39. * int main(int argc, char **argv) {
  40. * return (0);
  41. * }
  42. *
  43. * On a slightly customized FreeBSD 5 system that I used around
  44. * 2005, hello above compiled to 89k compared to untar of 69k. So at
  45. * that time, libarchive's tar reader and extract-to-disk routines
  46. * compiled to less code than printf().
  47. *
  48. * On my FreeBSD development system today (August, 2009):
  49. * hello: 195024 bytes
  50. * true: 194912 bytes
  51. * untar: 259924 bytes
  52. */
  53. #include <sys/types.h>
  54. __FBSDID("$FreeBSD$");
  55. #include <sys/stat.h>
  56. #include <archive.h>
  57. #include <archive_entry.h>
  58. #include <fcntl.h>
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <unistd.h>
  63. static void errmsg(const char *);
  64. static void extract(const char *filename, int do_extract, int flags);
  65. static void fail(const char *, const char *, int);
  66. static int copy_data(struct archive *, struct archive *);
  67. static void msg(const char *);
  68. static void usage(void);
  69. static void warn(const char *, const char *);
  70. static int verbose = 0;
  71. int
  72. main(int argc, const char **argv)
  73. {
  74. const char *filename = NULL;
  75. int compress, flags, mode, opt;
  76. (void)argc;
  77. mode = 'x';
  78. verbose = 0;
  79. compress = '\0';
  80. flags = ARCHIVE_EXTRACT_TIME;
  81. /* Among other sins, getopt(3) pulls in printf(3). */
  82. while (*++argv != NULL && **argv == '-') {
  83. const char *p = *argv + 1;
  84. while ((opt = *p++) != '\0') {
  85. switch (opt) {
  86. case 'f':
  87. if (*p != '\0')
  88. filename = p;
  89. else
  90. filename = *++argv;
  91. p += strlen(p);
  92. break;
  93. case 'p':
  94. flags |= ARCHIVE_EXTRACT_PERM;
  95. flags |= ARCHIVE_EXTRACT_ACL;
  96. flags |= ARCHIVE_EXTRACT_FFLAGS;
  97. break;
  98. case 't':
  99. mode = opt;
  100. break;
  101. case 'v':
  102. verbose++;
  103. break;
  104. case 'x':
  105. mode = opt;
  106. break;
  107. default:
  108. usage();
  109. }
  110. }
  111. }
  112. switch (mode) {
  113. case 't':
  114. extract(filename, 0, flags);
  115. break;
  116. case 'x':
  117. extract(filename, 1, flags);
  118. break;
  119. }
  120. return (0);
  121. }
  122. static void
  123. extract(const char *filename, int do_extract, int flags)
  124. {
  125. struct archive *a;
  126. struct archive *ext;
  127. struct archive_entry *entry;
  128. int r;
  129. a = archive_read_new();
  130. ext = archive_write_disk_new();
  131. archive_write_disk_set_options(ext, flags);
  132. /*
  133. * Note: archive_write_disk_set_standard_lookup() is useful
  134. * here, but it requires library routines that can add 500k or
  135. * more to a static executable.
  136. */
  137. archive_read_support_format_tar(a);
  138. /*
  139. * On my system, enabling other archive formats adds 20k-30k
  140. * each. Enabling gzip decompression adds about 20k.
  141. * Enabling bzip2 is more expensive because the libbz2 library
  142. * isn't very well factored.
  143. */
  144. if (filename != NULL && strcmp(filename, "-") == 0)
  145. filename = NULL;
  146. if ((r = archive_read_open_file(a, filename, 10240)))
  147. fail("archive_read_open_file()",
  148. archive_error_string(a), r);
  149. for (;;) {
  150. r = archive_read_next_header(a, &entry);
  151. if (r == ARCHIVE_EOF)
  152. break;
  153. if (r != ARCHIVE_OK)
  154. fail("archive_read_next_header()",
  155. archive_error_string(a), 1);
  156. if (verbose && do_extract)
  157. msg("x ");
  158. if (verbose || !do_extract)
  159. msg(archive_entry_pathname(entry));
  160. if (do_extract) {
  161. r = archive_write_header(ext, entry);
  162. if (r != ARCHIVE_OK)
  163. warn("archive_write_header()",
  164. archive_error_string(ext));
  165. else {
  166. copy_data(a, ext);
  167. r = archive_write_finish_entry(ext);
  168. if (r != ARCHIVE_OK)
  169. fail("archive_write_finish_entry()",
  170. archive_error_string(ext), 1);
  171. }
  172. }
  173. if (verbose || !do_extract)
  174. msg("\n");
  175. }
  176. archive_read_close(a);
  177. archive_read_finish(a);
  178. exit(0);
  179. }
  180. static int
  181. copy_data(struct archive *ar, struct archive *aw)
  182. {
  183. int r;
  184. const void *buff;
  185. size_t size;
  186. off_t offset;
  187. for (;;) {
  188. r = archive_read_data_block(ar, &buff, &size, &offset);
  189. if (r == ARCHIVE_EOF)
  190. return (ARCHIVE_OK);
  191. if (r != ARCHIVE_OK)
  192. return (r);
  193. r = archive_write_data_block(aw, buff, size, offset);
  194. if (r != ARCHIVE_OK) {
  195. warn("archive_write_data_block()",
  196. archive_error_string(aw));
  197. return (r);
  198. }
  199. }
  200. }
  201. /*
  202. * These reporting functions use low-level I/O; on some systems, this
  203. * is a significant code reduction. Of course, on many server and
  204. * desktop operating systems, malloc() and even crt rely on printf(),
  205. * which in turn pulls in most of the rest of stdio, so this is not an
  206. * optimization at all there. (If you're going to pay 100k or more
  207. * for printf() anyway, you may as well use it!)
  208. */
  209. static void
  210. msg(const char *m)
  211. {
  212. write(1, m, strlen(m));
  213. }
  214. static void
  215. errmsg(const char *m)
  216. {
  217. write(2, m, strlen(m));
  218. }
  219. static void
  220. warn(const char *f, const char *m)
  221. {
  222. errmsg(f);
  223. errmsg(" failed: ");
  224. errmsg(m);
  225. errmsg("\n");
  226. }
  227. static void
  228. fail(const char *f, const char *m, int r)
  229. {
  230. warn(f, m);
  231. exit(r);
  232. }
  233. static void
  234. usage(void)
  235. {
  236. const char *m = "Usage: untar [-tvx] [-f file] [file]\n";
  237. errmsg(m);
  238. exit(1);
  239. }