1
0

wrapper.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. while ((i = th_read(t)) == 0)
  37. {
  38. filename = th_get_pathname(t);
  39. if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD))
  40. {
  41. if (TH_ISREG(t) && tar_skip_regfile(t))
  42. return -1;
  43. continue;
  44. }
  45. if (t->options & TAR_VERBOSE)
  46. th_print_long_ls(t);
  47. if (prefix != NULL)
  48. snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
  49. else
  50. strlcpy(buf, filename, sizeof(buf));
  51. if (tar_extract_file(t, filename) != 0)
  52. return -1;
  53. }
  54. return (i == 1 ? 0 : -1);
  55. }
  56. int
  57. tar_extract_all(TAR *t, char *prefix)
  58. {
  59. char *filename;
  60. char buf[TAR_MAXPATHLEN];
  61. int i;
  62. #ifdef DEBUG
  63. printf("==> tar_extract_all(TAR *t, \"%s\")\n",
  64. (prefix ? prefix : "(null)"));
  65. #endif
  66. while ((i = th_read(t)) == 0)
  67. {
  68. #ifdef DEBUG
  69. puts(" tar_extract_all(): calling th_get_pathname()");
  70. #endif
  71. filename = th_get_pathname(t);
  72. if (t->options & TAR_VERBOSE)
  73. th_print_long_ls(t);
  74. if (prefix != NULL)
  75. snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
  76. else
  77. strlcpy(buf, filename, sizeof(buf));
  78. #ifdef DEBUG
  79. printf(" tar_extract_all(): calling tar_extract_file(t, "
  80. "\"%s\")\n", buf);
  81. #endif
  82. if (tar_extract_file(t, buf) != 0)
  83. return -1;
  84. }
  85. return (i == 1 ? 0 : -1);
  86. }
  87. int
  88. tar_append_tree(TAR *t, char *realdir, char *savedir)
  89. {
  90. char realpath[TAR_MAXPATHLEN];
  91. char savepath[TAR_MAXPATHLEN];
  92. size_t plen;
  93. #if defined(HAVE_DIRENT_H)
  94. struct dirent *dent;
  95. DIR *dp;
  96. #else
  97. kwDirEntry * dent;
  98. kwDirectory *dp;
  99. #endif
  100. struct stat s;
  101. strncpy(realpath, realdir, sizeof(realpath));
  102. realpath[sizeof(realpath)-1] = 0;
  103. plen = strlen(realpath);
  104. if ( realpath[plen-1] == '/' )
  105. {
  106. realpath[plen-1] = 0;
  107. }
  108. #ifdef DEBUG
  109. printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
  110. t, realdir, (savedir ? savedir : "[NULL]"));
  111. #endif
  112. if (tar_append_file(t, realdir, savedir) != 0)
  113. return -1;
  114. #ifdef DEBUG
  115. puts(" tar_append_tree(): done with tar_append_file()...");
  116. #endif
  117. if ( stat(realpath, &s) != 0 )
  118. {
  119. return -1;
  120. }
  121. if (
  122. #if defined(_WIN32) && !defined(__CYGWIN__)
  123. (s.st_mode & _S_IFDIR) == 0
  124. #else
  125. !S_ISDIR(s.st_mode)
  126. #endif
  127. )
  128. return 0;
  129. #if defined(HAVE_DIRENT_H)
  130. dp = opendir(realdir);
  131. #else
  132. dp = kwOpenDir(realdir);
  133. #endif
  134. if (dp == NULL)
  135. {
  136. if (errno == ENOTDIR)
  137. return 0;
  138. return -1;
  139. }
  140. #if defined(HAVE_DIRENT_H)
  141. while ((dent = readdir(dp)) != NULL)
  142. #else
  143. while ((dent = kwReadDir(dp)) != NULL)
  144. #endif
  145. {
  146. if (strcmp(dent->d_name, ".") == 0 ||
  147. strcmp(dent->d_name, "..") == 0)
  148. continue;
  149. snprintf(realpath, TAR_MAXPATHLEN, "%s/%s", realdir,
  150. dent->d_name);
  151. if (savedir)
  152. snprintf(savepath, TAR_MAXPATHLEN, "%s/%s", savedir,
  153. dent->d_name);
  154. #ifndef WIN32
  155. if (lstat(realpath, &s) != 0)
  156. return -1;
  157. #else
  158. if (stat(realpath, &s) != 0)
  159. return -1;
  160. #endif
  161. if (S_ISDIR(s.st_mode))
  162. {
  163. if (tar_append_tree(t, realpath,
  164. (savedir ? savepath : NULL)) != 0)
  165. return -1;
  166. continue;
  167. }
  168. if (tar_append_file(t, realpath,
  169. (savedir ? savepath : NULL)) != 0)
  170. return -1;
  171. }
  172. #if defined(HAVE_DIRENT_H)
  173. closedir(dp);
  174. #else
  175. kwCloseDir(dp);
  176. #endif
  177. return 0;
  178. }