libtar.c 7.4 KB

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