misc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /* Miscellaneous functions, not really specific to GNU tar.
  2. Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
  3. 2003, 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include <system.h>
  16. #include <rmt.h>
  17. #include "common.h"
  18. #include <quotearg.h>
  19. #include <save-cwd.h>
  20. #include <xgetcwd.h>
  21. #include <unlinkdir.h>
  22. #include <utimens.h>
  23. #if HAVE_STROPTS_H
  24. # include <stropts.h>
  25. #endif
  26. #if HAVE_SYS_FILIO_H
  27. # include <sys/filio.h>
  28. #endif
  29. #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
  30. # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
  31. #endif
  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 (char **string, const char *value)
  37. {
  38. if (*string)
  39. free (*string);
  40. *string = value ? xstrdup (value) : 0;
  41. }
  42. #if 0
  43. /* This function is currently unused; perhaps it should be removed? */
  44. /* Allocate a copy of the string quoted as in C, and returns that. If
  45. the string does not have to be quoted, it returns a null pointer.
  46. The allocated copy should normally be freed with free() after the
  47. caller is done with it.
  48. This is used in one context only: generating the directory file in
  49. incremental dumps. The quoted string is not intended for human
  50. consumption; it is intended only for unquote_string. The quoting
  51. is locale-independent, so that users needn't worry about locale
  52. when reading directory files. This means that we can't use
  53. quotearg, as quotearg is locale-dependent and is meant for human
  54. consumption. */
  55. static char *
  56. quote_copy_string (const char *string)
  57. {
  58. const char *source = string;
  59. char *destination = 0;
  60. char *buffer = 0;
  61. int copying = 0;
  62. while (*source)
  63. {
  64. int character = *source++;
  65. switch (character)
  66. {
  67. case '\n': case '\\':
  68. if (!copying)
  69. {
  70. size_t length = (source - string) - 1;
  71. copying = 1;
  72. buffer = xmalloc (length + 2 + 2 * strlen (source) + 1);
  73. memcpy (buffer, string, length);
  74. destination = buffer + length;
  75. }
  76. *destination++ = '\\';
  77. *destination++ = character == '\\' ? '\\' : 'n';
  78. break;
  79. default:
  80. if (copying)
  81. *destination++ = character;
  82. break;
  83. }
  84. }
  85. if (copying)
  86. {
  87. *destination = '\0';
  88. return buffer;
  89. }
  90. return 0;
  91. }
  92. #endif
  93. /* Takes a quoted C string (like those produced by quote_copy_string)
  94. and turns it back into the un-quoted original. This is done in
  95. place. Returns 0 only if the string was not properly quoted, but
  96. completes the unquoting anyway.
  97. This is used for reading the saved directory file in incremental
  98. dumps. It is used for decoding old `N' records (demangling names).
  99. But also, it is used for decoding file arguments, would they come
  100. from the shell or a -T file, and for decoding the --exclude
  101. argument. */
  102. int
  103. unquote_string (char *string)
  104. {
  105. int result = 1;
  106. char *source = string;
  107. char *destination = string;
  108. /* Escape sequences other than \\ and \n are no longer generated by
  109. quote_copy_string, but accept them for backwards compatibility,
  110. and also because unquote_string is used for purposes other than
  111. parsing the output of quote_copy_string. */
  112. while (*source)
  113. if (*source == '\\')
  114. switch (*++source)
  115. {
  116. case '\\':
  117. *destination++ = '\\';
  118. source++;
  119. break;
  120. case 'a':
  121. *destination++ = '\a';
  122. source++;
  123. break;
  124. case 'b':
  125. *destination++ = '\b';
  126. source++;
  127. break;
  128. case 'f':
  129. *destination++ = '\f';
  130. source++;
  131. break;
  132. case 'n':
  133. *destination++ = '\n';
  134. source++;
  135. break;
  136. case 'r':
  137. *destination++ = '\r';
  138. source++;
  139. break;
  140. case 't':
  141. *destination++ = '\t';
  142. source++;
  143. break;
  144. case 'v':
  145. *destination++ = '\v';
  146. source++;
  147. break;
  148. case '?':
  149. *destination++ = 0177;
  150. source++;
  151. break;
  152. case '0':
  153. case '1':
  154. case '2':
  155. case '3':
  156. case '4':
  157. case '5':
  158. case '6':
  159. case '7':
  160. {
  161. int value = *source++ - '0';
  162. if (*source < '0' || *source > '7')
  163. {
  164. *destination++ = value;
  165. break;
  166. }
  167. value = value * 8 + *source++ - '0';
  168. if (*source < '0' || *source > '7')
  169. {
  170. *destination++ = value;
  171. break;
  172. }
  173. value = value * 8 + *source++ - '0';
  174. *destination++ = value;
  175. break;
  176. }
  177. default:
  178. result = 0;
  179. *destination++ = '\\';
  180. if (*source)
  181. *destination++ = *source++;
  182. break;
  183. }
  184. else if (source != destination)
  185. *destination++ = *source++;
  186. else
  187. source++, destination++;
  188. if (source != destination)
  189. *destination = '\0';
  190. return result;
  191. }
  192. /* Zap trailing slashes. */
  193. char *
  194. zap_slashes (char *name)
  195. {
  196. char *q;
  197. if (!name || *name == 0)
  198. return name;
  199. q = name + strlen (name) - 1;
  200. while (q > name && ISSLASH (*q))
  201. *q-- = '\0';
  202. return name;
  203. }
  204. /* Normalize FILE_NAME by removing redundant slashes and "."
  205. components, including redundant trailing slashes. Leave ".."
  206. alone, as it may be significant in the presence of symlinks and on
  207. platforms where "/.." != "/". Destructive version: modifies its
  208. argument. */
  209. static void
  210. normalize_filename_x (char *file_name)
  211. {
  212. char *name = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
  213. char *p;
  214. char const *q;
  215. char c;
  216. /* Don't squeeze leading "//" to "/", on hosts where they're distinct. */
  217. name += (DOUBLE_SLASH_IS_DISTINCT_ROOT
  218. && ISSLASH (*name) && ISSLASH (name[1]) && ! ISSLASH (name[2]));
  219. /* Omit redundant leading "." components. */
  220. for (q = p = name; (*p = *q) == '.' && ISSLASH (q[1]); p += !*q)
  221. for (q += 2; ISSLASH (*q); q++)
  222. continue;
  223. /* Copy components from Q to P, omitting redundant slashes and
  224. internal "." components. */
  225. while ((*p++ = c = *q++) != '\0')
  226. if (ISSLASH (c))
  227. while (ISSLASH (q[*q == '.']))
  228. q += (*q == '.') + 1;
  229. /* Omit redundant trailing "." component and slash. */
  230. if (2 < p - name)
  231. {
  232. p -= p[-2] == '.' && ISSLASH (p[-3]);
  233. p -= 2 < p - name && ISSLASH (p[-2]);
  234. p[-1] = '\0';
  235. }
  236. }
  237. /* Normalize NAME by removing redundant slashes and "." components,
  238. including redundant trailing slashes. Return a normalized
  239. newly-allocated copy. */
  240. char *
  241. normalize_filename (const char *name)
  242. {
  243. char *copy = NULL;
  244. if (IS_RELATIVE_FILE_NAME (name))
  245. {
  246. /* Set COPY to the absolute file name if possible.
  247. FIXME: There should be no need to get the absolute file name.
  248. getcwd is slow, it might fail, and it does not necessarily
  249. return a canonical name even when it succeeds. Perhaps we
  250. can use dev+ino pairs instead of names? */
  251. copy = xgetcwd ();
  252. if (copy)
  253. {
  254. size_t copylen = strlen (copy);
  255. bool need_separator = ! (DOUBLE_SLASH_IS_DISTINCT_ROOT
  256. && copylen == 2 && ISSLASH (copy[1]));
  257. copy = xrealloc (copy, copylen + need_separator + strlen (name) + 1);
  258. copy[copylen] = DIRECTORY_SEPARATOR;
  259. strcpy (copy + copylen + need_separator, name);
  260. }
  261. else
  262. WARN ((0, errno, _("Cannot get working directory")));
  263. }
  264. if (! copy)
  265. copy = xstrdup (name);
  266. normalize_filename_x (copy);
  267. return copy;
  268. }
  269. void
  270. replace_prefix (char **pname, const char *samp, size_t slen,
  271. const char *repl, size_t rlen)
  272. {
  273. char *name = *pname;
  274. size_t nlen = strlen (name);
  275. if (nlen > slen && memcmp (name, samp, slen) == 0 && ISSLASH (name[slen]))
  276. {
  277. if (rlen > slen)
  278. {
  279. name = xrealloc (name, nlen - slen + rlen + 1);
  280. *pname = name;
  281. }
  282. memmove (name + rlen, name + slen, nlen - slen + 1);
  283. memcpy (name, repl, rlen);
  284. }
  285. }
  286. /* Handling numbers. */
  287. /* Output fraction and trailing digits appropriate for a nanoseconds
  288. count equal to NS, but don't output unnecessary '.' or trailing
  289. zeros. */
  290. void
  291. code_ns_fraction (int ns, char *p)
  292. {
  293. if (ns == 0)
  294. *p = '\0';
  295. else
  296. {
  297. int i = 9;
  298. *p++ = '.';
  299. while (ns % 10 == 0)
  300. {
  301. ns /= 10;
  302. i--;
  303. }
  304. p[i] = '\0';
  305. for (;;)
  306. {
  307. p[--i] = '0' + ns % 10;
  308. if (i == 0)
  309. break;
  310. ns /= 10;
  311. }
  312. }
  313. }
  314. char const *
  315. code_timespec (struct timespec t, char sbuf[TIMESPEC_STRSIZE_BOUND])
  316. {
  317. time_t s = t.tv_sec;
  318. int ns = t.tv_nsec;
  319. char *np;
  320. bool negative = s < 0;
  321. /* ignore invalid values of ns */
  322. if (BILLION <= ns || ns < 0)
  323. ns = 0;
  324. if (negative && ns != 0)
  325. {
  326. s++;
  327. ns = BILLION - ns;
  328. }
  329. np = umaxtostr (negative ? - (uintmax_t) s : (uintmax_t) s, sbuf + 1);
  330. if (negative)
  331. *--np = '-';
  332. code_ns_fraction (ns, sbuf + UINTMAX_STRSIZE_BOUND);
  333. return np;
  334. }
  335. /* File handling. */
  336. /* Saved names in case backup needs to be undone. */
  337. static char *before_backup_name;
  338. static char *after_backup_name;
  339. /* Return 1 if FILE_NAME is obviously "." or "/". */
  340. static bool
  341. must_be_dot_or_slash (char const *file_name)
  342. {
  343. file_name += FILE_SYSTEM_PREFIX_LEN (file_name);
  344. if (ISSLASH (file_name[0]))
  345. {
  346. for (;;)
  347. if (ISSLASH (file_name[1]))
  348. file_name++;
  349. else if (file_name[1] == '.'
  350. && ISSLASH (file_name[2 + (file_name[2] == '.')]))
  351. file_name += 2 + (file_name[2] == '.');
  352. else
  353. return ! file_name[1];
  354. }
  355. else
  356. {
  357. while (file_name[0] == '.' && ISSLASH (file_name[1]))
  358. {
  359. file_name += 2;
  360. while (ISSLASH (*file_name))
  361. file_name++;
  362. }
  363. return ! file_name[0] || (file_name[0] == '.' && ! file_name[1]);
  364. }
  365. }
  366. /* Some implementations of rmdir let you remove '.' or '/'.
  367. Report an error with errno set to zero for obvious cases of this;
  368. otherwise call rmdir. */
  369. static int
  370. safer_rmdir (const char *file_name)
  371. {
  372. if (must_be_dot_or_slash (file_name))
  373. {
  374. errno = 0;
  375. return -1;
  376. }
  377. return rmdir (file_name);
  378. }
  379. /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
  380. then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
  381. recursively; otherwise, remove it only if it is empty. If FILE_NAME is
  382. a directory that cannot be removed (e.g., because it is nonempty)
  383. and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
  384. Return 0 on error, with errno set; if FILE_NAME is obviously the working
  385. directory return zero with errno set to zero. */
  386. int
  387. remove_any_file (const char *file_name, enum remove_option option)
  388. {
  389. /* Try unlink first if we cannot unlink directories, as this saves
  390. us a system call in the common case where we're removing a
  391. non-directory. */
  392. bool try_unlink_first = cannot_unlink_dir ();
  393. if (try_unlink_first)
  394. {
  395. if (unlink (file_name) == 0)
  396. return 1;
  397. /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
  398. directory without appropriate privileges, but many Linux
  399. kernels return the more-sensible EISDIR. */
  400. if (errno != EPERM && errno != EISDIR)
  401. return 0;
  402. }
  403. if (safer_rmdir (file_name) == 0)
  404. return 1;
  405. switch (errno)
  406. {
  407. case ENOTDIR:
  408. return !try_unlink_first && unlink (file_name) == 0;
  409. case 0:
  410. case EEXIST:
  411. #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
  412. case ENOTEMPTY:
  413. #endif
  414. switch (option)
  415. {
  416. case ORDINARY_REMOVE_OPTION:
  417. break;
  418. case WANT_DIRECTORY_REMOVE_OPTION:
  419. return -1;
  420. case RECURSIVE_REMOVE_OPTION:
  421. {
  422. char *directory = savedir (file_name);
  423. char const *entry;
  424. size_t entrylen;
  425. if (! directory)
  426. return 0;
  427. for (entry = directory;
  428. (entrylen = strlen (entry)) != 0;
  429. entry += entrylen + 1)
  430. {
  431. char *file_name_buffer = new_name (file_name, entry);
  432. int r = remove_any_file (file_name_buffer,
  433. RECURSIVE_REMOVE_OPTION);
  434. int e = errno;
  435. free (file_name_buffer);
  436. if (! r)
  437. {
  438. free (directory);
  439. errno = e;
  440. return 0;
  441. }
  442. }
  443. free (directory);
  444. return safer_rmdir (file_name) == 0;
  445. }
  446. }
  447. break;
  448. }
  449. return 0;
  450. }
  451. /* Check if FILE_NAME already exists and make a backup of it right now.
  452. Return success (nonzero) only if the backup is either unneeded, or
  453. successful. For now, directories are considered to never need
  454. backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
  455. so, we do not have to backup block or character devices, nor remote
  456. entities. */
  457. bool
  458. maybe_backup_file (const char *file_name, bool this_is_the_archive)
  459. {
  460. struct stat file_stat;
  461. assign_string (&before_backup_name, file_name);
  462. /* A run situation may exist between Emacs or other GNU programs trying to
  463. make a backup for the same file simultaneously. If theoretically
  464. possible, real problems are unlikely. Doing any better would require a
  465. convention, GNU-wide, for all programs doing backups. */
  466. assign_string (&after_backup_name, 0);
  467. /* Check if we really need to backup the file. */
  468. if (this_is_the_archive && _remdev (file_name))
  469. return true;
  470. if (stat (file_name, &file_stat))
  471. {
  472. if (errno == ENOENT)
  473. return true;
  474. stat_error (file_name);
  475. return false;
  476. }
  477. if (S_ISDIR (file_stat.st_mode))
  478. return true;
  479. if (this_is_the_archive
  480. && (S_ISBLK (file_stat.st_mode) || S_ISCHR (file_stat.st_mode)))
  481. return true;
  482. after_backup_name = find_backup_file_name (file_name, backup_type);
  483. if (! after_backup_name)
  484. xalloc_die ();
  485. if (rename (before_backup_name, after_backup_name) == 0)
  486. {
  487. if (verbose_option)
  488. fprintf (stdlis, _("Renaming %s to %s\n"),
  489. quote_n (0, before_backup_name),
  490. quote_n (1, after_backup_name));
  491. return true;
  492. }
  493. else
  494. {
  495. /* The backup operation failed. */
  496. int e = errno;
  497. ERROR ((0, e, _("%s: Cannot rename to %s"),
  498. quotearg_colon (before_backup_name),
  499. quote_n (1, after_backup_name)));
  500. assign_string (&after_backup_name, 0);
  501. return false;
  502. }
  503. }
  504. /* Try to restore the recently backed up file to its original name.
  505. This is usually only needed after a failed extraction. */
  506. void
  507. undo_last_backup (void)
  508. {
  509. if (after_backup_name)
  510. {
  511. if (rename (after_backup_name, before_backup_name) != 0)
  512. {
  513. int e = errno;
  514. ERROR ((0, e, _("%s: Cannot rename to %s"),
  515. quotearg_colon (after_backup_name),
  516. quote_n (1, before_backup_name)));
  517. }
  518. if (verbose_option)
  519. fprintf (stdlis, _("Renaming %s back to %s\n"),
  520. quote_n (0, after_backup_name),
  521. quote_n (1, before_backup_name));
  522. assign_string (&after_backup_name, 0);
  523. }
  524. }
  525. /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
  526. int
  527. deref_stat (bool deref, char const *name, struct stat *buf)
  528. {
  529. return deref ? stat (name, buf) : lstat (name, buf);
  530. }
  531. /* Set FD's (i.e., FILE's) access time to TIMESPEC[0]. If that's not
  532. possible to do by itself, set its access and data modification
  533. times to TIMESPEC[0] and TIMESPEC[1], respectively. */
  534. int
  535. set_file_atime (int fd, char const *file, struct timespec const timespec[2])
  536. {
  537. #ifdef _FIOSATIME
  538. if (0 <= fd)
  539. {
  540. struct timeval timeval;
  541. timeval.tv_sec = timespec[0].tv_sec;
  542. timeval.tv_usec = timespec[0].tv_nsec / 1000;
  543. if (ioctl (fd, _FIOSATIME, &timeval) == 0)
  544. return 0;
  545. }
  546. #endif
  547. return gl_futimens (fd, file, timespec);
  548. }
  549. /* A description of a working directory. */
  550. struct wd
  551. {
  552. /* The directory's name. */
  553. char const *name;
  554. /* A negative value if no attempt has been made to save the
  555. directory, 0 if it was saved successfully, and a positive errno
  556. value if it was not saved successfully. */
  557. int err;
  558. /* The saved version of the directory, if ERR == 0. */
  559. struct saved_cwd saved_cwd;
  560. };
  561. /* A vector of chdir targets. wd[0] is the initial working directory. */
  562. static struct wd *wd;
  563. /* The number of working directories in the vector. */
  564. static size_t wd_count;
  565. /* The allocated size of the vector. */
  566. static size_t wd_alloc;
  567. int
  568. chdir_count ()
  569. {
  570. if (wd_count == 0)
  571. return wd_count;
  572. return wd_count - 1;
  573. }
  574. /* DIR is the operand of a -C option; add it to vector of chdir targets,
  575. and return the index of its location. */
  576. int
  577. chdir_arg (char const *dir)
  578. {
  579. if (wd_count == wd_alloc)
  580. {
  581. if (wd_alloc == 0)
  582. {
  583. wd_alloc = 2;
  584. wd = xmalloc (sizeof *wd * wd_alloc);
  585. }
  586. else
  587. wd = x2nrealloc (wd, &wd_alloc, sizeof *wd);
  588. if (! wd_count)
  589. {
  590. wd[wd_count].name = ".";
  591. wd[wd_count].err = -1;
  592. wd_count++;
  593. }
  594. }
  595. /* Optimize the common special case of the working directory,
  596. or the working directory as a prefix. */
  597. if (dir[0])
  598. {
  599. while (dir[0] == '.' && ISSLASH (dir[1]))
  600. for (dir += 2; ISSLASH (*dir); dir++)
  601. continue;
  602. if (! dir[dir[0] == '.'])
  603. return wd_count - 1;
  604. }
  605. wd[wd_count].name = dir;
  606. wd[wd_count].err = -1;
  607. return wd_count++;
  608. }
  609. /* Change to directory I. If I is 0, change to the initial working
  610. directory; otherwise, I must be a value returned by chdir_arg. */
  611. void
  612. chdir_do (int i)
  613. {
  614. static int previous;
  615. if (previous != i)
  616. {
  617. struct wd *prev = &wd[previous];
  618. struct wd *curr = &wd[i];
  619. if (prev->err < 0)
  620. {
  621. prev->err = 0;
  622. if (save_cwd (&prev->saved_cwd) != 0)
  623. prev->err = errno;
  624. else if (0 <= prev->saved_cwd.desc)
  625. {
  626. /* Make sure we still have at least one descriptor available. */
  627. int fd1 = prev->saved_cwd.desc;
  628. int fd2 = dup (fd1);
  629. if (0 <= fd2)
  630. close (fd2);
  631. else if (errno == EMFILE)
  632. {
  633. /* Force restore_cwd to use chdir_long. */
  634. close (fd1);
  635. prev->saved_cwd.desc = -1;
  636. prev->saved_cwd.name = xgetcwd ();
  637. if (! prev->saved_cwd.name)
  638. prev->err = errno;
  639. }
  640. else
  641. prev->err = errno;
  642. }
  643. }
  644. if (0 <= curr->err)
  645. {
  646. int err = curr->err;
  647. if (err == 0 && restore_cwd (&curr->saved_cwd) != 0)
  648. err = errno;
  649. if (err)
  650. FATAL_ERROR ((0, err, _("Cannot restore working directory")));
  651. }
  652. else
  653. {
  654. if (i && ! ISSLASH (curr->name[0]))
  655. chdir_do (i - 1);
  656. if (chdir (curr->name) != 0)
  657. chdir_fatal (curr->name);
  658. }
  659. previous = i;
  660. }
  661. }
  662. void
  663. close_diag (char const *name)
  664. {
  665. if (ignore_failed_read_option)
  666. close_warn (name);
  667. else
  668. close_error (name);
  669. }
  670. void
  671. open_diag (char const *name)
  672. {
  673. if (ignore_failed_read_option)
  674. open_warn (name);
  675. else
  676. open_error (name);
  677. }
  678. void
  679. read_diag_details (char const *name, off_t offset, size_t size)
  680. {
  681. if (ignore_failed_read_option)
  682. read_warn_details (name, offset, size);
  683. else
  684. read_error_details (name, offset, size);
  685. }
  686. void
  687. readlink_diag (char const *name)
  688. {
  689. if (ignore_failed_read_option)
  690. readlink_warn (name);
  691. else
  692. readlink_error (name);
  693. }
  694. void
  695. savedir_diag (char const *name)
  696. {
  697. if (ignore_failed_read_option)
  698. savedir_warn (name);
  699. else
  700. savedir_error (name);
  701. }
  702. void
  703. seek_diag_details (char const *name, off_t offset)
  704. {
  705. if (ignore_failed_read_option)
  706. seek_warn_details (name, offset);
  707. else
  708. seek_error_details (name, offset);
  709. }
  710. void
  711. stat_diag (char const *name)
  712. {
  713. if (ignore_failed_read_option)
  714. stat_warn (name);
  715. else
  716. stat_error (name);
  717. }
  718. void
  719. file_removed_diag (const char *name, bool top_level,
  720. void (*diagfn) (char const *name))
  721. {
  722. if (!top_level && errno == ENOENT)
  723. {
  724. WARNOPT (WARN_FILE_REMOVED,
  725. (0, 0, _("%s: File removed before we read it"),
  726. quotearg_colon (name)));
  727. set_exit_status (TAREXIT_DIFFERS);
  728. }
  729. else
  730. diagfn (name);
  731. }
  732. void
  733. dir_removed_diag (const char *name, bool top_level,
  734. void (*diagfn) (char const *name))
  735. {
  736. if (!top_level && errno == ENOENT)
  737. {
  738. WARNOPT (WARN_FILE_REMOVED,
  739. (0, 0, _("%s: Directory removed before we read it"),
  740. quotearg_colon (name)));
  741. set_exit_status (TAREXIT_DIFFERS);
  742. }
  743. else
  744. diagfn (name);
  745. }
  746. void
  747. write_fatal_details (char const *name, ssize_t status, size_t size)
  748. {
  749. write_error_details (name, status, size);
  750. fatal_exit ();
  751. }
  752. /* Fork, aborting if unsuccessful. */
  753. pid_t
  754. xfork (void)
  755. {
  756. pid_t p = fork ();
  757. if (p == (pid_t) -1)
  758. call_arg_fatal ("fork", _("child process"));
  759. return p;
  760. }
  761. /* Create a pipe, aborting if unsuccessful. */
  762. void
  763. xpipe (int fd[2])
  764. {
  765. if (pipe (fd) < 0)
  766. call_arg_fatal ("pipe", _("interprocess channel"));
  767. }
  768. /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
  769. ALIGNMENT must be nonzero. The caller must arrange for ((char *)
  770. PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
  771. locations. */
  772. static inline void *
  773. ptr_align (void *ptr, size_t alignment)
  774. {
  775. char *p0 = ptr;
  776. char *p1 = p0 + alignment - 1;
  777. return p1 - (size_t) p1 % alignment;
  778. }
  779. /* Return the address of a page-aligned buffer of at least SIZE bytes.
  780. The caller should free *PTR when done with the buffer. */
  781. void *
  782. page_aligned_alloc (void **ptr, size_t size)
  783. {
  784. size_t alignment = getpagesize ();
  785. size_t size1 = size + alignment;
  786. if (size1 < size)
  787. xalloc_die ();
  788. *ptr = xmalloc (size1);
  789. return ptr_align (*ptr, alignment);
  790. }
  791. struct namebuf
  792. {
  793. char *buffer; /* directory, `/', and directory member */
  794. size_t buffer_size; /* allocated size of name_buffer */
  795. size_t dir_length; /* length of directory part in buffer */
  796. };
  797. namebuf_t
  798. namebuf_create (const char *dir)
  799. {
  800. namebuf_t buf = xmalloc (sizeof (*buf));
  801. buf->buffer_size = strlen (dir) + 2;
  802. buf->buffer = xmalloc (buf->buffer_size);
  803. strcpy (buf->buffer, dir);
  804. buf->dir_length = strlen (buf->buffer);
  805. if (!ISSLASH (buf->buffer[buf->dir_length - 1]))
  806. buf->buffer[buf->dir_length++] = DIRECTORY_SEPARATOR;
  807. return buf;
  808. }
  809. void
  810. namebuf_free (namebuf_t buf)
  811. {
  812. free (buf->buffer);
  813. free (buf);
  814. }
  815. char *
  816. namebuf_name (namebuf_t buf, const char *name)
  817. {
  818. size_t len = strlen (name);
  819. while (buf->dir_length + len + 1 >= buf->buffer_size)
  820. buf->buffer = x2realloc (buf->buffer, &buf->buffer_size);
  821. strcpy (buf->buffer + buf->dir_length, name);
  822. return buf->buffer;
  823. }