extract.c 36 KB

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