extract.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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. /* Translate the sparse information on the header, and in any
  414. subsequent extended headers, into an array of structures with true
  415. numbers, as opposed to character strings. Return nonzero if
  416. successful.
  417. This function invalidates current_header. */
  418. bool
  419. fill_in_sparse_array (void)
  420. {
  421. off_t sparse_data_size = current_stat_info.stat.st_size;
  422. off_t file_size = OFF_FROM_HEADER (current_header->oldgnu_header.realsize);
  423. int sparses;
  424. int counter;
  425. union block *h = current_header;
  426. init_sparsearray ();
  427. for (sparses = 0; sparses < SPARSES_IN_OLDGNU_HEADER; sparses++)
  428. {
  429. struct sparse const *s = &h->oldgnu_header.sp[sparses];
  430. off_t offset;
  431. size_t numbytes;
  432. if (s->numbytes[0] == '\0')
  433. break;
  434. sparsearray[sparses].offset = offset = OFF_FROM_HEADER (s->offset);
  435. sparsearray[sparses].numbytes = numbytes =
  436. SIZE_FROM_HEADER (s->numbytes);
  437. sparse_data_size -= numbytes;
  438. if (offset < 0 || file_size < offset + numbytes || sparse_data_size < 0)
  439. goto invalid_member;
  440. }
  441. if (h->oldgnu_header.isextended)
  442. do
  443. {
  444. h = find_next_block ();
  445. if (! h)
  446. {
  447. ERROR ((0, 0, _("Unexpected EOF in archive")));
  448. return 0;
  449. }
  450. for (counter = 0; counter < SPARSES_IN_SPARSE_HEADER; counter++)
  451. {
  452. struct sparse const *s = &h->sparse_header.sp[counter];
  453. off_t offset;
  454. size_t numbytes;
  455. if (s->numbytes[0] == '\0')
  456. break;
  457. if (sparses == sp_array_size)
  458. {
  459. sp_array_size *= 2;
  460. sparsearray =
  461. xrealloc (sparsearray,
  462. sp_array_size * sizeof *sparsearray);
  463. }
  464. sparsearray[sparses].offset = offset =
  465. OFF_FROM_HEADER (s->offset);
  466. sparsearray[sparses].numbytes = numbytes =
  467. SIZE_FROM_HEADER (s->numbytes);
  468. sparse_data_size -= numbytes;
  469. if (offset < 0 || file_size < offset + numbytes
  470. || sparse_data_size < 0)
  471. goto invalid_member;
  472. sparses++;
  473. }
  474. set_next_block_after (h);
  475. } while (h->sparse_header.isextended);
  476. return 1;
  477. invalid_member:
  478. ERROR ((0, 0, _("%s: invalid sparse archive member"),
  479. current_stat_info.file_name));
  480. return 0;
  481. }
  482. static off_t
  483. extract_sparse_file (int fd, char const *name,
  484. off_t sizeleft, off_t file_size)
  485. {
  486. int sparse_ind = 0;
  487. while (sizeleft != 0)
  488. {
  489. size_t written;
  490. size_t count;
  491. union block *data_block = find_next_block ();
  492. if (! data_block)
  493. {
  494. ERROR ((0, 0, _("Unexpected EOF in archive")));
  495. return sizeleft;
  496. }
  497. if (lseek (fd, sparsearray[sparse_ind].offset, SEEK_SET) < 0)
  498. {
  499. seek_error_details (name, sparsearray[sparse_ind].offset);
  500. return sizeleft;
  501. }
  502. written = sparsearray[sparse_ind++].numbytes;
  503. while (written > BLOCKSIZE)
  504. {
  505. count = full_write (fd, data_block->buffer, BLOCKSIZE);
  506. written -= count;
  507. sizeleft -= count;
  508. if (count != BLOCKSIZE)
  509. {
  510. write_error_details (name, count, BLOCKSIZE);
  511. return sizeleft;
  512. }
  513. set_next_block_after (data_block);
  514. data_block = find_next_block ();
  515. if (! data_block)
  516. {
  517. ERROR ((0, 0, _("Unexpected EOF in archive")));
  518. return sizeleft;
  519. }
  520. }
  521. count = full_write (fd, data_block->buffer, written);
  522. sizeleft -= count;
  523. if (count != written)
  524. {
  525. write_error_details (name, count, written);
  526. return sizeleft;
  527. }
  528. set_next_block_after (data_block);
  529. }
  530. if (ftruncate (fd, file_size) != 0)
  531. truncate_error (name);
  532. return 0;
  533. }
  534. /* Fix the statuses of all directories whose statuses need fixing, and
  535. which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
  536. nonzero, do this for all such directories; otherwise, stop at the
  537. first directory that is marked to be fixed up only after delayed
  538. symlinks are applied. */
  539. static void
  540. apply_nonancestor_delayed_set_stat (char const *file_name, bool after_symlinks)
  541. {
  542. size_t file_name_len = strlen (file_name);
  543. bool check_for_renamed_directories = 0;
  544. while (delayed_set_stat_head)
  545. {
  546. struct delayed_set_stat *data = delayed_set_stat_head;
  547. bool skip_this_one = 0;
  548. struct stat st;
  549. struct stat const *cur_info = 0;
  550. check_for_renamed_directories |= data->after_symlinks;
  551. if (after_symlinks < data->after_symlinks
  552. || (data->file_name_len < file_name_len
  553. && file_name[data->file_name_len]
  554. && (ISSLASH (file_name[data->file_name_len])
  555. || ISSLASH (file_name[data->file_name_len - 1]))
  556. && memcmp (file_name, data->file_name, data->file_name_len) == 0))
  557. break;
  558. if (check_for_renamed_directories)
  559. {
  560. cur_info = &st;
  561. if (stat (data->file_name, &st) != 0)
  562. {
  563. stat_error (data->file_name);
  564. skip_this_one = 1;
  565. }
  566. else if (! (st.st_dev == data->stat_info.st_dev
  567. && (st.st_ino == data->stat_info.st_ino)))
  568. {
  569. ERROR ((0, 0,
  570. _("%s: Directory renamed before its status could be extracted"),
  571. quotearg_colon (data->file_name)));
  572. skip_this_one = 1;
  573. }
  574. }
  575. if (! skip_this_one)
  576. set_stat (data->file_name, &data->stat_info, cur_info,
  577. data->invert_permissions, data->permstatus, DIRTYPE);
  578. delayed_set_stat_head = data->next;
  579. free (data);
  580. }
  581. }
  582. /* Extract a file from the archive. */
  583. void
  584. extract_archive (void)
  585. {
  586. union block *data_block;
  587. int fd;
  588. int status;
  589. size_t count;
  590. size_t written;
  591. int openflag;
  592. mode_t mode;
  593. off_t size;
  594. off_t file_size;
  595. int interdir_made = 0;
  596. char typeflag;
  597. char *file_name;
  598. set_next_block_after (current_header);
  599. decode_header (current_header, &current_stat_info, &current_format, 1);
  600. if (interactive_option && !confirm ("extract", current_stat_info.file_name))
  601. {
  602. skip_member ();
  603. return;
  604. }
  605. /* Print the block from current_header and current_stat. */
  606. if (verbose_option)
  607. print_header (-1);
  608. file_name = safer_name_suffix (current_stat_info.file_name, 0);
  609. if (strip_path_elements)
  610. {
  611. size_t prefix_len = stripped_prefix_len (file_name, strip_path_elements);
  612. if (prefix_len == (size_t) -1)
  613. {
  614. skip_member ();
  615. return;
  616. }
  617. file_name += prefix_len;
  618. }
  619. apply_nonancestor_delayed_set_stat (file_name, 0);
  620. /* Take a safety backup of a previously existing file. */
  621. if (backup_option && !to_stdout_option)
  622. if (!maybe_backup_file (file_name, 0))
  623. {
  624. int e = errno;
  625. ERROR ((0, e, _("%s: Was unable to backup this file"),
  626. quotearg_colon (file_name)));
  627. skip_member ();
  628. return;
  629. }
  630. /* Extract the archive entry according to its type. */
  631. typeflag = current_header->header.typeflag;
  632. switch (typeflag)
  633. {
  634. case GNUTYPE_SPARSE:
  635. file_size = OFF_FROM_HEADER (current_header->oldgnu_header.realsize);
  636. if (! fill_in_sparse_array ())
  637. return;
  638. /* Fall through. */
  639. case AREGTYPE:
  640. case REGTYPE:
  641. case CONTTYPE:
  642. /* Appears to be a file. But BSD tar uses the convention that a slash
  643. suffix means a directory. */
  644. if (current_stat_info.had_trailing_slash)
  645. goto really_dir;
  646. /* FIXME: deal with protection issues. */
  647. again_file:
  648. openflag = (O_WRONLY | O_BINARY | O_CREAT
  649. | (old_files_option == OVERWRITE_OLD_FILES
  650. ? O_TRUNC
  651. : O_EXCL));
  652. mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
  653. if (to_stdout_option)
  654. {
  655. fd = STDOUT_FILENO;
  656. goto extract_file;
  657. }
  658. if (! prepare_to_extract (file_name, 0))
  659. {
  660. skip_member ();
  661. if (backup_option)
  662. undo_last_backup ();
  663. break;
  664. }
  665. #if O_CTG
  666. /* Contiguous files (on the Masscomp) have to specify the size in
  667. the open call that creates them. */
  668. if (typeflag == CONTTYPE)
  669. fd = open (file_name, openflag | O_CTG, mode, current_stat_info.stat.st_size);
  670. else
  671. fd = open (file_name, openflag, mode);
  672. #else /* not O_CTG */
  673. if (typeflag == CONTTYPE)
  674. {
  675. static int conttype_diagnosed;
  676. if (!conttype_diagnosed)
  677. {
  678. conttype_diagnosed = 1;
  679. WARN ((0, 0, _("Extracting contiguous files as regular files")));
  680. }
  681. }
  682. fd = open (file_name, openflag, mode);
  683. #endif /* not O_CTG */
  684. if (fd < 0)
  685. {
  686. if (maybe_recoverable (file_name, &interdir_made))
  687. goto again_file;
  688. open_error (file_name);
  689. skip_member ();
  690. if (backup_option)
  691. undo_last_backup ();
  692. break;
  693. }
  694. extract_file:
  695. if (typeflag == GNUTYPE_SPARSE)
  696. {
  697. char *name;
  698. size_t name_length_bis;
  699. /* Kludge alert. NAME is assigned to header.name because
  700. during the extraction, the space that contains the header
  701. will get scribbled on, and the name will get munged, so any
  702. error messages that happen to contain the filename will look
  703. REAL interesting unless we do this. */
  704. name_length_bis = strlen (file_name) + 1;
  705. name = xmalloc (name_length_bis);
  706. memcpy (name, file_name, name_length_bis);
  707. size = extract_sparse_file (fd, name,
  708. current_stat_info.stat.st_size, file_size);
  709. free (sparsearray);
  710. }
  711. else
  712. for (size = current_stat_info.stat.st_size; size > 0; )
  713. {
  714. if (multi_volume_option)
  715. {
  716. assign_string (&save_name, current_stat_info.file_name);
  717. save_totsize = current_stat_info.stat.st_size;
  718. save_sizeleft = size;
  719. }
  720. /* Locate data, determine max length writeable, write it,
  721. block that we have used the data, then check if the write
  722. worked. */
  723. data_block = find_next_block ();
  724. if (! data_block)
  725. {
  726. ERROR ((0, 0, _("Unexpected EOF in archive")));
  727. break; /* FIXME: What happens, then? */
  728. }
  729. written = available_space_after (data_block);
  730. if (written > size)
  731. written = size;
  732. errno = 0;
  733. count = full_write (fd, data_block->buffer, written);
  734. size -= count;
  735. set_next_block_after ((union block *)
  736. (data_block->buffer + written - 1));
  737. if (count != written)
  738. {
  739. write_error_details (file_name, count, written);
  740. break;
  741. }
  742. }
  743. skip_file (size);
  744. if (multi_volume_option)
  745. assign_string (&save_name, 0);
  746. /* If writing to stdout, don't try to do anything to the filename;
  747. it doesn't exist, or we don't want to touch it anyway. */
  748. if (to_stdout_option)
  749. break;
  750. status = close (fd);
  751. if (status < 0)
  752. {
  753. close_error (file_name);
  754. if (backup_option)
  755. undo_last_backup ();
  756. }
  757. set_stat (file_name, &current_stat_info.stat, 0, 0,
  758. (old_files_option == OVERWRITE_OLD_FILES
  759. ? UNKNOWN_PERMSTATUS
  760. : ARCHIVED_PERMSTATUS),
  761. typeflag);
  762. break;
  763. case SYMTYPE:
  764. #ifdef HAVE_SYMLINK
  765. if (! prepare_to_extract (file_name, 0))
  766. break;
  767. if (absolute_names_option
  768. || ! (ISSLASH (current_stat_info.link_name
  769. [FILESYSTEM_PREFIX_LEN (current_stat_info.link_name)])
  770. || contains_dot_dot (current_stat_info.link_name)))
  771. {
  772. while (status = symlink (current_stat_info.link_name, file_name),
  773. status != 0)
  774. if (!maybe_recoverable (file_name, &interdir_made))
  775. break;
  776. if (status == 0)
  777. set_stat (file_name, &current_stat_info.stat, 0, 0, 0, SYMTYPE);
  778. else
  779. symlink_error (current_stat_info.link_name, file_name);
  780. }
  781. else
  782. {
  783. /* This symbolic link is potentially dangerous. Don't
  784. create it now; instead, create a placeholder file, which
  785. will be replaced after other extraction is done. */
  786. struct stat st;
  787. while (fd = open (file_name, O_WRONLY | O_CREAT | O_EXCL, 0),
  788. fd < 0)
  789. if (! maybe_recoverable (file_name, &interdir_made))
  790. break;
  791. status = -1;
  792. if (fd < 0)
  793. open_error (file_name);
  794. else if (fstat (fd, &st) != 0)
  795. {
  796. stat_error (file_name);
  797. close (fd);
  798. }
  799. else if (close (fd) != 0)
  800. close_error (file_name);
  801. else
  802. {
  803. struct delayed_set_stat *h;
  804. struct delayed_symlink *p =
  805. xmalloc (offsetof (struct delayed_symlink, target)
  806. + strlen (current_stat_info.link_name) + 1);
  807. p->next = delayed_symlink_head;
  808. delayed_symlink_head = p;
  809. p->dev = st.st_dev;
  810. p->ino = st.st_ino;
  811. p->mtime = st.st_mtime;
  812. p->uid = current_stat_info.stat.st_uid;
  813. p->gid = current_stat_info.stat.st_gid;
  814. p->sources = xmalloc (offsetof (struct string_list, string)
  815. + strlen (file_name) + 1);
  816. p->sources->next = 0;
  817. strcpy (p->sources->string, file_name);
  818. strcpy (p->target, current_stat_info.link_name);
  819. h = delayed_set_stat_head;
  820. if (h && ! h->after_symlinks
  821. && strncmp (file_name, h->file_name, h->file_name_len) == 0
  822. && ISSLASH (file_name[h->file_name_len])
  823. && (base_name (file_name)
  824. == file_name + h->file_name_len + 1))
  825. {
  826. do
  827. {
  828. h->after_symlinks = 1;
  829. if (stat (h->file_name, &st) != 0)
  830. stat_error (h->file_name);
  831. else
  832. {
  833. h->stat_info.st_dev = st.st_dev;
  834. h->stat_info.st_ino = st.st_ino;
  835. }
  836. }
  837. while ((h = h->next) && ! h->after_symlinks);
  838. }
  839. status = 0;
  840. }
  841. }
  842. if (status != 0 && backup_option)
  843. undo_last_backup ();
  844. break;
  845. #else
  846. {
  847. static int warned_once;
  848. if (!warned_once)
  849. {
  850. warned_once = 1;
  851. WARN ((0, 0,
  852. _("Attempting extraction of symbolic links as hard links")));
  853. }
  854. }
  855. typeflag = LNKTYPE;
  856. /* Fall through. */
  857. #endif
  858. case LNKTYPE:
  859. if (! prepare_to_extract (file_name, 0))
  860. break;
  861. again_link:
  862. {
  863. char const *link_name = safer_name_suffix (current_stat_info.link_name, 1);
  864. struct stat st1, st2;
  865. int e;
  866. /* MSDOS does not implement links. However, djgpp's link() actually
  867. copies the file. */
  868. status = link (link_name, file_name);
  869. if (status == 0)
  870. {
  871. struct delayed_symlink *ds = delayed_symlink_head;
  872. if (ds && stat (link_name, &st1) == 0)
  873. for (; ds; ds = ds->next)
  874. if (ds->dev == st1.st_dev
  875. && ds->ino == st1.st_ino
  876. && ds->mtime == st1.st_mtime)
  877. {
  878. struct string_list *p =
  879. xmalloc (offsetof (struct string_list, string)
  880. + strlen (file_name) + 1);
  881. strcpy (p->string, file_name);
  882. p->next = ds->sources;
  883. ds->sources = p;
  884. break;
  885. }
  886. break;
  887. }
  888. if (maybe_recoverable (file_name, &interdir_made))
  889. goto again_link;
  890. if (incremental_option && errno == EEXIST)
  891. break;
  892. e = errno;
  893. if (stat (link_name, &st1) == 0
  894. && stat (file_name, &st2) == 0
  895. && st1.st_dev == st2.st_dev
  896. && st1.st_ino == st2.st_ino)
  897. break;
  898. link_error (link_name, file_name);
  899. if (backup_option)
  900. undo_last_backup ();
  901. }
  902. break;
  903. #if S_IFCHR
  904. case CHRTYPE:
  905. current_stat_info.stat.st_mode |= S_IFCHR;
  906. goto make_node;
  907. #endif
  908. #if S_IFBLK
  909. case BLKTYPE:
  910. current_stat_info.stat.st_mode |= S_IFBLK;
  911. #endif
  912. #if S_IFCHR || S_IFBLK
  913. make_node:
  914. if (! prepare_to_extract (file_name, 0))
  915. break;
  916. status = mknod (file_name, current_stat_info.stat.st_mode,
  917. current_stat_info.stat.st_rdev);
  918. if (status != 0)
  919. {
  920. if (maybe_recoverable (file_name, &interdir_made))
  921. goto make_node;
  922. mknod_error (file_name);
  923. if (backup_option)
  924. undo_last_backup ();
  925. break;
  926. };
  927. set_stat (file_name, &current_stat_info.stat, 0, 0,
  928. ARCHIVED_PERMSTATUS, typeflag);
  929. break;
  930. #endif
  931. #if HAVE_MKFIFO || defined mkfifo
  932. case FIFOTYPE:
  933. if (! prepare_to_extract (file_name, 0))
  934. break;
  935. while (status = mkfifo (file_name, current_stat_info.stat.st_mode),
  936. status != 0)
  937. if (!maybe_recoverable (file_name, &interdir_made))
  938. break;
  939. if (status == 0)
  940. set_stat (file_name, &current_stat_info.stat, NULL, 0,
  941. ARCHIVED_PERMSTATUS, typeflag);
  942. else
  943. {
  944. mkfifo_error (file_name);
  945. if (backup_option)
  946. undo_last_backup ();
  947. }
  948. break;
  949. #endif
  950. case DIRTYPE:
  951. case GNUTYPE_DUMPDIR:
  952. really_dir:
  953. if (incremental_option)
  954. {
  955. /* Read the entry and delete files that aren't listed in the
  956. archive. */
  957. gnu_restore (file_name);
  958. }
  959. else if (typeflag == GNUTYPE_DUMPDIR)
  960. skip_member ();
  961. mode = ((current_stat_info.stat.st_mode
  962. | (we_are_root ? 0 : MODE_WXUSR))
  963. & MODE_RWX);
  964. status = prepare_to_extract (file_name, 1);
  965. if (status == 0)
  966. break;
  967. if (status < 0)
  968. goto directory_exists;
  969. again_dir:
  970. status = mkdir (file_name, mode);
  971. if (status != 0)
  972. {
  973. if (errno == EEXIST
  974. && (interdir_made
  975. || old_files_option == DEFAULT_OLD_FILES
  976. || old_files_option == OVERWRITE_OLD_FILES))
  977. {
  978. struct stat st;
  979. if (stat (file_name, &st) == 0)
  980. {
  981. if (interdir_made)
  982. {
  983. repair_delayed_set_stat (file_name, &st);
  984. break;
  985. }
  986. if (S_ISDIR (st.st_mode))
  987. {
  988. mode = st.st_mode & ~ current_umask;
  989. goto directory_exists;
  990. }
  991. }
  992. errno = EEXIST;
  993. }
  994. if (maybe_recoverable (file_name, &interdir_made))
  995. goto again_dir;
  996. if (errno != EEXIST)
  997. {
  998. mkdir_error (file_name);
  999. if (backup_option)
  1000. undo_last_backup ();
  1001. break;
  1002. }
  1003. }
  1004. directory_exists:
  1005. if (status == 0
  1006. || old_files_option == DEFAULT_OLD_FILES
  1007. || old_files_option == OVERWRITE_OLD_FILES)
  1008. delay_set_stat (file_name, &current_stat_info.stat,
  1009. MODE_RWX & (mode ^ current_stat_info.stat.st_mode),
  1010. (status == 0
  1011. ? ARCHIVED_PERMSTATUS
  1012. : UNKNOWN_PERMSTATUS));
  1013. break;
  1014. case GNUTYPE_VOLHDR:
  1015. if (verbose_option)
  1016. fprintf (stdlis, _("Reading %s\n"), quote (current_stat_info.file_name));
  1017. break;
  1018. case GNUTYPE_NAMES:
  1019. extract_mangle ();
  1020. break;
  1021. case GNUTYPE_MULTIVOL:
  1022. ERROR ((0, 0,
  1023. _("%s: Cannot extract -- file is continued from another volume"),
  1024. quotearg_colon (current_stat_info.file_name)));
  1025. skip_member ();
  1026. if (backup_option)
  1027. undo_last_backup ();
  1028. break;
  1029. case GNUTYPE_LONGNAME:
  1030. case GNUTYPE_LONGLINK:
  1031. ERROR ((0, 0, _("Visible long name error")));
  1032. skip_member ();
  1033. if (backup_option)
  1034. undo_last_backup ();
  1035. break;
  1036. default:
  1037. WARN ((0, 0,
  1038. _("%s: Unknown file type '%c', extracted as normal file"),
  1039. quotearg_colon (file_name), typeflag));
  1040. goto again_file;
  1041. }
  1042. }
  1043. /* Extract the symbolic links whose final extraction were delayed. */
  1044. static void
  1045. apply_delayed_symlinks (void)
  1046. {
  1047. struct delayed_symlink *ds;
  1048. for (ds = delayed_symlink_head; ds; )
  1049. {
  1050. struct string_list *sources = ds->sources;
  1051. char const *valid_source = 0;
  1052. for (sources = ds->sources; sources; sources = sources->next)
  1053. {
  1054. char const *source = sources->string;
  1055. struct stat st;
  1056. /* Make sure the placeholder file is still there. If not,
  1057. don't create a symlink, as the placeholder was probably
  1058. removed by a later extraction. */
  1059. if (lstat (source, &st) == 0
  1060. && st.st_dev == ds->dev
  1061. && st.st_ino == ds->ino
  1062. && st.st_mtime == ds->mtime)
  1063. {
  1064. /* Unlink the placeholder, then create a hard link if possible,
  1065. a symbolic link otherwise. */
  1066. if (unlink (source) != 0)
  1067. unlink_error (source);
  1068. else if (valid_source && link (valid_source, source) == 0)
  1069. ;
  1070. else if (symlink (ds->target, source) != 0)
  1071. symlink_error (ds->target, source);
  1072. else
  1073. {
  1074. valid_source = source;
  1075. st.st_uid = ds->uid;
  1076. st.st_gid = ds->gid;
  1077. set_stat (source, &st, 0, 0, 0, SYMTYPE);
  1078. }
  1079. }
  1080. }
  1081. for (sources = ds->sources; sources; )
  1082. {
  1083. struct string_list *next = sources->next;
  1084. free (sources);
  1085. sources = next;
  1086. }
  1087. {
  1088. struct delayed_symlink *next = ds->next;
  1089. free (ds);
  1090. ds = next;
  1091. }
  1092. }
  1093. delayed_symlink_head = 0;
  1094. }
  1095. /* Finish the extraction of an archive. */
  1096. void
  1097. extract_finish (void)
  1098. {
  1099. /* First, fix the status of ordinary directories that need fixing. */
  1100. apply_nonancestor_delayed_set_stat ("", 0);
  1101. /* Then, apply delayed symlinks, so that they don't affect delayed
  1102. directory status-setting for ordinary directories. */
  1103. apply_delayed_symlinks ();
  1104. /* Finally, fix the status of directories that are ancestors
  1105. of delayed symlinks. */
  1106. apply_nonancestor_delayed_set_stat ("", 1);
  1107. }
  1108. void
  1109. fatal_exit (void)
  1110. {
  1111. extract_finish ();
  1112. error (TAREXIT_FAILURE, 0, _("Error is not recoverable: exiting now"));
  1113. abort ();
  1114. }