4
0

extract.c 46 KB

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