wrapper.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #ifdef _MSC_VER
  16. #include <filesystem.h>
  17. #else
  18. #include <sys/param.h>
  19. #include <dirent.h>
  20. #endif
  21. #include <errno.h>
  22. #ifdef HAVE_FNMATCH_H
  23. #include <fnmatch.h>
  24. #endif
  25. #ifdef STDC_HEADERS
  26. # include <string.h>
  27. #endif
  28. int
  29. tar_extract_glob(TAR *t, char *globname, char *prefix)
  30. {
  31. char *filename;
  32. char buf[MAXPATHLEN];
  33. int i;
  34. while ((i = th_read(t)) == 0)
  35. {
  36. filename = th_get_pathname(t);
  37. if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD))
  38. {
  39. if (TH_ISREG(t) && tar_skip_regfile(t))
  40. return -1;
  41. continue;
  42. }
  43. if (t->options & TAR_VERBOSE)
  44. th_print_long_ls(t);
  45. if (prefix != NULL)
  46. snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
  47. else
  48. strlcpy(buf, filename, sizeof(buf));
  49. if (tar_extract_file(t, filename) != 0)
  50. return -1;
  51. }
  52. return (i == 1 ? 0 : -1);
  53. }
  54. int
  55. tar_extract_all(TAR *t, char *prefix)
  56. {
  57. char *filename;
  58. char buf[MAXPATHLEN];
  59. int i;
  60. #ifdef DEBUG
  61. printf("==> tar_extract_all(TAR *t, \"%s\")\n",
  62. (prefix ? prefix : "(null)"));
  63. #endif
  64. while ((i = th_read(t)) == 0)
  65. {
  66. #ifdef DEBUG
  67. puts(" tar_extract_all(): calling th_get_pathname()");
  68. #endif
  69. filename = th_get_pathname(t);
  70. if (t->options & TAR_VERBOSE)
  71. th_print_long_ls(t);
  72. if (prefix != NULL)
  73. snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
  74. else
  75. strlcpy(buf, filename, sizeof(buf));
  76. #ifdef DEBUG
  77. printf(" tar_extract_all(): calling tar_extract_file(t, "
  78. "\"%s\")\n", buf);
  79. #endif
  80. if (tar_extract_file(t, buf) != 0)
  81. return -1;
  82. }
  83. return (i == 1 ? 0 : -1);
  84. }
  85. int
  86. tar_append_tree(TAR *t, char *realdir, char *savedir)
  87. {
  88. char realpath[MAXPATHLEN];
  89. char savepath[MAXPATHLEN];
  90. #ifndef _MSC_VER
  91. struct dirent *dent;
  92. DIR *dp;
  93. #else
  94. kwDirEntry * dent;
  95. kwDirectory *dp;
  96. #endif
  97. struct stat s;
  98. #ifdef DEBUG
  99. printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
  100. t, realdir, (savedir ? savedir : "[NULL]"));
  101. #endif
  102. if (tar_append_file(t, realdir, savedir) != 0)
  103. return -1;
  104. #ifdef DEBUG
  105. puts(" tar_append_tree(): done with tar_append_file()...");
  106. #endif
  107. #ifdef _MSC_VER
  108. dp = kwOpenDir(realdir);
  109. #else
  110. dp = opendir(realdir);
  111. #endif
  112. if (dp == NULL)
  113. {
  114. if (errno == ENOTDIR)
  115. return 0;
  116. return -1;
  117. }
  118. #ifdef _MSC_VER
  119. while ((dent = kwReadDir(dp)) != NULL)
  120. #else
  121. while ((dent = readdir(dp)) != NULL)
  122. #endif
  123. {
  124. if (strcmp(dent->d_name, ".") == 0 ||
  125. strcmp(dent->d_name, "..") == 0)
  126. continue;
  127. snprintf(realpath, MAXPATHLEN, "%s/%s", realdir,
  128. dent->d_name);
  129. if (savedir)
  130. snprintf(savepath, MAXPATHLEN, "%s/%s", savedir,
  131. dent->d_name);
  132. #ifndef WIN32
  133. if (lstat(realpath, &s) != 0)
  134. return -1;
  135. #else
  136. if (stat(realpath, &s) != 0)
  137. return -1;
  138. #endif
  139. if (S_ISDIR(s.st_mode))
  140. {
  141. if (tar_append_tree(t, realpath,
  142. (savedir ? savepath : NULL)) != 0)
  143. return -1;
  144. continue;
  145. }
  146. if (tar_append_file(t, realpath,
  147. (savedir ? savepath : NULL)) != 0)
  148. return -1;
  149. }
  150. #ifdef _MSC_VER
  151. kwCloseDir(dp);
  152. #else
  153. closedir(dp);
  154. #endif
  155. return 0;
  156. }