wrapper.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. }
  65. return -1;
  66. }
  67. if (pathname)
  68. {
  69. free(pathname);
  70. }
  71. }
  72. return (i == 1 ? 0 : -1);
  73. }
  74. int
  75. tar_extract_all(TAR *t, char *prefix)
  76. {
  77. char *filename;
  78. char buf[TAR_MAXPATHLEN];
  79. int i;
  80. char *pathname;
  81. #ifdef DEBUG
  82. printf("==> tar_extract_all(TAR *t, \"%s\")\n",
  83. (prefix ? prefix : "(null)"));
  84. #endif
  85. while ((i = th_read(t)) == 0)
  86. {
  87. #ifdef DEBUG
  88. puts(" tar_extract_all(): calling th_get_pathname()");
  89. #endif
  90. pathname = th_get_pathname(t);
  91. filename = pathname;
  92. if (t->options & TAR_VERBOSE)
  93. th_print_long_ls(t);
  94. if (prefix != NULL)
  95. snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
  96. else
  97. strlcpy(buf, filename, sizeof(buf));
  98. if (pathname)
  99. {
  100. free(pathname);
  101. }
  102. #ifdef DEBUG
  103. printf(" tar_extract_all(): calling tar_extract_file(t, "
  104. "\"%s\")\n", buf);
  105. #endif
  106. if (tar_extract_file(t, buf) != 0)
  107. return -1;
  108. }
  109. return (i == 1 ? 0 : -1);
  110. }
  111. int
  112. tar_append_tree(TAR *t, char *realdir, char *savedir)
  113. {
  114. char realpath[TAR_MAXPATHLEN];
  115. char savepath[TAR_MAXPATHLEN];
  116. size_t plen;
  117. #if defined(HAVE_DIRENT_H)
  118. struct dirent *dent;
  119. DIR *dp;
  120. #else
  121. kwDirEntry * dent;
  122. kwDirectory *dp;
  123. #endif
  124. struct stat s;
  125. strncpy(realpath, realdir, sizeof(realpath));
  126. realpath[sizeof(realpath)-1] = 0;
  127. plen = strlen(realpath);
  128. if ( realpath[plen-1] == '/' )
  129. {
  130. realpath[plen-1] = 0;
  131. }
  132. #ifdef DEBUG
  133. printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
  134. t, realdir, (savedir ? savedir : "[NULL]"));
  135. #endif
  136. if (tar_append_file(t, realdir, savedir) != 0)
  137. return -1;
  138. #ifdef DEBUG
  139. puts(" tar_append_tree(): done with tar_append_file()...");
  140. #endif
  141. if ( stat(realpath, &s) != 0 )
  142. {
  143. return -1;
  144. }
  145. if (
  146. #if defined(_WIN32) && !defined(__CYGWIN__)
  147. (s.st_mode & _S_IFDIR) == 0
  148. #else
  149. !S_ISDIR(s.st_mode)
  150. #endif
  151. )
  152. return 0;
  153. #if defined(HAVE_DIRENT_H)
  154. dp = opendir(realdir);
  155. #else
  156. dp = kwOpenDir(realdir);
  157. #endif
  158. if (dp == NULL)
  159. {
  160. if (errno == ENOTDIR)
  161. return 0;
  162. return -1;
  163. }
  164. #if defined(HAVE_DIRENT_H)
  165. while ((dent = readdir(dp)) != NULL)
  166. #else
  167. while ((dent = kwReadDir(dp)) != NULL)
  168. #endif
  169. {
  170. if (strcmp(dent->d_name, ".") == 0 ||
  171. strcmp(dent->d_name, "..") == 0)
  172. continue;
  173. snprintf(realpath, TAR_MAXPATHLEN, "%s/%s", realdir,
  174. dent->d_name);
  175. if (savedir)
  176. snprintf(savepath, TAR_MAXPATHLEN, "%s/%s", savedir,
  177. dent->d_name);
  178. #ifndef WIN32
  179. if (lstat(realpath, &s) != 0)
  180. return -1;
  181. #else
  182. if (stat(realpath, &s) != 0)
  183. return -1;
  184. #endif
  185. if (S_ISDIR(s.st_mode))
  186. {
  187. if (tar_append_tree(t, realpath,
  188. (savedir ? savepath : NULL)) != 0)
  189. return -1;
  190. continue;
  191. }
  192. if (tar_append_file(t, realpath,
  193. (savedir ? savepath : NULL)) != 0)
  194. return -1;
  195. }
  196. #if defined(HAVE_DIRENT_H)
  197. closedir(dp);
  198. #else
  199. kwCloseDir(dp);
  200. #endif
  201. return 0;
  202. }