4
0

misc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /* Miscellaneous functions, not really specific to GNU tar.
  2. Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
  3. 2003 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 PATH is obviously "." or "/". */
  176. static bool
  177. must_be_dot_or_slash (char const *path)
  178. {
  179. path += FILESYSTEM_PREFIX_LEN (path);
  180. if (ISSLASH (path[0]))
  181. {
  182. for (;;)
  183. if (ISSLASH (path[1]))
  184. path++;
  185. else if (path[1] == '.' && ISSLASH (path[2 + (path[2] == '.')]))
  186. path += 2 + (path[2] == '.');
  187. else
  188. return ! path[1];
  189. }
  190. else
  191. {
  192. while (path[0] == '.' && ISSLASH (path[1]))
  193. {
  194. path += 2;
  195. while (ISSLASH (*path))
  196. path++;
  197. }
  198. return ! path[0] || (path[0] == '.' && ! path[1]);
  199. }
  200. }
  201. /* Some implementations of rmdir let you remove '.' or '/'.
  202. Report an error with errno set to zero for obvious cases of this;
  203. otherwise call rmdir. */
  204. static int
  205. safer_rmdir (const char *path)
  206. {
  207. if (must_be_dot_or_slash (path))
  208. {
  209. errno = 0;
  210. return -1;
  211. }
  212. return rmdir (path);
  213. }
  214. /* Remove PATH, returning 1 on success. If PATH is a directory, then
  215. if OPTION is RECURSIVE_REMOVE_OPTION is set remove PATH
  216. recursively; otherwise, remove it only if it is empty. If PATH is
  217. a directory that cannot be removed (e.g., because it is nonempty)
  218. and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
  219. Return 0 on error, with errno set; if PATH is obviously the working
  220. directory return zero with errno set to zero. */
  221. int
  222. remove_any_file (const char *path, enum remove_option option)
  223. {
  224. /* Try unlink first if we are not root, as this saves us a system
  225. call in the common case where we're removing a non-directory. */
  226. if (! we_are_root)
  227. {
  228. if (unlink (path) == 0)
  229. return 1;
  230. /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
  231. directory without appropriate privileges, but many Linux
  232. kernels return the more-sensible EISDIR. */
  233. if (errno != EPERM && errno != EISDIR)
  234. return 0;
  235. }
  236. if (safer_rmdir (path) == 0)
  237. return 1;
  238. switch (errno)
  239. {
  240. case ENOTDIR:
  241. return we_are_root && unlink (path) == 0;
  242. case 0:
  243. case EEXIST:
  244. #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
  245. case ENOTEMPTY:
  246. #endif
  247. switch (option)
  248. {
  249. case ORDINARY_REMOVE_OPTION:
  250. break;
  251. case WANT_DIRECTORY_REMOVE_OPTION:
  252. return -1;
  253. case RECURSIVE_REMOVE_OPTION:
  254. {
  255. char *directory = savedir (path);
  256. char const *entry;
  257. size_t entrylen;
  258. if (! directory)
  259. return 0;
  260. for (entry = directory;
  261. (entrylen = strlen (entry)) != 0;
  262. entry += entrylen + 1)
  263. {
  264. char *path_buffer = new_name (path, entry);
  265. int r = remove_any_file (path_buffer, 1);
  266. int e = errno;
  267. free (path_buffer);
  268. if (! r)
  269. {
  270. free (directory);
  271. errno = e;
  272. return 0;
  273. }
  274. }
  275. free (directory);
  276. return safer_rmdir (path) == 0;
  277. }
  278. }
  279. break;
  280. }
  281. return 0;
  282. }
  283. /* Check if PATH already exists and make a backup of it right now.
  284. Return success (nonzero) only if the backup is either unneeded, or
  285. successful. For now, directories are considered to never need
  286. backup. If ARCHIVE is nonzero, this is the archive and so, we do
  287. not have to backup block or character devices, nor remote entities. */
  288. bool
  289. maybe_backup_file (const char *path, int archive)
  290. {
  291. struct stat file_stat;
  292. /* Check if we really need to backup the file. */
  293. if (archive && _remdev (path))
  294. return true;
  295. if (stat (path, &file_stat))
  296. {
  297. if (errno == ENOENT)
  298. return true;
  299. stat_error (path);
  300. return false;
  301. }
  302. if (S_ISDIR (file_stat.st_mode))
  303. return true;
  304. if (archive && (S_ISBLK (file_stat.st_mode) || S_ISCHR (file_stat.st_mode)))
  305. return true;
  306. assign_string (&before_backup_name, path);
  307. /* A run situation may exist between Emacs or other GNU programs trying to
  308. make a backup for the same file simultaneously. If theoretically
  309. possible, real problems are unlikely. Doing any better would require a
  310. convention, GNU-wide, for all programs doing backups. */
  311. assign_string (&after_backup_name, 0);
  312. after_backup_name = find_backup_file_name (path, backup_type);
  313. if (! after_backup_name)
  314. xalloc_die ();
  315. if (rename (before_backup_name, after_backup_name) == 0)
  316. {
  317. if (verbose_option)
  318. fprintf (stdlis, _("Renaming %s to %s\n"),
  319. quote_n (0, before_backup_name),
  320. quote_n (1, after_backup_name));
  321. return true;
  322. }
  323. else
  324. {
  325. /* The backup operation failed. */
  326. int e = errno;
  327. ERROR ((0, e, _("%s: Cannot rename to %s"),
  328. quotearg_colon (before_backup_name),
  329. quote_n (1, after_backup_name)));
  330. assign_string (&after_backup_name, 0);
  331. return false;
  332. }
  333. }
  334. /* Try to restore the recently backed up file to its original name.
  335. This is usually only needed after a failed extraction. */
  336. void
  337. undo_last_backup (void)
  338. {
  339. if (after_backup_name)
  340. {
  341. if (rename (after_backup_name, before_backup_name) != 0)
  342. {
  343. int e = errno;
  344. ERROR ((0, e, _("%s: Cannot rename to %s"),
  345. quotearg_colon (after_backup_name),
  346. quote_n (1, before_backup_name)));
  347. }
  348. if (verbose_option)
  349. fprintf (stdlis, _("Renaming %s back to %s\n"),
  350. quote_n (0, after_backup_name),
  351. quote_n (1, before_backup_name));
  352. assign_string (&after_backup_name, 0);
  353. }
  354. }
  355. /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
  356. int
  357. deref_stat (bool deref, char const *name, struct stat *buf)
  358. {
  359. return deref ? stat (name, buf) : lstat (name, buf);
  360. }
  361. /* A description of a working directory. */
  362. struct wd
  363. {
  364. char const *name;
  365. int saved;
  366. struct saved_cwd saved_cwd;
  367. };
  368. /* A vector of chdir targets. wd[0] is the initial working directory. */
  369. static struct wd *wd;
  370. /* The number of working directories in the vector. */
  371. static size_t wds;
  372. /* The allocated size of the vector. */
  373. static size_t wd_alloc;
  374. /* DIR is the operand of a -C option; add it to vector of chdir targets,
  375. and return the index of its location. */
  376. int
  377. chdir_arg (char const *dir)
  378. {
  379. if (wds == wd_alloc)
  380. {
  381. wd_alloc = 2 * (wd_alloc + 1);
  382. wd = xrealloc (wd, sizeof *wd * wd_alloc);
  383. if (! wds)
  384. {
  385. wd[wds].name = ".";
  386. wd[wds].saved = 0;
  387. wds++;
  388. }
  389. }
  390. /* Optimize the common special case of the working directory,
  391. or the working directory as a prefix. */
  392. if (dir[0])
  393. {
  394. while (dir[0] == '.' && ISSLASH (dir[1]))
  395. for (dir += 2; ISSLASH (*dir); dir++)
  396. continue;
  397. if (! dir[dir[0] == '.'])
  398. return wds - 1;
  399. }
  400. wd[wds].name = dir;
  401. wd[wds].saved = 0;
  402. return wds++;
  403. }
  404. /* Change to directory I. If I is 0, change to the initial working
  405. directory; otherwise, I must be a value returned by chdir_arg. */
  406. void
  407. chdir_do (int i)
  408. {
  409. static int previous;
  410. if (previous != i)
  411. {
  412. struct wd *prev = &wd[previous];
  413. struct wd *curr = &wd[i];
  414. if (! prev->saved)
  415. {
  416. prev->saved = 1;
  417. if (save_cwd (&prev->saved_cwd) != 0)
  418. FATAL_ERROR ((0, 0, _("Cannot save working directory")));
  419. }
  420. if (curr->saved)
  421. {
  422. if (restore_cwd (&curr->saved_cwd))
  423. FATAL_ERROR ((0, 0, _("Cannot change working directory")));
  424. }
  425. else
  426. {
  427. if (i && ! ISSLASH (curr->name[0]))
  428. chdir_do (i - 1);
  429. if (chdir (curr->name) != 0)
  430. chdir_fatal (curr->name);
  431. }
  432. previous = i;
  433. }
  434. }
  435. /* Decode MODE from its binary form in a stat structure, and encode it
  436. into a 9-byte string STRING, terminated with a NUL. */
  437. void
  438. decode_mode (mode_t mode, char *string)
  439. {
  440. *string++ = mode & S_IRUSR ? 'r' : '-';
  441. *string++ = mode & S_IWUSR ? 'w' : '-';
  442. *string++ = (mode & S_ISUID
  443. ? (mode & S_IXUSR ? 's' : 'S')
  444. : (mode & S_IXUSR ? 'x' : '-'));
  445. *string++ = mode & S_IRGRP ? 'r' : '-';
  446. *string++ = mode & S_IWGRP ? 'w' : '-';
  447. *string++ = (mode & S_ISGID
  448. ? (mode & S_IXGRP ? 's' : 'S')
  449. : (mode & S_IXGRP ? 'x' : '-'));
  450. *string++ = mode & S_IROTH ? 'r' : '-';
  451. *string++ = mode & S_IWOTH ? 'w' : '-';
  452. *string++ = (mode & S_ISVTX
  453. ? (mode & S_IXOTH ? 't' : 'T')
  454. : (mode & S_IXOTH ? 'x' : '-'));
  455. *string = '\0';
  456. }
  457. /* Report an error associated with the system call CALL and the
  458. optional name NAME. */
  459. static void
  460. call_arg_error (char const *call, char const *name)
  461. {
  462. int e = errno;
  463. ERROR ((0, e, _("%s: Cannot %s"), quotearg_colon (name), call));
  464. }
  465. /* Report a fatal error associated with the system call CALL and
  466. the optional file name NAME. */
  467. static void
  468. call_arg_fatal (char const *call, char const *name)
  469. {
  470. int e = errno;
  471. FATAL_ERROR ((0, e, _("%s: Cannot %s"), quotearg_colon (name), call));
  472. }
  473. /* Report a warning associated with the system call CALL and
  474. the optional file name NAME. */
  475. static void
  476. call_arg_warn (char const *call, char const *name)
  477. {
  478. int e = errno;
  479. WARN ((0, e, _("%s: Warning: Cannot %s"), quotearg_colon (name), call));
  480. }
  481. void
  482. chdir_fatal (char const *name)
  483. {
  484. call_arg_fatal ("chdir", name);
  485. }
  486. void
  487. chmod_error_details (char const *name, mode_t mode)
  488. {
  489. int e = errno;
  490. char buf[10];
  491. decode_mode (mode, buf);
  492. ERROR ((0, e, _("%s: Cannot change mode to %s"),
  493. quotearg_colon (name), buf));
  494. }
  495. void
  496. chown_error_details (char const *name, uid_t uid, gid_t gid)
  497. {
  498. int e = errno;
  499. ERROR ((0, e, _("%s: Cannot change ownership to uid %lu, gid %lu"),
  500. quotearg_colon (name), (unsigned long) uid, (unsigned long) gid));
  501. }
  502. void
  503. close_error (char const *name)
  504. {
  505. call_arg_error ("close", name);
  506. }
  507. void
  508. close_fatal (char const *name)
  509. {
  510. call_arg_fatal ("close", name);
  511. }
  512. void
  513. close_warn (char const *name)
  514. {
  515. call_arg_warn ("close", name);
  516. }
  517. void
  518. close_diag (char const *name)
  519. {
  520. if (ignore_failed_read_option)
  521. close_warn (name);
  522. else
  523. close_error (name);
  524. }
  525. void
  526. exec_fatal (char const *name)
  527. {
  528. call_arg_fatal ("exec", name);
  529. }
  530. void
  531. link_error (char const *target, char const *source)
  532. {
  533. int e = errno;
  534. ERROR ((0, e, _("%s: Cannot hard link to %s"),
  535. quotearg_colon (source), quote_n (1, target)));
  536. }
  537. void
  538. mkdir_error (char const *name)
  539. {
  540. call_arg_error ("mkdir", name);
  541. }
  542. void
  543. mkfifo_error (char const *name)
  544. {
  545. call_arg_error ("mkfifo", name);
  546. }
  547. void
  548. mknod_error (char const *name)
  549. {
  550. call_arg_error ("mknod", name);
  551. }
  552. void
  553. open_error (char const *name)
  554. {
  555. call_arg_error ("open", name);
  556. }
  557. void
  558. open_fatal (char const *name)
  559. {
  560. call_arg_fatal ("open", name);
  561. }
  562. void
  563. open_warn (char const *name)
  564. {
  565. call_arg_warn ("open", name);
  566. }
  567. void
  568. open_diag (char const *name)
  569. {
  570. if (ignore_failed_read_option)
  571. open_warn (name);
  572. else
  573. open_error (name);
  574. }
  575. void
  576. read_error (char const *name)
  577. {
  578. call_arg_error ("read", name);
  579. }
  580. void
  581. read_error_details (char const *name, off_t offset, size_t size)
  582. {
  583. char buf[UINTMAX_STRSIZE_BOUND];
  584. int e = errno;
  585. ERROR ((0, e,
  586. ngettext ("%s: Read error at byte %s, reading %lu byte",
  587. "%s: Read error at byte %s, reading %lu bytes",
  588. size),
  589. quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
  590. (unsigned long) size));
  591. }
  592. void
  593. read_warn_details (char const *name, off_t offset, size_t size)
  594. {
  595. char buf[UINTMAX_STRSIZE_BOUND];
  596. int e = errno;
  597. WARN ((0, e,
  598. ngettext ("%s: Warning: Read error at byte %s, reading %lu byte",
  599. "%s: Warning: Read error at byte %s, reading %lu bytes",
  600. size),
  601. quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
  602. (unsigned long) size));
  603. }
  604. void
  605. read_diag_details (char const *name, off_t offset, size_t size)
  606. {
  607. if (ignore_failed_read_option)
  608. read_warn_details (name, offset, size);
  609. else
  610. read_error_details (name, offset, size);
  611. }
  612. void
  613. read_fatal (char const *name)
  614. {
  615. call_arg_fatal ("read", name);
  616. }
  617. void
  618. read_fatal_details (char const *name, off_t offset, size_t size)
  619. {
  620. char buf[UINTMAX_STRSIZE_BOUND];
  621. int e = errno;
  622. FATAL_ERROR ((0, e,
  623. ngettext ("%s: Read error at byte %s, reading %lu byte",
  624. "%s: Read error at byte %s, reading %lu bytes",
  625. size),
  626. quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
  627. (unsigned long) size));
  628. }
  629. void
  630. readlink_error (char const *name)
  631. {
  632. call_arg_error ("readlink", name);
  633. }
  634. void
  635. readlink_warn (char const *name)
  636. {
  637. call_arg_warn ("readlink", name);
  638. }
  639. void
  640. readlink_diag (char const *name)
  641. {
  642. if (ignore_failed_read_option)
  643. readlink_warn (name);
  644. else
  645. readlink_error (name);
  646. }
  647. void
  648. savedir_error (char const *name)
  649. {
  650. call_arg_error ("savedir", name);
  651. }
  652. void
  653. savedir_warn (char const *name)
  654. {
  655. call_arg_warn ("savedir", name);
  656. }
  657. void
  658. savedir_diag (char const *name)
  659. {
  660. if (ignore_failed_read_option)
  661. savedir_warn (name);
  662. else
  663. savedir_error (name);
  664. }
  665. void
  666. seek_error (char const *name)
  667. {
  668. call_arg_error ("seek", name);
  669. }
  670. void
  671. seek_error_details (char const *name, off_t offset)
  672. {
  673. char buf[UINTMAX_STRSIZE_BOUND];
  674. int e = errno;
  675. ERROR ((0, e, _("%s: Cannot seek to %s"),
  676. quotearg_colon (name),
  677. STRINGIFY_BIGINT (offset, buf)));
  678. }
  679. void
  680. seek_warn (char const *name)
  681. {
  682. call_arg_warn ("seek", name);
  683. }
  684. void
  685. seek_warn_details (char const *name, off_t offset)
  686. {
  687. char buf[UINTMAX_STRSIZE_BOUND];
  688. int e = errno;
  689. WARN ((0, e, _("%s: Warning: Cannot seek to %s"),
  690. quotearg_colon (name),
  691. STRINGIFY_BIGINT (offset, buf)));
  692. }
  693. void
  694. seek_diag_details (char const *name, off_t offset)
  695. {
  696. if (ignore_failed_read_option)
  697. seek_warn_details (name, offset);
  698. else
  699. seek_error_details (name, offset);
  700. }
  701. void
  702. symlink_error (char const *contents, char const *name)
  703. {
  704. int e = errno;
  705. ERROR ((0, e, _("%s: Cannot create symlink to %s"),
  706. quotearg_colon (name), quote_n (1, contents)));
  707. }
  708. void
  709. stat_error (char const *name)
  710. {
  711. call_arg_error ("stat", name);
  712. }
  713. void
  714. stat_warn (char const *name)
  715. {
  716. call_arg_warn ("stat", name);
  717. }
  718. void
  719. stat_diag (char const *name)
  720. {
  721. if (ignore_failed_read_option)
  722. stat_warn (name);
  723. else
  724. stat_error (name);
  725. }
  726. void
  727. truncate_error (char const *name)
  728. {
  729. call_arg_error ("truncate", name);
  730. }
  731. void
  732. truncate_warn (char const *name)
  733. {
  734. call_arg_warn ("truncate", name);
  735. }
  736. void
  737. unlink_error (char const *name)
  738. {
  739. call_arg_error ("unlink", name);
  740. }
  741. void
  742. utime_error (char const *name)
  743. {
  744. call_arg_error ("utime", name);
  745. }
  746. void
  747. waitpid_error (char const *name)
  748. {
  749. call_arg_error ("waitpid", name);
  750. }
  751. void
  752. write_error (char const *name)
  753. {
  754. call_arg_error ("write", name);
  755. }
  756. void
  757. write_error_details (char const *name, ssize_t status, size_t size)
  758. {
  759. if (status < 0)
  760. write_error (name);
  761. else
  762. ERROR ((0, 0,
  763. ngettext ("%s: Wrote only %lu of %lu byte",
  764. "%s: Wrote only %lu of %lu bytes",
  765. record_size),
  766. name, (unsigned long) status, (unsigned long) record_size));
  767. }
  768. void
  769. write_fatal (char const *name)
  770. {
  771. call_arg_fatal ("write", name);
  772. }
  773. void
  774. write_fatal_details (char const *name, ssize_t status, size_t size)
  775. {
  776. write_error_details (name, status, size);
  777. fatal_exit ();
  778. }
  779. /* Fork, aborting if unsuccessful. */
  780. pid_t
  781. xfork (void)
  782. {
  783. pid_t p = fork ();
  784. if (p == (pid_t) -1)
  785. call_arg_fatal ("fork", _("child process"));
  786. return p;
  787. }
  788. /* Create a pipe, aborting if unsuccessful. */
  789. void
  790. xpipe (int fd[2])
  791. {
  792. if (pipe (fd) < 0)
  793. call_arg_fatal ("pipe", _("interprocess channel"));
  794. }
  795. /* Return an unambiguous printable representation, allocated in slot N,
  796. for NAME, suitable for diagnostics. */
  797. char const *
  798. quote_n (int n, char const *name)
  799. {
  800. return quotearg_n_style (n, locale_quoting_style, name);
  801. }
  802. /* Return an unambiguous printable representation of NAME, suitable
  803. for diagnostics. */
  804. char const *
  805. quote (char const *name)
  806. {
  807. return quote_n (0, name);
  808. }