libtar.c 7.6 KB

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