1
0

libtar.c 7.6 KB

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