data_extract_all.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #if ENABLE_FEATURE_TAR_SELINUX
  13. char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
  14. if (!sctx)
  15. sctx = archive_handle->tar__sctx[PAX_GLOBAL];
  16. if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
  17. setfscreatecon(sctx);
  18. free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
  19. archive_handle->tar__sctx[PAX_NEXT_FILE] = 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. && !S_ISDIR(file_header->mode)
  69. ) {
  70. bb_error_msg("%s not created: newer or "
  71. "same age file exists", file_header->name);
  72. }
  73. data_skip(archive_handle);
  74. goto ret;
  75. }
  76. else if ((unlink(file_header->name) == -1) && (errno != EISDIR)) {
  77. bb_perror_msg_and_die("can't remove old file %s",
  78. file_header->name);
  79. }
  80. }
  81. /* Handle hard links separately
  82. * We encode hard links as regular files of size 0 with a symlink */
  83. if (S_ISREG(file_header->mode)
  84. && file_header->link_target
  85. && file_header->size == 0
  86. ) {
  87. /* hard link */
  88. res = link(file_header->link_target, file_header->name);
  89. if ((res == -1) && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
  90. bb_perror_msg("can't create %slink "
  91. "from %s to %s", "hard",
  92. file_header->name,
  93. file_header->link_target);
  94. }
  95. /* Hardlinks have no separate mode/ownership, skip chown/chmod */
  96. goto ret;
  97. }
  98. /* Create the filesystem entry */
  99. switch (file_header->mode & S_IFMT) {
  100. case S_IFREG: {
  101. /* Regular file */
  102. char *dst_name;
  103. int flags = O_WRONLY | O_CREAT | O_EXCL;
  104. if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
  105. flags = O_WRONLY | O_CREAT | O_TRUNC;
  106. dst_name = file_header->name;
  107. #ifdef ARCHIVE_REPLACE_VIA_RENAME
  108. if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
  109. /* rpm-style temp file name */
  110. dst_name = xasprintf("%s;%x", dst_name, (int)getpid());
  111. #endif
  112. dst_fd = xopen3(dst_name,
  113. flags,
  114. file_header->mode
  115. );
  116. bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
  117. close(dst_fd);
  118. #ifdef ARCHIVE_REPLACE_VIA_RENAME
  119. if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
  120. xrename(dst_name, file_header->name);
  121. free(dst_name);
  122. }
  123. #endif
  124. break;
  125. }
  126. case S_IFDIR:
  127. res = mkdir(file_header->name, file_header->mode);
  128. if ((res == -1)
  129. && (errno != EISDIR) /* btw, Linux doesn't return this */
  130. && (errno != EEXIST)
  131. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  132. ) {
  133. bb_perror_msg("can't make dir %s", file_header->name);
  134. }
  135. break;
  136. case S_IFLNK:
  137. /* Symlink */
  138. //TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
  139. res = symlink(file_header->link_target, file_header->name);
  140. if ((res == -1)
  141. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  142. ) {
  143. bb_perror_msg("can't create %slink "
  144. "from %s to %s", "sym",
  145. file_header->name,
  146. file_header->link_target);
  147. }
  148. break;
  149. case S_IFSOCK:
  150. case S_IFBLK:
  151. case S_IFCHR:
  152. case S_IFIFO:
  153. res = mknod(file_header->name, file_header->mode, file_header->device);
  154. if ((res == -1)
  155. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  156. ) {
  157. bb_perror_msg("can't create node %s", file_header->name);
  158. }
  159. break;
  160. default:
  161. bb_error_msg_and_die("unrecognized file type");
  162. }
  163. if (!S_ISLNK(file_header->mode)) {
  164. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
  165. uid_t uid = file_header->uid;
  166. gid_t gid = file_header->gid;
  167. #if ENABLE_FEATURE_TAR_UNAME_GNAME
  168. if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
  169. if (file_header->tar__uname) {
  170. //TODO: cache last name/id pair?
  171. struct passwd *pwd = getpwnam(file_header->tar__uname);
  172. if (pwd) uid = pwd->pw_uid;
  173. }
  174. if (file_header->tar__gname) {
  175. struct group *grp = getgrnam(file_header->tar__gname);
  176. if (grp) gid = grp->gr_gid;
  177. }
  178. }
  179. #endif
  180. /* GNU tar 1.15.1 uses chown, not lchown */
  181. chown(file_header->name, uid, gid);
  182. }
  183. /* uclibc has no lchmod, glibc is even stranger -
  184. * it has lchmod which seems to do nothing!
  185. * so we use chmod... */
  186. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
  187. chmod(file_header->name, file_header->mode);
  188. }
  189. if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
  190. struct timeval t[2];
  191. t[1].tv_sec = t[0].tv_sec = file_header->mtime;
  192. t[1].tv_usec = t[0].tv_usec = 0;
  193. utimes(file_header->name, t);
  194. }
  195. }
  196. ret: ;
  197. #if ENABLE_FEATURE_TAR_SELINUX
  198. if (sctx) {
  199. /* reset the context after creating an entry */
  200. setfscreatecon(NULL);
  201. }
  202. #endif
  203. }