misc.c 22 KB

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