extract.c 34 KB

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