misc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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 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. 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 <unlinkdir.h>
  21. /* Handling strings. */
  22. /* Assign STRING to a copy of VALUE if not zero, or to zero. If
  23. STRING was nonzero, it is freed first. */
  24. void
  25. assign_string (char **string, const char *value)
  26. {
  27. if (*string)
  28. free (*string);
  29. *string = value ? xstrdup (value) : 0;
  30. }
  31. /* Allocate a copy of the string quoted as in C, and returns that. If
  32. the string does not have to be quoted, it returns a null pointer.
  33. The allocated copy should normally be freed with free() after the
  34. caller is done with it.
  35. This is used in one context only: generating the directory file in
  36. incremental dumps. The quoted string is not intended for human
  37. consumption; it is intended only for unquote_string. The quoting
  38. is locale-independent, so that users needn't worry about locale
  39. when reading directory files. This means that we can't use
  40. quotearg, as quotearg is locale-dependent and is meant for human
  41. consumption. */
  42. char *
  43. quote_copy_string (const char *string)
  44. {
  45. const char *source = string;
  46. char *destination = 0;
  47. char *buffer = 0;
  48. int copying = 0;
  49. while (*source)
  50. {
  51. int character = *source++;
  52. switch (character)
  53. {
  54. case '\n': case '\\':
  55. if (!copying)
  56. {
  57. size_t length = (source - string) - 1;
  58. copying = 1;
  59. buffer = xmalloc (length + 2 + 2 * strlen (source) + 1);
  60. memcpy (buffer, string, length);
  61. destination = buffer + length;
  62. }
  63. *destination++ = '\\';
  64. *destination++ = character == '\\' ? '\\' : 'n';
  65. break;
  66. default:
  67. if (copying)
  68. *destination++ = character;
  69. break;
  70. }
  71. }
  72. if (copying)
  73. {
  74. *destination = '\0';
  75. return buffer;
  76. }
  77. return 0;
  78. }
  79. /* Takes a quoted C string (like those produced by quote_copy_string)
  80. and turns it back into the un-quoted original. This is done in
  81. place. Returns 0 only if the string was not properly quoted, but
  82. completes the unquoting anyway.
  83. This is used for reading the saved directory file in incremental
  84. dumps. It is used for decoding old `N' records (demangling names).
  85. But also, it is used for decoding file arguments, would they come
  86. from the shell or a -T file, and for decoding the --exclude
  87. argument. */
  88. int
  89. unquote_string (char *string)
  90. {
  91. int result = 1;
  92. char *source = string;
  93. char *destination = string;
  94. /* Escape sequences other than \\ and \n are no longer generated by
  95. quote_copy_string, but accept them for backwards compatibility,
  96. and also because unquote_string is used for purposes other than
  97. parsing the output of quote_copy_string. */
  98. while (*source)
  99. if (*source == '\\')
  100. switch (*++source)
  101. {
  102. case '\\':
  103. *destination++ = '\\';
  104. source++;
  105. break;
  106. case 'a':
  107. *destination++ = '\a';
  108. source++;
  109. break;
  110. case 'b':
  111. *destination++ = '\b';
  112. source++;
  113. break;
  114. case 'f':
  115. *destination++ = '\f';
  116. source++;
  117. break;
  118. case 'n':
  119. *destination++ = '\n';
  120. source++;
  121. break;
  122. case 'r':
  123. *destination++ = '\r';
  124. source++;
  125. break;
  126. case 't':
  127. *destination++ = '\t';
  128. source++;
  129. break;
  130. case 'v':
  131. *destination++ = '\v';
  132. source++;
  133. break;
  134. case '?':
  135. *destination++ = 0177;
  136. source++;
  137. break;
  138. case '0':
  139. case '1':
  140. case '2':
  141. case '3':
  142. case '4':
  143. case '5':
  144. case '6':
  145. case '7':
  146. {
  147. int value = *source++ - '0';
  148. if (*source < '0' || *source > '7')
  149. {
  150. *destination++ = value;
  151. break;
  152. }
  153. value = value * 8 + *source++ - '0';
  154. if (*source < '0' || *source > '7')
  155. {
  156. *destination++ = value;
  157. break;
  158. }
  159. value = value * 8 + *source++ - '0';
  160. *destination++ = value;
  161. break;
  162. }
  163. default:
  164. result = 0;
  165. *destination++ = '\\';
  166. if (*source)
  167. *destination++ = *source++;
  168. break;
  169. }
  170. else if (source != destination)
  171. *destination++ = *source++;
  172. else
  173. source++, destination++;
  174. if (source != destination)
  175. *destination = '\0';
  176. return result;
  177. }
  178. /* File handling. */
  179. /* Saved names in case backup needs to be undone. */
  180. static char *before_backup_name;
  181. static char *after_backup_name;
  182. /* Return 1 if FILE_NAME is obviously "." or "/". */
  183. static bool
  184. must_be_dot_or_slash (char const *file_name)
  185. {
  186. file_name += FILE_SYSTEM_PREFIX_LEN (file_name);
  187. if (ISSLASH (file_name[0]))
  188. {
  189. for (;;)
  190. if (ISSLASH (file_name[1]))
  191. file_name++;
  192. else if (file_name[1] == '.'
  193. && ISSLASH (file_name[2 + (file_name[2] == '.')]))
  194. file_name += 2 + (file_name[2] == '.');
  195. else
  196. return ! file_name[1];
  197. }
  198. else
  199. {
  200. while (file_name[0] == '.' && ISSLASH (file_name[1]))
  201. {
  202. file_name += 2;
  203. while (ISSLASH (*file_name))
  204. file_name++;
  205. }
  206. return ! file_name[0] || (file_name[0] == '.' && ! file_name[1]);
  207. }
  208. }
  209. /* Some implementations of rmdir let you remove '.' or '/'.
  210. Report an error with errno set to zero for obvious cases of this;
  211. otherwise call rmdir. */
  212. static int
  213. safer_rmdir (const char *file_name)
  214. {
  215. if (must_be_dot_or_slash (file_name))
  216. {
  217. errno = 0;
  218. return -1;
  219. }
  220. return rmdir (file_name);
  221. }
  222. /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
  223. then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
  224. recursively; otherwise, remove it only if it is empty. If FILE_NAME is
  225. a directory that cannot be removed (e.g., because it is nonempty)
  226. and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
  227. Return 0 on error, with errno set; if FILE_NAME is obviously the working
  228. directory return zero with errno set to zero. */
  229. int
  230. remove_any_file (const char *file_name, enum remove_option option)
  231. {
  232. /* Try unlink first if we cannot unlink directories, as this saves
  233. us a system call in the common case where we're removing a
  234. non-directory. */
  235. bool try_unlink_first = cannot_unlink_dir ();
  236. if (try_unlink_first)
  237. {
  238. if (unlink (file_name) == 0)
  239. return 1;
  240. /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
  241. directory without appropriate privileges, but many Linux
  242. kernels return the more-sensible EISDIR. */
  243. if (errno != EPERM && errno != EISDIR)
  244. return 0;
  245. }
  246. if (safer_rmdir (file_name) == 0)
  247. return 1;
  248. switch (errno)
  249. {
  250. case ENOTDIR:
  251. return !try_unlink_first && unlink (file_name) == 0;
  252. case 0:
  253. case EEXIST:
  254. #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
  255. case ENOTEMPTY:
  256. #endif
  257. switch (option)
  258. {
  259. case ORDINARY_REMOVE_OPTION:
  260. break;
  261. case WANT_DIRECTORY_REMOVE_OPTION:
  262. return -1;
  263. case RECURSIVE_REMOVE_OPTION:
  264. {
  265. char *directory = savedir (file_name);
  266. char const *entry;
  267. size_t entrylen;
  268. if (! directory)
  269. return 0;
  270. for (entry = directory;
  271. (entrylen = strlen (entry)) != 0;
  272. entry += entrylen + 1)
  273. {
  274. char *file_name_buffer = new_name (file_name, entry);
  275. int r = remove_any_file (file_name_buffer,
  276. RECURSIVE_REMOVE_OPTION);
  277. int e = errno;
  278. free (file_name_buffer);
  279. if (! r)
  280. {
  281. free (directory);
  282. errno = e;
  283. return 0;
  284. }
  285. }
  286. free (directory);
  287. return safer_rmdir (file_name) == 0;
  288. }
  289. }
  290. break;
  291. }
  292. return 0;
  293. }
  294. /* Check if FILE_NAME already exists and make a backup of it right now.
  295. Return success (nonzero) only if the backup is either unneeded, or
  296. successful. For now, directories are considered to never need
  297. backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
  298. so, we do not have to backup block or character devices, nor remote
  299. entities. */
  300. bool
  301. maybe_backup_file (const char *file_name, int this_is_the_archive)
  302. {
  303. struct stat file_stat;
  304. /* Check if we really need to backup the file. */
  305. if (this_is_the_archive && _remdev (file_name))
  306. return true;
  307. if (stat (file_name, &file_stat))
  308. {
  309. if (errno == ENOENT)
  310. return true;
  311. stat_error (file_name);
  312. return false;
  313. }
  314. if (S_ISDIR (file_stat.st_mode))
  315. return true;
  316. if (this_is_the_archive
  317. && (S_ISBLK (file_stat.st_mode) || S_ISCHR (file_stat.st_mode)))
  318. return true;
  319. assign_string (&before_backup_name, file_name);
  320. /* A run situation may exist between Emacs or other GNU programs trying to
  321. make a backup for the same file simultaneously. If theoretically
  322. possible, real problems are unlikely. Doing any better would require a
  323. convention, GNU-wide, for all programs doing backups. */
  324. assign_string (&after_backup_name, 0);
  325. after_backup_name = find_backup_file_name (file_name, backup_type);
  326. if (! after_backup_name)
  327. xalloc_die ();
  328. if (rename (before_backup_name, after_backup_name) == 0)
  329. {
  330. if (verbose_option)
  331. fprintf (stdlis, _("Renaming %s to %s\n"),
  332. quote_n (0, before_backup_name),
  333. quote_n (1, after_backup_name));
  334. return true;
  335. }
  336. else
  337. {
  338. /* The backup operation failed. */
  339. int e = errno;
  340. ERROR ((0, e, _("%s: Cannot rename to %s"),
  341. quotearg_colon (before_backup_name),
  342. quote_n (1, after_backup_name)));
  343. assign_string (&after_backup_name, 0);
  344. return false;
  345. }
  346. }
  347. /* Try to restore the recently backed up file to its original name.
  348. This is usually only needed after a failed extraction. */
  349. void
  350. undo_last_backup (void)
  351. {
  352. if (after_backup_name)
  353. {
  354. if (rename (after_backup_name, before_backup_name) != 0)
  355. {
  356. int e = errno;
  357. ERROR ((0, e, _("%s: Cannot rename to %s"),
  358. quotearg_colon (after_backup_name),
  359. quote_n (1, before_backup_name)));
  360. }
  361. if (verbose_option)
  362. fprintf (stdlis, _("Renaming %s back to %s\n"),
  363. quote_n (0, after_backup_name),
  364. quote_n (1, before_backup_name));
  365. assign_string (&after_backup_name, 0);
  366. }
  367. }
  368. /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
  369. int
  370. deref_stat (bool deref, char const *name, struct stat *buf)
  371. {
  372. return deref ? stat (name, buf) : lstat (name, buf);
  373. }
  374. /* A description of a working directory. */
  375. struct wd
  376. {
  377. char const *name;
  378. int saved;
  379. struct saved_cwd saved_cwd;
  380. };
  381. /* A vector of chdir targets. wd[0] is the initial working directory. */
  382. static struct wd *wd;
  383. /* The number of working directories in the vector. */
  384. static size_t wds;
  385. /* The allocated size of the vector. */
  386. static size_t wd_alloc;
  387. /* DIR is the operand of a -C option; add it to vector of chdir targets,
  388. and return the index of its location. */
  389. int
  390. chdir_arg (char const *dir)
  391. {
  392. if (wds == wd_alloc)
  393. {
  394. wd_alloc = 2 * (wd_alloc + 1);
  395. wd = xrealloc (wd, sizeof *wd * wd_alloc);
  396. if (! wds)
  397. {
  398. wd[wds].name = ".";
  399. wd[wds].saved = 0;
  400. wds++;
  401. }
  402. }
  403. /* Optimize the common special case of the working directory,
  404. or the working directory as a prefix. */
  405. if (dir[0])
  406. {
  407. while (dir[0] == '.' && ISSLASH (dir[1]))
  408. for (dir += 2; ISSLASH (*dir); dir++)
  409. continue;
  410. if (! dir[dir[0] == '.'])
  411. return wds - 1;
  412. }
  413. wd[wds].name = dir;
  414. wd[wds].saved = 0;
  415. return wds++;
  416. }
  417. /* Change to directory I. If I is 0, change to the initial working
  418. directory; otherwise, I must be a value returned by chdir_arg. */
  419. void
  420. chdir_do (int i)
  421. {
  422. static int previous;
  423. if (previous != i)
  424. {
  425. struct wd *prev = &wd[previous];
  426. struct wd *curr = &wd[i];
  427. if (! prev->saved)
  428. {
  429. prev->saved = 1;
  430. if (save_cwd (&prev->saved_cwd) != 0)
  431. FATAL_ERROR ((0, 0, _("Cannot save working directory")));
  432. }
  433. if (curr->saved)
  434. {
  435. if (restore_cwd (&curr->saved_cwd))
  436. FATAL_ERROR ((0, 0, _("Cannot change working directory")));
  437. }
  438. else
  439. {
  440. if (i && ! ISSLASH (curr->name[0]))
  441. chdir_do (i - 1);
  442. if (chdir (curr->name) != 0)
  443. chdir_fatal (curr->name);
  444. }
  445. previous = i;
  446. }
  447. }
  448. void
  449. close_diag (char const *name)
  450. {
  451. if (ignore_failed_read_option)
  452. close_warn (name);
  453. else
  454. close_error (name);
  455. }
  456. void
  457. open_diag (char const *name)
  458. {
  459. if (ignore_failed_read_option)
  460. open_warn (name);
  461. else
  462. open_error (name);
  463. }
  464. void
  465. read_diag_details (char const *name, off_t offset, size_t size)
  466. {
  467. if (ignore_failed_read_option)
  468. read_warn_details (name, offset, size);
  469. else
  470. read_error_details (name, offset, size);
  471. }
  472. void
  473. readlink_diag (char const *name)
  474. {
  475. if (ignore_failed_read_option)
  476. readlink_warn (name);
  477. else
  478. readlink_error (name);
  479. }
  480. void
  481. savedir_diag (char const *name)
  482. {
  483. if (ignore_failed_read_option)
  484. savedir_warn (name);
  485. else
  486. savedir_error (name);
  487. }
  488. void
  489. seek_diag_details (char const *name, off_t offset)
  490. {
  491. if (ignore_failed_read_option)
  492. seek_warn_details (name, offset);
  493. else
  494. seek_error_details (name, offset);
  495. }
  496. void
  497. stat_diag (char const *name)
  498. {
  499. if (ignore_failed_read_option)
  500. stat_warn (name);
  501. else
  502. stat_error (name);
  503. }
  504. void
  505. write_fatal_details (char const *name, ssize_t status, size_t size)
  506. {
  507. write_error_details (name, status, size);
  508. fatal_exit ();
  509. }
  510. /* Fork, aborting if unsuccessful. */
  511. pid_t
  512. xfork (void)
  513. {
  514. pid_t p = fork ();
  515. if (p == (pid_t) -1)
  516. call_arg_fatal ("fork", _("child process"));
  517. return p;
  518. }
  519. /* Create a pipe, aborting if unsuccessful. */
  520. void
  521. xpipe (int fd[2])
  522. {
  523. if (pipe (fd) < 0)
  524. call_arg_fatal ("pipe", _("interprocess channel"));
  525. }
  526. /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
  527. ALIGNMENT must be nonzero. The caller must arrange for ((char *)
  528. PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
  529. locations. */
  530. static inline void *
  531. ptr_align (void *ptr, size_t alignment)
  532. {
  533. char *p0 = ptr;
  534. char *p1 = p0 + alignment - 1;
  535. return p1 - (size_t) p1 % alignment;
  536. }
  537. /* Return the address of a page-aligned buffer of at least SIZE bytes.
  538. The caller should free *PTR when done with the buffer. */
  539. void *
  540. page_aligned_alloc (void **ptr, size_t size)
  541. {
  542. size_t alignment = getpagesize ();
  543. size_t size1 = size + alignment;
  544. if (size1 < size)
  545. xalloc_die ();
  546. *ptr = xmalloc (size1);
  547. return ptr_align (*ptr, alignment);
  548. }