extract.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  1. /* Extract files from a tar archive.
  2. Copyright 1988-2019 Free Software Foundation, Inc.
  3. This file is part of GNU tar.
  4. GNU tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. Written by John Gilmore, on 1985-11-19. */
  15. #include <system.h>
  16. #include <quotearg.h>
  17. #include <errno.h>
  18. #include <priv-set.h>
  19. #include <root-uid.h>
  20. #include <utimens.h>
  21. #include "common.h"
  22. static bool we_are_root; /* true if our effective uid == 0 */
  23. static mode_t newdir_umask; /* umask when creating new directories */
  24. static mode_t current_umask; /* current umask (which is set to 0 if -p) */
  25. #define ALL_MODE_BITS ((mode_t) ~ (mode_t) 0)
  26. #if ! HAVE_FCHMOD && ! defined fchmod
  27. # define fchmod(fd, mode) (errno = ENOSYS, -1)
  28. #endif
  29. #if ! HAVE_FCHOWN && ! defined fchown
  30. # define fchown(fd, uid, gid) (errno = ENOSYS, -1)
  31. #endif
  32. /* Return true if an error number ERR means the system call is
  33. supported in this case. */
  34. static bool
  35. implemented (int err)
  36. {
  37. return ! (err == ENOSYS
  38. || err == ENOTSUP
  39. || (EOPNOTSUPP != ENOTSUP && err == EOPNOTSUPP));
  40. }
  41. /* List of directories whose statuses we need to extract after we've
  42. finished extracting their subsidiary files. If you consider each
  43. contiguous subsequence of elements of the form [D]?[^D]*, where [D]
  44. represents an element where AFTER_LINKS is nonzero and [^D]
  45. represents an element where AFTER_LINKS is zero, then the head
  46. of the subsequence has the longest name, and each non-head element
  47. in the prefix is an ancestor (in the directory hierarchy) of the
  48. preceding element. */
  49. struct delayed_set_stat
  50. {
  51. /* Next directory in list. */
  52. struct delayed_set_stat *next;
  53. /* Metadata for this directory. */
  54. dev_t dev;
  55. ino_t ino;
  56. mode_t mode; /* The desired mode is MODE & ~ current_umask. */
  57. uid_t uid;
  58. gid_t gid;
  59. struct timespec atime;
  60. struct timespec mtime;
  61. /* An estimate of the directory's current mode, along with a mask
  62. specifying which bits of this estimate are known to be correct.
  63. If CURRENT_MODE_MASK is zero, CURRENT_MODE's value doesn't
  64. matter. */
  65. mode_t current_mode;
  66. mode_t current_mode_mask;
  67. /* This directory is an intermediate directory that was created
  68. as an ancestor of some other directory; it was not mentioned
  69. in the archive, so do not set its uid, gid, atime, or mtime,
  70. and don't alter its mode outside of MODE_RWX. */
  71. bool interdir;
  72. /* Whether symbolic links should be followed when accessing the
  73. directory. */
  74. int atflag;
  75. /* Do not set the status of this directory until after delayed
  76. links are created. */
  77. bool after_links;
  78. /* Directory that the name is relative to. */
  79. int change_dir;
  80. /* extended attributes*/
  81. char *cntx_name;
  82. char *acls_a_ptr;
  83. size_t acls_a_len;
  84. char *acls_d_ptr;
  85. size_t acls_d_len;
  86. size_t xattr_map_size;
  87. struct xattr_array *xattr_map;
  88. /* Length and contents of name. */
  89. size_t file_name_len;
  90. char *file_name;
  91. };
  92. static struct delayed_set_stat *delayed_set_stat_head;
  93. /* List of links whose creation we have delayed. */
  94. struct delayed_link
  95. {
  96. /* The next delayed link in the list. */
  97. struct delayed_link *next;
  98. /* The device, inode number and birthtime of the placeholder.
  99. birthtime.tv_nsec is negative if the birthtime is not available.
  100. Don't use mtime as this would allow for false matches if some
  101. other process removes the placeholder. Don't use ctime as
  102. this would cause race conditions and other screwups, e.g.,
  103. when restoring hard-linked symlinks. */
  104. dev_t dev;
  105. ino_t ino;
  106. struct timespec birthtime;
  107. /* True if the link is symbolic. */
  108. bool is_symlink;
  109. /* The desired metadata, valid only the link is symbolic. */
  110. mode_t mode;
  111. uid_t uid;
  112. gid_t gid;
  113. struct timespec atime;
  114. struct timespec mtime;
  115. /* The directory that the sources and target are relative to. */
  116. int change_dir;
  117. /* A list of sources for this link. The sources are all to be
  118. hard-linked together. */
  119. struct string_list *sources;
  120. /* SELinux context */
  121. char *cntx_name;
  122. /* ACLs */
  123. char *acls_a_ptr;
  124. size_t acls_a_len;
  125. char *acls_d_ptr;
  126. size_t acls_d_len;
  127. size_t xattr_map_size;
  128. struct xattr_array *xattr_map;
  129. /* The desired target of the desired link. */
  130. char target[1];
  131. };
  132. static struct delayed_link *delayed_link_head;
  133. struct string_list
  134. {
  135. struct string_list *next;
  136. char string[1];
  137. };
  138. /* Set up to extract files. */
  139. void
  140. extr_init (void)
  141. {
  142. we_are_root = geteuid () == ROOT_UID;
  143. same_permissions_option += we_are_root;
  144. same_owner_option += we_are_root;
  145. /* Option -p clears the kernel umask, so it does not affect proper
  146. restoration of file permissions. New intermediate directories will
  147. comply with umask at start of program. */
  148. newdir_umask = umask (0);
  149. if (0 < same_permissions_option)
  150. current_umask = 0;
  151. else
  152. {
  153. umask (newdir_umask); /* restore the kernel umask */
  154. current_umask = newdir_umask;
  155. }
  156. }
  157. /* Use fchmod if possible, fchmodat otherwise. */
  158. static int
  159. fd_chmod (int fd, char const *file, mode_t mode, int atflag)
  160. {
  161. if (0 <= fd)
  162. {
  163. int result = fchmod (fd, mode);
  164. if (result == 0 || implemented (errno))
  165. return result;
  166. }
  167. return fchmodat (chdir_fd, file, mode, atflag);
  168. }
  169. /* Use fchown if possible, fchownat otherwise. */
  170. static int
  171. fd_chown (int fd, char const *file, uid_t uid, gid_t gid, int atflag)
  172. {
  173. if (0 <= fd)
  174. {
  175. int result = fchown (fd, uid, gid);
  176. if (result == 0 || implemented (errno))
  177. return result;
  178. }
  179. return fchownat (chdir_fd, file, uid, gid, atflag);
  180. }
  181. /* Use fstat if possible, fstatat otherwise. */
  182. static int
  183. fd_stat (int fd, char const *file, struct stat *st, int atflag)
  184. {
  185. return (0 <= fd
  186. ? fstat (fd, st)
  187. : fstatat (chdir_fd, file, st, atflag));
  188. }
  189. /* Set the mode for FILE_NAME to MODE.
  190. MODE_MASK specifies the bits of MODE that we care about;
  191. thus if MODE_MASK is zero, do nothing.
  192. If FD is nonnegative, it is a file descriptor for the file.
  193. CURRENT_MODE and CURRENT_MODE_MASK specify information known about
  194. the file's current mode, using the style of struct delayed_set_stat.
  195. TYPEFLAG specifies the type of the file.
  196. ATFLAG specifies the flag to use when statting the file. */
  197. static void
  198. set_mode (char const *file_name,
  199. mode_t mode, mode_t mode_mask, int fd,
  200. mode_t current_mode, mode_t current_mode_mask,
  201. char typeflag, int atflag)
  202. {
  203. if (((current_mode ^ mode) | ~ current_mode_mask) & mode_mask)
  204. {
  205. if (MODE_ALL & ~ mode_mask & ~ current_mode_mask)
  206. {
  207. struct stat st;
  208. if (fd_stat (fd, file_name, &st, atflag) != 0)
  209. {
  210. stat_error (file_name);
  211. return;
  212. }
  213. current_mode = st.st_mode;
  214. }
  215. current_mode &= MODE_ALL;
  216. mode = (current_mode & ~ mode_mask) | (mode & mode_mask);
  217. if (current_mode != mode)
  218. {
  219. int chmod_errno =
  220. fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
  221. /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
  222. setuid-root files would otherwise be a backdoor. See
  223. http://opensolaris.org/jive/thread.jspa?threadID=95826
  224. (2009-09-03). */
  225. if (chmod_errno == EPERM && (mode & S_ISUID)
  226. && priv_set_restore_linkdir () == 0)
  227. {
  228. chmod_errno =
  229. fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
  230. priv_set_remove_linkdir ();
  231. }
  232. /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
  233. returns ENOTSUP even when operating on non-symlinks, try
  234. again with the flag disabled if it does not appear to be
  235. supported and if the file is not a symlink. This
  236. introduces a race, alas. */
  237. if (atflag && typeflag != SYMTYPE && ! implemented (chmod_errno))
  238. chmod_errno = fd_chmod (fd, file_name, mode, 0) == 0 ? 0 : errno;
  239. if (chmod_errno
  240. && (typeflag != SYMTYPE || implemented (chmod_errno)))
  241. {
  242. errno = chmod_errno;
  243. chmod_error_details (file_name, mode);
  244. }
  245. }
  246. }
  247. }
  248. /* Check time after successfully setting FILE_NAME's time stamp to T. */
  249. static void
  250. check_time (char const *file_name, struct timespec t)
  251. {
  252. if (t.tv_sec < 0)
  253. WARNOPT (WARN_TIMESTAMP,
  254. (0, 0, _("%s: implausibly old time stamp %s"),
  255. file_name, tartime (t, true)));
  256. else if (timespec_cmp (volume_start_time, t) < 0)
  257. {
  258. struct timespec now;
  259. gettime (&now);
  260. if (timespec_cmp (now, t) < 0)
  261. {
  262. char buf[TIMESPEC_STRSIZE_BOUND];
  263. struct timespec diff;
  264. diff.tv_sec = t.tv_sec - now.tv_sec;
  265. diff.tv_nsec = t.tv_nsec - now.tv_nsec;
  266. if (diff.tv_nsec < 0)
  267. {
  268. diff.tv_nsec += BILLION;
  269. diff.tv_sec--;
  270. }
  271. WARNOPT (WARN_TIMESTAMP,
  272. (0, 0, _("%s: time stamp %s is %s s in the future"),
  273. file_name, tartime (t, true), code_timespec (diff, buf)));
  274. }
  275. }
  276. }
  277. /* Restore stat attributes (owner, group, mode and times) for
  278. FILE_NAME, using information given in *ST.
  279. If FD is nonnegative, it is a file descriptor for the file.
  280. CURRENT_MODE and CURRENT_MODE_MASK specify information known about
  281. the file's current mode, using the style of struct delayed_set_stat.
  282. TYPEFLAG specifies the type of the file.
  283. If INTERDIR, this is an intermediate directory.
  284. ATFLAG specifies the flag to use when statting the file. */
  285. static void
  286. set_stat (char const *file_name,
  287. struct tar_stat_info const *st,
  288. int fd, mode_t current_mode, mode_t current_mode_mask,
  289. char typeflag, bool interdir, int atflag)
  290. {
  291. /* Do the utime before the chmod because some versions of utime are
  292. broken and trash the modes of the file. */
  293. if (! touch_option && ! interdir)
  294. {
  295. struct timespec ts[2];
  296. if (incremental_option)
  297. ts[0] = st->atime;
  298. else
  299. ts[0].tv_nsec = UTIME_OMIT;
  300. ts[1] = st->mtime;
  301. if (fdutimensat (fd, chdir_fd, file_name, ts, atflag) == 0)
  302. {
  303. if (incremental_option)
  304. check_time (file_name, ts[0]);
  305. check_time (file_name, ts[1]);
  306. }
  307. else if (typeflag != SYMTYPE || implemented (errno))
  308. utime_error (file_name);
  309. }
  310. if (0 < same_owner_option && ! interdir)
  311. {
  312. /* Some systems allow non-root users to give files away. Once this
  313. done, it is not possible anymore to change file permissions.
  314. However, setting file permissions now would be incorrect, since
  315. they would apply to the wrong user, and there would be a race
  316. condition. So, don't use systems that allow non-root users to
  317. give files away. */
  318. uid_t uid = st->stat.st_uid;
  319. gid_t gid = st->stat.st_gid;
  320. if (fd_chown (fd, file_name, uid, gid, atflag) == 0)
  321. {
  322. /* Changing the owner can clear st_mode bits in some cases. */
  323. if ((current_mode | ~ current_mode_mask) & S_IXUGO)
  324. current_mode_mask &= ~ (current_mode & (S_ISUID | S_ISGID));
  325. }
  326. else if (typeflag != SYMTYPE || implemented (errno))
  327. chown_error_details (file_name, uid, gid);
  328. }
  329. set_mode (file_name,
  330. st->stat.st_mode & ~ current_umask,
  331. 0 < same_permissions_option && ! interdir ? MODE_ALL : MODE_RWX,
  332. fd, current_mode, current_mode_mask, typeflag, atflag);
  333. /* these three calls must be done *after* fd_chown() call because fd_chown
  334. causes that linux capabilities becomes cleared. */
  335. xattrs_xattrs_set (st, file_name, typeflag, 1);
  336. xattrs_acls_set (st, file_name, typeflag);
  337. xattrs_selinux_set (st, file_name, typeflag);
  338. }
  339. /* Find the direct ancestor of FILE_NAME in the delayed_set_stat list.
  340. */
  341. static struct delayed_set_stat *
  342. find_direct_ancestor (char const *file_name)
  343. {
  344. struct delayed_set_stat *h = delayed_set_stat_head;
  345. while (h)
  346. {
  347. if (! h->after_links
  348. && strncmp (file_name, h->file_name, h->file_name_len) == 0
  349. && ISSLASH (file_name[h->file_name_len])
  350. && (last_component (file_name) == file_name + h->file_name_len + 1))
  351. break;
  352. h = h->next;
  353. }
  354. return h;
  355. }
  356. /* For each entry H in the leading prefix of entries in HEAD that do
  357. not have after_links marked, mark H and fill in its dev and ino
  358. members. Assume HEAD && ! HEAD->after_links. */
  359. static void
  360. mark_after_links (struct delayed_set_stat *head)
  361. {
  362. struct delayed_set_stat *h = head;
  363. do
  364. {
  365. struct stat st;
  366. h->after_links = 1;
  367. if (deref_stat (h->file_name, &st) != 0)
  368. stat_error (h->file_name);
  369. else
  370. {
  371. h->dev = st.st_dev;
  372. h->ino = st.st_ino;
  373. }
  374. }
  375. while ((h = h->next) && ! h->after_links);
  376. }
  377. /* Remember to restore stat attributes (owner, group, mode and times)
  378. for the directory FILE_NAME, using information given in *ST,
  379. once we stop extracting files into that directory.
  380. If ST is null, merely create a placeholder node for an intermediate
  381. directory that was created by make_directories.
  382. NOTICE: this works only if the archive has usual member order, i.e.
  383. directory, then the files in that directory. Incremental archive have
  384. somewhat reversed order: first go subdirectories, then all other
  385. members. To help cope with this case the variable
  386. delay_directory_restore_option is set by prepare_to_extract.
  387. If an archive was explicitely created so that its member order is
  388. reversed, some directory timestamps can be restored incorrectly,
  389. e.g.:
  390. tar --no-recursion -cf archive dir dir/file1 foo dir/file2
  391. */
  392. static void
  393. delay_set_stat (char const *file_name, struct tar_stat_info const *st,
  394. mode_t current_mode, mode_t current_mode_mask,
  395. mode_t mode, int atflag)
  396. {
  397. size_t file_name_len = strlen (file_name);
  398. struct delayed_set_stat *data;
  399. for (data = delayed_set_stat_head; data; data = data->next)
  400. if (strcmp (data->file_name, file_name) == 0)
  401. break;
  402. if (data)
  403. {
  404. if (data->interdir)
  405. {
  406. struct stat real_st;
  407. if (fstatat (chdir_fd, data->file_name,
  408. &real_st, data->atflag) != 0)
  409. {
  410. stat_error (data->file_name);
  411. }
  412. else
  413. {
  414. data->dev = real_st.st_dev;
  415. data->ino = real_st.st_ino;
  416. }
  417. }
  418. }
  419. else
  420. {
  421. data = xmalloc (sizeof (*data));
  422. data->next = delayed_set_stat_head;
  423. delayed_set_stat_head = data;
  424. data->file_name_len = file_name_len;
  425. data->file_name = xstrdup (file_name);
  426. data->after_links = false;
  427. if (st)
  428. {
  429. data->dev = st->stat.st_dev;
  430. data->ino = st->stat.st_ino;
  431. }
  432. }
  433. data->mode = mode;
  434. if (st)
  435. {
  436. data->uid = st->stat.st_uid;
  437. data->gid = st->stat.st_gid;
  438. data->atime = st->atime;
  439. data->mtime = st->mtime;
  440. }
  441. data->current_mode = current_mode;
  442. data->current_mode_mask = current_mode_mask;
  443. data->interdir = ! st;
  444. data->atflag = atflag;
  445. data->change_dir = chdir_current;
  446. data->cntx_name = NULL;
  447. if (st)
  448. assign_string (&data->cntx_name, st->cntx_name);
  449. if (st && st->acls_a_ptr)
  450. {
  451. data->acls_a_ptr = xmemdup (st->acls_a_ptr, st->acls_a_len + 1);
  452. data->acls_a_len = st->acls_a_len;
  453. }
  454. else
  455. {
  456. data->acls_a_ptr = NULL;
  457. data->acls_a_len = 0;
  458. }
  459. if (st && st->acls_d_ptr)
  460. {
  461. data->acls_d_ptr = xmemdup (st->acls_d_ptr, st->acls_d_len + 1);
  462. data->acls_d_len = st->acls_d_len;
  463. }
  464. else
  465. {
  466. data->acls_d_ptr = NULL;
  467. data->acls_d_len = 0;
  468. }
  469. if (st)
  470. xheader_xattr_copy (st, &data->xattr_map, &data->xattr_map_size);
  471. else
  472. {
  473. data->xattr_map = NULL;
  474. data->xattr_map_size = 0;
  475. }
  476. if (must_be_dot_or_slash (file_name))
  477. mark_after_links (data);
  478. }
  479. /* Update the delayed_set_stat info for an intermediate directory
  480. created within the file name of DIR. The intermediate directory turned
  481. out to be the same as this directory, e.g. due to ".." or symbolic
  482. links. *DIR_STAT_INFO is the status of the directory. */
  483. static void
  484. repair_delayed_set_stat (char const *dir,
  485. struct stat const *dir_stat_info)
  486. {
  487. struct delayed_set_stat *data;
  488. for (data = delayed_set_stat_head; data; data = data->next)
  489. {
  490. struct stat st;
  491. if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
  492. {
  493. stat_error (data->file_name);
  494. return;
  495. }
  496. if (st.st_dev == dir_stat_info->st_dev
  497. && st.st_ino == dir_stat_info->st_ino)
  498. {
  499. data->dev = current_stat_info.stat.st_dev;
  500. data->ino = current_stat_info.stat.st_ino;
  501. data->mode = current_stat_info.stat.st_mode;
  502. data->uid = current_stat_info.stat.st_uid;
  503. data->gid = current_stat_info.stat.st_gid;
  504. data->atime = current_stat_info.atime;
  505. data->mtime = current_stat_info.mtime;
  506. data->current_mode = st.st_mode;
  507. data->current_mode_mask = ALL_MODE_BITS;
  508. data->interdir = false;
  509. return;
  510. }
  511. }
  512. ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
  513. quotearg_colon (dir)));
  514. }
  515. static void
  516. free_delayed_set_stat (struct delayed_set_stat *data)
  517. {
  518. free (data->file_name);
  519. xheader_xattr_free (data->xattr_map, data->xattr_map_size);
  520. free (data->cntx_name);
  521. free (data->acls_a_ptr);
  522. free (data->acls_d_ptr);
  523. free (data);
  524. }
  525. void
  526. remove_delayed_set_stat (const char *fname)
  527. {
  528. struct delayed_set_stat *data, *next, *prev = NULL;
  529. for (data = delayed_set_stat_head; data; data = next)
  530. {
  531. next = data->next;
  532. if (chdir_current == data->change_dir
  533. && strcmp (data->file_name, fname) == 0)
  534. {
  535. free_delayed_set_stat (data);
  536. if (prev)
  537. prev->next = next;
  538. else
  539. delayed_set_stat_head = next;
  540. return;
  541. }
  542. else
  543. prev = data;
  544. }
  545. }
  546. static void
  547. fixup_delayed_set_stat (char const *src, char const *dst)
  548. {
  549. struct delayed_set_stat *data;
  550. for (data = delayed_set_stat_head; data; data = data->next)
  551. {
  552. if (chdir_current == data->change_dir
  553. && strcmp (data->file_name, src) == 0)
  554. {
  555. free (data->file_name);
  556. data->file_name = xstrdup (dst);
  557. data->file_name_len = strlen (dst);
  558. return;
  559. }
  560. }
  561. }
  562. /* After a file/link/directory creation has failed, see if
  563. it's because some required directory was not present, and if so,
  564. create all required directories. Return zero if all the required
  565. directories were created, nonzero (issuing a diagnostic) otherwise.
  566. Set *INTERDIR_MADE if at least one directory was created. */
  567. static int
  568. make_directories (char *file_name, bool *interdir_made)
  569. {
  570. char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
  571. char *cursor; /* points into the file name */
  572. for (cursor = cursor0; *cursor; cursor++)
  573. {
  574. mode_t mode;
  575. mode_t desired_mode;
  576. int status;
  577. if (! ISSLASH (*cursor))
  578. continue;
  579. /* Avoid mkdir of empty string, if leading or double '/'. */
  580. if (cursor == cursor0 || ISSLASH (cursor[-1]))
  581. continue;
  582. /* Avoid mkdir where last part of file name is "." or "..". */
  583. if (cursor[-1] == '.'
  584. && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
  585. || (cursor[-2] == '.'
  586. && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
  587. continue;
  588. *cursor = '\0'; /* truncate the name there */
  589. desired_mode = MODE_RWX & ~ newdir_umask;
  590. mode = desired_mode | (we_are_root ? 0 : MODE_WXUSR);
  591. status = mkdirat (chdir_fd, file_name, mode);
  592. if (status == 0)
  593. {
  594. /* Create a struct delayed_set_stat even if
  595. mode == desired_mode, because
  596. repair_delayed_set_stat may need to update the struct. */
  597. delay_set_stat (file_name,
  598. 0, mode & ~ current_umask, MODE_RWX,
  599. desired_mode, AT_SYMLINK_NOFOLLOW);
  600. print_for_mkdir (file_name, cursor - file_name, desired_mode);
  601. *interdir_made = true;
  602. }
  603. else if (errno == EEXIST)
  604. status = 0;
  605. else
  606. {
  607. /* Check whether the desired file exists. Even when the
  608. file exists, mkdir can fail with some errno value E other
  609. than EEXIST, so long as E describes an error condition
  610. that also applies. */
  611. int e = errno;
  612. struct stat st;
  613. status = fstatat (chdir_fd, file_name, &st, 0);
  614. if (status)
  615. {
  616. errno = e;
  617. mkdir_error (file_name);
  618. }
  619. }
  620. *cursor = '/';
  621. if (status)
  622. return status;
  623. }
  624. return 0;
  625. }
  626. /* Return true if FILE_NAME (with status *STP, if STP) is not a
  627. directory, and has a time stamp newer than (or equal to) that of
  628. TAR_STAT. */
  629. static bool
  630. file_newer_p (const char *file_name, struct stat const *stp,
  631. struct tar_stat_info *tar_stat)
  632. {
  633. struct stat st;
  634. if (!stp)
  635. {
  636. if (deref_stat (file_name, &st) != 0)
  637. {
  638. if (errno != ENOENT)
  639. {
  640. stat_warn (file_name);
  641. /* Be safer: if the file exists, assume it is newer. */
  642. return true;
  643. }
  644. return false;
  645. }
  646. stp = &st;
  647. }
  648. return (! S_ISDIR (stp->st_mode)
  649. && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (stp)) <= 0);
  650. }
  651. #define RECOVER_NO 0
  652. #define RECOVER_OK 1
  653. #define RECOVER_SKIP 2
  654. /* Attempt repairing what went wrong with the extraction. Delete an
  655. already existing file or create missing intermediate directories.
  656. Return RECOVER_OK if we somewhat increased our chances at a successful
  657. extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
  658. caller should skip extraction of that member. The value of errno is
  659. properly restored on returning RECOVER_NO.
  660. If REGULAR, the caller was trying to extract onto a regular file.
  661. Set *INTERDIR_MADE if an intermediate directory is made as part of
  662. the recovery process. */
  663. static int
  664. maybe_recoverable (char *file_name, bool regular, bool *interdir_made)
  665. {
  666. int e = errno;
  667. struct stat st;
  668. struct stat const *stp = 0;
  669. if (*interdir_made)
  670. return RECOVER_NO;
  671. switch (e)
  672. {
  673. case ELOOP:
  674. /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
  675. but some operating systems do not conform to the standard. */
  676. #ifdef EFTYPE
  677. /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */
  678. case EFTYPE:
  679. #endif
  680. /* FreeBSD 8.1 uses errno == EMLINK. */
  681. case EMLINK:
  682. /* Tru64 5.1B uses errno == ENOTSUP. */
  683. case ENOTSUP:
  684. if (! regular
  685. || old_files_option != OVERWRITE_OLD_FILES || dereference_option)
  686. break;
  687. if (strchr (file_name, '/'))
  688. {
  689. if (deref_stat (file_name, &st) != 0)
  690. break;
  691. stp = &st;
  692. }
  693. /* The caller tried to open a symbolic link with O_NOFOLLOW.
  694. Fall through, treating it as an already-existing file. */
  695. FALLTHROUGH;
  696. case EEXIST:
  697. /* Remove an old file, if the options allow this. */
  698. switch (old_files_option)
  699. {
  700. case SKIP_OLD_FILES:
  701. WARNOPT (WARN_EXISTING_FILE,
  702. (0, 0, _("%s: skipping existing file"), file_name));
  703. return RECOVER_SKIP;
  704. case KEEP_OLD_FILES:
  705. return RECOVER_NO;
  706. case KEEP_NEWER_FILES:
  707. if (file_newer_p (file_name, stp, &current_stat_info))
  708. break;
  709. FALLTHROUGH;
  710. case DEFAULT_OLD_FILES:
  711. case NO_OVERWRITE_DIR_OLD_FILES:
  712. case OVERWRITE_OLD_FILES:
  713. if (0 < remove_any_file (file_name, ORDINARY_REMOVE_OPTION))
  714. return RECOVER_OK;
  715. break;
  716. case UNLINK_FIRST_OLD_FILES:
  717. break;
  718. }
  719. FALLTHROUGH;
  720. case ENOENT:
  721. /* Attempt creating missing intermediate directories. */
  722. if (make_directories (file_name, interdir_made) == 0 && *interdir_made)
  723. return RECOVER_OK;
  724. break;
  725. default:
  726. /* Just say we can't do anything about it... */
  727. break;
  728. }
  729. errno = e;
  730. return RECOVER_NO;
  731. }
  732. /* Restore stat extended attributes (xattr) for FILE_NAME, using information
  733. given in *ST. Restore before extraction because they may affect file layout
  734. (e.g. on Lustre distributed parallel filesystem - setting info about how many
  735. servers is this file striped over, stripe size, mirror copies, etc.
  736. in advance dramatically improves the following performance of reading and
  737. writing a file). If not restoring permissions, invert the INVERT_PERMISSIONS
  738. bits from the file's current permissions. TYPEFLAG specifies the type of the
  739. file. Returns non-zero when error occurs (while un-available xattrs is not
  740. an error, rather no-op). Non-zero FILE_CREATED indicates set_xattr has
  741. created the file. */
  742. static int
  743. set_xattr (char const *file_name, struct tar_stat_info const *st,
  744. mode_t invert_permissions, char typeflag, int *file_created)
  745. {
  746. #ifdef HAVE_XATTRS
  747. bool interdir_made = false;
  748. if ((xattrs_option > 0) && st->xattr_map_size)
  749. {
  750. mode_t mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
  751. for (;;)
  752. {
  753. if (!mknodat (chdir_fd, file_name, mode ^ invert_permissions, 0))
  754. {
  755. /* Successfully created file */
  756. xattrs_xattrs_set (st, file_name, typeflag, 0);
  757. *file_created = 1;
  758. return 0;
  759. }
  760. switch (maybe_recoverable ((char *)file_name, false, &interdir_made))
  761. {
  762. case RECOVER_OK:
  763. continue;
  764. case RECOVER_NO:
  765. skip_member ();
  766. open_error (file_name);
  767. return 1;
  768. case RECOVER_SKIP:
  769. return 0;
  770. }
  771. }
  772. }
  773. #endif
  774. return 0;
  775. }
  776. /* Fix the statuses of all directories whose statuses need fixing, and
  777. which are not ancestors of FILE_NAME. If AFTER_LINKS is
  778. nonzero, do this for all such directories; otherwise, stop at the
  779. first directory that is marked to be fixed up only after delayed
  780. links are applied. */
  781. static void
  782. apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links)
  783. {
  784. size_t file_name_len = strlen (file_name);
  785. bool check_for_renamed_directories = 0;
  786. while (delayed_set_stat_head)
  787. {
  788. struct delayed_set_stat *data = delayed_set_stat_head;
  789. bool skip_this_one = 0;
  790. struct stat st;
  791. mode_t current_mode = data->current_mode;
  792. mode_t current_mode_mask = data->current_mode_mask;
  793. check_for_renamed_directories |= data->after_links;
  794. if (after_links < data->after_links
  795. || (data->file_name_len < file_name_len
  796. && file_name[data->file_name_len]
  797. && (ISSLASH (file_name[data->file_name_len])
  798. || ISSLASH (file_name[data->file_name_len - 1]))
  799. && memcmp (file_name, data->file_name, data->file_name_len) == 0))
  800. break;
  801. chdir_do (data->change_dir);
  802. if (check_for_renamed_directories)
  803. {
  804. if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
  805. {
  806. stat_error (data->file_name);
  807. skip_this_one = 1;
  808. }
  809. else
  810. {
  811. current_mode = st.st_mode;
  812. current_mode_mask = ALL_MODE_BITS;
  813. if (! (st.st_dev == data->dev && st.st_ino == data->ino))
  814. {
  815. ERROR ((0, 0,
  816. _("%s: Directory renamed before its status could be extracted"),
  817. quotearg_colon (data->file_name)));
  818. skip_this_one = 1;
  819. }
  820. }
  821. }
  822. if (! skip_this_one)
  823. {
  824. struct tar_stat_info sb;
  825. sb.stat.st_mode = data->mode;
  826. sb.stat.st_uid = data->uid;
  827. sb.stat.st_gid = data->gid;
  828. sb.atime = data->atime;
  829. sb.mtime = data->mtime;
  830. sb.cntx_name = data->cntx_name;
  831. sb.acls_a_ptr = data->acls_a_ptr;
  832. sb.acls_a_len = data->acls_a_len;
  833. sb.acls_d_ptr = data->acls_d_ptr;
  834. sb.acls_d_len = data->acls_d_len;
  835. sb.xattr_map = data->xattr_map;
  836. sb.xattr_map_size = data->xattr_map_size;
  837. set_stat (data->file_name, &sb,
  838. -1, current_mode, current_mode_mask,
  839. DIRTYPE, data->interdir, data->atflag);
  840. }
  841. delayed_set_stat_head = data->next;
  842. free_delayed_set_stat (data);
  843. }
  844. }
  845. static bool
  846. is_directory_link (const char *file_name)
  847. {
  848. struct stat st;
  849. int e = errno;
  850. int res;
  851. res = (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0 &&
  852. S_ISLNK (st.st_mode) &&
  853. fstatat (chdir_fd, file_name, &st, 0) == 0 &&
  854. S_ISDIR (st.st_mode));
  855. errno = e;
  856. return res;
  857. }
  858. /* Extractor functions for various member types */
  859. static int
  860. extract_dir (char *file_name, int typeflag)
  861. {
  862. int status;
  863. mode_t mode;
  864. mode_t current_mode = 0;
  865. mode_t current_mode_mask = 0;
  866. int atflag = 0;
  867. bool interdir_made = false;
  868. /* Save 'root device' to avoid purging mount points. */
  869. if (one_file_system_option && root_device == 0)
  870. {
  871. struct stat st;
  872. if (fstatat (chdir_fd, ".", &st, 0) != 0)
  873. stat_diag (".");
  874. else
  875. root_device = st.st_dev;
  876. }
  877. if (incremental_option)
  878. /* Read the entry and delete files that aren't listed in the archive. */
  879. purge_directory (file_name);
  880. else if (typeflag == GNUTYPE_DUMPDIR)
  881. skip_member ();
  882. /* If ownership or permissions will be restored later, create the
  883. directory with restrictive permissions at first, so that in the
  884. meantime processes owned by other users do not inadvertently
  885. create files under this directory that inherit the wrong owner,
  886. group, or permissions from the directory. If not root, though,
  887. make the directory writeable and searchable at first, so that
  888. files can be created under it. */
  889. mode = ((current_stat_info.stat.st_mode
  890. & (0 < same_owner_option || 0 < same_permissions_option
  891. ? S_IRWXU
  892. : MODE_RWX))
  893. | (we_are_root ? 0 : MODE_WXUSR));
  894. for (;;)
  895. {
  896. status = mkdirat (chdir_fd, file_name, mode);
  897. if (status == 0)
  898. {
  899. current_mode = mode & ~ current_umask;
  900. current_mode_mask = MODE_RWX;
  901. atflag = AT_SYMLINK_NOFOLLOW;
  902. break;
  903. }
  904. if (errno == EEXIST
  905. && (interdir_made
  906. || keep_directory_symlink_option
  907. || old_files_option == DEFAULT_OLD_FILES
  908. || old_files_option == OVERWRITE_OLD_FILES))
  909. {
  910. struct stat st;
  911. if (keep_directory_symlink_option && is_directory_link (file_name))
  912. return 0;
  913. if (deref_stat (file_name, &st) == 0)
  914. {
  915. current_mode = st.st_mode;
  916. current_mode_mask = ALL_MODE_BITS;
  917. if (S_ISDIR (current_mode))
  918. {
  919. if (interdir_made)
  920. {
  921. repair_delayed_set_stat (file_name, &st);
  922. return 0;
  923. }
  924. break;
  925. }
  926. }
  927. errno = EEXIST;
  928. }
  929. switch (maybe_recoverable (file_name, false, &interdir_made))
  930. {
  931. case RECOVER_OK:
  932. continue;
  933. case RECOVER_SKIP:
  934. break;
  935. case RECOVER_NO:
  936. if (errno != EEXIST)
  937. {
  938. mkdir_error (file_name);
  939. return 1;
  940. }
  941. break;
  942. }
  943. break;
  944. }
  945. if (status == 0
  946. || old_files_option == DEFAULT_OLD_FILES
  947. || old_files_option == OVERWRITE_OLD_FILES)
  948. delay_set_stat (file_name, &current_stat_info,
  949. current_mode, current_mode_mask,
  950. current_stat_info.stat.st_mode, atflag);
  951. return status;
  952. }
  953. static int
  954. open_output_file (char const *file_name, int typeflag, mode_t mode,
  955. int file_created, mode_t *current_mode,
  956. mode_t *current_mode_mask)
  957. {
  958. int fd;
  959. bool overwriting_old_files = old_files_option == OVERWRITE_OLD_FILES;
  960. int openflag = (O_WRONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
  961. | O_CREAT
  962. | (overwriting_old_files
  963. ? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW)
  964. : O_EXCL));
  965. /* File might be created in set_xattr. So clear O_EXCL to avoid open() fail */
  966. if (file_created)
  967. openflag = openflag & ~O_EXCL;
  968. if (typeflag == CONTTYPE)
  969. {
  970. static int conttype_diagnosed;
  971. if (!conttype_diagnosed)
  972. {
  973. conttype_diagnosed = 1;
  974. WARNOPT (WARN_CONTIGUOUS_CAST,
  975. (0, 0, _("Extracting contiguous files as regular files")));
  976. }
  977. }
  978. /* If O_NOFOLLOW is needed but does not work, check for a symlink
  979. separately. There's a race condition, but that cannot be avoided
  980. on hosts lacking O_NOFOLLOW. */
  981. if (! HAVE_WORKING_O_NOFOLLOW
  982. && overwriting_old_files && ! dereference_option)
  983. {
  984. struct stat st;
  985. if (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0
  986. && S_ISLNK (st.st_mode))
  987. {
  988. errno = ELOOP;
  989. return -1;
  990. }
  991. }
  992. fd = openat (chdir_fd, file_name, openflag, mode);
  993. if (0 <= fd)
  994. {
  995. if (overwriting_old_files)
  996. {
  997. struct stat st;
  998. if (fstat (fd, &st) != 0)
  999. {
  1000. int e = errno;
  1001. close (fd);
  1002. errno = e;
  1003. return -1;
  1004. }
  1005. if (! S_ISREG (st.st_mode))
  1006. {
  1007. close (fd);
  1008. errno = EEXIST;
  1009. return -1;
  1010. }
  1011. *current_mode = st.st_mode;
  1012. *current_mode_mask = ALL_MODE_BITS;
  1013. }
  1014. else
  1015. {
  1016. *current_mode = mode & ~ current_umask;
  1017. *current_mode_mask = MODE_RWX;
  1018. }
  1019. }
  1020. return fd;
  1021. }
  1022. static int
  1023. extract_file (char *file_name, int typeflag)
  1024. {
  1025. int fd;
  1026. off_t size;
  1027. union block *data_block;
  1028. int status;
  1029. size_t count;
  1030. size_t written;
  1031. bool interdir_made = false;
  1032. mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
  1033. & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
  1034. mode_t invert_permissions = 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO)
  1035. : 0;
  1036. mode_t current_mode = 0;
  1037. mode_t current_mode_mask = 0;
  1038. if (to_stdout_option)
  1039. fd = STDOUT_FILENO;
  1040. else if (to_command_option)
  1041. {
  1042. fd = sys_exec_command (file_name, 'f', &current_stat_info);
  1043. if (fd < 0)
  1044. {
  1045. skip_member ();
  1046. return 0;
  1047. }
  1048. }
  1049. else
  1050. {
  1051. int file_created = 0;
  1052. if (set_xattr (file_name, &current_stat_info, invert_permissions,
  1053. typeflag, &file_created))
  1054. return 1;
  1055. while ((fd = open_output_file (file_name, typeflag, mode,
  1056. file_created, &current_mode,
  1057. &current_mode_mask))
  1058. < 0)
  1059. {
  1060. int recover = maybe_recoverable (file_name, true, &interdir_made);
  1061. if (recover != RECOVER_OK)
  1062. {
  1063. skip_member ();
  1064. if (recover == RECOVER_SKIP)
  1065. return 0;
  1066. open_error (file_name);
  1067. return 1;
  1068. }
  1069. }
  1070. }
  1071. mv_begin_read (&current_stat_info);
  1072. if (current_stat_info.is_sparse)
  1073. sparse_extract_file (fd, &current_stat_info, &size);
  1074. else
  1075. for (size = current_stat_info.stat.st_size; size > 0; )
  1076. {
  1077. mv_size_left (size);
  1078. /* Locate data, determine max length writeable, write it,
  1079. block that we have used the data, then check if the write
  1080. worked. */
  1081. data_block = find_next_block ();
  1082. if (! data_block)
  1083. {
  1084. ERROR ((0, 0, _("Unexpected EOF in archive")));
  1085. break; /* FIXME: What happens, then? */
  1086. }
  1087. written = available_space_after (data_block);
  1088. if (written > size)
  1089. written = size;
  1090. errno = 0;
  1091. count = blocking_write (fd, data_block->buffer, written);
  1092. size -= written;
  1093. set_next_block_after ((union block *)
  1094. (data_block->buffer + written - 1));
  1095. if (count != written)
  1096. {
  1097. if (!to_command_option)
  1098. write_error_details (file_name, count, written);
  1099. /* FIXME: shouldn't we restore from backup? */
  1100. break;
  1101. }
  1102. }
  1103. skip_file (size);
  1104. mv_end ();
  1105. /* If writing to stdout, don't try to do anything to the filename;
  1106. it doesn't exist, or we don't want to touch it anyway. */
  1107. if (to_stdout_option)
  1108. return 0;
  1109. if (! to_command_option)
  1110. set_stat (file_name, &current_stat_info, fd,
  1111. current_mode, current_mode_mask, typeflag, false,
  1112. (old_files_option == OVERWRITE_OLD_FILES
  1113. ? 0 : AT_SYMLINK_NOFOLLOW));
  1114. status = close (fd);
  1115. if (status < 0)
  1116. close_error (file_name);
  1117. if (to_command_option)
  1118. sys_wait_command ();
  1119. return status;
  1120. }
  1121. /* Create a placeholder file with name FILE_NAME, which will be
  1122. replaced after other extraction is done by a symbolic link if
  1123. IS_SYMLINK is true, and by a hard link otherwise. Set
  1124. *INTERDIR_MADE if an intermediate directory is made in the
  1125. process.
  1126. Install the created struct delayed_link after PREV, unless the
  1127. latter is NULL, in which case insert it at the head of the delayed
  1128. link list.
  1129. */
  1130. static int
  1131. create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made,
  1132. struct delayed_link *prev)
  1133. {
  1134. int fd;
  1135. struct stat st;
  1136. while ((fd = openat (chdir_fd, file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
  1137. {
  1138. switch (maybe_recoverable (file_name, false, interdir_made))
  1139. {
  1140. case RECOVER_OK:
  1141. continue;
  1142. case RECOVER_SKIP:
  1143. return 0;
  1144. case RECOVER_NO:
  1145. open_error (file_name);
  1146. return -1;
  1147. }
  1148. }
  1149. if (fstat (fd, &st) != 0)
  1150. {
  1151. stat_error (file_name);
  1152. close (fd);
  1153. }
  1154. else if (close (fd) != 0)
  1155. close_error (file_name);
  1156. else
  1157. {
  1158. struct delayed_set_stat *h;
  1159. struct delayed_link *p =
  1160. xmalloc (offsetof (struct delayed_link, target)
  1161. + strlen (current_stat_info.link_name)
  1162. + 1);
  1163. if (prev)
  1164. {
  1165. p->next = prev->next;
  1166. prev->next = p;
  1167. }
  1168. else
  1169. {
  1170. p->next = delayed_link_head;
  1171. delayed_link_head = p;
  1172. }
  1173. p->dev = st.st_dev;
  1174. p->ino = st.st_ino;
  1175. p->birthtime = get_stat_birthtime (&st);
  1176. p->is_symlink = is_symlink;
  1177. if (is_symlink)
  1178. {
  1179. p->mode = current_stat_info.stat.st_mode;
  1180. p->uid = current_stat_info.stat.st_uid;
  1181. p->gid = current_stat_info.stat.st_gid;
  1182. p->atime = current_stat_info.atime;
  1183. p->mtime = current_stat_info.mtime;
  1184. }
  1185. p->change_dir = chdir_current;
  1186. p->sources = xmalloc (offsetof (struct string_list, string)
  1187. + strlen (file_name) + 1);
  1188. p->sources->next = 0;
  1189. strcpy (p->sources->string, file_name);
  1190. p->cntx_name = NULL;
  1191. assign_string (&p->cntx_name, current_stat_info.cntx_name);
  1192. p->acls_a_ptr = NULL;
  1193. p->acls_a_len = 0;
  1194. p->acls_d_ptr = NULL;
  1195. p->acls_d_len = 0;
  1196. xheader_xattr_copy (&current_stat_info, &p->xattr_map,
  1197. &p->xattr_map_size);
  1198. strcpy (p->target, current_stat_info.link_name);
  1199. if ((h = find_direct_ancestor (file_name)) != NULL)
  1200. mark_after_links (h);
  1201. return 0;
  1202. }
  1203. return -1;
  1204. }
  1205. /* Find a delayed_link structure corresponding to the source NAME.
  1206. Such a structure exists in the delayed link list only if the link
  1207. placeholder file has been created. Therefore, try to stat the NAME
  1208. first. If it doesn't exist, there is no matching entry in the list.
  1209. Otherwise, look for the entry in list which has the matching dev
  1210. and ino numbers.
  1211. This approach avoids scanning the singly-linked list in obvious cases
  1212. and does not rely on comparing file names, which may differ for
  1213. various reasons (e.g. relative vs. absolute file names).
  1214. */
  1215. static struct delayed_link *
  1216. find_delayed_link_source (char const *name)
  1217. {
  1218. struct delayed_link *dl;
  1219. struct stat st;
  1220. if (!delayed_link_head)
  1221. return NULL;
  1222. if (fstatat (chdir_fd, name, &st, AT_SYMLINK_NOFOLLOW))
  1223. {
  1224. if (errno != ENOENT)
  1225. stat_error (name);
  1226. return NULL;
  1227. }
  1228. for (dl = delayed_link_head; dl; dl = dl->next)
  1229. {
  1230. if (dl->dev == st.st_dev && dl->ino == st.st_ino)
  1231. break;
  1232. }
  1233. return dl;
  1234. }
  1235. static int
  1236. extract_link (char *file_name, int typeflag)
  1237. {
  1238. bool interdir_made = false;
  1239. char const *link_name;
  1240. int rc;
  1241. struct delayed_link *dl;
  1242. link_name = current_stat_info.link_name;
  1243. if (! absolute_names_option && contains_dot_dot (link_name))
  1244. return create_placeholder_file (file_name, false, &interdir_made, NULL);
  1245. dl = find_delayed_link_source (link_name);
  1246. if (dl)
  1247. return create_placeholder_file (file_name, false, &interdir_made, dl);
  1248. do
  1249. {
  1250. struct stat st1, st2;
  1251. int e;
  1252. int status = linkat (chdir_fd, link_name, chdir_fd, file_name, 0);
  1253. e = errno;
  1254. if (status == 0)
  1255. {
  1256. struct delayed_link *ds = delayed_link_head;
  1257. if (ds
  1258. && fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) == 0)
  1259. for (; ds; ds = ds->next)
  1260. if (ds->change_dir == chdir_current
  1261. && ds->dev == st1.st_dev
  1262. && ds->ino == st1.st_ino
  1263. && (timespec_cmp (ds->birthtime, get_stat_birthtime (&st1))
  1264. == 0))
  1265. {
  1266. struct string_list *p = xmalloc (offsetof (struct string_list, string)
  1267. + strlen (file_name) + 1);
  1268. strcpy (p->string, file_name);
  1269. p->next = ds->sources;
  1270. ds->sources = p;
  1271. break;
  1272. }
  1273. return 0;
  1274. }
  1275. else if ((e == EEXIST && strcmp (link_name, file_name) == 0)
  1276. || ((fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW)
  1277. == 0)
  1278. && (fstatat (chdir_fd, file_name, &st2, AT_SYMLINK_NOFOLLOW)
  1279. == 0)
  1280. && st1.st_dev == st2.st_dev
  1281. && st1.st_ino == st2.st_ino))
  1282. return 0;
  1283. errno = e;
  1284. }
  1285. while ((rc = maybe_recoverable (file_name, false, &interdir_made))
  1286. == RECOVER_OK);
  1287. if (rc == RECOVER_SKIP)
  1288. return 0;
  1289. if (!(incremental_option && errno == EEXIST))
  1290. {
  1291. link_error (link_name, file_name);
  1292. return 1;
  1293. }
  1294. return 0;
  1295. }
  1296. static int
  1297. extract_symlink (char *file_name, int typeflag)
  1298. {
  1299. #ifdef HAVE_SYMLINK
  1300. bool interdir_made = false;
  1301. if (! absolute_names_option
  1302. && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name)
  1303. || contains_dot_dot (current_stat_info.link_name)))
  1304. return create_placeholder_file (file_name, true, &interdir_made, NULL);
  1305. while (symlinkat (current_stat_info.link_name, chdir_fd, file_name) != 0)
  1306. switch (maybe_recoverable (file_name, false, &interdir_made))
  1307. {
  1308. case RECOVER_OK:
  1309. continue;
  1310. case RECOVER_SKIP:
  1311. return 0;
  1312. case RECOVER_NO:
  1313. symlink_error (current_stat_info.link_name, file_name);
  1314. return -1;
  1315. }
  1316. set_stat (file_name, &current_stat_info, -1, 0, 0,
  1317. SYMTYPE, false, AT_SYMLINK_NOFOLLOW);
  1318. return 0;
  1319. #else
  1320. static int warned_once;
  1321. if (!warned_once)
  1322. {
  1323. warned_once = 1;
  1324. WARNOPT (WARN_SYMLINK_CAST,
  1325. (0, 0,
  1326. _("Attempting extraction of symbolic links as hard links")));
  1327. }
  1328. return extract_link (file_name, typeflag);
  1329. #endif
  1330. }
  1331. #if S_IFCHR || S_IFBLK
  1332. static int
  1333. extract_node (char *file_name, int typeflag)
  1334. {
  1335. bool interdir_made = false;
  1336. mode_t mode = (current_stat_info.stat.st_mode & (MODE_RWX | S_IFBLK | S_IFCHR)
  1337. & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
  1338. while (mknodat (chdir_fd, file_name, mode, current_stat_info.stat.st_rdev)
  1339. != 0)
  1340. switch (maybe_recoverable (file_name, false, &interdir_made))
  1341. {
  1342. case RECOVER_OK:
  1343. continue;
  1344. case RECOVER_SKIP:
  1345. return 0;
  1346. case RECOVER_NO:
  1347. mknod_error (file_name);
  1348. return -1;
  1349. }
  1350. set_stat (file_name, &current_stat_info, -1,
  1351. mode & ~ current_umask, MODE_RWX,
  1352. typeflag, false, AT_SYMLINK_NOFOLLOW);
  1353. return 0;
  1354. }
  1355. #endif
  1356. #if HAVE_MKFIFO || defined mkfifo
  1357. static int
  1358. extract_fifo (char *file_name, int typeflag)
  1359. {
  1360. bool interdir_made = false;
  1361. mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
  1362. & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
  1363. while (mkfifoat (chdir_fd, file_name, mode) != 0)
  1364. switch (maybe_recoverable (file_name, false, &interdir_made))
  1365. {
  1366. case RECOVER_OK:
  1367. continue;
  1368. case RECOVER_SKIP:
  1369. return 0;
  1370. case RECOVER_NO:
  1371. mkfifo_error (file_name);
  1372. return -1;
  1373. }
  1374. set_stat (file_name, &current_stat_info, -1,
  1375. mode & ~ current_umask, MODE_RWX,
  1376. typeflag, false, AT_SYMLINK_NOFOLLOW);
  1377. return 0;
  1378. }
  1379. #endif
  1380. static int
  1381. extract_volhdr (char *file_name, int typeflag)
  1382. {
  1383. skip_member ();
  1384. return 0;
  1385. }
  1386. static int
  1387. extract_failure (char *file_name, int typeflag)
  1388. {
  1389. return 1;
  1390. }
  1391. static int
  1392. extract_skip (char *file_name, int typeflag)
  1393. {
  1394. skip_member ();
  1395. return 0;
  1396. }
  1397. typedef int (*tar_extractor_t) (char *file_name, int typeflag);
  1398. /* Prepare to extract a file. Find extractor function.
  1399. Return zero if extraction should not proceed. */
  1400. static int
  1401. prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun)
  1402. {
  1403. int rc = 1;
  1404. if (EXTRACT_OVER_PIPE)
  1405. rc = 0;
  1406. /* Select the extractor */
  1407. switch (typeflag)
  1408. {
  1409. case GNUTYPE_SPARSE:
  1410. *fun = extract_file;
  1411. rc = 1;
  1412. break;
  1413. case AREGTYPE:
  1414. case REGTYPE:
  1415. case CONTTYPE:
  1416. /* Appears to be a file. But BSD tar uses the convention that a slash
  1417. suffix means a directory. */
  1418. if (current_stat_info.had_trailing_slash)
  1419. *fun = extract_dir;
  1420. else
  1421. {
  1422. *fun = extract_file;
  1423. rc = 1;
  1424. }
  1425. break;
  1426. case SYMTYPE:
  1427. *fun = extract_symlink;
  1428. break;
  1429. case LNKTYPE:
  1430. *fun = extract_link;
  1431. break;
  1432. #if S_IFCHR
  1433. case CHRTYPE:
  1434. current_stat_info.stat.st_mode |= S_IFCHR;
  1435. *fun = extract_node;
  1436. break;
  1437. #endif
  1438. #if S_IFBLK
  1439. case BLKTYPE:
  1440. current_stat_info.stat.st_mode |= S_IFBLK;
  1441. *fun = extract_node;
  1442. break;
  1443. #endif
  1444. #if HAVE_MKFIFO || defined mkfifo
  1445. case FIFOTYPE:
  1446. *fun = extract_fifo;
  1447. break;
  1448. #endif
  1449. case DIRTYPE:
  1450. case GNUTYPE_DUMPDIR:
  1451. *fun = extract_dir;
  1452. if (current_stat_info.is_dumpdir)
  1453. delay_directory_restore_option = true;
  1454. break;
  1455. case GNUTYPE_VOLHDR:
  1456. *fun = extract_volhdr;
  1457. break;
  1458. case GNUTYPE_MULTIVOL:
  1459. ERROR ((0, 0,
  1460. _("%s: Cannot extract -- file is continued from another volume"),
  1461. quotearg_colon (current_stat_info.file_name)));
  1462. *fun = extract_skip;
  1463. break;
  1464. case GNUTYPE_LONGNAME:
  1465. case GNUTYPE_LONGLINK:
  1466. ERROR ((0, 0, _("Unexpected long name header")));
  1467. *fun = extract_failure;
  1468. break;
  1469. default:
  1470. WARNOPT (WARN_UNKNOWN_CAST,
  1471. (0, 0,
  1472. _("%s: Unknown file type '%c', extracted as normal file"),
  1473. quotearg_colon (file_name), typeflag));
  1474. *fun = extract_file;
  1475. }
  1476. /* Determine whether the extraction should proceed */
  1477. if (rc == 0)
  1478. return 0;
  1479. switch (old_files_option)
  1480. {
  1481. case UNLINK_FIRST_OLD_FILES:
  1482. if (!remove_any_file (file_name,
  1483. recursive_unlink_option ? RECURSIVE_REMOVE_OPTION
  1484. : ORDINARY_REMOVE_OPTION)
  1485. && errno && errno != ENOENT)
  1486. {
  1487. unlink_error (file_name);
  1488. return 0;
  1489. }
  1490. break;
  1491. case KEEP_NEWER_FILES:
  1492. if (file_newer_p (file_name, 0, &current_stat_info))
  1493. {
  1494. WARNOPT (WARN_IGNORE_NEWER,
  1495. (0, 0, _("Current %s is newer or same age"),
  1496. quote (file_name)));
  1497. return 0;
  1498. }
  1499. break;
  1500. default:
  1501. break;
  1502. }
  1503. return 1;
  1504. }
  1505. /* Extract a file from the archive. */
  1506. void
  1507. extract_archive (void)
  1508. {
  1509. char typeflag;
  1510. tar_extractor_t fun;
  1511. bool skip_dotdot_name;
  1512. fatal_exit_hook = extract_finish;
  1513. set_next_block_after (current_header);
  1514. skip_dotdot_name = (!absolute_names_option
  1515. && contains_dot_dot (current_stat_info.orig_file_name));
  1516. if (skip_dotdot_name)
  1517. ERROR ((0, 0, _("%s: Member name contains '..'"),
  1518. quotearg_colon (current_stat_info.orig_file_name)));
  1519. if (!current_stat_info.file_name[0]
  1520. || skip_dotdot_name
  1521. || (interactive_option
  1522. && !confirm ("extract", current_stat_info.file_name)))
  1523. {
  1524. skip_member ();
  1525. return;
  1526. }
  1527. /* Print the block from current_header and current_stat. */
  1528. if (verbose_option)
  1529. print_header (&current_stat_info, current_header, -1);
  1530. /* Restore stats for all non-ancestor directories, unless
  1531. it is an incremental archive.
  1532. (see NOTICE in the comment to delay_set_stat above) */
  1533. if (!delay_directory_restore_option)
  1534. {
  1535. int dir = chdir_current;
  1536. apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0);
  1537. chdir_do (dir);
  1538. }
  1539. /* Take a safety backup of a previously existing file. */
  1540. if (backup_option)
  1541. if (!maybe_backup_file (current_stat_info.file_name, 0))
  1542. {
  1543. int e = errno;
  1544. ERROR ((0, e, _("%s: Was unable to backup this file"),
  1545. quotearg_colon (current_stat_info.file_name)));
  1546. skip_member ();
  1547. return;
  1548. }
  1549. /* Extract the archive entry according to its type. */
  1550. /* KLUDGE */
  1551. typeflag = sparse_member_p (&current_stat_info) ?
  1552. GNUTYPE_SPARSE : current_header->header.typeflag;
  1553. if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun))
  1554. {
  1555. if (fun && (*fun) (current_stat_info.file_name, typeflag)
  1556. && backup_option)
  1557. undo_last_backup ();
  1558. }
  1559. else
  1560. skip_member ();
  1561. }
  1562. /* Extract the links whose final extraction were delayed. */
  1563. static void
  1564. apply_delayed_links (void)
  1565. {
  1566. struct delayed_link *ds;
  1567. for (ds = delayed_link_head; ds; )
  1568. {
  1569. struct string_list *sources = ds->sources;
  1570. char const *valid_source = 0;
  1571. chdir_do (ds->change_dir);
  1572. for (sources = ds->sources; sources; sources = sources->next)
  1573. {
  1574. char const *source = sources->string;
  1575. struct stat st;
  1576. /* Make sure the placeholder file is still there. If not,
  1577. don't create a link, as the placeholder was probably
  1578. removed by a later extraction. */
  1579. if (fstatat (chdir_fd, source, &st, AT_SYMLINK_NOFOLLOW) == 0
  1580. && st.st_dev == ds->dev
  1581. && st.st_ino == ds->ino
  1582. && timespec_cmp (get_stat_birthtime (&st), ds->birthtime) == 0)
  1583. {
  1584. /* Unlink the placeholder, then create a hard link if possible,
  1585. a symbolic link otherwise. */
  1586. if (unlinkat (chdir_fd, source, 0) != 0)
  1587. unlink_error (source);
  1588. else if (valid_source
  1589. && (linkat (chdir_fd, valid_source, chdir_fd, source, 0)
  1590. == 0))
  1591. ;
  1592. else if (!ds->is_symlink)
  1593. {
  1594. if (linkat (chdir_fd, ds->target, chdir_fd, source, 0) != 0)
  1595. link_error (ds->target, source);
  1596. }
  1597. else if (symlinkat (ds->target, chdir_fd, source) != 0)
  1598. symlink_error (ds->target, source);
  1599. else
  1600. {
  1601. struct tar_stat_info st1;
  1602. st1.stat.st_mode = ds->mode;
  1603. st1.stat.st_uid = ds->uid;
  1604. st1.stat.st_gid = ds->gid;
  1605. st1.atime = ds->atime;
  1606. st1.mtime = ds->mtime;
  1607. st1.cntx_name = ds->cntx_name;
  1608. st1.acls_a_ptr = ds->acls_a_ptr;
  1609. st1.acls_a_len = ds->acls_a_len;
  1610. st1.acls_d_ptr = ds->acls_d_ptr;
  1611. st1.acls_d_len = ds->acls_d_len;
  1612. st1.xattr_map = ds->xattr_map;
  1613. st1.xattr_map_size = ds->xattr_map_size;
  1614. set_stat (source, &st1, -1, 0, 0, SYMTYPE,
  1615. false, AT_SYMLINK_NOFOLLOW);
  1616. valid_source = source;
  1617. }
  1618. }
  1619. }
  1620. for (sources = ds->sources; sources; )
  1621. {
  1622. struct string_list *next = sources->next;
  1623. free (sources);
  1624. sources = next;
  1625. }
  1626. xheader_xattr_free (ds->xattr_map, ds->xattr_map_size);
  1627. free (ds->cntx_name);
  1628. {
  1629. struct delayed_link *next = ds->next;
  1630. free (ds);
  1631. ds = next;
  1632. }
  1633. }
  1634. delayed_link_head = 0;
  1635. }
  1636. /* Finish the extraction of an archive. */
  1637. void
  1638. extract_finish (void)
  1639. {
  1640. /* First, fix the status of ordinary directories that need fixing. */
  1641. apply_nonancestor_delayed_set_stat ("", 0);
  1642. /* Then, apply delayed links, so that they don't affect delayed
  1643. directory status-setting for ordinary directories. */
  1644. apply_delayed_links ();
  1645. /* Finally, fix the status of directories that are ancestors
  1646. of delayed links. */
  1647. apply_nonancestor_delayed_set_stat ("", 1);
  1648. }
  1649. bool
  1650. rename_directory (char *src, char *dst)
  1651. {
  1652. if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
  1653. fixup_delayed_set_stat (src, dst);
  1654. else
  1655. {
  1656. int e = errno;
  1657. bool interdir_made;
  1658. switch (e)
  1659. {
  1660. case ENOENT:
  1661. if (make_directories (dst, &interdir_made) == 0)
  1662. {
  1663. if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
  1664. return true;
  1665. e = errno;
  1666. }
  1667. break;
  1668. case EXDEV:
  1669. /* FIXME: Fall back to recursive copying */
  1670. default:
  1671. break;
  1672. }
  1673. ERROR ((0, e, _("Cannot rename %s to %s"),
  1674. quote_n (0, src),
  1675. quote_n (1, dst)));
  1676. return false;
  1677. }
  1678. return true;
  1679. }