extract.c 54 KB

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