libtar.c 7.6 KB

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