4
0

misc.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /* Miscellaneous functions, not really specific to GNU tar.
  2. Copyright 1988-2021 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 3, or (at your option) any later
  6. version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  10. Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #define COMMON_INLINE _GL_EXTERN_INLINE
  14. #include <system.h>
  15. #include <rmt.h>
  16. #include "common.h"
  17. #include <quotearg.h>
  18. #include <xgetcwd.h>
  19. #include <unlinkdir.h>
  20. #include <utimens.h>
  21. #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
  22. # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
  23. #endif
  24. static void namebuf_add_dir (namebuf_t, char const *);
  25. static char *namebuf_finish (namebuf_t);
  26. static const char *tar_getcdpath (int);
  27. char const *
  28. quote_n_colon (int n, char const *arg)
  29. {
  30. return quotearg_n_style_colon (n, get_quoting_style (NULL), arg);
  31. }
  32. /* Handling strings. */
  33. /* Assign STRING to a copy of VALUE if not zero, or to zero. If
  34. STRING was nonzero, it is freed first. */
  35. void
  36. assign_string_or_null (char **string, const char *value)
  37. {
  38. if (value)
  39. assign_string (string, value);
  40. else
  41. assign_null (string);
  42. }
  43. void
  44. assign_string (char **string, const char *value)
  45. {
  46. free (*string);
  47. *string = xstrdup (value);
  48. }
  49. void
  50. assign_null (char **string)
  51. {
  52. char *old = *string;
  53. *string = NULL;
  54. free (old);
  55. }
  56. void
  57. assign_string_n (char **string, const char *value, size_t n)
  58. {
  59. free (*string);
  60. if (value)
  61. {
  62. size_t l = strnlen (value, n);
  63. char *p = xmalloc (l + 1);
  64. memcpy (p, value, l);
  65. p[l] = 0;
  66. *string = p;
  67. }
  68. else
  69. *string = NULL;
  70. }
  71. #if 0
  72. /* This function is currently unused; perhaps it should be removed? */
  73. /* Allocate a copy of the string quoted as in C, and returns that. If
  74. the string does not have to be quoted, it returns a null pointer.
  75. The allocated copy should normally be freed with free() after the
  76. caller is done with it.
  77. This is used in one context only: generating the directory file in
  78. incremental dumps. The quoted string is not intended for human
  79. consumption; it is intended only for unquote_string. The quoting
  80. is locale-independent, so that users needn't worry about locale
  81. when reading directory files. This means that we can't use
  82. quotearg, as quotearg is locale-dependent and is meant for human
  83. consumption. */
  84. static char *
  85. quote_copy_string (const char *string)
  86. {
  87. const char *source = string;
  88. char *destination = 0;
  89. char *buffer = 0;
  90. int copying = 0;
  91. while (*source)
  92. {
  93. int character = *source++;
  94. switch (character)
  95. {
  96. case '\n': case '\\':
  97. if (!copying)
  98. {
  99. size_t length = (source - string) - 1;
  100. copying = 1;
  101. buffer = xmalloc (length + 2 + 2 * strlen (source) + 1);
  102. memcpy (buffer, string, length);
  103. destination = buffer + length;
  104. }
  105. *destination++ = '\\';
  106. *destination++ = character == '\\' ? '\\' : 'n';
  107. break;
  108. default:
  109. if (copying)
  110. *destination++ = character;
  111. break;
  112. }
  113. }
  114. if (copying)
  115. {
  116. *destination = '\0';
  117. return buffer;
  118. }
  119. return 0;
  120. }
  121. #endif
  122. /* Takes a quoted C string (like those produced by quote_copy_string)
  123. and turns it back into the un-quoted original. This is done in
  124. place. Returns 0 only if the string was not properly quoted, but
  125. completes the unquoting anyway.
  126. This is used for reading the saved directory file in incremental
  127. dumps. It is used for decoding old 'N' records (demangling names).
  128. But also, it is used for decoding file arguments, would they come
  129. from the shell or a -T file, and for decoding the --exclude
  130. argument. */
  131. int
  132. unquote_string (char *string)
  133. {
  134. int result = 1;
  135. char *source = string;
  136. char *destination = string;
  137. /* Escape sequences other than \\ and \n are no longer generated by
  138. quote_copy_string, but accept them for backwards compatibility,
  139. and also because unquote_string is used for purposes other than
  140. parsing the output of quote_copy_string. */
  141. while (*source)
  142. if (*source == '\\')
  143. switch (*++source)
  144. {
  145. case '\\':
  146. *destination++ = '\\';
  147. source++;
  148. break;
  149. case 'a':
  150. *destination++ = '\a';
  151. source++;
  152. break;
  153. case 'b':
  154. *destination++ = '\b';
  155. source++;
  156. break;
  157. case 'f':
  158. *destination++ = '\f';
  159. source++;
  160. break;
  161. case 'n':
  162. *destination++ = '\n';
  163. source++;
  164. break;
  165. case 'r':
  166. *destination++ = '\r';
  167. source++;
  168. break;
  169. case 't':
  170. *destination++ = '\t';
  171. source++;
  172. break;
  173. case 'v':
  174. *destination++ = '\v';
  175. source++;
  176. break;
  177. case '?':
  178. *destination++ = 0177;
  179. source++;
  180. break;
  181. case '0':
  182. case '1':
  183. case '2':
  184. case '3':
  185. case '4':
  186. case '5':
  187. case '6':
  188. case '7':
  189. {
  190. int value = *source++ - '0';
  191. if (*source < '0' || *source > '7')
  192. {
  193. *destination++ = value;
  194. break;
  195. }
  196. value = value * 8 + *source++ - '0';
  197. if (*source < '0' || *source > '7')
  198. {
  199. *destination++ = value;
  200. break;
  201. }
  202. value = value * 8 + *source++ - '0';
  203. *destination++ = value;
  204. break;
  205. }
  206. default:
  207. result = 0;
  208. *destination++ = '\\';
  209. if (*source)
  210. *destination++ = *source++;
  211. break;
  212. }
  213. else if (source != destination)
  214. *destination++ = *source++;
  215. else
  216. source++, destination++;
  217. if (source != destination)
  218. *destination = '\0';
  219. return result;
  220. }
  221. /* Zap trailing slashes. */
  222. char *
  223. zap_slashes (char *name)
  224. {
  225. char *q;
  226. if (!name || *name == 0)
  227. return name;
  228. q = name + strlen (name) - 1;
  229. while (q > name && ISSLASH (*q))
  230. *q-- = '\0';
  231. return name;
  232. }
  233. /* Normalize FILE_NAME by removing redundant slashes and "."
  234. components, including redundant trailing slashes.
  235. Leave ".." alone, as it may be significant in the presence
  236. of symlinks and on platforms where "/.." != "/".
  237. Destructive version: modifies its argument. */
  238. void
  239. normalize_filename_x (char *file_name)
  240. {
  241. char *name = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
  242. char *p;
  243. char const *q;
  244. char c;
  245. /* Don't squeeze leading "//" to "/", on hosts where they're distinct. */
  246. name += (DOUBLE_SLASH_IS_DISTINCT_ROOT
  247. && ISSLASH (*name) && ISSLASH (name[1]) && ! ISSLASH (name[2]));
  248. /* Omit redundant leading "." components. */
  249. for (q = p = name; (*p = *q) == '.' && ISSLASH (q[1]); p += !*q)
  250. for (q += 2; ISSLASH (*q); q++)
  251. continue;
  252. /* Copy components from Q to P, omitting redundant slashes and
  253. internal "." components. */
  254. while ((*p++ = c = *q++) != '\0')
  255. if (ISSLASH (c))
  256. while (ISSLASH (q[*q == '.']))
  257. q += (*q == '.') + 1;
  258. /* Omit redundant trailing "." component and slash. */
  259. if (2 < p - name)
  260. {
  261. p -= p[-2] == '.' && ISSLASH (p[-3]);
  262. p -= 2 < p - name && ISSLASH (p[-2]);
  263. p[-1] = '\0';
  264. }
  265. }
  266. /* Normalize NAME by removing redundant slashes and "." components,
  267. including redundant trailing slashes.
  268. Return a normalized newly-allocated copy. */
  269. char *
  270. normalize_filename (int cdidx, const char *name)
  271. {
  272. char *copy = NULL;
  273. if (IS_RELATIVE_FILE_NAME (name))
  274. {
  275. /* Set COPY to the absolute path for this name.
  276. FIXME: There should be no need to get the absolute file name.
  277. tar_getcdpath does not return a true "canonical" path, so
  278. this following approach may lead to situations where the same
  279. file or directory is processed twice under different absolute
  280. paths without that duplication being detected. Perhaps we
  281. should use dev+ino pairs instead of names? (See listed03.at for
  282. a related test case.) */
  283. const char *cdpath = tar_getcdpath (cdidx);
  284. size_t copylen;
  285. bool need_separator;
  286. copylen = strlen (cdpath);
  287. need_separator = ! (DOUBLE_SLASH_IS_DISTINCT_ROOT
  288. && copylen == 2 && ISSLASH (cdpath[1]));
  289. copy = xmalloc (copylen + need_separator + strlen (name) + 1);
  290. strcpy (copy, cdpath);
  291. copy[copylen] = DIRECTORY_SEPARATOR;
  292. strcpy (copy + copylen + need_separator, name);
  293. }
  294. if (!copy)
  295. copy = xstrdup (name);
  296. normalize_filename_x (copy);
  297. return copy;
  298. }
  299. void
  300. replace_prefix (char **pname, const char *samp, size_t slen,
  301. const char *repl, size_t rlen)
  302. {
  303. char *name = *pname;
  304. size_t nlen = strlen (name);
  305. if (nlen > slen && memcmp (name, samp, slen) == 0 && ISSLASH (name[slen]))
  306. {
  307. if (rlen > slen)
  308. {
  309. name = xrealloc (name, nlen - slen + rlen + 1);
  310. *pname = name;
  311. }
  312. memmove (name + rlen, name + slen, nlen - slen + 1);
  313. memcpy (name, repl, rlen);
  314. }
  315. }
  316. /* Handling numbers. */
  317. /* Convert VALUE, which is converted from a system integer type whose
  318. minimum value is MINVAL and maximum MINVAL, to an decimal
  319. integer string. Use the storage in BUF and return a pointer to the
  320. converted string. If VALUE is converted from a negative integer in
  321. the range MINVAL .. -1, represent it with a string representation
  322. of the negative integer, using leading '-'. */
  323. #if ! (INTMAX_MAX <= UINTMAX_MAX / 2)
  324. # error "sysinttostr: uintmax_t cannot represent all intmax_t values"
  325. #endif
  326. char *
  327. sysinttostr (uintmax_t value, intmax_t minval, uintmax_t maxval,
  328. char buf[SYSINT_BUFSIZE])
  329. {
  330. if (value <= maxval)
  331. return umaxtostr (value, buf);
  332. else
  333. {
  334. intmax_t i = value - minval;
  335. return imaxtostr (i + minval, buf);
  336. }
  337. }
  338. /* Convert a prefix of the string ARG to a system integer type whose
  339. minimum value is MINVAL and maximum MAXVAL. If MINVAL is negative,
  340. negative integers MINVAL .. -1 are assumed to be represented using
  341. leading '-' in the usual way. If the represented value exceeds
  342. INTMAX_MAX, return a negative integer V such that (uintmax_t) V
  343. yields the represented value. If ARGLIM is nonnull, store into
  344. *ARGLIM a pointer to the first character after the prefix.
  345. This is the inverse of sysinttostr.
  346. On a normal return, set errno = 0.
  347. On conversion error, return 0 and set errno = EINVAL.
  348. On overflow, return an extreme value and set errno = ERANGE. */
  349. #if ! (INTMAX_MAX <= UINTMAX_MAX)
  350. # error "strtosysint: nonnegative intmax_t does not fit in uintmax_t"
  351. #endif
  352. intmax_t
  353. strtosysint (char const *arg, char **arglim, intmax_t minval, uintmax_t maxval)
  354. {
  355. errno = 0;
  356. if (maxval <= INTMAX_MAX)
  357. {
  358. if (ISDIGIT (arg[*arg == '-']))
  359. {
  360. intmax_t i = strtoimax (arg, arglim, 10);
  361. intmax_t imaxval = maxval;
  362. if (minval <= i && i <= imaxval)
  363. return i;
  364. errno = ERANGE;
  365. return i < minval ? minval : maxval;
  366. }
  367. }
  368. else
  369. {
  370. if (ISDIGIT (*arg))
  371. {
  372. uintmax_t i = strtoumax (arg, arglim, 10);
  373. if (i <= maxval)
  374. return represent_uintmax (i);
  375. errno = ERANGE;
  376. return maxval;
  377. }
  378. }
  379. errno = EINVAL;
  380. return 0;
  381. }
  382. /* Output fraction and trailing digits appropriate for a nanoseconds
  383. count equal to NS, but don't output unnecessary '.' or trailing
  384. zeros. */
  385. void
  386. code_ns_fraction (int ns, char *p)
  387. {
  388. if (ns == 0)
  389. *p = '\0';
  390. else
  391. {
  392. int i = 9;
  393. *p++ = '.';
  394. while (ns % 10 == 0)
  395. {
  396. ns /= 10;
  397. i--;
  398. }
  399. p[i] = '\0';
  400. for (;;)
  401. {
  402. p[--i] = '0' + ns % 10;
  403. if (i == 0)
  404. break;
  405. ns /= 10;
  406. }
  407. }
  408. }
  409. char const *
  410. code_timespec (struct timespec t, char sbuf[TIMESPEC_STRSIZE_BOUND])
  411. {
  412. time_t s = t.tv_sec;
  413. int ns = t.tv_nsec;
  414. char *np;
  415. bool negative = s < 0;
  416. /* ignore invalid values of ns */
  417. if (BILLION <= ns || ns < 0)
  418. ns = 0;
  419. if (negative && ns != 0)
  420. {
  421. s++;
  422. ns = BILLION - ns;
  423. }
  424. np = umaxtostr (negative ? - (uintmax_t) s : (uintmax_t) s, sbuf + 1);
  425. if (negative)
  426. *--np = '-';
  427. code_ns_fraction (ns, sbuf + UINTMAX_STRSIZE_BOUND);
  428. return np;
  429. }
  430. struct timespec
  431. decode_timespec (char const *arg, char **arg_lim, bool parse_fraction)
  432. {
  433. time_t s = TYPE_MINIMUM (time_t);
  434. int ns = -1;
  435. char const *p = arg;
  436. bool negative = *arg == '-';
  437. struct timespec r;
  438. if (! ISDIGIT (arg[negative]))
  439. errno = EINVAL;
  440. else
  441. {
  442. errno = 0;
  443. if (negative)
  444. {
  445. intmax_t i = strtoimax (arg, arg_lim, 10);
  446. if (TYPE_SIGNED (time_t) ? TYPE_MINIMUM (time_t) <= i : 0 <= i)
  447. s = i;
  448. else
  449. errno = ERANGE;
  450. }
  451. else
  452. {
  453. uintmax_t i = strtoumax (arg, arg_lim, 10);
  454. if (i <= TYPE_MAXIMUM (time_t))
  455. s = i;
  456. else
  457. errno = ERANGE;
  458. }
  459. p = *arg_lim;
  460. ns = 0;
  461. if (parse_fraction && *p == '.')
  462. {
  463. int digits = 0;
  464. bool trailing_nonzero = false;
  465. while (ISDIGIT (*++p))
  466. if (digits < LOG10_BILLION)
  467. digits++, ns = 10 * ns + (*p - '0');
  468. else
  469. trailing_nonzero |= *p != '0';
  470. while (digits < LOG10_BILLION)
  471. digits++, ns *= 10;
  472. if (negative)
  473. {
  474. /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
  475. I.e., truncate time stamps towards minus infinity while
  476. converting them to internal form. */
  477. ns += trailing_nonzero;
  478. if (ns != 0)
  479. {
  480. if (s == TYPE_MINIMUM (time_t))
  481. ns = -1;
  482. else
  483. {
  484. s--;
  485. ns = BILLION - ns;
  486. }
  487. }
  488. }
  489. }
  490. if (errno == ERANGE)
  491. ns = -1;
  492. }
  493. *arg_lim = (char *) p;
  494. r.tv_sec = s;
  495. r.tv_nsec = ns;
  496. return r;
  497. }
  498. /* File handling. */
  499. /* Saved names in case backup needs to be undone. */
  500. static char *before_backup_name;
  501. static char *after_backup_name;
  502. /* Return 1 if FILE_NAME is obviously "." or "/". */
  503. bool
  504. must_be_dot_or_slash (char const *file_name)
  505. {
  506. file_name += FILE_SYSTEM_PREFIX_LEN (file_name);
  507. if (ISSLASH (file_name[0]))
  508. {
  509. for (;;)
  510. if (ISSLASH (file_name[1]))
  511. file_name++;
  512. else if (file_name[1] == '.'
  513. && ISSLASH (file_name[2 + (file_name[2] == '.')]))
  514. file_name += 2 + (file_name[2] == '.');
  515. else
  516. return ! file_name[1];
  517. }
  518. else
  519. {
  520. while (file_name[0] == '.' && ISSLASH (file_name[1]))
  521. {
  522. file_name += 2;
  523. while (ISSLASH (*file_name))
  524. file_name++;
  525. }
  526. return ! file_name[0] || (file_name[0] == '.' && ! file_name[1]);
  527. }
  528. }
  529. /* Some implementations of rmdir let you remove '.' or '/'.
  530. Report an error with errno set to zero for obvious cases of this;
  531. otherwise call rmdir. */
  532. static int
  533. safer_rmdir (const char *file_name)
  534. {
  535. if (must_be_dot_or_slash (file_name))
  536. {
  537. errno = 0;
  538. return -1;
  539. }
  540. if (unlinkat (chdir_fd, file_name, AT_REMOVEDIR) == 0)
  541. {
  542. remove_delayed_set_stat (file_name);
  543. return 0;
  544. }
  545. return -1;
  546. }
  547. /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
  548. then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
  549. recursively; otherwise, remove it only if it is empty. If FILE_NAME is
  550. a directory that cannot be removed (e.g., because it is nonempty)
  551. and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
  552. Return 0 on error, with errno set; if FILE_NAME is obviously the working
  553. directory return zero with errno set to zero. */
  554. int
  555. remove_any_file (const char *file_name, enum remove_option option)
  556. {
  557. /* Try unlink first if we cannot unlink directories, as this saves
  558. us a system call in the common case where we're removing a
  559. non-directory. */
  560. bool try_unlink_first = cannot_unlink_dir ();
  561. if (try_unlink_first)
  562. {
  563. if (unlinkat (chdir_fd, file_name, 0) == 0)
  564. return 1;
  565. /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
  566. directory without appropriate privileges, but many Linux
  567. kernels return the more-sensible EISDIR. */
  568. if (errno != EPERM && errno != EISDIR)
  569. return 0;
  570. }
  571. if (safer_rmdir (file_name) == 0)
  572. return 1;
  573. switch (errno)
  574. {
  575. case ENOTDIR:
  576. return !try_unlink_first && unlinkat (chdir_fd, file_name, 0) == 0;
  577. case 0:
  578. case EEXIST:
  579. #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
  580. case ENOTEMPTY:
  581. #endif
  582. switch (option)
  583. {
  584. case ORDINARY_REMOVE_OPTION:
  585. break;
  586. case WANT_DIRECTORY_REMOVE_OPTION:
  587. return -1;
  588. case RECURSIVE_REMOVE_OPTION:
  589. {
  590. char *directory = tar_savedir (file_name, 0);
  591. char const *entry;
  592. size_t entrylen;
  593. if (! directory)
  594. return 0;
  595. for (entry = directory;
  596. (entrylen = strlen (entry)) != 0;
  597. entry += entrylen + 1)
  598. {
  599. char *file_name_buffer = make_file_name (file_name, entry);
  600. int r = remove_any_file (file_name_buffer,
  601. RECURSIVE_REMOVE_OPTION);
  602. int e = errno;
  603. free (file_name_buffer);
  604. if (! r)
  605. {
  606. free (directory);
  607. errno = e;
  608. return 0;
  609. }
  610. }
  611. free (directory);
  612. return safer_rmdir (file_name) == 0;
  613. }
  614. }
  615. break;
  616. }
  617. return 0;
  618. }
  619. /* Check if FILE_NAME already exists and make a backup of it right now.
  620. Return success (nonzero) only if the backup is either unneeded, or
  621. successful. For now, directories are considered to never need
  622. backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
  623. so, we do not have to backup block or character devices, nor remote
  624. entities. */
  625. bool
  626. maybe_backup_file (const char *file_name, bool this_is_the_archive)
  627. {
  628. struct stat file_stat;
  629. assign_string (&before_backup_name, file_name);
  630. /* A run situation may exist between Emacs or other GNU programs trying to
  631. make a backup for the same file simultaneously. If theoretically
  632. possible, real problems are unlikely. Doing any better would require a
  633. convention, GNU-wide, for all programs doing backups. */
  634. assign_null (&after_backup_name);
  635. /* Check if we really need to backup the file. */
  636. if (this_is_the_archive && _remdev (file_name))
  637. return true;
  638. if (deref_stat (file_name, &file_stat) != 0)
  639. {
  640. if (errno == ENOENT)
  641. return true;
  642. stat_error (file_name);
  643. return false;
  644. }
  645. if (S_ISDIR (file_stat.st_mode))
  646. return true;
  647. if (this_is_the_archive
  648. && (S_ISBLK (file_stat.st_mode) || S_ISCHR (file_stat.st_mode)))
  649. return true;
  650. after_backup_name = find_backup_file_name (chdir_fd, file_name, backup_type);
  651. if (! after_backup_name)
  652. xalloc_die ();
  653. if (renameat (chdir_fd, before_backup_name, chdir_fd, after_backup_name)
  654. == 0)
  655. {
  656. if (verbose_option)
  657. fprintf (stdlis, _("Renaming %s to %s\n"),
  658. quote_n (0, before_backup_name),
  659. quote_n (1, after_backup_name));
  660. return true;
  661. }
  662. else
  663. {
  664. /* The backup operation failed. */
  665. int e = errno;
  666. ERROR ((0, e, _("%s: Cannot rename to %s"),
  667. quotearg_colon (before_backup_name),
  668. quote_n (1, after_backup_name)));
  669. assign_null (&after_backup_name);
  670. return false;
  671. }
  672. }
  673. /* Try to restore the recently backed up file to its original name.
  674. This is usually only needed after a failed extraction. */
  675. void
  676. undo_last_backup (void)
  677. {
  678. if (after_backup_name)
  679. {
  680. if (renameat (chdir_fd, after_backup_name, chdir_fd, before_backup_name)
  681. != 0)
  682. {
  683. int e = errno;
  684. ERROR ((0, e, _("%s: Cannot rename to %s"),
  685. quotearg_colon (after_backup_name),
  686. quote_n (1, before_backup_name)));
  687. }
  688. if (verbose_option)
  689. fprintf (stdlis, _("Renaming %s back to %s\n"),
  690. quote_n (0, after_backup_name),
  691. quote_n (1, before_backup_name));
  692. assign_null (&after_backup_name);
  693. }
  694. }
  695. /* Apply either stat or lstat to (NAME, BUF), depending on the
  696. presence of the --dereference option. NAME is relative to the
  697. most-recent argument to chdir_do. */
  698. int
  699. deref_stat (char const *name, struct stat *buf)
  700. {
  701. return fstatat (chdir_fd, name, buf, fstatat_flags);
  702. }
  703. /* Read from FD into the buffer BUF with COUNT bytes. Attempt to fill
  704. BUF. Wait until input is available; this matters because files are
  705. opened O_NONBLOCK for security reasons, and on some file systems
  706. this can cause read to fail with errno == EAGAIN. Return the
  707. actual number of bytes read, zero for EOF, or
  708. SAFE_READ_ERROR upon error. */
  709. size_t
  710. blocking_read (int fd, void *buf, size_t count)
  711. {
  712. size_t bytes = safe_read (fd, buf, count);
  713. #if defined F_SETFL && O_NONBLOCK
  714. if (bytes == SAFE_READ_ERROR && errno == EAGAIN)
  715. {
  716. int flags = fcntl (fd, F_GETFL);
  717. if (0 <= flags && flags & O_NONBLOCK
  718. && fcntl (fd, F_SETFL, flags & ~O_NONBLOCK) != -1)
  719. bytes = safe_read (fd, buf, count);
  720. }
  721. #endif
  722. return bytes;
  723. }
  724. /* Write to FD from the buffer BUF with COUNT bytes. Do a full write.
  725. Wait until an output buffer is available; this matters because
  726. files are opened O_NONBLOCK for security reasons, and on some file
  727. systems this can cause write to fail with errno == EAGAIN. Return
  728. the actual number of bytes written, setting errno if that is less
  729. than COUNT. */
  730. size_t
  731. blocking_write (int fd, void const *buf, size_t count)
  732. {
  733. size_t bytes = full_write (fd, buf, count);
  734. #if defined F_SETFL && O_NONBLOCK
  735. if (bytes < count && errno == EAGAIN)
  736. {
  737. int flags = fcntl (fd, F_GETFL);
  738. if (0 <= flags && flags & O_NONBLOCK
  739. && fcntl (fd, F_SETFL, flags & ~O_NONBLOCK) != -1)
  740. {
  741. char const *buffer = buf;
  742. bytes += full_write (fd, buffer + bytes, count - bytes);
  743. }
  744. }
  745. #endif
  746. return bytes;
  747. }
  748. /* Set FD's (i.e., assuming the working directory is PARENTFD, FILE's)
  749. access time to ATIME. */
  750. int
  751. set_file_atime (int fd, int parentfd, char const *file, struct timespec atime)
  752. {
  753. struct timespec ts[2];
  754. ts[0] = atime;
  755. ts[1].tv_nsec = UTIME_OMIT;
  756. return fdutimensat (fd, parentfd, file, ts, fstatat_flags);
  757. }
  758. /* A description of a working directory. */
  759. struct wd
  760. {
  761. /* The directory's name. */
  762. char const *name;
  763. /* "Absolute" path representing this directory; in the contrast to
  764. the real absolute pathname, it can contain /../ components (see
  765. normalize_filename_x for the reason of it). It is NULL if the
  766. absolute path could not be determined. */
  767. char *abspath;
  768. /* If nonzero, the file descriptor of the directory, or AT_FDCWD if
  769. the working directory. If zero, the directory needs to be opened
  770. to be used. */
  771. int fd;
  772. };
  773. /* A vector of chdir targets. wd[0] is the initial working directory. */
  774. static struct wd *wd;
  775. /* The number of working directories in the vector. */
  776. static size_t wd_count;
  777. /* The allocated size of the vector. */
  778. static size_t wd_alloc;
  779. /* The maximum number of chdir targets with open directories.
  780. Don't make it too large, as many operating systems have a small
  781. limit on the number of open file descriptors. Also, the current
  782. implementation does not scale well. */
  783. enum { CHDIR_CACHE_SIZE = 16 };
  784. /* Indexes into WD of chdir targets with open file descriptors, sorted
  785. most-recently used first. Zero indexes are unused. */
  786. static int wdcache[CHDIR_CACHE_SIZE];
  787. /* Number of nonzero entries in WDCACHE. */
  788. static size_t wdcache_count;
  789. int
  790. chdir_count (void)
  791. {
  792. if (wd_count == 0)
  793. return wd_count;
  794. return wd_count - 1;
  795. }
  796. /* DIR is the operand of a -C option; add it to vector of chdir targets,
  797. and return the index of its location. */
  798. int
  799. chdir_arg (char const *dir)
  800. {
  801. if (wd_count == wd_alloc)
  802. {
  803. if (wd_alloc == 0)
  804. wd_alloc = 2;
  805. wd = x2nrealloc (wd, &wd_alloc, sizeof *wd);
  806. if (! wd_count)
  807. {
  808. wd[wd_count].name = ".";
  809. wd[wd_count].abspath = NULL;
  810. wd[wd_count].fd = AT_FDCWD;
  811. wd_count++;
  812. }
  813. }
  814. /* Optimize the common special case of the working directory,
  815. or the working directory as a prefix. */
  816. if (dir[0])
  817. {
  818. while (dir[0] == '.' && ISSLASH (dir[1]))
  819. for (dir += 2; ISSLASH (*dir); dir++)
  820. continue;
  821. if (! dir[dir[0] == '.'])
  822. return wd_count - 1;
  823. }
  824. wd[wd_count].name = dir;
  825. wd[wd_count].abspath = NULL;
  826. wd[wd_count].fd = 0;
  827. return wd_count++;
  828. }
  829. /* Index of current directory. */
  830. int chdir_current;
  831. /* Value suitable for use as the first argument to openat, and in
  832. similar locations for fstatat, etc. This is an open file
  833. descriptor, or AT_FDCWD if the working directory is current. It is
  834. valid until the next invocation of chdir_do. */
  835. int chdir_fd = AT_FDCWD;
  836. /* Change to directory I, in a virtual way. This does not actually
  837. invoke chdir; it merely sets chdir_fd to an int suitable as the
  838. first argument for openat, etc. If I is 0, change to the initial
  839. working directory; otherwise, I must be a value returned by
  840. chdir_arg. */
  841. void
  842. chdir_do (int i)
  843. {
  844. if (chdir_current != i)
  845. {
  846. struct wd *curr = &wd[i];
  847. int fd = curr->fd;
  848. if (! fd)
  849. {
  850. if (! IS_ABSOLUTE_FILE_NAME (curr->name))
  851. chdir_do (i - 1);
  852. fd = openat (chdir_fd, curr->name,
  853. open_searchdir_flags & ~ O_NOFOLLOW);
  854. if (fd < 0)
  855. open_fatal (curr->name);
  856. curr->fd = fd;
  857. /* Add I to the cache, tossing out the lowest-ranking entry if the
  858. cache is full. */
  859. if (wdcache_count < CHDIR_CACHE_SIZE)
  860. wdcache[wdcache_count++] = i;
  861. else
  862. {
  863. struct wd *stale = &wd[wdcache[CHDIR_CACHE_SIZE - 1]];
  864. if (close (stale->fd) != 0)
  865. close_diag (stale->name);
  866. stale->fd = 0;
  867. wdcache[CHDIR_CACHE_SIZE - 1] = i;
  868. }
  869. }
  870. if (0 < fd)
  871. {
  872. /* Move the i value to the front of the cache. This is
  873. O(CHDIR_CACHE_SIZE), but the cache is small. */
  874. size_t ci;
  875. int prev = wdcache[0];
  876. for (ci = 1; prev != i; ci++)
  877. {
  878. int cur = wdcache[ci];
  879. wdcache[ci] = prev;
  880. if (cur == i)
  881. break;
  882. prev = cur;
  883. }
  884. wdcache[0] = i;
  885. }
  886. chdir_current = i;
  887. chdir_fd = fd;
  888. }
  889. }
  890. const char *
  891. tar_dirname (void)
  892. {
  893. return wd[chdir_current].name;
  894. }
  895. /* Return the absolute path that represents the working
  896. directory referenced by IDX.
  897. If wd is empty, then there were no -C options given, and
  898. chdir_args() has never been called, so we simply return the
  899. process's actual cwd. (Note that in this case IDX is ignored,
  900. since it should always be 0.) */
  901. static const char *
  902. tar_getcdpath (int idx)
  903. {
  904. if (!wd)
  905. {
  906. static char *cwd;
  907. if (!cwd)
  908. {
  909. cwd = xgetcwd ();
  910. if (!cwd)
  911. call_arg_fatal ("getcwd", ".");
  912. }
  913. return cwd;
  914. }
  915. if (!wd[idx].abspath)
  916. {
  917. int i;
  918. int save_cwdi = chdir_current;
  919. for (i = idx; i >= 0; i--)
  920. if (wd[i].abspath)
  921. break;
  922. while (++i <= idx)
  923. {
  924. chdir_do (i);
  925. if (i == 0)
  926. {
  927. if ((wd[i].abspath = xgetcwd ()) == NULL)
  928. call_arg_fatal ("getcwd", ".");
  929. }
  930. else if (IS_ABSOLUTE_FILE_NAME (wd[i].name))
  931. /* If the given name is absolute, use it to represent this
  932. directory; otherwise, construct a name based on the
  933. previous -C option. */
  934. wd[i].abspath = xstrdup (wd[i].name);
  935. else
  936. {
  937. namebuf_t nbuf = namebuf_create (wd[i - 1].abspath);
  938. namebuf_add_dir (nbuf, wd[i].name);
  939. wd[i].abspath = namebuf_finish (nbuf);
  940. }
  941. }
  942. chdir_do (save_cwdi);
  943. }
  944. return wd[idx].abspath;
  945. }
  946. void
  947. close_diag (char const *name)
  948. {
  949. if (ignore_failed_read_option)
  950. {
  951. if (WARNING_ENABLED(WARN_FAILED_READ))
  952. close_warn (name);
  953. }
  954. else
  955. close_error (name);
  956. }
  957. void
  958. open_diag (char const *name)
  959. {
  960. if (ignore_failed_read_option)
  961. {
  962. if (WARNING_ENABLED(WARN_FAILED_READ))
  963. open_warn (name);
  964. }
  965. else
  966. open_error (name);
  967. }
  968. void
  969. read_diag_details (char const *name, off_t offset, size_t size)
  970. {
  971. if (ignore_failed_read_option)
  972. {
  973. if (WARNING_ENABLED(WARN_FAILED_READ))
  974. read_warn_details (name, offset, size);
  975. }
  976. else
  977. read_error_details (name, offset, size);
  978. }
  979. void
  980. readlink_diag (char const *name)
  981. {
  982. if (ignore_failed_read_option)
  983. {
  984. if (WARNING_ENABLED(WARN_FAILED_READ))
  985. readlink_warn (name);
  986. }
  987. else
  988. readlink_error (name);
  989. }
  990. void
  991. savedir_diag (char const *name)
  992. {
  993. if (ignore_failed_read_option)
  994. {
  995. if (WARNING_ENABLED(WARN_FAILED_READ))
  996. savedir_warn (name);
  997. }
  998. else
  999. savedir_error (name);
  1000. }
  1001. void
  1002. seek_diag_details (char const *name, off_t offset)
  1003. {
  1004. if (ignore_failed_read_option)
  1005. {
  1006. if (WARNING_ENABLED(WARN_FAILED_READ))
  1007. seek_warn_details (name, offset);
  1008. }
  1009. else
  1010. seek_error_details (name, offset);
  1011. }
  1012. void
  1013. stat_diag (char const *name)
  1014. {
  1015. if (ignore_failed_read_option)
  1016. {
  1017. if (WARNING_ENABLED(WARN_FAILED_READ))
  1018. stat_warn (name);
  1019. }
  1020. else
  1021. stat_error (name);
  1022. }
  1023. void
  1024. file_removed_diag (const char *name, bool top_level,
  1025. void (*diagfn) (char const *name))
  1026. {
  1027. if (!top_level && errno == ENOENT)
  1028. {
  1029. WARNOPT (WARN_FILE_REMOVED,
  1030. (0, 0, _("%s: File removed before we read it"),
  1031. quotearg_colon (name)));
  1032. set_exit_status (TAREXIT_DIFFERS);
  1033. }
  1034. else
  1035. diagfn (name);
  1036. }
  1037. /* Fork, aborting if unsuccessful. */
  1038. pid_t
  1039. xfork (void)
  1040. {
  1041. pid_t p = fork ();
  1042. if (p == (pid_t) -1)
  1043. call_arg_fatal ("fork", _("child process"));
  1044. return p;
  1045. }
  1046. /* Create a pipe, aborting if unsuccessful. */
  1047. void
  1048. xpipe (int fd[2])
  1049. {
  1050. if (pipe (fd) < 0)
  1051. call_arg_fatal ("pipe", _("interprocess channel"));
  1052. }
  1053. /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
  1054. ALIGNMENT must be nonzero. The caller must arrange for ((char *)
  1055. PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
  1056. locations. */
  1057. static void *
  1058. ptr_align (void *ptr, size_t alignment)
  1059. {
  1060. char *p0 = ptr;
  1061. char *p1 = p0 + alignment - 1;
  1062. return p1 - (size_t) p1 % alignment;
  1063. }
  1064. /* Return the address of a page-aligned buffer of at least SIZE bytes.
  1065. The caller should free *PTR when done with the buffer. */
  1066. void *
  1067. page_aligned_alloc (void **ptr, size_t size)
  1068. {
  1069. size_t alignment = getpagesize ();
  1070. size_t size1 = size + alignment;
  1071. if (size1 < size)
  1072. xalloc_die ();
  1073. *ptr = xmalloc (size1);
  1074. return ptr_align (*ptr, alignment);
  1075. }
  1076. struct namebuf
  1077. {
  1078. char *buffer; /* directory, '/', and directory member */
  1079. size_t buffer_size; /* allocated size of name_buffer */
  1080. size_t dir_length; /* length of directory part in buffer */
  1081. };
  1082. namebuf_t
  1083. namebuf_create (const char *dir)
  1084. {
  1085. namebuf_t buf = xmalloc (sizeof (*buf));
  1086. buf->buffer_size = strlen (dir) + 2;
  1087. buf->buffer = xmalloc (buf->buffer_size);
  1088. strcpy (buf->buffer, dir);
  1089. buf->dir_length = strlen (buf->buffer);
  1090. if (!ISSLASH (buf->buffer[buf->dir_length - 1]))
  1091. buf->buffer[buf->dir_length++] = DIRECTORY_SEPARATOR;
  1092. return buf;
  1093. }
  1094. void
  1095. namebuf_free (namebuf_t buf)
  1096. {
  1097. free (buf->buffer);
  1098. free (buf);
  1099. }
  1100. char *
  1101. namebuf_name (namebuf_t buf, const char *name)
  1102. {
  1103. size_t len = strlen (name);
  1104. while (buf->dir_length + len + 1 >= buf->buffer_size)
  1105. buf->buffer = x2realloc (buf->buffer, &buf->buffer_size);
  1106. strcpy (buf->buffer + buf->dir_length, name);
  1107. return buf->buffer;
  1108. }
  1109. static void
  1110. namebuf_add_dir (namebuf_t buf, const char *name)
  1111. {
  1112. static char dirsep[] = { DIRECTORY_SEPARATOR, 0 };
  1113. if (!ISSLASH (buf->buffer[buf->dir_length - 1]))
  1114. {
  1115. namebuf_name (buf, dirsep);
  1116. buf->dir_length++;
  1117. }
  1118. namebuf_name (buf, name);
  1119. buf->dir_length += strlen (name);
  1120. }
  1121. static char *
  1122. namebuf_finish (namebuf_t buf)
  1123. {
  1124. char *res = buf->buffer;
  1125. if (ISSLASH (buf->buffer[buf->dir_length - 1]))
  1126. buf->buffer[buf->dir_length] = 0;
  1127. free (buf);
  1128. return res;
  1129. }
  1130. /* Return the filenames in directory NAME, relative to the chdir_fd.
  1131. If the directory does not exist, report error if MUST_EXIST is
  1132. true.
  1133. Return NULL on errors.
  1134. */
  1135. char *
  1136. tar_savedir (const char *name, int must_exist)
  1137. {
  1138. char *ret = NULL;
  1139. DIR *dir = NULL;
  1140. int fd = openat (chdir_fd, name, open_read_flags | O_DIRECTORY);
  1141. if (fd < 0)
  1142. {
  1143. if (!must_exist && errno == ENOENT)
  1144. return NULL;
  1145. open_error (name);
  1146. }
  1147. else if (! ((dir = fdopendir (fd))
  1148. && (ret = streamsavedir (dir, savedir_sort_order))))
  1149. savedir_error (name);
  1150. if (dir ? closedir (dir) != 0 : 0 <= fd && close (fd) != 0)
  1151. savedir_error (name);
  1152. return ret;
  1153. }