extract.c 53 KB

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