wrapper.c 4.4 KB

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