libtar.c 6.4 KB

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