wrapper.c 4.4 KB

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