extract.c 30 KB

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