extract.c 36 KB

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