extract.c 35 KB

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