extract.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /* Extract files from a tar archive.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
  3. 2001, 2003, 2004 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 2, 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. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  16. #include "system.h"
  17. #include <quotearg.h>
  18. #include <errno.h>
  19. #if HAVE_UTIME_H
  20. # include <utime.h>
  21. #else
  22. struct utimbuf
  23. {
  24. long actime;
  25. long modtime;
  26. };
  27. #endif
  28. #include "common.h"
  29. bool we_are_root; /* true if our effective uid == 0 */
  30. static mode_t newdir_umask; /* umask when creating new directories */
  31. static mode_t current_umask; /* current umask (which is set to 0 if -p) */
  32. /* Status of the permissions of a file that we are extracting. */
  33. enum permstatus
  34. {
  35. /* This file may have existed already; its permissions are unknown. */
  36. UNKNOWN_PERMSTATUS,
  37. /* This file was created using the permissions from the archive. */
  38. ARCHIVED_PERMSTATUS,
  39. /* This is an intermediate directory; the archive did not specify
  40. its permissions. */
  41. INTERDIR_PERMSTATUS
  42. };
  43. /* List of directories whose statuses we need to extract after we've
  44. finished extracting their subsidiary files. If you consider each
  45. contiguous subsequence of elements of the form [D]?[^D]*, where [D]
  46. represents an element where AFTER_SYMLINKS is nonzero and [^D]
  47. represents an element where AFTER_SYMLINKS is zero, then the head
  48. of the subsequence has the longest name, and each non-head element
  49. in the prefix is an ancestor (in the directory hierarchy) of the
  50. preceding element. */
  51. struct delayed_set_stat
  52. {
  53. struct delayed_set_stat *next;
  54. struct stat stat_info;
  55. size_t file_name_len;
  56. mode_t invert_permissions;
  57. enum permstatus permstatus;
  58. bool after_symlinks;
  59. char file_name[1];
  60. };
  61. static struct delayed_set_stat *delayed_set_stat_head;
  62. /* List of symbolic links whose creation we have delayed. */
  63. struct delayed_symlink
  64. {
  65. /* The next delayed symbolic link in the list. */
  66. struct delayed_symlink *next;
  67. /* The device, inode number and last-modified time of the placeholder. */
  68. dev_t dev;
  69. ino_t ino;
  70. time_t mtime;
  71. /* The desired owner and group of the symbolic link. */
  72. uid_t uid;
  73. gid_t gid;
  74. /* A list of sources for this symlink. The sources are all to be
  75. hard-linked together. */
  76. struct string_list *sources;
  77. /* The desired target of the desired link. */
  78. char target[1];
  79. };
  80. static struct delayed_symlink *delayed_symlink_head;
  81. struct string_list
  82. {
  83. struct string_list *next;
  84. char string[1];
  85. };
  86. /* Set up to extract files. */
  87. void
  88. extr_init (void)
  89. {
  90. we_are_root = geteuid () == 0;
  91. same_permissions_option += we_are_root;
  92. same_owner_option += we_are_root;
  93. xalloc_fail_func = extract_finish;
  94. /* Option -p clears the kernel umask, so it does not affect proper
  95. restoration of file permissions. New intermediate directories will
  96. comply with umask at start of program. */
  97. newdir_umask = umask (0);
  98. if (0 < same_permissions_option)
  99. current_umask = 0;
  100. else
  101. {
  102. umask (newdir_umask); /* restore the kernel umask */
  103. current_umask = newdir_umask;
  104. }
  105. }
  106. /* If restoring permissions, restore the mode for FILE_NAME from
  107. information given in *STAT_INFO (where *CUR_INFO gives
  108. the current status if CUR_INFO is nonzero); otherwise invert the
  109. INVERT_PERMISSIONS bits from the file's current permissions.
  110. PERMSTATUS specifies the status of the file's permissions.
  111. TYPEFLAG specifies the type of the file. */
  112. static void
  113. set_mode (char const *file_name,
  114. struct stat const *stat_info,
  115. struct stat const *cur_info,
  116. mode_t invert_permissions, enum permstatus permstatus,
  117. char typeflag)
  118. {
  119. mode_t mode;
  120. if (0 < same_permissions_option
  121. && permstatus != INTERDIR_PERMSTATUS)
  122. {
  123. mode = stat_info->st_mode;
  124. /* If we created the file and it has a usual mode, then its mode
  125. is normally set correctly already. But on many hosts, some
  126. directories inherit the setgid bits from their parents, so we
  127. we must set directories' modes explicitly. */
  128. if (permstatus == ARCHIVED_PERMSTATUS
  129. && ! (mode & ~ MODE_RWX)
  130. && typeflag != DIRTYPE
  131. && typeflag != GNUTYPE_DUMPDIR)
  132. return;
  133. }
  134. else if (! invert_permissions)
  135. return;
  136. else
  137. {
  138. /* We must inspect a directory's current permissions, since the
  139. directory may have inherited its setgid bit from its parent.
  140. INVERT_PERMISSIONS happens to be nonzero only for directories
  141. that we created, so there's no point optimizing this code for
  142. other cases. */
  143. struct stat st;
  144. if (! cur_info)
  145. {
  146. if (stat (file_name, &st) != 0)
  147. {
  148. stat_error (file_name);
  149. return;
  150. }
  151. cur_info = &st;
  152. }
  153. mode = cur_info->st_mode ^ invert_permissions;
  154. }
  155. if (chmod (file_name, mode) != 0)
  156. chmod_error_details (file_name, mode);
  157. }
  158. /* Check time after successfully setting FILE_NAME's time stamp to T. */
  159. static void
  160. check_time (char const *file_name, time_t t)
  161. {
  162. time_t now;
  163. if (t <= 0)
  164. WARN ((0, 0, _("%s: implausibly old time stamp %s"),
  165. file_name, tartime (t)));
  166. else if (start_time < t && (now = time (0)) < t)
  167. WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
  168. file_name, tartime (t), (unsigned long) (t - now)));
  169. }
  170. /* Restore stat attributes (owner, group, mode and times) for
  171. FILE_NAME, using information given in *STAT_INFO.
  172. If CUR_INFO is nonzero, *CUR_INFO is the
  173. file's currernt status.
  174. If not restoring permissions, invert the
  175. INVERT_PERMISSIONS bits from the file's current permissions.
  176. PERMSTATUS specifies the status of the file's permissions.
  177. TYPEFLAG specifies the type of the file. */
  178. /* FIXME: About proper restoration of symbolic link attributes, we still do
  179. not have it right. Pretesters' reports tell us we need further study and
  180. probably more configuration. For now, just use lchown if it exists, and
  181. punt for the rest. Sigh! */
  182. static void
  183. set_stat (char const *file_name,
  184. struct stat const *stat_info,
  185. struct stat const *cur_info,
  186. mode_t invert_permissions, enum permstatus permstatus,
  187. char typeflag)
  188. {
  189. struct utimbuf utimbuf;
  190. if (typeflag != SYMTYPE)
  191. {
  192. /* We do the utime before the chmod because some versions of utime are
  193. broken and trash the modes of the file. */
  194. if (! touch_option && permstatus != INTERDIR_PERMSTATUS)
  195. {
  196. /* We set the accessed time to `now', which is really the time we
  197. started extracting files, unless incremental_option is used, in
  198. which case .st_atime is used. */
  199. /* FIXME: incremental_option should set ctime too, but how? */
  200. if (incremental_option)
  201. utimbuf.actime = stat_info->st_atime;
  202. else
  203. utimbuf.actime = start_time;
  204. utimbuf.modtime = stat_info->st_mtime;
  205. if (utime (file_name, &utimbuf) < 0)
  206. utime_error (file_name);
  207. else
  208. {
  209. check_time (file_name, utimbuf.actime);
  210. check_time (file_name, utimbuf.modtime);
  211. }
  212. }
  213. /* Some systems allow non-root users to give files away. Once this
  214. done, it is not possible anymore to change file permissions, so we
  215. have to set permissions prior to possibly giving files away. */
  216. set_mode (file_name, stat_info, cur_info,
  217. invert_permissions, permstatus, typeflag);
  218. }
  219. if (0 < same_owner_option && permstatus != INTERDIR_PERMSTATUS)
  220. {
  221. /* When lchown exists, it should be used to change the attributes of
  222. the symbolic link itself. In this case, a mere chown would change
  223. the attributes of the file the symbolic link is pointing to, and
  224. should be avoided. */
  225. if (typeflag == SYMTYPE)
  226. {
  227. #if HAVE_LCHOWN
  228. if (lchown (file_name, stat_info->st_uid, stat_info->st_gid) < 0)
  229. chown_error_details (file_name,
  230. stat_info->st_uid, stat_info->st_gid);
  231. #endif
  232. }
  233. else
  234. {
  235. if (chown (file_name, stat_info->st_uid, stat_info->st_gid) < 0)
  236. chown_error_details (file_name,
  237. stat_info->st_uid, stat_info->st_gid);
  238. /* On a few systems, and in particular, those allowing to give files
  239. away, changing the owner or group destroys the suid or sgid bits.
  240. So let's attempt setting these bits once more. */
  241. if (stat_info->st_mode & (S_ISUID | S_ISGID | S_ISVTX))
  242. set_mode (file_name, stat_info, 0,
  243. invert_permissions, permstatus, typeflag);
  244. }
  245. }
  246. }
  247. /* Remember to restore stat attributes (owner, group, mode and times)
  248. for the directory FILE_NAME, using information given in *STAT_INFO,
  249. once we stop extracting files into that directory.
  250. If not restoring permissions, remember to invert the
  251. INVERT_PERMISSIONS bits from the file's current permissions.
  252. PERMSTATUS specifies the status of the file's permissions. */
  253. static void
  254. delay_set_stat (char const *file_name, struct stat const *stat_info,
  255. mode_t invert_permissions, enum permstatus permstatus)
  256. {
  257. size_t file_name_len = strlen (file_name);
  258. struct delayed_set_stat *data =
  259. xmalloc (offsetof (struct delayed_set_stat, file_name)
  260. + file_name_len + 1);
  261. data->file_name_len = file_name_len;
  262. strcpy (data->file_name, file_name);
  263. data->invert_permissions = invert_permissions;
  264. data->permstatus = permstatus;
  265. data->after_symlinks = 0;
  266. data->stat_info = *stat_info;
  267. data->next = delayed_set_stat_head;
  268. delayed_set_stat_head = data;
  269. }
  270. /* Update the delayed_set_stat info for an intermediate directory
  271. created on the path to DIR. The intermediate directory turned
  272. out to be the same as this directory, e.g. due to ".." or symbolic
  273. links. *DIR_STAT_INFO is the status of the directory. */
  274. static void
  275. repair_delayed_set_stat (char const *dir,
  276. struct stat const *dir_stat_info)
  277. {
  278. struct delayed_set_stat *data;
  279. for (data = delayed_set_stat_head; data; data = data->next)
  280. {
  281. struct stat st;
  282. if (stat (data->file_name, &st) != 0)
  283. {
  284. stat_error (data->file_name);
  285. return;
  286. }
  287. if (st.st_dev == dir_stat_info->st_dev
  288. && st.st_ino == dir_stat_info->st_ino)
  289. {
  290. data->stat_info = current_stat_info.stat;
  291. data->invert_permissions =
  292. (MODE_RWX & (current_stat_info.stat.st_mode ^ st.st_mode));
  293. data->permstatus = ARCHIVED_PERMSTATUS;
  294. return;
  295. }
  296. }
  297. ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
  298. quotearg_colon (dir)));
  299. }
  300. /* After a file/link/symlink/directory creation has failed, see if
  301. it's because some required directory was not present, and if so,
  302. create all required directories. Return non-zero if a directory
  303. was created. */
  304. static int
  305. make_directories (char *file_name)
  306. {
  307. char *cursor0 = file_name + FILESYSTEM_PREFIX_LEN (file_name);
  308. char *cursor; /* points into path */
  309. int did_something = 0; /* did we do anything yet? */
  310. int mode;
  311. int invert_permissions;
  312. int status;
  313. for (cursor = cursor0; *cursor; cursor++)
  314. {
  315. if (! ISSLASH (*cursor))
  316. continue;
  317. /* Avoid mkdir of empty string, if leading or double '/'. */
  318. if (cursor == cursor0 || ISSLASH (cursor[-1]))
  319. continue;
  320. /* Avoid mkdir where last part of path is "." or "..". */
  321. if (cursor[-1] == '.'
  322. && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
  323. || (cursor[-2] == '.'
  324. && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
  325. continue;
  326. *cursor = '\0'; /* truncate the path there */
  327. mode = MODE_RWX & ~ newdir_umask;
  328. invert_permissions = we_are_root ? 0 : MODE_WXUSR & ~ mode;
  329. status = mkdir (file_name, mode ^ invert_permissions);
  330. if (status == 0)
  331. {
  332. /* Create a struct delayed_set_stat even if
  333. invert_permissions is zero, because
  334. repair_delayed_set_stat may need to update the struct. */
  335. delay_set_stat (file_name,
  336. &current_stat_info.stat /* ignored */,
  337. invert_permissions, INTERDIR_PERMSTATUS);
  338. print_for_mkdir (file_name, cursor - file_name, mode);
  339. did_something = 1;
  340. *cursor = '/';
  341. continue;
  342. }
  343. *cursor = '/';
  344. if (errno == EEXIST)
  345. continue; /* Directory already exists. */
  346. else if ((errno == ENOSYS /* Automounted dirs on Solaris return
  347. this. Reported by Warren Hyde
  348. <Warren.Hyde@motorola.com> */
  349. || ERRNO_IS_EACCES) /* Turbo C mkdir gives a funny errno. */
  350. && access (file_name, W_OK) == 0)
  351. continue;
  352. /* Some other error in the mkdir. We return to the caller. */
  353. break;
  354. }
  355. return did_something; /* tell them to retry if we made one */
  356. }
  357. static bool
  358. file_newer_p (const char *file_name, struct tar_stat_info *tar_stat)
  359. {
  360. struct stat st;
  361. if (stat (file_name, &st))
  362. {
  363. stat_warn (file_name);
  364. return true; /* Be on the safe side */
  365. }
  366. if (!S_ISDIR (st.st_mode)
  367. && st.st_mtime >= tar_stat->stat.st_mtime)
  368. {
  369. return true;
  370. }
  371. return false;
  372. }
  373. /* Prepare to extract a file.
  374. Return zero if extraction should not proceed. */
  375. static int
  376. prepare_to_extract (char const *file_name)
  377. {
  378. if (to_stdout_option)
  379. return 0;
  380. switch (old_files_option)
  381. {
  382. case UNLINK_FIRST_OLD_FILES:
  383. if (!remove_any_file (file_name, recursive_unlink_option)
  384. && errno && errno != ENOENT)
  385. {
  386. unlink_error (file_name);
  387. return 0;
  388. }
  389. break;
  390. case KEEP_NEWER_FILES:
  391. if (file_newer_p (file_name, &current_stat_info))
  392. {
  393. WARN ((0, 0, _("Current `%s' is newer"), file_name));
  394. return 0;
  395. }
  396. break;
  397. default:
  398. break;
  399. }
  400. return 1;
  401. }
  402. /* Attempt repairing what went wrong with the extraction. Delete an
  403. already existing file or create missing intermediate directories.
  404. Return nonzero if we somewhat increased our chances at a successful
  405. extraction. errno is properly restored on zero return. */
  406. static int
  407. maybe_recoverable (char *file_name, int *interdir_made)
  408. {
  409. int e = errno;
  410. if (*interdir_made)
  411. return 0;
  412. switch (errno)
  413. {
  414. case EEXIST:
  415. /* Remove an old file, if the options allow this. */
  416. switch (old_files_option)
  417. {
  418. case KEEP_OLD_FILES:
  419. return 0;
  420. case KEEP_NEWER_FILES:
  421. if (file_newer_p (file_name, &current_stat_info))
  422. {
  423. errno = e;
  424. return 0;
  425. }
  426. /* FALL THROUGH */
  427. case DEFAULT_OLD_FILES:
  428. case NO_OVERWRITE_DIR_OLD_FILES:
  429. case OVERWRITE_OLD_FILES:
  430. {
  431. int r = remove_any_file (file_name, 0);
  432. errno = EEXIST;
  433. return r;
  434. }
  435. case UNLINK_FIRST_OLD_FILES:
  436. break;
  437. }
  438. case ENOENT:
  439. /* Attempt creating missing intermediate directories. */
  440. if (! make_directories (file_name))
  441. {
  442. errno = ENOENT;
  443. return 0;
  444. }
  445. *interdir_made = 1;
  446. return 1;
  447. default:
  448. /* Just say we can't do anything about it... */
  449. return 0;
  450. }
  451. }
  452. /* Fix the statuses of all directories whose statuses need fixing, and
  453. which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
  454. nonzero, do this for all such directories; otherwise, stop at the
  455. first directory that is marked to be fixed up only after delayed
  456. symlinks are applied. */
  457. static void
  458. apply_nonancestor_delayed_set_stat (char const *file_name, bool after_symlinks)
  459. {
  460. size_t file_name_len = strlen (file_name);
  461. bool check_for_renamed_directories = 0;
  462. while (delayed_set_stat_head)
  463. {
  464. struct delayed_set_stat *data = delayed_set_stat_head;
  465. bool skip_this_one = 0;
  466. struct stat st;
  467. struct stat const *cur_info = 0;
  468. check_for_renamed_directories |= data->after_symlinks;
  469. if (after_symlinks < data->after_symlinks
  470. || (data->file_name_len < file_name_len
  471. && file_name[data->file_name_len]
  472. && (ISSLASH (file_name[data->file_name_len])
  473. || ISSLASH (file_name[data->file_name_len - 1]))
  474. && memcmp (file_name, data->file_name, data->file_name_len) == 0))
  475. break;
  476. if (check_for_renamed_directories)
  477. {
  478. cur_info = &st;
  479. if (stat (data->file_name, &st) != 0)
  480. {
  481. stat_error (data->file_name);
  482. skip_this_one = 1;
  483. }
  484. else if (! (st.st_dev == data->stat_info.st_dev
  485. && (st.st_ino == data->stat_info.st_ino)))
  486. {
  487. ERROR ((0, 0,
  488. _("%s: Directory renamed before its status could be extracted"),
  489. quotearg_colon (data->file_name)));
  490. skip_this_one = 1;
  491. }
  492. }
  493. if (! skip_this_one)
  494. set_stat (data->file_name, &data->stat_info, cur_info,
  495. data->invert_permissions, data->permstatus, DIRTYPE);
  496. delayed_set_stat_head = data->next;
  497. free (data);
  498. }
  499. }
  500. /* Extract a file from the archive. */
  501. void
  502. extract_archive (void)
  503. {
  504. union block *data_block;
  505. int fd;
  506. int status;
  507. size_t count;
  508. size_t written;
  509. int openflag;
  510. mode_t mode;
  511. off_t size;
  512. int interdir_made = 0;
  513. char typeflag;
  514. char *file_name;
  515. set_next_block_after (current_header);
  516. decode_header (current_header, &current_stat_info, &current_format, 1);
  517. if (interactive_option && !confirm ("extract", current_stat_info.file_name))
  518. {
  519. skip_member ();
  520. return;
  521. }
  522. /* Print the block from current_header and current_stat. */
  523. if (verbose_option)
  524. print_header (&current_stat_info, -1);
  525. file_name = safer_name_suffix (current_stat_info.file_name, false);
  526. if (strip_name_components)
  527. {
  528. size_t prefix_len = stripped_prefix_len (file_name, strip_name_components);
  529. if (prefix_len == (size_t) -1)
  530. {
  531. skip_member ();
  532. return;
  533. }
  534. file_name += prefix_len;
  535. }
  536. apply_nonancestor_delayed_set_stat (file_name, 0);
  537. /* Take a safety backup of a previously existing file. */
  538. if (backup_option && !to_stdout_option)
  539. if (!maybe_backup_file (file_name, 0))
  540. {
  541. int e = errno;
  542. ERROR ((0, e, _("%s: Was unable to backup this file"),
  543. quotearg_colon (file_name)));
  544. skip_member ();
  545. return;
  546. }
  547. /* Extract the archive entry according to its type. */
  548. /* KLUDGE */
  549. typeflag = sparse_member_p (&current_stat_info) ?
  550. GNUTYPE_SPARSE : current_header->header.typeflag;
  551. switch (typeflag)
  552. {
  553. case GNUTYPE_SPARSE:
  554. /* Fall through. */
  555. case AREGTYPE:
  556. case REGTYPE:
  557. case CONTTYPE:
  558. /* Appears to be a file. But BSD tar uses the convention that a slash
  559. suffix means a directory. */
  560. if (current_stat_info.had_trailing_slash)
  561. goto really_dir;
  562. /* FIXME: deal with protection issues. */
  563. again_file:
  564. openflag = (O_WRONLY | O_BINARY | O_CREAT
  565. | (old_files_option == OVERWRITE_OLD_FILES
  566. ? O_TRUNC
  567. : O_EXCL));
  568. mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
  569. if (to_stdout_option)
  570. {
  571. fd = STDOUT_FILENO;
  572. goto extract_file;
  573. }
  574. if (! prepare_to_extract (file_name))
  575. {
  576. skip_member ();
  577. if (backup_option)
  578. undo_last_backup ();
  579. break;
  580. }
  581. #if O_CTG
  582. /* Contiguous files (on the Masscomp) have to specify the size in
  583. the open call that creates them. */
  584. if (typeflag == CONTTYPE)
  585. fd = open (file_name, openflag | O_CTG, mode, current_stat_info.stat.st_size);
  586. else
  587. fd = open (file_name, openflag, mode);
  588. #else /* not O_CTG */
  589. if (typeflag == CONTTYPE)
  590. {
  591. static int conttype_diagnosed;
  592. if (!conttype_diagnosed)
  593. {
  594. conttype_diagnosed = 1;
  595. WARN ((0, 0, _("Extracting contiguous files as regular files")));
  596. }
  597. }
  598. fd = open (file_name, openflag, mode);
  599. #endif /* not O_CTG */
  600. if (fd < 0)
  601. {
  602. if (maybe_recoverable (file_name, &interdir_made))
  603. goto again_file;
  604. open_error (file_name);
  605. skip_member ();
  606. if (backup_option)
  607. undo_last_backup ();
  608. break;
  609. }
  610. extract_file:
  611. if (current_stat_info.is_sparse)
  612. {
  613. sparse_extract_file (fd, &current_stat_info, &size);
  614. }
  615. else
  616. for (size = current_stat_info.stat.st_size; size > 0; )
  617. {
  618. if (multi_volume_option)
  619. {
  620. assign_string (&save_name, current_stat_info.file_name);
  621. save_totsize = current_stat_info.stat.st_size;
  622. save_sizeleft = size;
  623. }
  624. /* Locate data, determine max length writeable, write it,
  625. block that we have used the data, then check if the write
  626. worked. */
  627. data_block = find_next_block ();
  628. if (! data_block)
  629. {
  630. ERROR ((0, 0, _("Unexpected EOF in archive")));
  631. break; /* FIXME: What happens, then? */
  632. }
  633. written = available_space_after (data_block);
  634. if (written > size)
  635. written = size;
  636. errno = 0;
  637. count = full_write (fd, data_block->buffer, written);
  638. size -= count;
  639. set_next_block_after ((union block *)
  640. (data_block->buffer + written - 1));
  641. if (count != written)
  642. {
  643. write_error_details (file_name, count, written);
  644. break;
  645. }
  646. }
  647. skip_file (size);
  648. if (multi_volume_option)
  649. assign_string (&save_name, 0);
  650. /* If writing to stdout, don't try to do anything to the filename;
  651. it doesn't exist, or we don't want to touch it anyway. */
  652. if (to_stdout_option)
  653. break;
  654. status = close (fd);
  655. if (status < 0)
  656. {
  657. close_error (file_name);
  658. if (backup_option)
  659. undo_last_backup ();
  660. }
  661. set_stat (file_name, &current_stat_info.stat, 0, 0,
  662. (old_files_option == OVERWRITE_OLD_FILES
  663. ? UNKNOWN_PERMSTATUS
  664. : ARCHIVED_PERMSTATUS),
  665. typeflag);
  666. break;
  667. case SYMTYPE:
  668. #ifdef HAVE_SYMLINK
  669. if (! prepare_to_extract (file_name))
  670. break;
  671. if (absolute_names_option
  672. || ! (ISSLASH (current_stat_info.link_name
  673. [FILESYSTEM_PREFIX_LEN (current_stat_info.link_name)])
  674. || contains_dot_dot (current_stat_info.link_name)))
  675. {
  676. while (status = symlink (current_stat_info.link_name, file_name),
  677. status != 0)
  678. if (!maybe_recoverable (file_name, &interdir_made))
  679. break;
  680. if (status == 0)
  681. set_stat (file_name, &current_stat_info.stat, 0, 0, 0, SYMTYPE);
  682. else
  683. symlink_error (current_stat_info.link_name, file_name);
  684. }
  685. else
  686. {
  687. /* This symbolic link is potentially dangerous. Don't
  688. create it now; instead, create a placeholder file, which
  689. will be replaced after other extraction is done. */
  690. struct stat st;
  691. while (fd = open (file_name, O_WRONLY | O_CREAT | O_EXCL, 0),
  692. fd < 0)
  693. if (! maybe_recoverable (file_name, &interdir_made))
  694. break;
  695. status = -1;
  696. if (fd < 0)
  697. open_error (file_name);
  698. else if (fstat (fd, &st) != 0)
  699. {
  700. stat_error (file_name);
  701. close (fd);
  702. }
  703. else if (close (fd) != 0)
  704. close_error (file_name);
  705. else
  706. {
  707. struct delayed_set_stat *h;
  708. struct delayed_symlink *p =
  709. xmalloc (offsetof (struct delayed_symlink, target)
  710. + strlen (current_stat_info.link_name) + 1);
  711. p->next = delayed_symlink_head;
  712. delayed_symlink_head = p;
  713. p->dev = st.st_dev;
  714. p->ino = st.st_ino;
  715. p->mtime = st.st_mtime;
  716. p->uid = current_stat_info.stat.st_uid;
  717. p->gid = current_stat_info.stat.st_gid;
  718. p->sources = xmalloc (offsetof (struct string_list, string)
  719. + strlen (file_name) + 1);
  720. p->sources->next = 0;
  721. strcpy (p->sources->string, file_name);
  722. strcpy (p->target, current_stat_info.link_name);
  723. h = delayed_set_stat_head;
  724. if (h && ! h->after_symlinks
  725. && strncmp (file_name, h->file_name, h->file_name_len) == 0
  726. && ISSLASH (file_name[h->file_name_len])
  727. && (base_name (file_name)
  728. == file_name + h->file_name_len + 1))
  729. {
  730. do
  731. {
  732. h->after_symlinks = 1;
  733. if (stat (h->file_name, &st) != 0)
  734. stat_error (h->file_name);
  735. else
  736. {
  737. h->stat_info.st_dev = st.st_dev;
  738. h->stat_info.st_ino = st.st_ino;
  739. }
  740. }
  741. while ((h = h->next) && ! h->after_symlinks);
  742. }
  743. status = 0;
  744. }
  745. }
  746. if (status != 0 && backup_option)
  747. undo_last_backup ();
  748. break;
  749. #else
  750. {
  751. static int warned_once;
  752. if (!warned_once)
  753. {
  754. warned_once = 1;
  755. WARN ((0, 0,
  756. _("Attempting extraction of symbolic links as hard links")));
  757. }
  758. }
  759. typeflag = LNKTYPE;
  760. /* Fall through. */
  761. #endif
  762. case LNKTYPE:
  763. if (! prepare_to_extract (file_name))
  764. break;
  765. again_link:
  766. {
  767. char const *link_name = safer_name_suffix (current_stat_info.link_name,
  768. true);
  769. struct stat st1, st2;
  770. int e;
  771. /* MSDOS does not implement links. However, djgpp's link() actually
  772. copies the file. */
  773. status = link (link_name, file_name);
  774. if (status == 0)
  775. {
  776. struct delayed_symlink *ds = delayed_symlink_head;
  777. if (ds && stat (link_name, &st1) == 0)
  778. for (; ds; ds = ds->next)
  779. if (ds->dev == st1.st_dev
  780. && ds->ino == st1.st_ino
  781. && ds->mtime == st1.st_mtime)
  782. {
  783. struct string_list *p =
  784. xmalloc (offsetof (struct string_list, string)
  785. + strlen (file_name) + 1);
  786. strcpy (p->string, file_name);
  787. p->next = ds->sources;
  788. ds->sources = p;
  789. break;
  790. }
  791. break;
  792. }
  793. if (maybe_recoverable (file_name, &interdir_made))
  794. goto again_link;
  795. if (incremental_option && errno == EEXIST)
  796. break;
  797. e = errno;
  798. if (stat (link_name, &st1) == 0
  799. && stat (file_name, &st2) == 0
  800. && st1.st_dev == st2.st_dev
  801. && st1.st_ino == st2.st_ino)
  802. break;
  803. link_error (link_name, file_name);
  804. if (backup_option)
  805. undo_last_backup ();
  806. }
  807. break;
  808. #if S_IFCHR
  809. case CHRTYPE:
  810. current_stat_info.stat.st_mode |= S_IFCHR;
  811. goto make_node;
  812. #endif
  813. #if S_IFBLK
  814. case BLKTYPE:
  815. current_stat_info.stat.st_mode |= S_IFBLK;
  816. #endif
  817. #if S_IFCHR || S_IFBLK
  818. make_node:
  819. if (! prepare_to_extract (file_name))
  820. break;
  821. status = mknod (file_name, current_stat_info.stat.st_mode,
  822. current_stat_info.stat.st_rdev);
  823. if (status != 0)
  824. {
  825. if (maybe_recoverable (file_name, &interdir_made))
  826. goto make_node;
  827. mknod_error (file_name);
  828. if (backup_option)
  829. undo_last_backup ();
  830. break;
  831. };
  832. set_stat (file_name, &current_stat_info.stat, 0, 0,
  833. ARCHIVED_PERMSTATUS, typeflag);
  834. break;
  835. #endif
  836. #if HAVE_MKFIFO || defined mkfifo
  837. case FIFOTYPE:
  838. if (! prepare_to_extract (file_name))
  839. break;
  840. while (status = mkfifo (file_name, current_stat_info.stat.st_mode),
  841. status != 0)
  842. if (!maybe_recoverable (file_name, &interdir_made))
  843. break;
  844. if (status == 0)
  845. set_stat (file_name, &current_stat_info.stat, NULL, 0,
  846. ARCHIVED_PERMSTATUS, typeflag);
  847. else
  848. {
  849. mkfifo_error (file_name);
  850. if (backup_option)
  851. undo_last_backup ();
  852. }
  853. break;
  854. #endif
  855. case DIRTYPE:
  856. case GNUTYPE_DUMPDIR:
  857. really_dir:
  858. if (incremental_option)
  859. {
  860. /* Read the entry and delete files that aren't listed in the
  861. archive. */
  862. gnu_restore (file_name);
  863. }
  864. else if (typeflag == GNUTYPE_DUMPDIR)
  865. skip_member ();
  866. mode = ((current_stat_info.stat.st_mode
  867. | (we_are_root ? 0 : MODE_WXUSR))
  868. & MODE_RWX);
  869. status = prepare_to_extract (file_name);
  870. if (status == 0)
  871. break;
  872. if (status < 0)
  873. goto directory_exists;
  874. again_dir:
  875. status = mkdir (file_name, mode);
  876. if (status != 0)
  877. {
  878. if (errno == EEXIST
  879. && (interdir_made
  880. || old_files_option == DEFAULT_OLD_FILES
  881. || old_files_option == OVERWRITE_OLD_FILES))
  882. {
  883. struct stat st;
  884. if (stat (file_name, &st) == 0)
  885. {
  886. if (interdir_made)
  887. {
  888. repair_delayed_set_stat (file_name, &st);
  889. break;
  890. }
  891. if (S_ISDIR (st.st_mode))
  892. {
  893. mode = st.st_mode & ~ current_umask;
  894. goto directory_exists;
  895. }
  896. }
  897. errno = EEXIST;
  898. }
  899. if (maybe_recoverable (file_name, &interdir_made))
  900. goto again_dir;
  901. if (errno != EEXIST)
  902. {
  903. mkdir_error (file_name);
  904. if (backup_option)
  905. undo_last_backup ();
  906. break;
  907. }
  908. }
  909. directory_exists:
  910. if (status == 0
  911. || old_files_option == DEFAULT_OLD_FILES
  912. || old_files_option == OVERWRITE_OLD_FILES)
  913. delay_set_stat (file_name, &current_stat_info.stat,
  914. MODE_RWX & (mode ^ current_stat_info.stat.st_mode),
  915. (status == 0
  916. ? ARCHIVED_PERMSTATUS
  917. : UNKNOWN_PERMSTATUS));
  918. break;
  919. case GNUTYPE_VOLHDR:
  920. if (verbose_option)
  921. fprintf (stdlis, _("Reading %s\n"), quote (current_stat_info.file_name));
  922. break;
  923. case GNUTYPE_NAMES:
  924. extract_mangle ();
  925. break;
  926. case GNUTYPE_MULTIVOL:
  927. ERROR ((0, 0,
  928. _("%s: Cannot extract -- file is continued from another volume"),
  929. quotearg_colon (current_stat_info.file_name)));
  930. skip_member ();
  931. if (backup_option)
  932. undo_last_backup ();
  933. break;
  934. case GNUTYPE_LONGNAME:
  935. case GNUTYPE_LONGLINK:
  936. ERROR ((0, 0, _("Visible long name error")));
  937. skip_member ();
  938. if (backup_option)
  939. undo_last_backup ();
  940. break;
  941. default:
  942. WARN ((0, 0,
  943. _("%s: Unknown file type '%c', extracted as normal file"),
  944. quotearg_colon (file_name), typeflag));
  945. goto again_file;
  946. }
  947. }
  948. /* Extract the symbolic links whose final extraction were delayed. */
  949. static void
  950. apply_delayed_symlinks (void)
  951. {
  952. struct delayed_symlink *ds;
  953. for (ds = delayed_symlink_head; ds; )
  954. {
  955. struct string_list *sources = ds->sources;
  956. char const *valid_source = 0;
  957. for (sources = ds->sources; sources; sources = sources->next)
  958. {
  959. char const *source = sources->string;
  960. struct stat st;
  961. /* Make sure the placeholder file is still there. If not,
  962. don't create a symlink, as the placeholder was probably
  963. removed by a later extraction. */
  964. if (lstat (source, &st) == 0
  965. && st.st_dev == ds->dev
  966. && st.st_ino == ds->ino
  967. && st.st_mtime == ds->mtime)
  968. {
  969. /* Unlink the placeholder, then create a hard link if possible,
  970. a symbolic link otherwise. */
  971. if (unlink (source) != 0)
  972. unlink_error (source);
  973. else if (valid_source && link (valid_source, source) == 0)
  974. ;
  975. else if (symlink (ds->target, source) != 0)
  976. symlink_error (ds->target, source);
  977. else
  978. {
  979. valid_source = source;
  980. st.st_uid = ds->uid;
  981. st.st_gid = ds->gid;
  982. set_stat (source, &st, 0, 0, 0, SYMTYPE);
  983. }
  984. }
  985. }
  986. for (sources = ds->sources; sources; )
  987. {
  988. struct string_list *next = sources->next;
  989. free (sources);
  990. sources = next;
  991. }
  992. {
  993. struct delayed_symlink *next = ds->next;
  994. free (ds);
  995. ds = next;
  996. }
  997. }
  998. delayed_symlink_head = 0;
  999. }
  1000. /* Finish the extraction of an archive. */
  1001. void
  1002. extract_finish (void)
  1003. {
  1004. /* First, fix the status of ordinary directories that need fixing. */
  1005. apply_nonancestor_delayed_set_stat ("", 0);
  1006. /* Then, apply delayed symlinks, so that they don't affect delayed
  1007. directory status-setting for ordinary directories. */
  1008. apply_delayed_symlinks ();
  1009. /* Finally, fix the status of directories that are ancestors
  1010. of delayed symlinks. */
  1011. apply_nonancestor_delayed_set_stat ("", 1);
  1012. }
  1013. void
  1014. fatal_exit (void)
  1015. {
  1016. extract_finish ();
  1017. error (TAREXIT_FAILURE, 0, _("Error is not recoverable: exiting now"));
  1018. abort ();
  1019. }