libtar.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. ** Copyright 1998-2003 University of Illinois Board of Trustees
  3. ** Copyright 1998-2003 Mark D. Roth
  4. ** All rights reserved.
  5. **
  6. ** libtar.c - demo driver program for libtar
  7. **
  8. ** Mark D. Roth <[email protected]>
  9. ** Campus Information Technologies and Educational Services
  10. ** University of Illinois at Urbana-Champaign
  11. */
  12. #include <libtar/config.h>
  13. #include <libtar/libtar.h>
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #if !defined(_WIN32) || defined(__CYGWIN__)
  18. #include <libtar/compat.h>
  19. #include <io.h>
  20. #else
  21. #include <sys/param.h>
  22. #endif
  23. #ifdef STDC_HEADERS
  24. # include <string.h>
  25. #endif
  26. #ifdef HAVE_UNISTD_H
  27. # include <unistd.h>
  28. # include <stdlib.h>
  29. #endif
  30. #ifdef DEBUG
  31. # include <signal.h>
  32. #endif
  33. #ifdef HAVE_LIBZ
  34. # include <cmzlib/zlib.h>
  35. #endif
  36. #include <libtar/compat.h>
  37. char *progname;
  38. int verbose = 0;
  39. int use_gnu = 0;
  40. #ifdef DEBUG
  41. void
  42. segv_handler(int sig)
  43. {
  44. puts("OOPS! Caught SIGSEGV, bailing out...");
  45. fflush(stdout);
  46. fflush(stderr);
  47. }
  48. #endif
  49. #ifdef HAVE_LIBZ
  50. int use_zlib = 0;
  51. int
  52. gzopen_frontend(char *pathname, int oflags, int mode)
  53. {
  54. char *gzoflags;
  55. gzFile gzf;
  56. int fd;
  57. switch (oflags & O_ACCMODE)
  58. {
  59. case O_WRONLY:
  60. gzoflags = "wb";
  61. break;
  62. case O_RDONLY:
  63. gzoflags = "rb";
  64. break;
  65. default:
  66. case O_RDWR:
  67. errno = EINVAL;
  68. return -1;
  69. }
  70. fd = open(pathname, oflags, mode);
  71. if (fd == -1)
  72. return -1;
  73. #if !defined(_WIN32) || defined(__CYGWIN__)
  74. if ((oflags & O_CREAT) && fchmod(fd, mode))
  75. return -1;
  76. #endif
  77. gzf = gzdopen(fd, gzoflags);
  78. if (!gzf)
  79. {
  80. errno = ENOMEM;
  81. return -1;
  82. }
  83. return (int)gzf;
  84. }
  85. tartype_t gztype = { (openfunc_t) gzopen_frontend, (closefunc_t) gzclose,
  86. (readfunc_t) gzread, (writefunc_t) gzwrite
  87. };
  88. #endif /* HAVE_LIBZ */
  89. int
  90. create(char *tarfile, char *rootdir, libtar_list_t *l)
  91. {
  92. TAR *t;
  93. char *pathname;
  94. char buf[MAXPATHLEN];
  95. libtar_listptr_t lp;
  96. if (tar_open(&t, tarfile,
  97. #ifdef HAVE_LIBZ
  98. (use_zlib ? &gztype : NULL),
  99. #else
  100. NULL,
  101. #endif
  102. O_WRONLY | O_CREAT, 0644,
  103. (verbose ? TAR_VERBOSE : 0)
  104. | (use_gnu ? TAR_GNU : 0)) == -1)
  105. {
  106. fprintf(stderr, "tar_open(): %s\n", strerror(errno));
  107. return -1;
  108. }
  109. libtar_listptr_reset(&lp);
  110. while (libtar_list_next(l, &lp) != 0)
  111. {
  112. pathname = (char *)libtar_listptr_data(&lp);
  113. if (pathname[0] != '/' && rootdir != NULL)
  114. snprintf(buf, sizeof(buf), "%s/%s", rootdir, pathname);
  115. else
  116. strlcpy(buf, pathname, sizeof(buf));
  117. if (tar_append_tree(t, buf, pathname) != 0)
  118. {
  119. fprintf(stderr,
  120. "tar_append_tree(\"%s\", \"%s\"): %s\n", buf,
  121. pathname, strerror(errno));
  122. tar_close(t);
  123. return -1;
  124. }
  125. }
  126. if (tar_append_eof(t) != 0)
  127. {
  128. fprintf(stderr, "tar_append_eof(): %s\n", strerror(errno));
  129. tar_close(t);
  130. return -1;
  131. }
  132. if (tar_close(t) != 0)
  133. {
  134. fprintf(stderr, "tar_close(): %s\n", strerror(errno));
  135. return -1;
  136. }
  137. return 0;
  138. }
  139. int
  140. list(char *tarfile)
  141. {
  142. TAR *t;
  143. int i;
  144. if (tar_open(&t, tarfile,
  145. #ifdef HAVE_LIBZ
  146. (use_zlib ? &gztype : NULL),
  147. #else
  148. NULL,
  149. #endif
  150. O_RDONLY, 0,
  151. (verbose ? TAR_VERBOSE : 0)
  152. | (use_gnu ? TAR_GNU : 0)) == -1)
  153. {
  154. fprintf(stderr, "tar_open(): %s\n", strerror(errno));
  155. return -1;
  156. }
  157. while ((i = th_read(t)) == 0)
  158. {
  159. th_print_long_ls(t);
  160. #ifdef DEBUG
  161. th_print(t);
  162. #endif
  163. if (TH_ISREG(t) && tar_skip_regfile(t) != 0)
  164. {
  165. fprintf(stderr, "tar_skip_regfile(): %s\n",
  166. strerror(errno));
  167. return -1;
  168. }
  169. }
  170. #ifdef DEBUG
  171. printf("th_read() returned %d\n", i);
  172. printf("EOF mark encountered after %ld bytes\n",
  173. # ifdef HAVE_LIBZ
  174. (use_zlib
  175. ? gzseek((gzFile) t->fd, 0, SEEK_CUR)
  176. :
  177. # endif
  178. lseek(t->fd, 0, SEEK_CUR)
  179. # ifdef HAVE_LIBZ
  180. )
  181. # endif
  182. );
  183. #endif
  184. if (tar_close(t) != 0)
  185. {
  186. fprintf(stderr, "tar_close(): %s\n", strerror(errno));
  187. return -1;
  188. }
  189. return 0;
  190. }
  191. int
  192. extract(char *tarfile, char *rootdir)
  193. {
  194. TAR *t;
  195. #ifdef DEBUG
  196. puts("opening tarfile...");
  197. #endif
  198. if (tar_open(&t, tarfile,
  199. #ifdef HAVE_LIBZ
  200. (use_zlib ? &gztype : NULL),
  201. #else
  202. NULL,
  203. #endif
  204. O_RDONLY, 0,
  205. (verbose ? TAR_VERBOSE : 0)
  206. | (use_gnu ? TAR_GNU : 0)) == -1)
  207. {
  208. fprintf(stderr, "tar_open(): %s\n", strerror(errno));
  209. return -1;
  210. }
  211. #ifdef DEBUG
  212. puts("extracting tarfile...");
  213. #endif
  214. if (tar_extract_all(t, rootdir) != 0)
  215. {
  216. fprintf(stderr, "tar_extract_all(): %s\n", strerror(errno));
  217. return -1;
  218. }
  219. #ifdef DEBUG
  220. puts("closing tarfile...");
  221. #endif
  222. if (tar_close(t) != 0)
  223. {
  224. fprintf(stderr, "tar_close(): %s\n", strerror(errno));
  225. return -1;
  226. }
  227. return 0;
  228. }
  229. void
  230. usage()
  231. {
  232. printf("Usage: %s [-C rootdir] [-g] [-z] -x|-t filename.tar\n",
  233. progname);
  234. printf(" %s [-C rootdir] [-g] [-z] -c filename.tar ...\n",
  235. progname);
  236. exit(-1);
  237. }
  238. #define MODE_LIST 1
  239. #define MODE_CREATE 2
  240. #define MODE_EXTRACT 3
  241. int
  242. main(int argc, char *argv[])
  243. {
  244. char *tarfile = NULL;
  245. char *rootdir = NULL;
  246. int c;
  247. int mode = 0;
  248. libtar_list_t *l;
  249. #if defined(_WIN32) && !defined(__CYGWIN__)
  250. int optind;
  251. #endif
  252. progname = basename(argv[0]);
  253. #if !defined(_WIN32) || defined(__CYGWIN__)
  254. while ((c = getopt(argc, argv, "cC:gtvVxz")) != -1)
  255. switch (c)
  256. {
  257. case 'V':
  258. printf("libtar %s by Mark D. Roth <[email protected]>\n",
  259. libtar_version);
  260. break;
  261. case 'C':
  262. rootdir = strdup(optarg);
  263. break;
  264. case 'v':
  265. verbose = 1;
  266. break;
  267. case 'g':
  268. use_gnu = 1;
  269. break;
  270. case 'c':
  271. if (mode)
  272. usage();
  273. mode = MODE_CREATE;
  274. break;
  275. case 'x':
  276. if (mode)
  277. usage();
  278. mode = MODE_EXTRACT;
  279. break;
  280. case 't':
  281. if (mode)
  282. usage();
  283. mode = MODE_LIST;
  284. break;
  285. #ifdef HAVE_LIBZ
  286. case 'z':
  287. use_zlib = 1;
  288. break;
  289. #endif /* HAVE_LIBZ */
  290. default:
  291. usage();
  292. }
  293. if (!mode || ((argc - optind) < (mode == MODE_CREATE ? 2 : 1)))
  294. {
  295. #ifdef DEBUG
  296. printf("argc - optind == %d\tmode == %d\n", argc - optind,
  297. mode);
  298. #endif
  299. usage();
  300. }
  301. #else
  302. mode = MODE_EXTRACT;
  303. use_zlib=1;
  304. optind = 1;
  305. #endif
  306. #ifdef DEBUG
  307. signal(SIGSEGV, segv_handler);
  308. #endif
  309. switch (mode)
  310. {
  311. case MODE_EXTRACT:
  312. return extract(argv[optind], rootdir);
  313. case MODE_CREATE:
  314. tarfile = argv[optind];
  315. l = libtar_list_new(LIST_QUEUE, NULL);
  316. for (c = optind + 1; c < argc; c++)
  317. libtar_list_add(l, argv[c]);
  318. return create(tarfile, rootdir, l);
  319. case MODE_LIST:
  320. return list(argv[optind]);
  321. default:
  322. break;
  323. }
  324. /* NOTREACHED */
  325. return -2;
  326. }