extract.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. /* Extract files from a tar archive.
  2. Copyright 1988, 1992-1994, 1996-2001, 2003-2007, 2010, 2012-2013
  3. Free Software Foundation, Inc.
  4. This file is part of GNU tar.
  5. GNU tar is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. GNU tar is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. Written by John Gilmore, on 1985-11-19. */
  16. #include <system.h>
  17. #include <quotearg.h>
  18. #include <errno.h>
  19. #include <priv-set.h>
  20. #include <root-uid.h>
  21. #include <utimens.h>
  22. #include "common.h"
  23. static bool we_are_root; /* true if our effective uid == 0 */
  24. static mode_t newdir_umask; /* umask when creating new directories */
  25. static mode_t current_umask; /* current umask (which is set to 0 if -p) */
  26. #define ALL_MODE_BITS ((mode_t) ~ (mode_t) 0)
  27. #if ! HAVE_FCHMOD && ! defined fchmod
  28. # define fchmod(fd, mode) (errno = ENOSYS, -1)
  29. #endif
  30. #if ! HAVE_FCHOWN && ! defined fchown
  31. # define fchown(fd, uid, gid) (errno = ENOSYS, -1)
  32. #endif
  33. /* Return true if an error number ERR means the system call is
  34. supported in this case. */
  35. static bool
  36. implemented (int err)
  37. {
  38. return ! (err == ENOSYS
  39. || err == ENOTSUP
  40. || (EOPNOTSUPP != ENOTSUP && err == EOPNOTSUPP));
  41. }
  42. /* List of directories whose statuses we need to extract after we've
  43. finished extracting their subsidiary files. If you consider each
  44. contiguous subsequence of elements of the form [D]?[^D]*, where [D]
  45. represents an element where AFTER_LINKS is nonzero and [^D]
  46. represents an element where AFTER_LINKS is zero, then the head
  47. of the subsequence has the longest name, and each non-head element
  48. in the prefix is an ancestor (in the directory hierarchy) of the
  49. preceding element. */
  50. struct delayed_set_stat
  51. {
  52. /* Next directory in list. */
  53. struct delayed_set_stat *next;
  54. /* Metadata for this directory. */
  55. dev_t dev;
  56. ino_t ino;
  57. mode_t mode; /* The desired mode is MODE & ~ current_umask. */
  58. uid_t uid;
  59. gid_t gid;
  60. struct timespec atime;
  61. struct timespec mtime;
  62. /* An estimate of the directory's current mode, along with a mask
  63. specifying which bits of this estimate are known to be correct.
  64. If CURRENT_MODE_MASK is zero, CURRENT_MODE's value doesn't
  65. matter. */
  66. mode_t current_mode;
  67. mode_t current_mode_mask;
  68. /* This directory is an intermediate directory that was created
  69. as an ancestor of some other directory; it was not mentioned
  70. in the archive, so do not set its uid, gid, atime, or mtime,
  71. and don't alter its mode outside of MODE_RWX. */
  72. bool interdir;
  73. /* Whether symbolic links should be followed when accessing the
  74. directory. */
  75. int atflag;
  76. /* Do not set the status of this directory until after delayed
  77. links are created. */
  78. bool after_links;
  79. /* Directory that the name is relative to. */
  80. int change_dir;
  81. /* extended attributes*/
  82. char *cntx_name;
  83. char *acls_a_ptr;
  84. size_t acls_a_len;
  85. char *acls_d_ptr;
  86. size_t acls_d_len;
  87. size_t xattr_map_size;
  88. struct xattr_array *xattr_map;
  89. /* Length and contents of name. */
  90. size_t file_name_len;
  91. char file_name[1];
  92. };
  93. static struct delayed_set_stat *delayed_set_stat_head;
  94. /* List of links whose creation we have delayed. */
  95. struct delayed_link
  96. {
  97. /* The next delayed link in the list. */
  98. struct delayed_link *next;
  99. /* The device, inode number and birthtime of the placeholder.
  100. birthtime.tv_nsec is negative if the birthtime is not available.
  101. Don't use mtime as this would allow for false matches if some
  102. other process removes the placeholder. Don't use ctime as
  103. this would cause race conditions and other screwups, e.g.,
  104. when restoring hard-linked symlinks. */
  105. dev_t dev;
  106. ino_t ino;
  107. struct timespec birthtime;
  108. /* True if the link is symbolic. */
  109. bool is_symlink;
  110. /* The desired metadata, valid only the link is symbolic. */
  111. mode_t mode;
  112. uid_t uid;
  113. gid_t gid;
  114. struct timespec atime;
  115. struct timespec mtime;
  116. /* The directory that the sources and target are relative to. */
  117. int change_dir;
  118. /* A list of sources for this link. The sources are all to be
  119. hard-linked together. */
  120. struct string_list *sources;
  121. /* SELinux context */
  122. char *cntx_name;
  123. /* ACLs */
  124. char *acls_a_ptr;
  125. size_t acls_a_len;
  126. char *acls_d_ptr;
  127. size_t acls_d_len;
  128. size_t xattr_map_size;
  129. struct xattr_array *xattr_map;
  130. /* The desired target of the desired link. */
  131. char target[1];
  132. };
  133. static struct delayed_link *delayed_link_head;
  134. struct string_list
  135. {
  136. struct string_list *next;
  137. char string[1];
  138. };
  139. /* Set up to extract files. */
  140. void
  141. extr_init (void)
  142. {
  143. we_are_root = geteuid () == ROOT_UID;
  144. same_permissions_option += we_are_root;
  145. same_owner_option += we_are_root;
  146. /* Option -p clears the kernel umask, so it does not affect proper
  147. restoration of file permissions. New intermediate directories will
  148. comply with umask at start of program. */
  149. newdir_umask = umask (0);
  150. if (0 < same_permissions_option)
  151. current_umask = 0;
  152. else
  153. {
  154. umask (newdir_umask); /* restore the kernel umask */
  155. current_umask = newdir_umask;
  156. }
  157. }
  158. /* Use fchmod if possible, fchmodat otherwise. */
  159. static int
  160. fd_chmod (int fd, char const *file, mode_t mode, int atflag)
  161. {
  162. if (0 <= fd)
  163. {
  164. int result = fchmod (fd, mode);
  165. if (result == 0 || implemented (errno))
  166. return result;
  167. }
  168. return fchmodat (chdir_fd, file, mode, atflag);
  169. }
  170. /* Use fchown if possible, fchownat otherwise. */
  171. static int
  172. fd_chown (int fd, char const *file, uid_t uid, gid_t gid, int atflag)
  173. {
  174. if (0 <= fd)
  175. {
  176. int result = fchown (fd, uid, gid);
  177. if (result == 0 || implemented (errno))
  178. return result;
  179. }
  180. return fchownat (chdir_fd, file, uid, gid, atflag);
  181. }
  182. /* Use fstat if possible, fstatat otherwise. */
  183. static int
  184. fd_stat (int fd, char const *file, struct stat *st, int atflag)
  185. {
  186. return (0 <= fd
  187. ? fstat (fd, st)
  188. : fstatat (chdir_fd, file, st, atflag));
  189. }
  190. /* Set the mode for FILE_NAME to MODE.
  191. MODE_MASK specifies the bits of MODE that we care about;
  192. thus if MODE_MASK is zero, do nothing.
  193. If FD is nonnegative, it is a file descriptor for the file.
  194. CURRENT_MODE and CURRENT_MODE_MASK specify information known about
  195. the file's current mode, using the style of struct delayed_set_stat.
  196. TYPEFLAG specifies the type of the file.
  197. ATFLAG specifies the flag to use when statting the file. */
  198. static void
  199. set_mode (char const *file_name,
  200. mode_t mode, mode_t mode_mask, int fd,
  201. mode_t current_mode, mode_t current_mode_mask,
  202. char typeflag, int atflag)
  203. {
  204. if (((current_mode ^ mode) | ~ current_mode_mask) & mode_mask)
  205. {
  206. if (MODE_ALL & ~ mode_mask & ~ current_mode_mask)
  207. {
  208. struct stat st;
  209. if (fd_stat (fd, file_name, &st, atflag) != 0)
  210. {
  211. stat_error (file_name);
  212. return;
  213. }
  214. current_mode = st.st_mode;
  215. }
  216. current_mode &= MODE_ALL;
  217. mode = (current_mode & ~ mode_mask) | (mode & mode_mask);
  218. if (current_mode != mode)
  219. {
  220. int chmod_errno =
  221. fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
  222. /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
  223. setuid-root files would otherwise be a backdoor. See
  224. http://opensolaris.org/jive/thread.jspa?threadID=95826
  225. (2009-09-03). */
  226. if (chmod_errno == EPERM && (mode & S_ISUID)
  227. && priv_set_restore_linkdir () == 0)
  228. {
  229. chmod_errno =
  230. fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
  231. priv_set_remove_linkdir ();
  232. }
  233. /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
  234. returns ENOTSUP even when operating on non-symlinks, try
  235. again with the flag disabled if it does not appear to be
  236. supported and if the file is not a symlink. This
  237. introduces a race, alas. */
  238. if (atflag && typeflag != SYMTYPE && ! implemented (chmod_errno))
  239. chmod_errno = fd_chmod (fd, file_name, mode, 0) == 0 ? 0 : errno;
  240. if (chmod_errno
  241. && (typeflag != SYMTYPE || implemented (chmod_errno)))
  242. {
  243. errno = chmod_errno;
  244. chmod_error_details (file_name, mode);
  245. }
  246. }
  247. }
  248. }
  249. /* Check time after successfully setting FILE_NAME's time stamp to T. */
  250. static void
  251. check_time (char const *file_name, struct timespec t)
  252. {
  253. if (t.tv_sec < 0)
  254. WARNOPT (WARN_TIMESTAMP,
  255. (0, 0, _("%s: implausibly old time stamp %s"),
  256. file_name, tartime (t, true)));
  257. else if (timespec_cmp (volume_start_time, t) < 0)
  258. {
  259. struct timespec now;
  260. gettime (&now);
  261. if (timespec_cmp (now, t) < 0)
  262. {
  263. char buf[TIMESPEC_STRSIZE_BOUND];
  264. struct timespec diff;
  265. diff.tv_sec = t.tv_sec - now.tv_sec;
  266. diff.tv_nsec = t.tv_nsec - now.tv_nsec;
  267. if (diff.tv_nsec < 0)
  268. {
  269. diff.tv_nsec += BILLION;
  270. diff.tv_sec--;
  271. }
  272. WARNOPT (WARN_TIMESTAMP,
  273. (0, 0, _("%s: time stamp %s is %s s in the future"),
  274. file_name, tartime (t, true), code_timespec (diff, buf)));
  275. }
  276. }
  277. }
  278. /* Restore stat attributes (owner, group, mode and times) for
  279. FILE_NAME, using information given in *ST.
  280. If FD is nonnegative, it is a file descriptor for the file.
  281. CURRENT_MODE and CURRENT_MODE_MASK specify information known about
  282. the file's current mode, using the style of struct delayed_set_stat.
  283. TYPEFLAG specifies the type of the file.
  284. If INTERDIR, this is an intermediate directory.
  285. ATFLAG specifies the flag to use when statting the file. */
  286. static void
  287. set_stat (char const *file_name,
  288. struct tar_stat_info const *st,
  289. int fd, mode_t current_mode, mode_t current_mode_mask,
  290. char typeflag, bool interdir, int atflag)
  291. {
  292. /* Do the utime before the chmod because some versions of utime are
  293. broken and trash the modes of the file. */
  294. if (! touch_option && ! interdir)
  295. {
  296. struct timespec ts[2];
  297. if (incremental_option)
  298. ts[0] = st->atime;
  299. else
  300. ts[0].tv_nsec = UTIME_OMIT;
  301. ts[1] = st->mtime;
  302. if (fdutimensat (fd, chdir_fd, file_name, ts, atflag) == 0)
  303. {
  304. if (incremental_option)
  305. check_time (file_name, ts[0]);
  306. check_time (file_name, ts[1]);
  307. }
  308. else if (typeflag != SYMTYPE || implemented (errno))
  309. utime_error (file_name);
  310. }
  311. if (0 < same_owner_option && ! interdir)
  312. {
  313. /* Some systems allow non-root users to give files away. Once this
  314. done, it is not possible anymore to change file permissions.
  315. However, setting file permissions now would be incorrect, since
  316. they would apply to the wrong user, and there would be a race
  317. condition. So, don't use systems that allow non-root users to
  318. give files away. */
  319. uid_t uid = st->stat.st_uid;
  320. gid_t gid = st->stat.st_gid;
  321. if (fd_chown (fd, file_name, uid, gid, atflag) == 0)
  322. {
  323. /* Changing the owner can clear st_mode bits in some cases. */
  324. if ((current_mode | ~ current_mode_mask) & S_IXUGO)
  325. current_mode_mask &= ~ (current_mode & (S_ISUID | S_ISGID));
  326. }
  327. else if (typeflag != SYMTYPE || implemented (errno))
  328. chown_error_details (file_name, uid, gid);
  329. }
  330. set_mode (file_name,
  331. st->stat.st_mode & ~ current_umask,
  332. 0 < same_permissions_option && ! interdir ? MODE_ALL : MODE_RWX,
  333. fd, current_mode, current_mode_mask, typeflag, atflag);
  334. /* these three calls must be done *after* fd_chown() call because fd_chown
  335. causes that linux capabilities becomes cleared. */
  336. xattrs_xattrs_set (st, file_name, typeflag, 1);
  337. xattrs_acls_set (st, file_name, typeflag);
  338. xattrs_selinux_set (st, file_name, typeflag);
  339. }
  340. /* For each entry H in the leading prefix of entries in HEAD that do
  341. not have after_links marked, mark H and fill in its dev and ino
  342. members. Assume HEAD && ! HEAD->after_links. */
  343. static void
  344. mark_after_links (struct delayed_set_stat *head)
  345. {
  346. struct delayed_set_stat *h = head;
  347. do
  348. {
  349. struct stat st;
  350. h->after_links = 1;
  351. if (deref_stat (h->file_name, &st) != 0)
  352. stat_error (h->file_name);
  353. else
  354. {
  355. h->dev = st.st_dev;
  356. h->ino = st.st_ino;
  357. }
  358. }
  359. while ((h = h->next) && ! h->after_links);
  360. }
  361. /* Remember to restore stat attributes (owner, group, mode and times)
  362. for the directory FILE_NAME, using information given in *ST,
  363. once we stop extracting files into that directory.
  364. If ST is null, merely create a placeholder node for an intermediate
  365. directory that was created by make_directories.
  366. NOTICE: this works only if the archive has usual member order, i.e.
  367. directory, then the files in that directory. Incremental archive have
  368. somewhat reversed order: first go subdirectories, then all other
  369. members. To help cope with this case the variable
  370. delay_directory_restore_option is set by prepare_to_extract.
  371. If an archive was explicitely created so that its member order is
  372. reversed, some directory timestamps can be restored incorrectly,
  373. e.g.:
  374. tar --no-recursion -cf archive dir dir/file1 foo dir/file2
  375. */
  376. static void
  377. delay_set_stat (char const *file_name, struct tar_stat_info const *st,
  378. mode_t current_mode, mode_t current_mode_mask,
  379. mode_t mode, int atflag)
  380. {
  381. size_t file_name_len = strlen (file_name);
  382. struct delayed_set_stat *data =
  383. xmalloc (offsetof (struct delayed_set_stat, file_name)
  384. + file_name_len + 1);
  385. data->next = delayed_set_stat_head;
  386. data->mode = mode;
  387. if (st)
  388. {
  389. data->dev = st->stat.st_dev;
  390. data->ino = st->stat.st_ino;
  391. data->uid = st->stat.st_uid;
  392. data->gid = st->stat.st_gid;
  393. data->atime = st->atime;
  394. data->mtime = st->mtime;
  395. }
  396. data->file_name_len = file_name_len;
  397. data->current_mode = current_mode;
  398. data->current_mode_mask = current_mode_mask;
  399. data->interdir = ! st;
  400. data->atflag = atflag;
  401. data->after_links = 0;
  402. data->change_dir = chdir_current;
  403. data->cntx_name = NULL;
  404. if (st)
  405. assign_string (&data->cntx_name, st->cntx_name);
  406. if (st && st->acls_a_ptr)
  407. {
  408. data->acls_a_ptr = xmemdup (st->acls_a_ptr, st->acls_a_len + 1);
  409. data->acls_a_len = st->acls_a_len;
  410. }
  411. else
  412. {
  413. data->acls_a_ptr = NULL;
  414. data->acls_a_len = 0;
  415. }
  416. if (st && st->acls_d_ptr)
  417. {
  418. data->acls_d_ptr = xmemdup (st->acls_d_ptr, st->acls_d_len + 1);
  419. data->acls_d_len = st->acls_d_len;
  420. }
  421. else
  422. {
  423. data->acls_d_ptr = NULL;
  424. data->acls_d_len = 0;
  425. }
  426. if (st)
  427. xheader_xattr_copy (st, &data->xattr_map, &data->xattr_map_size);
  428. else
  429. {
  430. data->xattr_map = NULL;
  431. data->xattr_map_size = 0;
  432. }
  433. strcpy (data->file_name, file_name);
  434. delayed_set_stat_head = data;
  435. if (must_be_dot_or_slash (file_name))
  436. mark_after_links (data);
  437. }
  438. /* Update the delayed_set_stat info for an intermediate directory
  439. created within the file name of DIR. The intermediate directory turned
  440. out to be the same as this directory, e.g. due to ".." or symbolic
  441. links. *DIR_STAT_INFO is the status of the directory. */
  442. static void
  443. repair_delayed_set_stat (char const *dir,
  444. struct stat const *dir_stat_info)
  445. {
  446. struct delayed_set_stat *data;
  447. for (data = delayed_set_stat_head; data; data = data->next)
  448. {
  449. struct stat st;
  450. if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
  451. {
  452. stat_error (data->file_name);
  453. return;
  454. }
  455. if (st.st_dev == dir_stat_info->st_dev
  456. && st.st_ino == dir_stat_info->st_ino)
  457. {
  458. data->dev = current_stat_info.stat.st_dev;
  459. data->ino = current_stat_info.stat.st_ino;
  460. data->mode = current_stat_info.stat.st_mode;
  461. data->uid = current_stat_info.stat.st_uid;
  462. data->gid = current_stat_info.stat.st_gid;
  463. data->atime = current_stat_info.atime;
  464. data->mtime = current_stat_info.mtime;
  465. data->current_mode = st.st_mode;
  466. data->current_mode_mask = ALL_MODE_BITS;
  467. data->interdir = false;
  468. return;
  469. }
  470. }
  471. ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
  472. quotearg_colon (dir)));
  473. }
  474. /* After a file/link/directory creation has failed, see if
  475. it's because some required directory was not present, and if so,
  476. create all required directories. Return zero if all the required
  477. directories were created, nonzero (issuing a diagnostic) otherwise.
  478. Set *INTERDIR_MADE if at least one directory was created. */
  479. static int
  480. make_directories (char *file_name, bool *interdir_made)
  481. {
  482. char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
  483. char *cursor; /* points into the file name */
  484. for (cursor = cursor0; *cursor; cursor++)
  485. {
  486. mode_t mode;
  487. mode_t desired_mode;
  488. int status;
  489. if (! ISSLASH (*cursor))
  490. continue;
  491. /* Avoid mkdir of empty string, if leading or double '/'. */
  492. if (cursor == cursor0 || ISSLASH (cursor[-1]))
  493. continue;
  494. /* Avoid mkdir where last part of file name is "." or "..". */
  495. if (cursor[-1] == '.'
  496. && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
  497. || (cursor[-2] == '.'
  498. && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
  499. continue;
  500. *cursor = '\0'; /* truncate the name there */
  501. desired_mode = MODE_RWX & ~ newdir_umask;
  502. mode = desired_mode | (we_are_root ? 0 : MODE_WXUSR);
  503. status = mkdirat (chdir_fd, file_name, mode);
  504. if (status == 0)
  505. {
  506. /* Create a struct delayed_set_stat even if
  507. mode == desired_mode, because
  508. repair_delayed_set_stat may need to update the struct. */
  509. delay_set_stat (file_name,
  510. 0, mode & ~ current_umask, MODE_RWX,
  511. desired_mode, AT_SYMLINK_NOFOLLOW);
  512. print_for_mkdir (file_name, cursor - file_name, desired_mode);
  513. *interdir_made = true;
  514. }
  515. else if (errno == EEXIST)
  516. status = 0;
  517. else
  518. {
  519. /* Check whether the desired file exists. Even when the
  520. file exists, mkdir can fail with some errno value E other
  521. than EEXIST, so long as E describes an error condition
  522. that also applies. */
  523. int e = errno;
  524. struct stat st;
  525. status = fstatat (chdir_fd, file_name, &st, 0);
  526. if (status)
  527. {
  528. errno = e;
  529. mkdir_error (file_name);
  530. }
  531. }
  532. *cursor = '/';
  533. if (status)
  534. return status;
  535. }
  536. return 0;
  537. }
  538. /* Return true if FILE_NAME (with status *STP, if STP) is not a
  539. directory, and has a time stamp newer than (or equal to) that of
  540. TAR_STAT. */
  541. static bool
  542. file_newer_p (const char *file_name, struct stat const *stp,
  543. struct tar_stat_info *tar_stat)
  544. {
  545. struct stat st;
  546. if (!stp)
  547. {
  548. if (deref_stat (file_name, &st) != 0)
  549. {
  550. if (errno != ENOENT)
  551. {
  552. stat_warn (file_name);
  553. /* Be safer: if the file exists, assume it is newer. */
  554. return true;
  555. }
  556. return false;
  557. }
  558. stp = &st;
  559. }
  560. return (! S_ISDIR (stp->st_mode)
  561. && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (stp)) <= 0);
  562. }
  563. #define RECOVER_NO 0
  564. #define RECOVER_OK 1
  565. #define RECOVER_SKIP 2
  566. /* Attempt repairing what went wrong with the extraction. Delete an
  567. already existing file or create missing intermediate directories.
  568. Return RECOVER_OK if we somewhat increased our chances at a successful
  569. extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
  570. caller should skip extraction of that member. The value of errno is
  571. properly restored on returning RECOVER_NO.
  572. If REGULAR, the caller was trying to extract onto a regular file.
  573. Set *INTERDIR_MADE if an intermediate directory is made as part of
  574. the recovery process. */
  575. static int
  576. maybe_recoverable (char *file_name, bool regular, bool *interdir_made)
  577. {
  578. int e = errno;
  579. struct stat st;
  580. struct stat const *stp = 0;
  581. if (*interdir_made)
  582. return RECOVER_NO;
  583. switch (e)
  584. {
  585. case ELOOP:
  586. /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
  587. but some operating systems do not conform to the standard. */
  588. #ifdef EFTYPE
  589. /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */
  590. case EFTYPE:
  591. #endif
  592. /* FreeBSD 8.1 uses errno == EMLINK. */
  593. case EMLINK:
  594. /* Tru64 5.1B uses errno == ENOTSUP. */
  595. case ENOTSUP:
  596. if (! regular
  597. || old_files_option != OVERWRITE_OLD_FILES || dereference_option)
  598. break;
  599. if (strchr (file_name, '/'))
  600. {
  601. if (deref_stat (file_name, &st) != 0)
  602. break;
  603. stp = &st;
  604. }
  605. /* The caller tried to open a symbolic link with O_NOFOLLOW.
  606. Fall through, treating it as an already-existing file. */
  607. case EEXIST:
  608. /* Remove an old file, if the options allow this. */
  609. switch (old_files_option)
  610. {
  611. case SKIP_OLD_FILES:
  612. WARNOPT (WARN_EXISTING_FILE,
  613. (0, 0, _("%s: skipping existing file"), file_name));
  614. return RECOVER_SKIP;
  615. case KEEP_OLD_FILES:
  616. return RECOVER_NO;
  617. case KEEP_NEWER_FILES:
  618. if (file_newer_p (file_name, stp, &current_stat_info))
  619. break;
  620. /* FALL THROUGH */
  621. case DEFAULT_OLD_FILES:
  622. case NO_OVERWRITE_DIR_OLD_FILES:
  623. case OVERWRITE_OLD_FILES:
  624. if (0 < remove_any_file (file_name, ORDINARY_REMOVE_OPTION))
  625. return RECOVER_OK;
  626. break;
  627. case UNLINK_FIRST_OLD_FILES:
  628. break;
  629. }
  630. case ENOENT:
  631. /* Attempt creating missing intermediate directories. */
  632. if (make_directories (file_name, interdir_made) == 0 && *interdir_made)
  633. return RECOVER_OK;
  634. break;
  635. default:
  636. /* Just say we can't do anything about it... */
  637. break;
  638. }
  639. errno = e;
  640. return RECOVER_NO;
  641. }
  642. /* Restore stat extended attributes (xattr) for FILE_NAME, using information
  643. given in *ST. Restore before extraction because they may affect file layout
  644. (e.g. on Lustre distributed parallel filesystem - setting info about how many
  645. servers is this file striped over, stripe size, mirror copies, etc.
  646. in advance dramatically improves the following performance of reading and
  647. writing a file). If not restoring permissions, invert the INVERT_PERMISSIONS
  648. bits from the file's current permissions. TYPEFLAG specifies the type of the
  649. file. FILE_CREATED indicates set_xattr has created the file */
  650. static int
  651. set_xattr (char const *file_name, struct tar_stat_info const *st,
  652. mode_t invert_permissions, char typeflag, int *file_created)
  653. {
  654. int status = 0;
  655. #ifdef HAVE_XATTRS
  656. bool interdir_made = false;
  657. if ((xattrs_option > 0) && st->xattr_map_size)
  658. {
  659. mode_t mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
  660. do
  661. status = mknodat (chdir_fd, file_name, mode ^ invert_permissions, 0);
  662. while (status && maybe_recoverable ((char *)file_name, false,
  663. &interdir_made));
  664. xattrs_xattrs_set (st, file_name, typeflag, 0);
  665. *file_created = 1;
  666. }
  667. #endif
  668. return(status);
  669. }
  670. /* Fix the statuses of all directories whose statuses need fixing, and
  671. which are not ancestors of FILE_NAME. If AFTER_LINKS is
  672. nonzero, do this for all such directories; otherwise, stop at the
  673. first directory that is marked to be fixed up only after delayed
  674. links are applied. */
  675. static void
  676. apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links)
  677. {
  678. size_t file_name_len = strlen (file_name);
  679. bool check_for_renamed_directories = 0;
  680. while (delayed_set_stat_head)
  681. {
  682. struct delayed_set_stat *data = delayed_set_stat_head;
  683. bool skip_this_one = 0;
  684. struct stat st;
  685. mode_t current_mode = data->current_mode;
  686. mode_t current_mode_mask = data->current_mode_mask;
  687. check_for_renamed_directories |= data->after_links;
  688. if (after_links < data->after_links
  689. || (data->file_name_len < file_name_len
  690. && file_name[data->file_name_len]
  691. && (ISSLASH (file_name[data->file_name_len])
  692. || ISSLASH (file_name[data->file_name_len - 1]))
  693. && memcmp (file_name, data->file_name, data->file_name_len) == 0))
  694. break;
  695. chdir_do (data->change_dir);
  696. if (check_for_renamed_directories)
  697. {
  698. if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
  699. {
  700. stat_error (data->file_name);
  701. skip_this_one = 1;
  702. }
  703. else
  704. {
  705. current_mode = st.st_mode;
  706. current_mode_mask = ALL_MODE_BITS;
  707. if (! (st.st_dev == data->dev && st.st_ino == data->ino))
  708. {
  709. ERROR ((0, 0,
  710. _("%s: Directory renamed before its status could be extracted"),
  711. quotearg_colon (data->file_name)));
  712. skip_this_one = 1;
  713. }
  714. }
  715. }
  716. if (! skip_this_one)
  717. {
  718. struct tar_stat_info sb;
  719. sb.stat.st_mode = data->mode;
  720. sb.stat.st_uid = data->uid;
  721. sb.stat.st_gid = data->gid;
  722. sb.atime = data->atime;
  723. sb.mtime = data->mtime;
  724. sb.cntx_name = data->cntx_name;
  725. sb.acls_a_ptr = data->acls_a_ptr;
  726. sb.acls_a_len = data->acls_a_len;
  727. sb.acls_d_ptr = data->acls_d_ptr;
  728. sb.acls_d_len = data->acls_d_len;
  729. sb.xattr_map = data->xattr_map;
  730. sb.xattr_map_size = data->xattr_map_size;
  731. set_stat (data->file_name, &sb,
  732. -1, current_mode, current_mode_mask,
  733. DIRTYPE, data->interdir, data->atflag);
  734. }
  735. delayed_set_stat_head = data->next;
  736. xheader_xattr_free (data->xattr_map, data->xattr_map_size);
  737. free (data->cntx_name);
  738. free (data->acls_a_ptr);
  739. free (data->acls_d_ptr);
  740. free (data);
  741. }
  742. }
  743. static bool
  744. is_directory_link (const char *file_name)
  745. {
  746. struct stat st;
  747. int e = errno;
  748. int res;
  749. res = (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0 &&
  750. S_ISLNK (st.st_mode) &&
  751. fstatat (chdir_fd, file_name, &st, 0) == 0 &&
  752. S_ISDIR (st.st_mode));
  753. errno = e;
  754. return res;
  755. }
  756. /* Extractor functions for various member types */
  757. static int
  758. extract_dir (char *file_name, int typeflag)
  759. {
  760. int status;
  761. mode_t mode;
  762. mode_t current_mode = 0;
  763. mode_t current_mode_mask = 0;
  764. int atflag = 0;
  765. bool interdir_made = false;
  766. /* Save 'root device' to avoid purging mount points. */
  767. if (one_file_system_option && root_device == 0)
  768. {
  769. struct stat st;
  770. if (fstatat (chdir_fd, ".", &st, 0) != 0)
  771. stat_diag (".");
  772. else
  773. root_device = st.st_dev;
  774. }
  775. if (incremental_option)
  776. /* Read the entry and delete files that aren't listed in the archive. */
  777. purge_directory (file_name);
  778. else if (typeflag == GNUTYPE_DUMPDIR)
  779. skip_member ();
  780. /* If ownership or permissions will be restored later, create the
  781. directory with restrictive permissions at first, so that in the
  782. meantime processes owned by other users do not inadvertently
  783. create files under this directory that inherit the wrong owner,
  784. group, or permissions from the directory. If not root, though,
  785. make the directory writeable and searchable at first, so that
  786. files can be created under it. */
  787. mode = ((current_stat_info.stat.st_mode
  788. & (0 < same_owner_option || 0 < same_permissions_option
  789. ? S_IRWXU
  790. : MODE_RWX))
  791. | (we_are_root ? 0 : MODE_WXUSR));
  792. for (;;)
  793. {
  794. status = mkdirat (chdir_fd, file_name, mode);
  795. if (status == 0)
  796. {
  797. current_mode = mode & ~ current_umask;
  798. current_mode_mask = MODE_RWX;
  799. atflag = AT_SYMLINK_NOFOLLOW;
  800. break;
  801. }
  802. if (errno == EEXIST
  803. && (interdir_made
  804. || keep_directory_symlink_option
  805. || old_files_option == DEFAULT_OLD_FILES
  806. || old_files_option == OVERWRITE_OLD_FILES))
  807. {
  808. struct stat st;
  809. if (keep_directory_symlink_option && is_directory_link (file_name))
  810. return 0;
  811. if (deref_stat (file_name, &st) == 0)
  812. {
  813. current_mode = st.st_mode;
  814. current_mode_mask = ALL_MODE_BITS;
  815. if (S_ISDIR (current_mode))
  816. {
  817. if (interdir_made)
  818. {
  819. repair_delayed_set_stat (file_name, &st);
  820. return 0;
  821. }
  822. break;
  823. }
  824. }
  825. errno = EEXIST;
  826. }
  827. switch (maybe_recoverable (file_name, false, &interdir_made))
  828. {
  829. case RECOVER_OK:
  830. continue;
  831. case RECOVER_SKIP:
  832. break;
  833. case RECOVER_NO:
  834. if (errno != EEXIST)
  835. {
  836. mkdir_error (file_name);
  837. return 1;
  838. }
  839. break;
  840. }
  841. break;
  842. }
  843. if (status == 0
  844. || old_files_option == DEFAULT_OLD_FILES
  845. || old_files_option == OVERWRITE_OLD_FILES)
  846. delay_set_stat (file_name, &current_stat_info,
  847. current_mode, current_mode_mask,
  848. current_stat_info.stat.st_mode, atflag);
  849. return status;
  850. }
  851. static int
  852. open_output_file (char const *file_name, int typeflag, mode_t mode,
  853. int file_created, mode_t *current_mode,
  854. mode_t *current_mode_mask)
  855. {
  856. int fd;
  857. bool overwriting_old_files = old_files_option == OVERWRITE_OLD_FILES;
  858. int openflag = (O_WRONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
  859. | O_CREAT
  860. | (overwriting_old_files
  861. ? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW)
  862. : O_EXCL));
  863. /* File might be created in set_xattr. So clear O_EXCL to avoid open() fail */
  864. if (file_created)
  865. openflag = openflag & ~O_EXCL;
  866. if (typeflag == CONTTYPE)
  867. {
  868. static int conttype_diagnosed;
  869. if (!conttype_diagnosed)
  870. {
  871. conttype_diagnosed = 1;
  872. WARNOPT (WARN_CONTIGUOUS_CAST,
  873. (0, 0, _("Extracting contiguous files as regular files")));
  874. }
  875. }
  876. /* If O_NOFOLLOW is needed but does not work, check for a symlink
  877. separately. There's a race condition, but that cannot be avoided
  878. on hosts lacking O_NOFOLLOW. */
  879. if (! HAVE_WORKING_O_NOFOLLOW
  880. && overwriting_old_files && ! dereference_option)
  881. {
  882. struct stat st;
  883. if (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0
  884. && S_ISLNK (st.st_mode))
  885. {
  886. errno = ELOOP;
  887. return -1;
  888. }
  889. }
  890. fd = openat (chdir_fd, file_name, openflag, mode);
  891. if (0 <= fd)
  892. {
  893. if (overwriting_old_files)
  894. {
  895. struct stat st;
  896. if (fstat (fd, &st) != 0)
  897. {
  898. int e = errno;
  899. close (fd);
  900. errno = e;
  901. return -1;
  902. }
  903. if (! S_ISREG (st.st_mode))
  904. {
  905. close (fd);
  906. errno = EEXIST;
  907. return -1;
  908. }
  909. *current_mode = st.st_mode;
  910. *current_mode_mask = ALL_MODE_BITS;
  911. }
  912. else
  913. {
  914. *current_mode = mode & ~ current_umask;
  915. *current_mode_mask = MODE_RWX;
  916. }
  917. }
  918. return fd;
  919. }
  920. static int
  921. extract_file (char *file_name, int typeflag)
  922. {
  923. int fd;
  924. off_t size;
  925. union block *data_block;
  926. int status;
  927. size_t count;
  928. size_t written;
  929. bool interdir_made = false;
  930. mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
  931. & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
  932. mode_t invert_permissions = 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO)
  933. : 0;
  934. mode_t current_mode = 0;
  935. mode_t current_mode_mask = 0;
  936. if (to_stdout_option)
  937. fd = STDOUT_FILENO;
  938. else if (to_command_option)
  939. {
  940. fd = sys_exec_command (file_name, 'f', &current_stat_info);
  941. if (fd < 0)
  942. {
  943. skip_member ();
  944. return 0;
  945. }
  946. }
  947. else
  948. {
  949. int file_created = 0;
  950. if (set_xattr (file_name, &current_stat_info, invert_permissions,
  951. typeflag, &file_created))
  952. {
  953. skip_member ();
  954. open_error (file_name);
  955. return 1;
  956. }
  957. while ((fd = open_output_file (file_name, typeflag, mode,
  958. file_created, &current_mode,
  959. &current_mode_mask))
  960. < 0)
  961. {
  962. int recover = maybe_recoverable (file_name, true, &interdir_made);
  963. if (recover != RECOVER_OK)
  964. {
  965. skip_member ();
  966. if (recover == RECOVER_SKIP)
  967. return 0;
  968. open_error (file_name);
  969. return 1;
  970. }
  971. }
  972. }
  973. mv_begin_read (&current_stat_info);
  974. if (current_stat_info.is_sparse)
  975. sparse_extract_file (fd, &current_stat_info, &size);
  976. else
  977. for (size = current_stat_info.stat.st_size; size > 0; )
  978. {
  979. mv_size_left (size);
  980. /* Locate data, determine max length writeable, write it,
  981. block that we have used the data, then check if the write
  982. worked. */
  983. data_block = find_next_block ();
  984. if (! data_block)
  985. {
  986. ERROR ((0, 0, _("Unexpected EOF in archive")));
  987. break; /* FIXME: What happens, then? */
  988. }
  989. written = available_space_after (data_block);
  990. if (written > size)
  991. written = size;
  992. errno = 0;
  993. count = blocking_write (fd, data_block->buffer, written);
  994. size -= written;
  995. set_next_block_after ((union block *)
  996. (data_block->buffer + written - 1));
  997. if (count != written)
  998. {
  999. if (!to_command_option)
  1000. write_error_details (file_name, count, written);
  1001. /* FIXME: shouldn't we restore from backup? */
  1002. break;
  1003. }
  1004. }
  1005. skip_file (size);
  1006. mv_end ();
  1007. /* If writing to stdout, don't try to do anything to the filename;
  1008. it doesn't exist, or we don't want to touch it anyway. */
  1009. if (to_stdout_option)
  1010. return 0;
  1011. if (! to_command_option)
  1012. set_stat (file_name, &current_stat_info, fd,
  1013. current_mode, current_mode_mask, typeflag, false,
  1014. (old_files_option == OVERWRITE_OLD_FILES
  1015. ? 0 : AT_SYMLINK_NOFOLLOW));
  1016. status = close (fd);
  1017. if (status < 0)
  1018. close_error (file_name);
  1019. if (to_command_option)
  1020. sys_wait_command ();
  1021. return status;
  1022. }
  1023. /* Create a placeholder file with name FILE_NAME, which will be
  1024. replaced after other extraction is done by a symbolic link if
  1025. IS_SYMLINK is true, and by a hard link otherwise. Set
  1026. *INTERDIR_MADE if an intermediate directory is made in the
  1027. process. */
  1028. static int
  1029. create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made)
  1030. {
  1031. int fd;
  1032. struct stat st;
  1033. while ((fd = openat (chdir_fd, file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
  1034. {
  1035. switch (maybe_recoverable (file_name, false, interdir_made))
  1036. {
  1037. case RECOVER_OK:
  1038. continue;
  1039. case RECOVER_SKIP:
  1040. return 0;
  1041. case RECOVER_NO:
  1042. open_error (file_name);
  1043. return -1;
  1044. }
  1045. }
  1046. if (fstat (fd, &st) != 0)
  1047. {
  1048. stat_error (file_name);
  1049. close (fd);
  1050. }
  1051. else if (close (fd) != 0)
  1052. close_error (file_name);
  1053. else
  1054. {
  1055. struct delayed_set_stat *h;
  1056. struct delayed_link *p =
  1057. xmalloc (offsetof (struct delayed_link, target)
  1058. + strlen (current_stat_info.link_name)
  1059. + 1);
  1060. p->next = delayed_link_head;
  1061. delayed_link_head = p;
  1062. p->dev = st.st_dev;
  1063. p->ino = st.st_ino;
  1064. p->birthtime = get_stat_birthtime (&st);
  1065. p->is_symlink = is_symlink;
  1066. if (is_symlink)
  1067. {
  1068. p->mode = current_stat_info.stat.st_mode;
  1069. p->uid = current_stat_info.stat.st_uid;
  1070. p->gid = current_stat_info.stat.st_gid;
  1071. p->atime = current_stat_info.atime;
  1072. p->mtime = current_stat_info.mtime;
  1073. }
  1074. p->change_dir = chdir_current;
  1075. p->sources = xmalloc (offsetof (struct string_list, string)
  1076. + strlen (file_name) + 1);
  1077. p->sources->next = 0;
  1078. strcpy (p->sources->string, file_name);
  1079. p->cntx_name = NULL;
  1080. assign_string (&p->cntx_name, current_stat_info.cntx_name);
  1081. p->acls_a_ptr = NULL;
  1082. p->acls_a_len = 0;
  1083. p->acls_d_ptr = NULL;
  1084. p->acls_d_len = 0;
  1085. xheader_xattr_copy (&current_stat_info, &p->xattr_map, &p->xattr_map_size);
  1086. strcpy (p->target, current_stat_info.link_name);
  1087. h = delayed_set_stat_head;
  1088. if (h && ! h->after_links
  1089. && strncmp (file_name, h->file_name, h->file_name_len) == 0
  1090. && ISSLASH (file_name[h->file_name_len])
  1091. && (last_component (file_name) == file_name + h->file_name_len + 1))
  1092. mark_after_links (h);
  1093. return 0;
  1094. }
  1095. return -1;
  1096. }
  1097. static int
  1098. extract_link (char *file_name, int typeflag)
  1099. {
  1100. bool interdir_made = false;
  1101. char const *link_name;
  1102. int rc;
  1103. link_name = current_stat_info.link_name;
  1104. if (! absolute_names_option && contains_dot_dot (link_name))
  1105. return create_placeholder_file (file_name, false, &interdir_made);
  1106. do
  1107. {
  1108. struct stat st1, st2;
  1109. int e;
  1110. int status = linkat (chdir_fd, link_name, chdir_fd, file_name, 0);
  1111. e = errno;
  1112. if (status == 0)
  1113. {
  1114. struct delayed_link *ds = delayed_link_head;
  1115. if (ds
  1116. && fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) == 0)
  1117. for (; ds; ds = ds->next)
  1118. if (ds->change_dir == chdir_current
  1119. && ds->dev == st1.st_dev
  1120. && ds->ino == st1.st_ino
  1121. && (timespec_cmp (ds->birthtime, get_stat_birthtime (&st1))
  1122. == 0))
  1123. {
  1124. struct string_list *p = xmalloc (offsetof (struct string_list, string)
  1125. + strlen (file_name) + 1);
  1126. strcpy (p->string, file_name);
  1127. p->next = ds->sources;
  1128. ds->sources = p;
  1129. break;
  1130. }
  1131. return 0;
  1132. }
  1133. else if ((e == EEXIST && strcmp (link_name, file_name) == 0)
  1134. || ((fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW)
  1135. == 0)
  1136. && (fstatat (chdir_fd, file_name, &st2, AT_SYMLINK_NOFOLLOW)
  1137. == 0)
  1138. && st1.st_dev == st2.st_dev
  1139. && st1.st_ino == st2.st_ino))
  1140. return 0;
  1141. errno = e;
  1142. }
  1143. while ((rc = maybe_recoverable (file_name, false, &interdir_made))
  1144. == RECOVER_OK);
  1145. if (rc == RECOVER_SKIP)
  1146. return 0;
  1147. if (!(incremental_option && errno == EEXIST))
  1148. {
  1149. link_error (link_name, file_name);
  1150. return 1;
  1151. }
  1152. return 0;
  1153. }
  1154. static int
  1155. extract_symlink (char *file_name, int typeflag)
  1156. {
  1157. #ifdef HAVE_SYMLINK
  1158. bool interdir_made = false;
  1159. if (! absolute_names_option
  1160. && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name)
  1161. || contains_dot_dot (current_stat_info.link_name)))
  1162. return create_placeholder_file (file_name, true, &interdir_made);
  1163. while (symlinkat (current_stat_info.link_name, chdir_fd, file_name) != 0)
  1164. switch (maybe_recoverable (file_name, false, &interdir_made))
  1165. {
  1166. case RECOVER_OK:
  1167. continue;
  1168. case RECOVER_SKIP:
  1169. return 0;
  1170. case RECOVER_NO:
  1171. symlink_error (current_stat_info.link_name, file_name);
  1172. return -1;
  1173. }
  1174. set_stat (file_name, &current_stat_info, -1, 0, 0,
  1175. SYMTYPE, false, AT_SYMLINK_NOFOLLOW);
  1176. return 0;
  1177. #else
  1178. static int warned_once;
  1179. if (!warned_once)
  1180. {
  1181. warned_once = 1;
  1182. WARNOPT (WARN_SYMLINK_CAST,
  1183. (0, 0,
  1184. _("Attempting extraction of symbolic links as hard links")));
  1185. }
  1186. return extract_link (file_name, typeflag);
  1187. #endif
  1188. }
  1189. #if S_IFCHR || S_IFBLK
  1190. static int
  1191. extract_node (char *file_name, int typeflag)
  1192. {
  1193. bool interdir_made = false;
  1194. mode_t mode = (current_stat_info.stat.st_mode & (MODE_RWX | S_IFBLK | S_IFCHR)
  1195. & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
  1196. while (mknodat (chdir_fd, file_name, mode, current_stat_info.stat.st_rdev)
  1197. != 0)
  1198. switch (maybe_recoverable (file_name, false, &interdir_made))
  1199. {
  1200. case RECOVER_OK:
  1201. continue;
  1202. case RECOVER_SKIP:
  1203. return 0;
  1204. case RECOVER_NO:
  1205. mknod_error (file_name);
  1206. return -1;
  1207. }
  1208. set_stat (file_name, &current_stat_info, -1,
  1209. mode & ~ current_umask, MODE_RWX,
  1210. typeflag, false, AT_SYMLINK_NOFOLLOW);
  1211. return 0;
  1212. }
  1213. #endif
  1214. #if HAVE_MKFIFO || defined mkfifo
  1215. static int
  1216. extract_fifo (char *file_name, int typeflag)
  1217. {
  1218. bool interdir_made = false;
  1219. mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
  1220. & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
  1221. while (mkfifoat (chdir_fd, file_name, mode) != 0)
  1222. switch (maybe_recoverable (file_name, false, &interdir_made))
  1223. {
  1224. case RECOVER_OK:
  1225. continue;
  1226. case RECOVER_SKIP:
  1227. return 0;
  1228. case RECOVER_NO:
  1229. mkfifo_error (file_name);
  1230. return -1;
  1231. }
  1232. set_stat (file_name, &current_stat_info, -1,
  1233. mode & ~ current_umask, MODE_RWX,
  1234. typeflag, false, AT_SYMLINK_NOFOLLOW);
  1235. return 0;
  1236. }
  1237. #endif
  1238. static int
  1239. extract_volhdr (char *file_name, int typeflag)
  1240. {
  1241. skip_member ();
  1242. return 0;
  1243. }
  1244. static int
  1245. extract_failure (char *file_name, int typeflag)
  1246. {
  1247. return 1;
  1248. }
  1249. static int
  1250. extract_skip (char *file_name, int typeflag)
  1251. {
  1252. skip_member ();
  1253. return 0;
  1254. }
  1255. typedef int (*tar_extractor_t) (char *file_name, int typeflag);
  1256. /* Prepare to extract a file. Find extractor function.
  1257. Return zero if extraction should not proceed. */
  1258. static int
  1259. prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun)
  1260. {
  1261. int rc = 1;
  1262. if (EXTRACT_OVER_PIPE)
  1263. rc = 0;
  1264. /* Select the extractor */
  1265. switch (typeflag)
  1266. {
  1267. case GNUTYPE_SPARSE:
  1268. *fun = extract_file;
  1269. rc = 1;
  1270. break;
  1271. case AREGTYPE:
  1272. case REGTYPE:
  1273. case CONTTYPE:
  1274. /* Appears to be a file. But BSD tar uses the convention that a slash
  1275. suffix means a directory. */
  1276. if (current_stat_info.had_trailing_slash)
  1277. *fun = extract_dir;
  1278. else
  1279. {
  1280. *fun = extract_file;
  1281. rc = 1;
  1282. }
  1283. break;
  1284. case SYMTYPE:
  1285. *fun = extract_symlink;
  1286. break;
  1287. case LNKTYPE:
  1288. *fun = extract_link;
  1289. break;
  1290. #if S_IFCHR
  1291. case CHRTYPE:
  1292. current_stat_info.stat.st_mode |= S_IFCHR;
  1293. *fun = extract_node;
  1294. break;
  1295. #endif
  1296. #if S_IFBLK
  1297. case BLKTYPE:
  1298. current_stat_info.stat.st_mode |= S_IFBLK;
  1299. *fun = extract_node;
  1300. break;
  1301. #endif
  1302. #if HAVE_MKFIFO || defined mkfifo
  1303. case FIFOTYPE:
  1304. *fun = extract_fifo;
  1305. break;
  1306. #endif
  1307. case DIRTYPE:
  1308. case GNUTYPE_DUMPDIR:
  1309. *fun = extract_dir;
  1310. if (current_stat_info.is_dumpdir)
  1311. delay_directory_restore_option = true;
  1312. break;
  1313. case GNUTYPE_VOLHDR:
  1314. *fun = extract_volhdr;
  1315. break;
  1316. case GNUTYPE_MULTIVOL:
  1317. ERROR ((0, 0,
  1318. _("%s: Cannot extract -- file is continued from another volume"),
  1319. quotearg_colon (current_stat_info.file_name)));
  1320. *fun = extract_skip;
  1321. break;
  1322. case GNUTYPE_LONGNAME:
  1323. case GNUTYPE_LONGLINK:
  1324. ERROR ((0, 0, _("Unexpected long name header")));
  1325. *fun = extract_failure;
  1326. break;
  1327. default:
  1328. WARNOPT (WARN_UNKNOWN_CAST,
  1329. (0, 0,
  1330. _("%s: Unknown file type '%c', extracted as normal file"),
  1331. quotearg_colon (file_name), typeflag));
  1332. *fun = extract_file;
  1333. }
  1334. /* Determine whether the extraction should proceed */
  1335. if (rc == 0)
  1336. return 0;
  1337. switch (old_files_option)
  1338. {
  1339. case UNLINK_FIRST_OLD_FILES:
  1340. if (!remove_any_file (file_name,
  1341. recursive_unlink_option ? RECURSIVE_REMOVE_OPTION
  1342. : ORDINARY_REMOVE_OPTION)
  1343. && errno && errno != ENOENT)
  1344. {
  1345. unlink_error (file_name);
  1346. return 0;
  1347. }
  1348. break;
  1349. case KEEP_NEWER_FILES:
  1350. if (file_newer_p (file_name, 0, &current_stat_info))
  1351. {
  1352. WARNOPT (WARN_IGNORE_NEWER,
  1353. (0, 0, _("Current %s is newer or same age"),
  1354. quote (file_name)));
  1355. return 0;
  1356. }
  1357. break;
  1358. default:
  1359. break;
  1360. }
  1361. return 1;
  1362. }
  1363. /* Extract a file from the archive. */
  1364. void
  1365. extract_archive (void)
  1366. {
  1367. char typeflag;
  1368. tar_extractor_t fun;
  1369. fatal_exit_hook = extract_finish;
  1370. set_next_block_after (current_header);
  1371. if (!current_stat_info.file_name[0]
  1372. || (interactive_option
  1373. && !confirm ("extract", current_stat_info.file_name)))
  1374. {
  1375. skip_member ();
  1376. return;
  1377. }
  1378. /* Print the block from current_header and current_stat. */
  1379. if (verbose_option)
  1380. print_header (&current_stat_info, current_header, -1);
  1381. /* Restore stats for all non-ancestor directories, unless
  1382. it is an incremental archive.
  1383. (see NOTICE in the comment to delay_set_stat above) */
  1384. if (!delay_directory_restore_option)
  1385. {
  1386. int dir = chdir_current;
  1387. apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0);
  1388. chdir_do (dir);
  1389. }
  1390. /* Take a safety backup of a previously existing file. */
  1391. if (backup_option)
  1392. if (!maybe_backup_file (current_stat_info.file_name, 0))
  1393. {
  1394. int e = errno;
  1395. ERROR ((0, e, _("%s: Was unable to backup this file"),
  1396. quotearg_colon (current_stat_info.file_name)));
  1397. skip_member ();
  1398. return;
  1399. }
  1400. /* Extract the archive entry according to its type. */
  1401. /* KLUDGE */
  1402. typeflag = sparse_member_p (&current_stat_info) ?
  1403. GNUTYPE_SPARSE : current_header->header.typeflag;
  1404. if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun))
  1405. {
  1406. if (fun && (*fun) (current_stat_info.file_name, typeflag)
  1407. && backup_option)
  1408. undo_last_backup ();
  1409. }
  1410. else
  1411. skip_member ();
  1412. }
  1413. /* Extract the links whose final extraction were delayed. */
  1414. static void
  1415. apply_delayed_links (void)
  1416. {
  1417. struct delayed_link *ds;
  1418. for (ds = delayed_link_head; ds; )
  1419. {
  1420. struct string_list *sources = ds->sources;
  1421. char const *valid_source = 0;
  1422. chdir_do (ds->change_dir);
  1423. for (sources = ds->sources; sources; sources = sources->next)
  1424. {
  1425. char const *source = sources->string;
  1426. struct stat st;
  1427. /* Make sure the placeholder file is still there. If not,
  1428. don't create a link, as the placeholder was probably
  1429. removed by a later extraction. */
  1430. if (fstatat (chdir_fd, source, &st, AT_SYMLINK_NOFOLLOW) == 0
  1431. && st.st_dev == ds->dev
  1432. && st.st_ino == ds->ino
  1433. && timespec_cmp (get_stat_birthtime (&st), ds->birthtime) == 0)
  1434. {
  1435. /* Unlink the placeholder, then create a hard link if possible,
  1436. a symbolic link otherwise. */
  1437. if (unlinkat (chdir_fd, source, 0) != 0)
  1438. unlink_error (source);
  1439. else if (valid_source
  1440. && (linkat (chdir_fd, valid_source, chdir_fd, source, 0)
  1441. == 0))
  1442. ;
  1443. else if (!ds->is_symlink)
  1444. {
  1445. if (linkat (chdir_fd, ds->target, chdir_fd, source, 0) != 0)
  1446. link_error (ds->target, source);
  1447. }
  1448. else if (symlinkat (ds->target, chdir_fd, source) != 0)
  1449. symlink_error (ds->target, source);
  1450. else
  1451. {
  1452. struct tar_stat_info st1;
  1453. st1.stat.st_mode = ds->mode;
  1454. st1.stat.st_uid = ds->uid;
  1455. st1.stat.st_gid = ds->gid;
  1456. st1.atime = ds->atime;
  1457. st1.mtime = ds->mtime;
  1458. st1.cntx_name = ds->cntx_name;
  1459. st1.acls_a_ptr = ds->acls_a_ptr;
  1460. st1.acls_a_len = ds->acls_a_len;
  1461. st1.acls_d_ptr = ds->acls_d_ptr;
  1462. st1.acls_d_len = ds->acls_d_len;
  1463. st1.xattr_map = ds->xattr_map;
  1464. st1.xattr_map_size = ds->xattr_map_size;
  1465. set_stat (source, &st1, -1, 0, 0, SYMTYPE,
  1466. false, AT_SYMLINK_NOFOLLOW);
  1467. valid_source = source;
  1468. }
  1469. }
  1470. }
  1471. for (sources = ds->sources; sources; )
  1472. {
  1473. struct string_list *next = sources->next;
  1474. free (sources);
  1475. sources = next;
  1476. }
  1477. xheader_xattr_free (ds->xattr_map, ds->xattr_map_size);
  1478. free (ds->cntx_name);
  1479. {
  1480. struct delayed_link *next = ds->next;
  1481. free (ds);
  1482. ds = next;
  1483. }
  1484. }
  1485. delayed_link_head = 0;
  1486. }
  1487. /* Finish the extraction of an archive. */
  1488. void
  1489. extract_finish (void)
  1490. {
  1491. /* First, fix the status of ordinary directories that need fixing. */
  1492. apply_nonancestor_delayed_set_stat ("", 0);
  1493. /* Then, apply delayed links, so that they don't affect delayed
  1494. directory status-setting for ordinary directories. */
  1495. apply_delayed_links ();
  1496. /* Finally, fix the status of directories that are ancestors
  1497. of delayed links. */
  1498. apply_nonancestor_delayed_set_stat ("", 1);
  1499. }
  1500. bool
  1501. rename_directory (char *src, char *dst)
  1502. {
  1503. if (renameat (chdir_fd, src, chdir_fd, dst) != 0)
  1504. {
  1505. int e = errno;
  1506. bool interdir_made;
  1507. switch (e)
  1508. {
  1509. case ENOENT:
  1510. if (make_directories (dst, &interdir_made) == 0)
  1511. {
  1512. if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
  1513. return true;
  1514. e = errno;
  1515. }
  1516. break;
  1517. case EXDEV:
  1518. /* FIXME: Fall back to recursive copying */
  1519. default:
  1520. break;
  1521. }
  1522. ERROR ((0, e, _("Cannot rename %s to %s"),
  1523. quote_n (0, src),
  1524. quote_n (1, dst)));
  1525. return false;
  1526. }
  1527. return true;
  1528. }