misc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /* Miscellaneous functions, not really specific to GNU tar.
  2. Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
  3. 2003, 2004 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 2, 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. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #include "system.h"
  16. #include "rmt.h"
  17. #include "common.h"
  18. #include <quotearg.h>
  19. #include <save-cwd.h>
  20. static void call_arg_fatal (char const *, char const *)
  21. __attribute__ ((noreturn));
  22. /* Handling strings. */
  23. /* Assign STRING to a copy of VALUE if not zero, or to zero. If
  24. STRING was nonzero, it is freed first. */
  25. void
  26. assign_string (char **string, const char *value)
  27. {
  28. if (*string)
  29. free (*string);
  30. *string = value ? xstrdup (value) : 0;
  31. }
  32. /* Allocate a copy of the string quoted as in C, and returns that. If
  33. the string does not have to be quoted, it returns a null pointer.
  34. The allocated copy should normally be freed with free() after the
  35. caller is done with it.
  36. This is used in one context only: generating the directory file in
  37. incremental dumps. The quoted string is not intended for human
  38. consumption; it is intended only for unquote_string. The quoting
  39. is locale-independent, so that users needn't worry about locale
  40. when reading directory files. This means that we can't use
  41. quotearg, as quotearg is locale-dependent and is meant for human
  42. consumption. */
  43. char *
  44. quote_copy_string (const char *string)
  45. {
  46. const char *source = string;
  47. char *destination = 0;
  48. char *buffer = 0;
  49. int copying = 0;
  50. while (*source)
  51. {
  52. int character = *source++;
  53. switch (character)
  54. {
  55. case '\n': case '\\':
  56. if (!copying)
  57. {
  58. size_t length = (source - string) - 1;
  59. copying = 1;
  60. buffer = xmalloc (length + 2 + 2 * strlen (source) + 1);
  61. memcpy (buffer, string, length);
  62. destination = buffer + length;
  63. }
  64. *destination++ = '\\';
  65. *destination++ = character == '\\' ? '\\' : 'n';
  66. break;
  67. default:
  68. if (copying)
  69. *destination++ = character;
  70. break;
  71. }
  72. }
  73. if (copying)
  74. {
  75. *destination = '\0';
  76. return buffer;
  77. }
  78. return 0;
  79. }
  80. /* Takes a quoted C string (like those produced by quote_copy_string)
  81. and turns it back into the un-quoted original. This is done in
  82. place. Returns 0 only if the string was not properly quoted, but
  83. completes the unquoting anyway.
  84. This is used for reading the saved directory file in incremental
  85. dumps. It is used for decoding old `N' records (demangling names).
  86. But also, it is used for decoding file arguments, would they come
  87. from the shell or a -T file, and for decoding the --exclude
  88. argument. */
  89. int
  90. unquote_string (char *string)
  91. {
  92. int result = 1;
  93. char *source = string;
  94. char *destination = string;
  95. /* Escape sequences other than \\ and \n are no longer generated by
  96. quote_copy_string, but accept them for backwards compatibility,
  97. and also because unquote_string is used for purposes other than
  98. parsing the output of quote_copy_string. */
  99. while (*source)
  100. if (*source == '\\')
  101. switch (*++source)
  102. {
  103. case '\\':
  104. *destination++ = '\\';
  105. source++;
  106. break;
  107. case 'n':
  108. *destination++ = '\n';
  109. source++;
  110. break;
  111. case 't':
  112. *destination++ = '\t';
  113. source++;
  114. break;
  115. case 'f':
  116. *destination++ = '\f';
  117. source++;
  118. break;
  119. case 'b':
  120. *destination++ = '\b';
  121. source++;
  122. break;
  123. case 'r':
  124. *destination++ = '\r';
  125. source++;
  126. break;
  127. case '?':
  128. *destination++ = 0177;
  129. source++;
  130. break;
  131. case '0':
  132. case '1':
  133. case '2':
  134. case '3':
  135. case '4':
  136. case '5':
  137. case '6':
  138. case '7':
  139. {
  140. int value = *source++ - '0';
  141. if (*source < '0' || *source > '7')
  142. {
  143. *destination++ = value;
  144. break;
  145. }
  146. value = value * 8 + *source++ - '0';
  147. if (*source < '0' || *source > '7')
  148. {
  149. *destination++ = value;
  150. break;
  151. }
  152. value = value * 8 + *source++ - '0';
  153. *destination++ = value;
  154. break;
  155. }
  156. default:
  157. result = 0;
  158. *destination++ = '\\';
  159. if (*source)
  160. *destination++ = *source++;
  161. break;
  162. }
  163. else if (source != destination)
  164. *destination++ = *source++;
  165. else
  166. source++, destination++;
  167. if (source != destination)
  168. *destination = '\0';
  169. return result;
  170. }
  171. /* File handling. */
  172. /* Saved names in case backup needs to be undone. */
  173. static char *before_backup_name;
  174. static char *after_backup_name;
  175. /* Return 1 if FILE_NAME is obviously "." or "/". */
  176. static bool
  177. must_be_dot_or_slash (char const *file_name)
  178. {
  179. file_name += FILE_SYSTEM_PREFIX_LEN (file_name);
  180. if (ISSLASH (file_name[0]))
  181. {
  182. for (;;)
  183. if (ISSLASH (file_name[1]))
  184. file_name++;
  185. else if (file_name[1] == '.'
  186. && ISSLASH (file_name[2 + (file_name[2] == '.')]))
  187. file_name += 2 + (file_name[2] == '.');
  188. else
  189. return ! file_name[1];
  190. }
  191. else
  192. {
  193. while (file_name[0] == '.' && ISSLASH (file_name[1]))
  194. {
  195. file_name += 2;
  196. while (ISSLASH (*file_name))
  197. file_name++;
  198. }
  199. return ! file_name[0] || (file_name[0] == '.' && ! file_name[1]);
  200. }
  201. }
  202. /* Some implementations of rmdir let you remove '.' or '/'.
  203. Report an error with errno set to zero for obvious cases of this;
  204. otherwise call rmdir. */
  205. static int
  206. safer_rmdir (const char *file_name)
  207. {
  208. if (must_be_dot_or_slash (file_name))
  209. {
  210. errno = 0;
  211. return -1;
  212. }
  213. return rmdir (file_name);
  214. }
  215. /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
  216. then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
  217. recursively; otherwise, remove it only if it is empty. If FILE_NAME is
  218. a directory that cannot be removed (e.g., because it is nonempty)
  219. and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
  220. Return 0 on error, with errno set; if FILE_NAME is obviously the working
  221. directory return zero with errno set to zero. */
  222. int
  223. remove_any_file (const char *file_name, enum remove_option option)
  224. {
  225. /* Try unlink first if we are not root, as this saves us a system
  226. call in the common case where we're removing a non-directory. */
  227. if (! we_are_root)
  228. {
  229. if (unlink (file_name) == 0)
  230. return 1;
  231. /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
  232. directory without appropriate privileges, but many Linux
  233. kernels return the more-sensible EISDIR. */
  234. if (errno != EPERM && errno != EISDIR)
  235. return 0;
  236. }
  237. if (safer_rmdir (file_name) == 0)
  238. return 1;
  239. switch (errno)
  240. {
  241. case ENOTDIR:
  242. return we_are_root && unlink (file_name) == 0;
  243. case 0:
  244. case EEXIST:
  245. #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
  246. case ENOTEMPTY:
  247. #endif
  248. switch (option)
  249. {
  250. case ORDINARY_REMOVE_OPTION:
  251. break;
  252. case WANT_DIRECTORY_REMOVE_OPTION:
  253. return -1;
  254. case RECURSIVE_REMOVE_OPTION:
  255. {
  256. char *directory = savedir (file_name);
  257. char const *entry;
  258. size_t entrylen;
  259. if (! directory)
  260. return 0;
  261. for (entry = directory;
  262. (entrylen = strlen (entry)) != 0;
  263. entry += entrylen + 1)
  264. {
  265. char *file_name_buffer = new_name (file_name, entry);
  266. int r = remove_any_file (file_name_buffer, 1);
  267. int e = errno;
  268. free (file_name_buffer);
  269. if (! r)
  270. {
  271. free (directory);
  272. errno = e;
  273. return 0;
  274. }
  275. }
  276. free (directory);
  277. return safer_rmdir (file_name) == 0;
  278. }
  279. }
  280. break;
  281. }
  282. return 0;
  283. }
  284. /* Check if FILE_NAME already exists and make a backup of it right now.
  285. Return success (nonzero) only if the backup is either unneeded, or
  286. successful. For now, directories are considered to never need
  287. backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
  288. so, we do not have to backup block or character devices, nor remote
  289. entities. */
  290. bool
  291. maybe_backup_file (const char *file_name, int this_is_the_archive)
  292. {
  293. struct stat file_stat;
  294. /* Check if we really need to backup the file. */
  295. if (this_is_the_archive && _remdev (file_name))
  296. return true;
  297. if (stat (file_name, &file_stat))
  298. {
  299. if (errno == ENOENT)
  300. return true;
  301. stat_error (file_name);
  302. return false;
  303. }
  304. if (S_ISDIR (file_stat.st_mode))
  305. return true;
  306. if (this_is_the_archive
  307. && (S_ISBLK (file_stat.st_mode) || S_ISCHR (file_stat.st_mode)))
  308. return true;
  309. assign_string (&before_backup_name, file_name);
  310. /* A run situation may exist between Emacs or other GNU programs trying to
  311. make a backup for the same file simultaneously. If theoretically
  312. possible, real problems are unlikely. Doing any better would require a
  313. convention, GNU-wide, for all programs doing backups. */
  314. assign_string (&after_backup_name, 0);
  315. after_backup_name = find_backup_file_name (file_name, backup_type);
  316. if (! after_backup_name)
  317. xalloc_die ();
  318. if (rename (before_backup_name, after_backup_name) == 0)
  319. {
  320. if (verbose_option)
  321. fprintf (stdlis, _("Renaming %s to %s\n"),
  322. quote_n (0, before_backup_name),
  323. quote_n (1, after_backup_name));
  324. return true;
  325. }
  326. else
  327. {
  328. /* The backup operation failed. */
  329. int e = errno;
  330. ERROR ((0, e, _("%s: Cannot rename to %s"),
  331. quotearg_colon (before_backup_name),
  332. quote_n (1, after_backup_name)));
  333. assign_string (&after_backup_name, 0);
  334. return false;
  335. }
  336. }
  337. /* Try to restore the recently backed up file to its original name.
  338. This is usually only needed after a failed extraction. */
  339. void
  340. undo_last_backup (void)
  341. {
  342. if (after_backup_name)
  343. {
  344. if (rename (after_backup_name, before_backup_name) != 0)
  345. {
  346. int e = errno;
  347. ERROR ((0, e, _("%s: Cannot rename to %s"),
  348. quotearg_colon (after_backup_name),
  349. quote_n (1, before_backup_name)));
  350. }
  351. if (verbose_option)
  352. fprintf (stdlis, _("Renaming %s back to %s\n"),
  353. quote_n (0, after_backup_name),
  354. quote_n (1, before_backup_name));
  355. assign_string (&after_backup_name, 0);
  356. }
  357. }
  358. /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
  359. int
  360. deref_stat (bool deref, char const *name, struct stat *buf)
  361. {
  362. return deref ? stat (name, buf) : lstat (name, buf);
  363. }
  364. /* A description of a working directory. */
  365. struct wd
  366. {
  367. char const *name;
  368. int saved;
  369. struct saved_cwd saved_cwd;
  370. };
  371. /* A vector of chdir targets. wd[0] is the initial working directory. */
  372. static struct wd *wd;
  373. /* The number of working directories in the vector. */
  374. static size_t wds;
  375. /* The allocated size of the vector. */
  376. static size_t wd_alloc;
  377. /* DIR is the operand of a -C option; add it to vector of chdir targets,
  378. and return the index of its location. */
  379. int
  380. chdir_arg (char const *dir)
  381. {
  382. if (wds == wd_alloc)
  383. {
  384. wd_alloc = 2 * (wd_alloc + 1);
  385. wd = xrealloc (wd, sizeof *wd * wd_alloc);
  386. if (! wds)
  387. {
  388. wd[wds].name = ".";
  389. wd[wds].saved = 0;
  390. wds++;
  391. }
  392. }
  393. /* Optimize the common special case of the working directory,
  394. or the working directory as a prefix. */
  395. if (dir[0])
  396. {
  397. while (dir[0] == '.' && ISSLASH (dir[1]))
  398. for (dir += 2; ISSLASH (*dir); dir++)
  399. continue;
  400. if (! dir[dir[0] == '.'])
  401. return wds - 1;
  402. }
  403. wd[wds].name = dir;
  404. wd[wds].saved = 0;
  405. return wds++;
  406. }
  407. /* Change to directory I. If I is 0, change to the initial working
  408. directory; otherwise, I must be a value returned by chdir_arg. */
  409. void
  410. chdir_do (int i)
  411. {
  412. static int previous;
  413. if (previous != i)
  414. {
  415. struct wd *prev = &wd[previous];
  416. struct wd *curr = &wd[i];
  417. if (! prev->saved)
  418. {
  419. prev->saved = 1;
  420. if (save_cwd (&prev->saved_cwd) != 0)
  421. FATAL_ERROR ((0, 0, _("Cannot save working directory")));
  422. }
  423. if (curr->saved)
  424. {
  425. if (restore_cwd (&curr->saved_cwd))
  426. FATAL_ERROR ((0, 0, _("Cannot change working directory")));
  427. }
  428. else
  429. {
  430. if (i && ! ISSLASH (curr->name[0]))
  431. chdir_do (i - 1);
  432. if (chdir (curr->name) != 0)
  433. chdir_fatal (curr->name);
  434. }
  435. previous = i;
  436. }
  437. }
  438. /* Decode MODE from its binary form in a stat structure, and encode it
  439. into a 9-byte string STRING, terminated with a NUL. */
  440. void
  441. decode_mode (mode_t mode, char *string)
  442. {
  443. *string++ = mode & S_IRUSR ? 'r' : '-';
  444. *string++ = mode & S_IWUSR ? 'w' : '-';
  445. *string++ = (mode & S_ISUID
  446. ? (mode & S_IXUSR ? 's' : 'S')
  447. : (mode & S_IXUSR ? 'x' : '-'));
  448. *string++ = mode & S_IRGRP ? 'r' : '-';
  449. *string++ = mode & S_IWGRP ? 'w' : '-';
  450. *string++ = (mode & S_ISGID
  451. ? (mode & S_IXGRP ? 's' : 'S')
  452. : (mode & S_IXGRP ? 'x' : '-'));
  453. *string++ = mode & S_IROTH ? 'r' : '-';
  454. *string++ = mode & S_IWOTH ? 'w' : '-';
  455. *string++ = (mode & S_ISVTX
  456. ? (mode & S_IXOTH ? 't' : 'T')
  457. : (mode & S_IXOTH ? 'x' : '-'));
  458. *string = '\0';
  459. }
  460. /* Report an error associated with the system call CALL and the
  461. optional name NAME. */
  462. static void
  463. call_arg_error (char const *call, char const *name)
  464. {
  465. int e = errno;
  466. ERROR ((0, e, _("%s: Cannot %s"), quotearg_colon (name), call));
  467. }
  468. /* Report a fatal error associated with the system call CALL and
  469. the optional file name NAME. */
  470. static void
  471. call_arg_fatal (char const *call, char const *name)
  472. {
  473. int e = errno;
  474. FATAL_ERROR ((0, e, _("%s: Cannot %s"), quotearg_colon (name), call));
  475. }
  476. /* Report a warning associated with the system call CALL and
  477. the optional file name NAME. */
  478. static void
  479. call_arg_warn (char const *call, char const *name)
  480. {
  481. int e = errno;
  482. WARN ((0, e, _("%s: Warning: Cannot %s"), quotearg_colon (name), call));
  483. }
  484. void
  485. chdir_fatal (char const *name)
  486. {
  487. call_arg_fatal ("chdir", name);
  488. }
  489. void
  490. chmod_error_details (char const *name, mode_t mode)
  491. {
  492. int e = errno;
  493. char buf[10];
  494. decode_mode (mode, buf);
  495. ERROR ((0, e, _("%s: Cannot change mode to %s"),
  496. quotearg_colon (name), buf));
  497. }
  498. void
  499. chown_error_details (char const *name, uid_t uid, gid_t gid)
  500. {
  501. int e = errno;
  502. ERROR ((0, e, _("%s: Cannot change ownership to uid %lu, gid %lu"),
  503. quotearg_colon (name), (unsigned long) uid, (unsigned long) gid));
  504. }
  505. void
  506. close_error (char const *name)
  507. {
  508. call_arg_error ("close", name);
  509. }
  510. void
  511. close_warn (char const *name)
  512. {
  513. call_arg_warn ("close", name);
  514. }
  515. void
  516. close_diag (char const *name)
  517. {
  518. if (ignore_failed_read_option)
  519. close_warn (name);
  520. else
  521. close_error (name);
  522. }
  523. void
  524. exec_fatal (char const *name)
  525. {
  526. call_arg_fatal ("exec", name);
  527. }
  528. void
  529. link_error (char const *target, char const *source)
  530. {
  531. int e = errno;
  532. ERROR ((0, e, _("%s: Cannot hard link to %s"),
  533. quotearg_colon (source), quote_n (1, target)));
  534. }
  535. void
  536. mkdir_error (char const *name)
  537. {
  538. call_arg_error ("mkdir", name);
  539. }
  540. void
  541. mkfifo_error (char const *name)
  542. {
  543. call_arg_error ("mkfifo", name);
  544. }
  545. void
  546. mknod_error (char const *name)
  547. {
  548. call_arg_error ("mknod", name);
  549. }
  550. void
  551. open_error (char const *name)
  552. {
  553. call_arg_error ("open", name);
  554. }
  555. void
  556. open_fatal (char const *name)
  557. {
  558. call_arg_fatal ("open", name);
  559. }
  560. void
  561. open_warn (char const *name)
  562. {
  563. call_arg_warn ("open", name);
  564. }
  565. void
  566. open_diag (char const *name)
  567. {
  568. if (ignore_failed_read_option)
  569. open_warn (name);
  570. else
  571. open_error (name);
  572. }
  573. void
  574. read_error (char const *name)
  575. {
  576. call_arg_error ("read", name);
  577. }
  578. void
  579. read_error_details (char const *name, off_t offset, size_t size)
  580. {
  581. char buf[UINTMAX_STRSIZE_BOUND];
  582. int e = errno;
  583. ERROR ((0, e,
  584. ngettext ("%s: Read error at byte %s, reading %lu byte",
  585. "%s: Read error at byte %s, reading %lu bytes",
  586. size),
  587. quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
  588. (unsigned long) size));
  589. }
  590. void
  591. read_warn_details (char const *name, off_t offset, size_t size)
  592. {
  593. char buf[UINTMAX_STRSIZE_BOUND];
  594. int e = errno;
  595. WARN ((0, e,
  596. ngettext ("%s: Warning: Read error at byte %s, reading %lu byte",
  597. "%s: Warning: Read error at byte %s, reading %lu bytes",
  598. size),
  599. quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
  600. (unsigned long) size));
  601. }
  602. void
  603. read_diag_details (char const *name, off_t offset, size_t size)
  604. {
  605. if (ignore_failed_read_option)
  606. read_warn_details (name, offset, size);
  607. else
  608. read_error_details (name, offset, size);
  609. }
  610. void
  611. read_fatal (char const *name)
  612. {
  613. call_arg_fatal ("read", name);
  614. }
  615. void
  616. read_fatal_details (char const *name, off_t offset, size_t size)
  617. {
  618. char buf[UINTMAX_STRSIZE_BOUND];
  619. int e = errno;
  620. FATAL_ERROR ((0, e,
  621. ngettext ("%s: Read error at byte %s, reading %lu byte",
  622. "%s: Read error at byte %s, reading %lu bytes",
  623. size),
  624. quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
  625. (unsigned long) size));
  626. }
  627. void
  628. readlink_error (char const *name)
  629. {
  630. call_arg_error ("readlink", name);
  631. }
  632. void
  633. readlink_warn (char const *name)
  634. {
  635. call_arg_warn ("readlink", name);
  636. }
  637. void
  638. readlink_diag (char const *name)
  639. {
  640. if (ignore_failed_read_option)
  641. readlink_warn (name);
  642. else
  643. readlink_error (name);
  644. }
  645. void
  646. savedir_error (char const *name)
  647. {
  648. call_arg_error ("savedir", name);
  649. }
  650. void
  651. savedir_warn (char const *name)
  652. {
  653. call_arg_warn ("savedir", name);
  654. }
  655. void
  656. savedir_diag (char const *name)
  657. {
  658. if (ignore_failed_read_option)
  659. savedir_warn (name);
  660. else
  661. savedir_error (name);
  662. }
  663. void
  664. seek_error (char const *name)
  665. {
  666. call_arg_error ("seek", name);
  667. }
  668. void
  669. seek_error_details (char const *name, off_t offset)
  670. {
  671. char buf[UINTMAX_STRSIZE_BOUND];
  672. int e = errno;
  673. ERROR ((0, e, _("%s: Cannot seek to %s"),
  674. quotearg_colon (name),
  675. STRINGIFY_BIGINT (offset, buf)));
  676. }
  677. void
  678. seek_warn (char const *name)
  679. {
  680. call_arg_warn ("seek", name);
  681. }
  682. void
  683. seek_warn_details (char const *name, off_t offset)
  684. {
  685. char buf[UINTMAX_STRSIZE_BOUND];
  686. int e = errno;
  687. WARN ((0, e, _("%s: Warning: Cannot seek to %s"),
  688. quotearg_colon (name),
  689. STRINGIFY_BIGINT (offset, buf)));
  690. }
  691. void
  692. seek_diag_details (char const *name, off_t offset)
  693. {
  694. if (ignore_failed_read_option)
  695. seek_warn_details (name, offset);
  696. else
  697. seek_error_details (name, offset);
  698. }
  699. void
  700. symlink_error (char const *contents, char const *name)
  701. {
  702. int e = errno;
  703. ERROR ((0, e, _("%s: Cannot create symlink to %s"),
  704. quotearg_colon (name), quote_n (1, contents)));
  705. }
  706. void
  707. stat_error (char const *name)
  708. {
  709. call_arg_error ("stat", name);
  710. }
  711. void
  712. stat_warn (char const *name)
  713. {
  714. call_arg_warn ("stat", name);
  715. }
  716. void
  717. stat_diag (char const *name)
  718. {
  719. if (ignore_failed_read_option)
  720. stat_warn (name);
  721. else
  722. stat_error (name);
  723. }
  724. void
  725. truncate_error (char const *name)
  726. {
  727. call_arg_error ("truncate", name);
  728. }
  729. void
  730. truncate_warn (char const *name)
  731. {
  732. call_arg_warn ("truncate", name);
  733. }
  734. void
  735. unlink_error (char const *name)
  736. {
  737. call_arg_error ("unlink", name);
  738. }
  739. void
  740. utime_error (char const *name)
  741. {
  742. call_arg_error ("utime", name);
  743. }
  744. void
  745. waitpid_error (char const *name)
  746. {
  747. call_arg_error ("waitpid", name);
  748. }
  749. void
  750. write_error (char const *name)
  751. {
  752. call_arg_error ("write", name);
  753. }
  754. void
  755. write_error_details (char const *name, size_t status, size_t size)
  756. {
  757. if (status == 0)
  758. write_error (name);
  759. else
  760. ERROR ((0, 0,
  761. ngettext ("%s: Wrote only %lu of %lu byte",
  762. "%s: Wrote only %lu of %lu bytes",
  763. size),
  764. name, (unsigned long int) status, (unsigned long int) size));
  765. }
  766. void
  767. write_fatal (char const *name)
  768. {
  769. call_arg_fatal ("write", name);
  770. }
  771. void
  772. write_fatal_details (char const *name, ssize_t status, size_t size)
  773. {
  774. write_error_details (name, status, size);
  775. fatal_exit ();
  776. }
  777. /* Fork, aborting if unsuccessful. */
  778. pid_t
  779. xfork (void)
  780. {
  781. pid_t p = fork ();
  782. if (p == (pid_t) -1)
  783. call_arg_fatal ("fork", _("child process"));
  784. return p;
  785. }
  786. /* Create a pipe, aborting if unsuccessful. */
  787. void
  788. xpipe (int fd[2])
  789. {
  790. if (pipe (fd) < 0)
  791. call_arg_fatal ("pipe", _("interprocess channel"));
  792. }
  793. /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
  794. ALIGNMENT must be nonzero. The caller must arrange for ((char *)
  795. PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
  796. locations. */
  797. static inline void *
  798. ptr_align (void *ptr, size_t alignment)
  799. {
  800. char *p0 = ptr;
  801. char *p1 = p0 + alignment - 1;
  802. return p1 - (size_t) p1 % alignment;
  803. }
  804. /* Return the address of a page-aligned buffer of at least SIZE bytes.
  805. The caller should free *PTR when done with the buffer. */
  806. void *
  807. page_aligned_alloc (void **ptr, size_t size)
  808. {
  809. size_t alignment = getpagesize ();
  810. size_t size1 = size + alignment;
  811. if (size1 < size)
  812. xalloc_die ();
  813. *ptr = xmalloc (size1);
  814. return ptr_align (*ptr, alignment);
  815. }