data_extract_all.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. bb_perror_msg("can't create %slink "
  123. "from %s to %s", "hard",
  124. dst_name,
  125. hard_link);
  126. }
  127. /* Hardlinks have no separate mode/ownership, skip chown/chmod */
  128. goto ret;
  129. }
  130. /* Create the filesystem entry */
  131. switch (file_header->mode & S_IFMT) {
  132. case S_IFREG: {
  133. /* Regular file */
  134. char *dst_nameN;
  135. int flags = O_WRONLY | O_CREAT | O_EXCL;
  136. if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
  137. flags = O_WRONLY | O_CREAT | O_TRUNC;
  138. dst_nameN = dst_name;
  139. #ifdef ARCHIVE_REPLACE_VIA_RENAME
  140. if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
  141. /* rpm-style temp file name */
  142. dst_nameN = xasprintf("%s;%x", dst_name, (int)getpid());
  143. #endif
  144. dst_fd = xopen3(dst_nameN,
  145. flags,
  146. file_header->mode
  147. );
  148. bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
  149. close(dst_fd);
  150. #ifdef ARCHIVE_REPLACE_VIA_RENAME
  151. if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
  152. xrename(dst_nameN, dst_name);
  153. free(dst_nameN);
  154. }
  155. #endif
  156. break;
  157. }
  158. case S_IFDIR:
  159. res = mkdir(dst_name, file_header->mode);
  160. if ((res == -1)
  161. && (errno != EISDIR) /* btw, Linux doesn't return this */
  162. && (errno != EEXIST)
  163. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  164. ) {
  165. bb_perror_msg("can't make dir %s", dst_name);
  166. }
  167. break;
  168. case S_IFLNK:
  169. /* Symlink */
  170. //TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
  171. res = symlink(file_header->link_target, dst_name);
  172. if (res != 0
  173. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  174. ) {
  175. bb_perror_msg("can't create %slink "
  176. "from %s to %s", "sym",
  177. dst_name,
  178. file_header->link_target);
  179. }
  180. break;
  181. case S_IFSOCK:
  182. case S_IFBLK:
  183. case S_IFCHR:
  184. case S_IFIFO:
  185. res = mknod(dst_name, file_header->mode, file_header->device);
  186. if ((res == -1)
  187. && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
  188. ) {
  189. bb_perror_msg("can't create node %s", dst_name);
  190. }
  191. break;
  192. default:
  193. bb_error_msg_and_die("unrecognized file type");
  194. }
  195. if (!S_ISLNK(file_header->mode)) {
  196. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
  197. uid_t uid = file_header->uid;
  198. gid_t gid = file_header->gid;
  199. #if ENABLE_FEATURE_TAR_UNAME_GNAME
  200. if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
  201. if (file_header->tar__uname) {
  202. //TODO: cache last name/id pair?
  203. struct passwd *pwd = getpwnam(file_header->tar__uname);
  204. if (pwd) uid = pwd->pw_uid;
  205. }
  206. if (file_header->tar__gname) {
  207. struct group *grp = getgrnam(file_header->tar__gname);
  208. if (grp) gid = grp->gr_gid;
  209. }
  210. }
  211. #endif
  212. /* GNU tar 1.15.1 uses chown, not lchown */
  213. chown(dst_name, uid, gid);
  214. }
  215. /* uclibc has no lchmod, glibc is even stranger -
  216. * it has lchmod which seems to do nothing!
  217. * so we use chmod... */
  218. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
  219. chmod(dst_name, file_header->mode);
  220. }
  221. if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
  222. struct timeval t[2];
  223. t[1].tv_sec = t[0].tv_sec = file_header->mtime;
  224. t[1].tv_usec = t[0].tv_usec = 0;
  225. utimes(dst_name, t);
  226. }
  227. }
  228. ret: ;
  229. #if ENABLE_FEATURE_TAR_SELINUX
  230. if (sctx) {
  231. /* reset the context after creating an entry */
  232. setfscreatecon(NULL);
  233. }
  234. #endif
  235. }