data_extract_all.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. */
  5. #include "libbb.h"
  6. #include "bb_archive.h"
  7. void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
  8. {
  9. file_header_t *file_header = archive_handle->file_header;
  10. int dst_fd;
  11. int res;
  12. char *hard_link;
  13. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  14. char *dst_name;
  15. #else
  16. # define dst_name (file_header->name)
  17. #endif
  18. #if ENABLE_FEATURE_TAR_SELINUX
  19. char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
  20. if (!sctx)
  21. sctx = archive_handle->tar__sctx[PAX_GLOBAL];
  22. if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
  23. setfscreatecon(sctx);
  24. free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
  25. archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
  26. }
  27. #endif
  28. /* Hard links are encoded as regular files of size 0
  29. * with a nonempty link field */
  30. hard_link = NULL;
  31. if (S_ISREG(file_header->mode) && file_header->size == 0)
  32. hard_link = file_header->link_target;
  33. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  34. dst_name = file_header->name;
  35. if (archive_handle->tar__strip_components) {
  36. unsigned n = archive_handle->tar__strip_components;
  37. do {
  38. dst_name = strchr(dst_name, '/');
  39. if (!dst_name || dst_name[1] == '\0') {
  40. data_skip(archive_handle);
  41. goto ret;
  42. }
  43. dst_name++;
  44. /*
  45. * Link target is shortened only for hardlinks:
  46. * softlinks restored unchanged.
  47. */
  48. if (hard_link) {
  49. // GNU tar 1.26 does not check that we reached end of link name:
  50. // if "dir/hardlink" is hardlinked to "file",
  51. // tar xvf a.tar --strip-components=1 says:
  52. // tar: hardlink: Cannot hard link to '': No such file or directory
  53. // and continues processing. We silently skip such entries.
  54. hard_link = strchr(hard_link, '/');
  55. if (!hard_link || hard_link[1] == '\0') {
  56. data_skip(archive_handle);
  57. goto ret;
  58. }
  59. hard_link++;
  60. }
  61. } while (--n != 0);
  62. }
  63. #endif
  64. if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
  65. char *slash = strrchr(dst_name, '/');
  66. if (slash) {
  67. *slash = '\0';
  68. bb_make_directory(dst_name, -1, FILEUTILS_RECUR);
  69. *slash = '/';
  70. }
  71. }
  72. if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
  73. /* Remove the entry if it exists */
  74. if (!S_ISDIR(file_header->mode)) {
  75. if (hard_link) {
  76. /* Ugly special case:
  77. * tar cf t.tar hardlink1 hardlink2 hardlink1
  78. * results in this tarball structure:
  79. * hardlink1
  80. * hardlink2 -> hardlink1
  81. * hardlink1 -> hardlink1 <== !!!
  82. */
  83. if (strcmp(hard_link, dst_name) == 0)
  84. goto ret;
  85. }
  86. /* Proceed with deleting */
  87. if (unlink(dst_name) == -1
  88. && errno != ENOENT
  89. ) {
  90. bb_perror_msg_and_die("can't remove old file %s",
  91. dst_name);
  92. }
  93. }
  94. }
  95. else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
  96. /* Remove the existing entry if its older than the extracted entry */
  97. struct stat existing_sb;
  98. if (lstat(dst_name, &existing_sb) == -1) {
  99. if (errno != ENOENT) {
  100. bb_perror_msg_and_die("can't stat old file");
  101. }
  102. }
  103. else if (existing_sb.st_mtime >= file_header->mtime) {
  104. if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  105. && !S_ISDIR(file_header->mode)
  106. ) {
  107. bb_error_msg("%s not created: newer or "
  108. "same age file exists", dst_name);
  109. }
  110. data_skip(archive_handle);
  111. goto ret;
  112. }
  113. else if ((unlink(dst_name) == -1) && (errno != EISDIR)) {
  114. bb_perror_msg_and_die("can't remove old file %s",
  115. dst_name);
  116. }
  117. }
  118. /* Handle hard links separately */
  119. if (hard_link) {
  120. res = link(hard_link, dst_name);
  121. if (res != 0 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
  122. /* shared message */
  123. bb_perror_msg("can't create %slink "
  124. "%s to %s", "hard",
  125. dst_name,
  126. hard_link);
  127. }
  128. /* Hardlinks have no separate mode/ownership, skip chown/chmod */
  129. goto ret;
  130. }
  131. /* Create the filesystem entry */
  132. switch (file_header->mode & S_IFMT) {
  133. case S_IFREG: {
  134. /* Regular file */
  135. char *dst_nameN;
  136. int flags = O_WRONLY | O_CREAT | O_EXCL;
  137. if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
  138. flags = O_WRONLY | O_CREAT | O_TRUNC;
  139. dst_nameN = dst_name;
  140. #ifdef ARCHIVE_REPLACE_VIA_RENAME
  141. if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
  142. /* rpm-style temp file name */
  143. dst_nameN = xasprintf("%s;%x", dst_name, (int)getpid());
  144. #endif
  145. dst_fd = xopen3(dst_nameN,
  146. flags,
  147. file_header->mode
  148. );
  149. bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
  150. close(dst_fd);
  151. #ifdef ARCHIVE_REPLACE_VIA_RENAME
  152. if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
  153. xrename(dst_nameN, dst_name);
  154. free(dst_nameN);
  155. }
  156. #endif
  157. break;
  158. }
  159. case S_IFDIR:
  160. res = mkdir(dst_name, file_header->mode);
  161. if ((res == -1)
  162. && (errno != EISDIR) /* btw, Linux doesn't return this */
  163. && (errno != EEXIST)
  164. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  165. ) {
  166. bb_perror_msg("can't make dir %s", dst_name);
  167. }
  168. break;
  169. case S_IFLNK:
  170. /* Symlink */
  171. //TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
  172. res = symlink(file_header->link_target, dst_name);
  173. if (res != 0
  174. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  175. ) {
  176. /* shared message */
  177. bb_perror_msg("can't create %slink "
  178. "%s to %s", "sym",
  179. dst_name,
  180. file_header->link_target);
  181. }
  182. break;
  183. case S_IFSOCK:
  184. case S_IFBLK:
  185. case S_IFCHR:
  186. case S_IFIFO:
  187. res = mknod(dst_name, file_header->mode, file_header->device);
  188. if ((res == -1)
  189. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  190. ) {
  191. bb_perror_msg("can't create node %s", dst_name);
  192. }
  193. break;
  194. default:
  195. bb_error_msg_and_die("unrecognized file type");
  196. }
  197. if (!S_ISLNK(file_header->mode)) {
  198. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
  199. uid_t uid = file_header->uid;
  200. gid_t gid = file_header->gid;
  201. #if ENABLE_FEATURE_TAR_UNAME_GNAME
  202. if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
  203. if (file_header->tar__uname) {
  204. //TODO: cache last name/id pair?
  205. struct passwd *pwd = getpwnam(file_header->tar__uname);
  206. if (pwd) uid = pwd->pw_uid;
  207. }
  208. if (file_header->tar__gname) {
  209. struct group *grp = getgrnam(file_header->tar__gname);
  210. if (grp) gid = grp->gr_gid;
  211. }
  212. }
  213. #endif
  214. /* GNU tar 1.15.1 uses chown, not lchown */
  215. chown(dst_name, uid, gid);
  216. }
  217. /* uclibc has no lchmod, glibc is even stranger -
  218. * it has lchmod which seems to do nothing!
  219. * so we use chmod... */
  220. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
  221. chmod(dst_name, file_header->mode);
  222. }
  223. if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
  224. struct timeval t[2];
  225. t[1].tv_sec = t[0].tv_sec = file_header->mtime;
  226. t[1].tv_usec = t[0].tv_usec = 0;
  227. utimes(dst_name, t);
  228. }
  229. }
  230. ret: ;
  231. #if ENABLE_FEATURE_TAR_SELINUX
  232. if (sctx) {
  233. /* reset the context after creating an entry */
  234. setfscreatecon(NULL);
  235. }
  236. #endif
  237. }