wrapper.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. ** Copyright 1998-2003 University of Illinois Board of Trustees
  3. ** Copyright 1998-2003 Mark D. Roth
  4. ** All rights reserved.
  5. **
  6. ** wrapper.c - libtar high-level wrapper code
  7. **
  8. ** Mark D. Roth <[email protected]>
  9. ** Campus Information Technologies and Educational Services
  10. ** University of Illinois at Urbana-Champaign
  11. */
  12. #include <libtarint/internal.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <libtar/compat.h>
  16. #if defined(HAVE_SYS_PARAM_H)
  17. #include <sys/param.h>
  18. #endif
  19. #if defined(HAVE_DIRENT_H)
  20. #include <dirent.h>
  21. #else
  22. #include <libtarint/filesystem.h>
  23. #endif
  24. #include <errno.h>
  25. #ifdef HAVE_FNMATCH_H
  26. #include <fnmatch.h>
  27. #endif
  28. #ifdef STDC_HEADERS
  29. # include <string.h>
  30. #endif
  31. int
  32. tar_extract_glob(TAR *t, char *globname, char *prefix)
  33. {
  34. char *filename;
  35. char buf[TAR_MAXPATHLEN];
  36. int i;
  37. char *pathname;
  38. while ((i = th_read(t)) == 0)
  39. {
  40. pathname = th_get_pathname(t);
  41. filename = pathname;
  42. if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD))
  43. {
  44. if (pathname)
  45. {
  46. free(pathname);
  47. pathname = 0;
  48. }
  49. if (TH_ISREG(t) && tar_skip_regfile(t))
  50. return -1;
  51. continue;
  52. }
  53. if (t->options & TAR_VERBOSE)
  54. th_print_long_ls(t);
  55. if (prefix != NULL)
  56. snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
  57. else
  58. strlcpy(buf, filename, sizeof(buf));
  59. if (tar_extract_file(t, filename) != 0)
  60. {
  61. if (pathname)
  62. {
  63. free(pathname);
  64. pathname = 0;
  65. }
  66. return -1;
  67. }
  68. if (pathname)
  69. {
  70. free(pathname);
  71. pathname = 0;
  72. }
  73. }
  74. return (i == 1 ? 0 : -1);
  75. }
  76. int
  77. tar_extract_all(TAR *t, char *prefix)
  78. {
  79. char *filename;
  80. char buf[TAR_MAXPATHLEN];
  81. int i;
  82. char *pathname;
  83. #ifdef DEBUG
  84. printf("==> tar_extract_all(TAR *t, \"%s\")\n",
  85. (prefix ? prefix : "(null)"));
  86. #endif
  87. while ((i = th_read(t)) == 0)
  88. {
  89. #ifdef DEBUG
  90. puts(" tar_extract_all(): calling th_get_pathname()");
  91. #endif
  92. pathname = th_get_pathname(t);
  93. filename = pathname;
  94. if (t->options & TAR_VERBOSE)
  95. th_print_long_ls(t);
  96. if (prefix != NULL)
  97. snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
  98. else
  99. strlcpy(buf, filename, sizeof(buf));
  100. if (pathname)
  101. {
  102. free(pathname);
  103. pathname = 0;
  104. }
  105. #ifdef DEBUG
  106. printf(" tar_extract_all(): calling tar_extract_file(t, "
  107. "\"%s\")\n", buf);
  108. #endif
  109. if (tar_extract_file(t, buf) != 0)
  110. return -1;
  111. }
  112. return (i == 1 ? 0 : -1);
  113. }
  114. int
  115. tar_append_tree(TAR *t, char *realdir, char *savedir)
  116. {
  117. char realpath[TAR_MAXPATHLEN];
  118. char savepath[TAR_MAXPATHLEN];
  119. size_t plen;
  120. #if defined(HAVE_DIRENT_H)
  121. struct dirent *dent;
  122. DIR *dp;
  123. #else
  124. kwDirEntry * dent;
  125. kwDirectory *dp;
  126. #endif
  127. struct stat s;
  128. strncpy(realpath, realdir, sizeof(realpath));
  129. realpath[sizeof(realpath)-1] = 0;
  130. plen = strlen(realpath);
  131. if ( realpath[plen-1] == '/' )
  132. {
  133. realpath[plen-1] = 0;
  134. }
  135. #ifdef DEBUG
  136. printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
  137. t, realdir, (savedir ? savedir : "[NULL]"));
  138. #endif
  139. if (tar_append_file(t, realdir, savedir) != 0)
  140. return -1;
  141. #ifdef DEBUG
  142. puts(" tar_append_tree(): done with tar_append_file()...");
  143. #endif
  144. if ( stat(realpath, &s) != 0 )
  145. {
  146. return -1;
  147. }
  148. if (
  149. #if defined(_WIN32) && !defined(__CYGWIN__)
  150. (s.st_mode & _S_IFDIR) == 0
  151. #else
  152. !S_ISDIR(s.st_mode)
  153. #endif
  154. )
  155. return 0;
  156. #if defined(HAVE_DIRENT_H)
  157. dp = opendir(realdir);
  158. #else
  159. dp = kwOpenDir(realdir);
  160. #endif
  161. if (dp == NULL)
  162. {
  163. if (errno == ENOTDIR)
  164. return 0;
  165. return -1;
  166. }
  167. #if defined(HAVE_DIRENT_H)
  168. while ((dent = readdir(dp)) != NULL)
  169. #else
  170. while ((dent = kwReadDir(dp)) != NULL)
  171. #endif
  172. {
  173. if (strcmp(dent->d_name, ".") == 0 ||
  174. strcmp(dent->d_name, "..") == 0)
  175. continue;
  176. snprintf(realpath, TAR_MAXPATHLEN, "%s/%s", realdir,
  177. dent->d_name);
  178. if (savedir)
  179. snprintf(savepath, TAR_MAXPATHLEN, "%s/%s", savedir,
  180. dent->d_name);
  181. #ifndef WIN32
  182. if (lstat(realpath, &s) != 0)
  183. return -1;
  184. #else
  185. if (stat(realpath, &s) != 0)
  186. return -1;
  187. #endif
  188. if (S_ISDIR(s.st_mode))
  189. {
  190. if (tar_append_tree(t, realpath,
  191. (savedir ? savepath : NULL)) != 0)
  192. return -1;
  193. continue;
  194. }
  195. if (tar_append_file(t, realpath,
  196. (savedir ? savepath : NULL)) != 0)
  197. return -1;
  198. }
  199. #if defined(HAVE_DIRENT_H)
  200. closedir(dp);
  201. #else
  202. kwCloseDir(dp);
  203. #endif
  204. return 0;
  205. }