data_extract_all.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  4. */
  5. #include "libbb.h"
  6. #include "unarchive.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. #if ENABLE_FEATURE_TAR_SELINUX
  13. char *sctx = archive_handle->tar__next_file_sctx;
  14. if (!sctx)
  15. sctx = archive_handle->tar__global_sctx;
  16. if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
  17. setfscreatecon(sctx);
  18. free(archive_handle->tar__next_file_sctx);
  19. archive_handle->tar__next_file_sctx = NULL;
  20. }
  21. #endif
  22. if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
  23. char *slash = strrchr(file_header->name, '/');
  24. if (slash) {
  25. *slash = '\0';
  26. bb_make_directory(file_header->name, -1, FILEUTILS_RECUR);
  27. *slash = '/';
  28. }
  29. }
  30. if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
  31. /* Remove the entry if it exists */
  32. if (!S_ISDIR(file_header->mode)) {
  33. /* Is it hardlink?
  34. * We encode hard links as regular files of size 0 with a symlink */
  35. if (S_ISREG(file_header->mode)
  36. && file_header->link_target
  37. && file_header->size == 0
  38. ) {
  39. /* Ugly special case:
  40. * tar cf t.tar hardlink1 hardlink2 hardlink1
  41. * results in this tarball structure:
  42. * hardlink1
  43. * hardlink2 -> hardlink1
  44. * hardlink1 -> hardlink1 <== !!!
  45. */
  46. if (strcmp(file_header->link_target, file_header->name) == 0)
  47. goto ret;
  48. }
  49. /* Proceed with deleting */
  50. if (unlink(file_header->name) == -1
  51. && errno != ENOENT
  52. ) {
  53. bb_perror_msg_and_die("can't remove old file %s",
  54. file_header->name);
  55. }
  56. }
  57. }
  58. else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
  59. /* Remove the existing entry if its older than the extracted entry */
  60. struct stat existing_sb;
  61. if (lstat(file_header->name, &existing_sb) == -1) {
  62. if (errno != ENOENT) {
  63. bb_perror_msg_and_die("can't stat old file");
  64. }
  65. }
  66. else if (existing_sb.st_mtime >= file_header->mtime) {
  67. if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
  68. bb_error_msg("%s not created: newer or "
  69. "same age file exists", file_header->name);
  70. }
  71. data_skip(archive_handle);
  72. goto ret;
  73. }
  74. else if ((unlink(file_header->name) == -1) && (errno != EISDIR)) {
  75. bb_perror_msg_and_die("can't remove old file %s",
  76. file_header->name);
  77. }
  78. }
  79. /* Handle hard links separately
  80. * We encode hard links as regular files of size 0 with a symlink */
  81. if (S_ISREG(file_header->mode)
  82. && file_header->link_target
  83. && file_header->size == 0
  84. ) {
  85. /* hard link */
  86. res = link(file_header->link_target, file_header->name);
  87. if ((res == -1) && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
  88. bb_perror_msg("can't create %slink "
  89. "from %s to %s", "hard",
  90. file_header->name,
  91. file_header->link_target);
  92. }
  93. /* Hardlinks have no separate mode/ownership, skip chown/chmod */
  94. goto ret;
  95. }
  96. /* Create the filesystem entry */
  97. switch (file_header->mode & S_IFMT) {
  98. case S_IFREG: {
  99. /* Regular file */
  100. int flags = O_WRONLY | O_CREAT | O_EXCL;
  101. if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
  102. flags = O_WRONLY | O_CREAT | O_TRUNC;
  103. dst_fd = xopen3(file_header->name,
  104. flags,
  105. file_header->mode
  106. );
  107. bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
  108. close(dst_fd);
  109. break;
  110. }
  111. case S_IFDIR:
  112. res = mkdir(file_header->name, file_header->mode);
  113. if ((res == -1)
  114. && (errno != EISDIR) /* btw, Linux doesn't return this */
  115. && (errno != EEXIST)
  116. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  117. ) {
  118. bb_perror_msg("can't make dir %s", file_header->name);
  119. }
  120. break;
  121. case S_IFLNK:
  122. /* Symlink */
  123. res = symlink(file_header->link_target, file_header->name);
  124. if ((res == -1)
  125. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  126. ) {
  127. bb_perror_msg("can't create %slink "
  128. "from %s to %s", "sym",
  129. file_header->name,
  130. file_header->link_target);
  131. }
  132. break;
  133. case S_IFSOCK:
  134. case S_IFBLK:
  135. case S_IFCHR:
  136. case S_IFIFO:
  137. res = mknod(file_header->name, file_header->mode, file_header->device);
  138. if ((res == -1)
  139. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  140. ) {
  141. bb_perror_msg("can't create node %s", file_header->name);
  142. }
  143. break;
  144. default:
  145. bb_error_msg_and_die("unrecognized file type");
  146. }
  147. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
  148. #if ENABLE_FEATURE_TAR_UNAME_GNAME
  149. if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
  150. uid_t uid = file_header->uid;
  151. gid_t gid = file_header->gid;
  152. if (file_header->tar__uname) {
  153. //TODO: cache last name/id pair?
  154. struct passwd *pwd = getpwnam(file_header->tar__uname);
  155. if (pwd) uid = pwd->pw_uid;
  156. }
  157. if (file_header->tar__gname) {
  158. struct group *grp = getgrnam(file_header->tar__gname);
  159. if (grp) gid = grp->gr_gid;
  160. }
  161. /* GNU tar 1.15.1 uses chown, not lchown */
  162. chown(file_header->name, uid, gid);
  163. } else
  164. #endif
  165. chown(file_header->name, file_header->uid, file_header->gid);
  166. }
  167. if (!S_ISLNK(file_header->mode)) {
  168. /* uclibc has no lchmod, glibc is even stranger -
  169. * it has lchmod which seems to do nothing!
  170. * so we use chmod... */
  171. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
  172. chmod(file_header->name, file_header->mode);
  173. }
  174. /* same for utime */
  175. if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
  176. struct timeval t[2];
  177. t[1].tv_sec = t[0].tv_sec = file_header->mtime;
  178. t[1].tv_usec = t[0].tv_usec = 0;
  179. utimes(file_header->name, t);
  180. }
  181. }
  182. ret: ;
  183. #if ENABLE_FEATURE_TAR_SELINUX
  184. if (sctx) {
  185. /* reset the context after creating an entry */
  186. setfscreatecon(NULL);
  187. }
  188. #endif
  189. }