extract.c 40 KB

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