extract.c 31 KB

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