4
0

extract.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /* Extract files from a tar archive.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
  3. 2001, 2003 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_NAME. 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_name,
  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_name)));
  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. /* Prepare to extract a file.
  358. Return zero if extraction should not proceed. */
  359. static int
  360. prepare_to_extract (char const *file_name, bool directory)
  361. {
  362. if (to_stdout_option)
  363. return 0;
  364. if (old_files_option == UNLINK_FIRST_OLD_FILES
  365. && !remove_any_file (file_name, recursive_unlink_option)
  366. && errno && errno != ENOENT)
  367. {
  368. unlink_error (file_name);
  369. return 0;
  370. }
  371. return 1;
  372. }
  373. /* Attempt repairing what went wrong with the extraction. Delete an
  374. already existing file or create missing intermediate directories.
  375. Return nonzero if we somewhat increased our chances at a successful
  376. extraction. errno is properly restored on zero return. */
  377. static int
  378. maybe_recoverable (char *file_name, int *interdir_made)
  379. {
  380. if (*interdir_made)
  381. return 0;
  382. switch (errno)
  383. {
  384. case EEXIST:
  385. /* Remove an old file, if the options allow this. */
  386. switch (old_files_option)
  387. {
  388. default:
  389. return 0;
  390. case DEFAULT_OLD_FILES:
  391. case NO_OVERWRITE_DIR_OLD_FILES:
  392. case OVERWRITE_OLD_FILES:
  393. {
  394. int r = remove_any_file (file_name, 0);
  395. errno = EEXIST;
  396. return r;
  397. }
  398. }
  399. case ENOENT:
  400. /* Attempt creating missing intermediate directories. */
  401. if (! make_directories (file_name))
  402. {
  403. errno = ENOENT;
  404. return 0;
  405. }
  406. *interdir_made = 1;
  407. return 1;
  408. default:
  409. /* Just say we can't do anything about it... */
  410. return 0;
  411. }
  412. }
  413. /* Fix the statuses of all directories whose statuses need fixing, and
  414. which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
  415. nonzero, do this for all such directories; otherwise, stop at the
  416. first directory that is marked to be fixed up only after delayed
  417. symlinks are applied. */
  418. static void
  419. apply_nonancestor_delayed_set_stat (char const *file_name, bool after_symlinks)
  420. {
  421. size_t file_name_len = strlen (file_name);
  422. bool check_for_renamed_directories = 0;
  423. while (delayed_set_stat_head)
  424. {
  425. struct delayed_set_stat *data = delayed_set_stat_head;
  426. bool skip_this_one = 0;
  427. struct stat st;
  428. struct stat const *cur_info = 0;
  429. check_for_renamed_directories |= data->after_symlinks;
  430. if (after_symlinks < data->after_symlinks
  431. || (data->file_name_len < file_name_len
  432. && file_name[data->file_name_len]
  433. && (ISSLASH (file_name[data->file_name_len])
  434. || ISSLASH (file_name[data->file_name_len - 1]))
  435. && memcmp (file_name, data->file_name, data->file_name_len) == 0))
  436. break;
  437. if (check_for_renamed_directories)
  438. {
  439. cur_info = &st;
  440. if (stat (data->file_name, &st) != 0)
  441. {
  442. stat_error (data->file_name);
  443. skip_this_one = 1;
  444. }
  445. else if (! (st.st_dev == data->stat_info.st_dev
  446. && (st.st_ino == data->stat_info.st_ino)))
  447. {
  448. ERROR ((0, 0,
  449. _("%s: Directory renamed before its status could be extracted"),
  450. quotearg_colon (data->file_name)));
  451. skip_this_one = 1;
  452. }
  453. }
  454. if (! skip_this_one)
  455. set_stat (data->file_name, &data->stat_info, cur_info,
  456. data->invert_permissions, data->permstatus, DIRTYPE);
  457. delayed_set_stat_head = data->next;
  458. free (data);
  459. }
  460. }
  461. /* Extract a file from the archive. */
  462. void
  463. extract_archive (void)
  464. {
  465. union block *data_block;
  466. int fd;
  467. int status;
  468. size_t count;
  469. size_t written;
  470. int openflag;
  471. mode_t mode;
  472. off_t size;
  473. off_t file_size;
  474. int interdir_made = 0;
  475. char typeflag;
  476. char *file_name;
  477. set_next_block_after (current_header);
  478. decode_header (current_header, &current_stat_info, &current_format, 1);
  479. if (interactive_option && !confirm ("extract", current_stat_info.file_name))
  480. {
  481. skip_member ();
  482. return;
  483. }
  484. /* Print the block from current_header and current_stat. */
  485. if (verbose_option)
  486. print_header (&current_stat_info, -1);
  487. file_name = safer_name_suffix (current_stat_info.file_name, false);
  488. if (strip_path_elements)
  489. {
  490. size_t prefix_len = stripped_prefix_len (file_name, strip_path_elements);
  491. if (prefix_len == (size_t) -1)
  492. {
  493. skip_member ();
  494. return;
  495. }
  496. file_name += prefix_len;
  497. }
  498. apply_nonancestor_delayed_set_stat (file_name, 0);
  499. /* Take a safety backup of a previously existing file. */
  500. if (backup_option && !to_stdout_option)
  501. if (!maybe_backup_file (file_name, 0))
  502. {
  503. int e = errno;
  504. ERROR ((0, e, _("%s: Was unable to backup this file"),
  505. quotearg_colon (file_name)));
  506. skip_member ();
  507. return;
  508. }
  509. /* Extract the archive entry according to its type. */
  510. typeflag = current_header->header.typeflag;
  511. /*KLUDGE */
  512. if (current_format == POSIX_FORMAT
  513. && current_stat_info.archive_file_size != current_stat_info.stat.st_size)
  514. typeflag = GNUTYPE_SPARSE;
  515. switch (typeflag)
  516. {
  517. case GNUTYPE_SPARSE:
  518. /* Fall through. */
  519. case AREGTYPE:
  520. case REGTYPE:
  521. case CONTTYPE:
  522. /* Appears to be a file. But BSD tar uses the convention that a slash
  523. suffix means a directory. */
  524. if (current_stat_info.had_trailing_slash)
  525. goto really_dir;
  526. /* FIXME: deal with protection issues. */
  527. again_file:
  528. openflag = (O_WRONLY | O_BINARY | O_CREAT
  529. | (old_files_option == OVERWRITE_OLD_FILES
  530. ? O_TRUNC
  531. : O_EXCL));
  532. mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
  533. if (to_stdout_option)
  534. {
  535. fd = STDOUT_FILENO;
  536. goto extract_file;
  537. }
  538. if (! prepare_to_extract (file_name, 0))
  539. {
  540. skip_member ();
  541. if (backup_option)
  542. undo_last_backup ();
  543. break;
  544. }
  545. #if O_CTG
  546. /* Contiguous files (on the Masscomp) have to specify the size in
  547. the open call that creates them. */
  548. if (typeflag == CONTTYPE)
  549. fd = open (file_name, openflag | O_CTG, mode, current_stat_info.stat.st_size);
  550. else
  551. fd = open (file_name, openflag, mode);
  552. #else /* not O_CTG */
  553. if (typeflag == CONTTYPE)
  554. {
  555. static int conttype_diagnosed;
  556. if (!conttype_diagnosed)
  557. {
  558. conttype_diagnosed = 1;
  559. WARN ((0, 0, _("Extracting contiguous files as regular files")));
  560. }
  561. }
  562. fd = open (file_name, openflag, mode);
  563. #endif /* not O_CTG */
  564. if (fd < 0)
  565. {
  566. if (maybe_recoverable (file_name, &interdir_made))
  567. goto again_file;
  568. open_error (file_name);
  569. skip_member ();
  570. if (backup_option)
  571. undo_last_backup ();
  572. break;
  573. }
  574. extract_file:
  575. if (typeflag == GNUTYPE_SPARSE)
  576. {
  577. sparse_extract_file (fd, &current_stat_info, &size);
  578. }
  579. else
  580. for (size = current_stat_info.stat.st_size; size > 0; )
  581. {
  582. if (multi_volume_option)
  583. {
  584. assign_string (&save_name, current_stat_info.file_name);
  585. save_totsize = current_stat_info.stat.st_size;
  586. save_sizeleft = size;
  587. }
  588. /* Locate data, determine max length writeable, write it,
  589. block that we have used the data, then check if the write
  590. worked. */
  591. data_block = find_next_block ();
  592. if (! data_block)
  593. {
  594. ERROR ((0, 0, _("Unexpected EOF in archive")));
  595. break; /* FIXME: What happens, then? */
  596. }
  597. written = available_space_after (data_block);
  598. if (written > size)
  599. written = size;
  600. errno = 0;
  601. count = full_write (fd, data_block->buffer, written);
  602. size -= count;
  603. set_next_block_after ((union block *)
  604. (data_block->buffer + written - 1));
  605. if (count != written)
  606. {
  607. write_error_details (file_name, count, written);
  608. break;
  609. }
  610. }
  611. skip_file (size);
  612. if (multi_volume_option)
  613. assign_string (&save_name, 0);
  614. /* If writing to stdout, don't try to do anything to the filename;
  615. it doesn't exist, or we don't want to touch it anyway. */
  616. if (to_stdout_option)
  617. break;
  618. status = close (fd);
  619. if (status < 0)
  620. {
  621. close_error (file_name);
  622. if (backup_option)
  623. undo_last_backup ();
  624. }
  625. set_stat (file_name, &current_stat_info.stat, 0, 0,
  626. (old_files_option == OVERWRITE_OLD_FILES
  627. ? UNKNOWN_PERMSTATUS
  628. : ARCHIVED_PERMSTATUS),
  629. typeflag);
  630. break;
  631. case SYMTYPE:
  632. #ifdef HAVE_SYMLINK
  633. if (! prepare_to_extract (file_name, 0))
  634. break;
  635. if (absolute_names_option
  636. || ! (ISSLASH (current_stat_info.link_name
  637. [FILESYSTEM_PREFIX_LEN (current_stat_info.link_name)])
  638. || contains_dot_dot (current_stat_info.link_name)))
  639. {
  640. while (status = symlink (current_stat_info.link_name, file_name),
  641. status != 0)
  642. if (!maybe_recoverable (file_name, &interdir_made))
  643. break;
  644. if (status == 0)
  645. set_stat (file_name, &current_stat_info.stat, 0, 0, 0, SYMTYPE);
  646. else
  647. symlink_error (current_stat_info.link_name, file_name);
  648. }
  649. else
  650. {
  651. /* This symbolic link is potentially dangerous. Don't
  652. create it now; instead, create a placeholder file, which
  653. will be replaced after other extraction is done. */
  654. struct stat st;
  655. while (fd = open (file_name, O_WRONLY | O_CREAT | O_EXCL, 0),
  656. fd < 0)
  657. if (! maybe_recoverable (file_name, &interdir_made))
  658. break;
  659. status = -1;
  660. if (fd < 0)
  661. open_error (file_name);
  662. else if (fstat (fd, &st) != 0)
  663. {
  664. stat_error (file_name);
  665. close (fd);
  666. }
  667. else if (close (fd) != 0)
  668. close_error (file_name);
  669. else
  670. {
  671. struct delayed_set_stat *h;
  672. struct delayed_symlink *p =
  673. xmalloc (offsetof (struct delayed_symlink, target)
  674. + strlen (current_stat_info.link_name) + 1);
  675. p->next = delayed_symlink_head;
  676. delayed_symlink_head = p;
  677. p->dev = st.st_dev;
  678. p->ino = st.st_ino;
  679. p->mtime = st.st_mtime;
  680. p->uid = current_stat_info.stat.st_uid;
  681. p->gid = current_stat_info.stat.st_gid;
  682. p->sources = xmalloc (offsetof (struct string_list, string)
  683. + strlen (file_name) + 1);
  684. p->sources->next = 0;
  685. strcpy (p->sources->string, file_name);
  686. strcpy (p->target, current_stat_info.link_name);
  687. h = delayed_set_stat_head;
  688. if (h && ! h->after_symlinks
  689. && strncmp (file_name, h->file_name, h->file_name_len) == 0
  690. && ISSLASH (file_name[h->file_name_len])
  691. && (base_name (file_name)
  692. == file_name + h->file_name_len + 1))
  693. {
  694. do
  695. {
  696. h->after_symlinks = 1;
  697. if (stat (h->file_name, &st) != 0)
  698. stat_error (h->file_name);
  699. else
  700. {
  701. h->stat_info.st_dev = st.st_dev;
  702. h->stat_info.st_ino = st.st_ino;
  703. }
  704. }
  705. while ((h = h->next) && ! h->after_symlinks);
  706. }
  707. status = 0;
  708. }
  709. }
  710. if (status != 0 && backup_option)
  711. undo_last_backup ();
  712. break;
  713. #else
  714. {
  715. static int warned_once;
  716. if (!warned_once)
  717. {
  718. warned_once = 1;
  719. WARN ((0, 0,
  720. _("Attempting extraction of symbolic links as hard links")));
  721. }
  722. }
  723. typeflag = LNKTYPE;
  724. /* Fall through. */
  725. #endif
  726. case LNKTYPE:
  727. if (! prepare_to_extract (file_name, 0))
  728. break;
  729. again_link:
  730. {
  731. char const *link_name = safer_name_suffix (current_stat_info.link_name,
  732. true);
  733. struct stat st1, st2;
  734. int e;
  735. /* MSDOS does not implement links. However, djgpp's link() actually
  736. copies the file. */
  737. status = link (link_name, file_name);
  738. if (status == 0)
  739. {
  740. struct delayed_symlink *ds = delayed_symlink_head;
  741. if (ds && stat (link_name, &st1) == 0)
  742. for (; ds; ds = ds->next)
  743. if (ds->dev == st1.st_dev
  744. && ds->ino == st1.st_ino
  745. && ds->mtime == st1.st_mtime)
  746. {
  747. struct string_list *p =
  748. xmalloc (offsetof (struct string_list, string)
  749. + strlen (file_name) + 1);
  750. strcpy (p->string, file_name);
  751. p->next = ds->sources;
  752. ds->sources = p;
  753. break;
  754. }
  755. break;
  756. }
  757. if (maybe_recoverable (file_name, &interdir_made))
  758. goto again_link;
  759. if (incremental_option && errno == EEXIST)
  760. break;
  761. e = errno;
  762. if (stat (link_name, &st1) == 0
  763. && stat (file_name, &st2) == 0
  764. && st1.st_dev == st2.st_dev
  765. && st1.st_ino == st2.st_ino)
  766. break;
  767. link_error (link_name, file_name);
  768. if (backup_option)
  769. undo_last_backup ();
  770. }
  771. break;
  772. #if S_IFCHR
  773. case CHRTYPE:
  774. current_stat_info.stat.st_mode |= S_IFCHR;
  775. goto make_node;
  776. #endif
  777. #if S_IFBLK
  778. case BLKTYPE:
  779. current_stat_info.stat.st_mode |= S_IFBLK;
  780. #endif
  781. #if S_IFCHR || S_IFBLK
  782. make_node:
  783. if (! prepare_to_extract (file_name, 0))
  784. break;
  785. status = mknod (file_name, current_stat_info.stat.st_mode,
  786. current_stat_info.stat.st_rdev);
  787. if (status != 0)
  788. {
  789. if (maybe_recoverable (file_name, &interdir_made))
  790. goto make_node;
  791. mknod_error (file_name);
  792. if (backup_option)
  793. undo_last_backup ();
  794. break;
  795. };
  796. set_stat (file_name, &current_stat_info.stat, 0, 0,
  797. ARCHIVED_PERMSTATUS, typeflag);
  798. break;
  799. #endif
  800. #if HAVE_MKFIFO || defined mkfifo
  801. case FIFOTYPE:
  802. if (! prepare_to_extract (file_name, 0))
  803. break;
  804. while (status = mkfifo (file_name, current_stat_info.stat.st_mode),
  805. status != 0)
  806. if (!maybe_recoverable (file_name, &interdir_made))
  807. break;
  808. if (status == 0)
  809. set_stat (file_name, &current_stat_info.stat, NULL, 0,
  810. ARCHIVED_PERMSTATUS, typeflag);
  811. else
  812. {
  813. mkfifo_error (file_name);
  814. if (backup_option)
  815. undo_last_backup ();
  816. }
  817. break;
  818. #endif
  819. case DIRTYPE:
  820. case GNUTYPE_DUMPDIR:
  821. really_dir:
  822. if (incremental_option)
  823. {
  824. /* Read the entry and delete files that aren't listed in the
  825. archive. */
  826. gnu_restore (file_name);
  827. }
  828. else if (typeflag == GNUTYPE_DUMPDIR)
  829. skip_member ();
  830. mode = ((current_stat_info.stat.st_mode
  831. | (we_are_root ? 0 : MODE_WXUSR))
  832. & MODE_RWX);
  833. status = prepare_to_extract (file_name, 1);
  834. if (status == 0)
  835. break;
  836. if (status < 0)
  837. goto directory_exists;
  838. again_dir:
  839. status = mkdir (file_name, mode);
  840. if (status != 0)
  841. {
  842. if (errno == EEXIST
  843. && (interdir_made
  844. || old_files_option == DEFAULT_OLD_FILES
  845. || old_files_option == OVERWRITE_OLD_FILES))
  846. {
  847. struct stat st;
  848. if (stat (file_name, &st) == 0)
  849. {
  850. if (interdir_made)
  851. {
  852. repair_delayed_set_stat (file_name, &st);
  853. break;
  854. }
  855. if (S_ISDIR (st.st_mode))
  856. {
  857. mode = st.st_mode & ~ current_umask;
  858. goto directory_exists;
  859. }
  860. }
  861. errno = EEXIST;
  862. }
  863. if (maybe_recoverable (file_name, &interdir_made))
  864. goto again_dir;
  865. if (errno != EEXIST)
  866. {
  867. mkdir_error (file_name);
  868. if (backup_option)
  869. undo_last_backup ();
  870. break;
  871. }
  872. }
  873. directory_exists:
  874. if (status == 0
  875. || old_files_option == DEFAULT_OLD_FILES
  876. || old_files_option == OVERWRITE_OLD_FILES)
  877. delay_set_stat (file_name, &current_stat_info.stat,
  878. MODE_RWX & (mode ^ current_stat_info.stat.st_mode),
  879. (status == 0
  880. ? ARCHIVED_PERMSTATUS
  881. : UNKNOWN_PERMSTATUS));
  882. break;
  883. case GNUTYPE_VOLHDR:
  884. if (verbose_option)
  885. fprintf (stdlis, _("Reading %s\n"), quote (current_stat_info.file_name));
  886. break;
  887. case GNUTYPE_NAMES:
  888. extract_mangle ();
  889. break;
  890. case GNUTYPE_MULTIVOL:
  891. ERROR ((0, 0,
  892. _("%s: Cannot extract -- file is continued from another volume"),
  893. quotearg_colon (current_stat_info.file_name)));
  894. skip_member ();
  895. if (backup_option)
  896. undo_last_backup ();
  897. break;
  898. case GNUTYPE_LONGNAME:
  899. case GNUTYPE_LONGLINK:
  900. ERROR ((0, 0, _("Visible long name error")));
  901. skip_member ();
  902. if (backup_option)
  903. undo_last_backup ();
  904. break;
  905. default:
  906. WARN ((0, 0,
  907. _("%s: Unknown file type '%c', extracted as normal file"),
  908. quotearg_colon (file_name), typeflag));
  909. goto again_file;
  910. }
  911. }
  912. /* Extract the symbolic links whose final extraction were delayed. */
  913. static void
  914. apply_delayed_symlinks (void)
  915. {
  916. struct delayed_symlink *ds;
  917. for (ds = delayed_symlink_head; ds; )
  918. {
  919. struct string_list *sources = ds->sources;
  920. char const *valid_source = 0;
  921. for (sources = ds->sources; sources; sources = sources->next)
  922. {
  923. char const *source = sources->string;
  924. struct stat st;
  925. /* Make sure the placeholder file is still there. If not,
  926. don't create a symlink, as the placeholder was probably
  927. removed by a later extraction. */
  928. if (lstat (source, &st) == 0
  929. && st.st_dev == ds->dev
  930. && st.st_ino == ds->ino
  931. && st.st_mtime == ds->mtime)
  932. {
  933. /* Unlink the placeholder, then create a hard link if possible,
  934. a symbolic link otherwise. */
  935. if (unlink (source) != 0)
  936. unlink_error (source);
  937. else if (valid_source && link (valid_source, source) == 0)
  938. ;
  939. else if (symlink (ds->target, source) != 0)
  940. symlink_error (ds->target, source);
  941. else
  942. {
  943. valid_source = source;
  944. st.st_uid = ds->uid;
  945. st.st_gid = ds->gid;
  946. set_stat (source, &st, 0, 0, 0, SYMTYPE);
  947. }
  948. }
  949. }
  950. for (sources = ds->sources; sources; )
  951. {
  952. struct string_list *next = sources->next;
  953. free (sources);
  954. sources = next;
  955. }
  956. {
  957. struct delayed_symlink *next = ds->next;
  958. free (ds);
  959. ds = next;
  960. }
  961. }
  962. delayed_symlink_head = 0;
  963. }
  964. /* Finish the extraction of an archive. */
  965. void
  966. extract_finish (void)
  967. {
  968. /* First, fix the status of ordinary directories that need fixing. */
  969. apply_nonancestor_delayed_set_stat ("", 0);
  970. /* Then, apply delayed symlinks, so that they don't affect delayed
  971. directory status-setting for ordinary directories. */
  972. apply_delayed_symlinks ();
  973. /* Finally, fix the status of directories that are ancestors
  974. of delayed symlinks. */
  975. apply_nonancestor_delayed_set_stat ("", 1);
  976. }
  977. void
  978. fatal_exit (void)
  979. {
  980. extract_finish ();
  981. error (TAREXIT_FAILURE, 0, _("Error is not recoverable: exiting now"));
  982. abort ();
  983. }