misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* Miscellaneous functions, not really specific to GNU tar.
  2. Copyright (C) 1988, 92, 94, 95, 96, 97 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 2, or (at your option) any later
  6. version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  10. Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 59 Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #include "system.h"
  15. #include "backupfile.h"
  16. #include "rmt.h"
  17. /* The following inclusion for crosschecking prototypes, only. */
  18. #include "common.h"
  19. /* Handling strings. */
  20. #define ISPRINT(Char) (ISASCII (Char) && isprint (Char))
  21. /*-------------------------------------------------------------------------.
  22. | Assign STRING to a copy of VALUE if not NULL, or to NULL. If STRING was |
  23. | not NULL, it is freed first. |
  24. `-------------------------------------------------------------------------*/
  25. void
  26. assign_string (char **string, const char *value)
  27. {
  28. if (*string)
  29. free (*string);
  30. *string = value ? xstrdup (value) : NULL;
  31. }
  32. /*------------------------------------------------------------------------.
  33. | Allocate a copy of the string quoted as in C, and returns that. If the |
  34. | string does not have to be quoted, it returns the NULL string. The |
  35. | allocated copy should normally be freed with free() after the caller is |
  36. | done with it. |
  37. | |
  38. | This is used in two contexts only: either listing a tar file for the |
  39. | --list (-t) option, or generating the directory file in incremental |
  40. | dumps. |
  41. `------------------------------------------------------------------------*/
  42. char *
  43. quote_copy_string (const char *string)
  44. {
  45. const char *source = string;
  46. char *destination = NULL;
  47. char *buffer = NULL;
  48. int copying = 0;
  49. while (*source)
  50. {
  51. int character = (unsigned char) *source++;
  52. if (character == '\\')
  53. {
  54. if (!copying)
  55. {
  56. int length = (source - string) - 1;
  57. copying = 1;
  58. buffer = (char *) xmalloc (length + 5 + strlen (source) * 4);
  59. memcpy (buffer, string, (size_t) length);
  60. destination = buffer + length;
  61. }
  62. *destination++ = '\\';
  63. *destination++ = '\\';
  64. }
  65. else if (ISPRINT (character))
  66. {
  67. if (copying)
  68. *destination++ = character;
  69. }
  70. else
  71. {
  72. if (!copying)
  73. {
  74. int length = (source - string) - 1;
  75. copying = 1;
  76. buffer = (char *) xmalloc (length + 5 + strlen (source) * 4);
  77. memcpy (buffer, string, (size_t) length);
  78. destination = buffer + length;
  79. }
  80. *destination++ = '\\';
  81. switch (character)
  82. {
  83. case '\n':
  84. *destination++ = 'n';
  85. break;
  86. case '\t':
  87. *destination++ = 't';
  88. break;
  89. case '\f':
  90. *destination++ = 'f';
  91. break;
  92. case '\b':
  93. *destination++ = 'b';
  94. break;
  95. case '\r':
  96. *destination++ = 'r';
  97. break;
  98. case '\177':
  99. *destination++ = '?';
  100. break;
  101. default:
  102. *destination++ = (character >> 6) + '0';
  103. *destination++ = ((character >> 3) & 07) + '0';
  104. *destination++ = (character & 07) + '0';
  105. break;
  106. }
  107. }
  108. }
  109. if (copying)
  110. {
  111. *destination = '\0';
  112. return buffer;
  113. }
  114. return NULL;
  115. }
  116. /*-------------------------------------------------------------------------.
  117. | Takes a quoted C string (like those produced by quote_copy_string) and |
  118. | turns it back into the un-quoted original. This is done in place. |
  119. | Returns 0 only if the string was not properly quoted, but completes the |
  120. | unquoting anyway. |
  121. | |
  122. | This is used for reading the saved directory file in incremental dumps. |
  123. | It is used for decoding old `N' records (demangling names). But also, |
  124. | it is used for decoding file arguments, would they come from the shell |
  125. | or a -T file, and for decoding the --exclude argument. |
  126. `-------------------------------------------------------------------------*/
  127. int
  128. unquote_string (char *string)
  129. {
  130. int result = 1;
  131. char *source = string;
  132. char *destination = string;
  133. while (*source)
  134. if (*source == '\\')
  135. switch (*++source)
  136. {
  137. case '\\':
  138. *destination++ = '\\';
  139. source++;
  140. break;
  141. case 'n':
  142. *destination++ = '\n';
  143. source++;
  144. break;
  145. case 't':
  146. *destination++ = '\t';
  147. source++;
  148. break;
  149. case 'f':
  150. *destination++ = '\f';
  151. source++;
  152. break;
  153. case 'b':
  154. *destination++ = '\b';
  155. source++;
  156. break;
  157. case 'r':
  158. *destination++ = '\r';
  159. source++;
  160. break;
  161. case '?':
  162. *destination++ = 0177;
  163. source++;
  164. break;
  165. case '0':
  166. case '1':
  167. case '2':
  168. case '3':
  169. case '4':
  170. case '5':
  171. case '6':
  172. case '7':
  173. {
  174. int value = *source++ - '0';
  175. if (*source < '0' || *source > '7')
  176. {
  177. *destination++ = value;
  178. break;
  179. }
  180. value = value * 8 + *source++ - '0';
  181. if (*source < '0' || *source > '7')
  182. {
  183. *destination++ = value;
  184. break;
  185. }
  186. value = value * 8 + *source++ - '0';
  187. *destination++ = value;
  188. break;
  189. }
  190. default:
  191. result = 0;
  192. *destination++ = '\\';
  193. if (*source)
  194. *destination++ = *source++;
  195. break;
  196. }
  197. else if (source != destination)
  198. *destination++ = *source++;
  199. else
  200. source++, destination++;
  201. if (source != destination)
  202. *destination = '\0';
  203. return result;
  204. }
  205. /* Sorting lists. */
  206. /*---.
  207. | ? |
  208. `---*/
  209. char *
  210. merge_sort (char *list, int length, int offset, int (*compare) (char *, char *))
  211. {
  212. char *first_list;
  213. char *second_list;
  214. int first_length;
  215. int second_length;
  216. char *result;
  217. char **merge_point;
  218. char *cursor;
  219. int counter;
  220. #define SUCCESSOR(Pointer) \
  221. (*((char **) (((char *) (Pointer)) + offset)))
  222. if (length == 1)
  223. return list;
  224. if (length == 2)
  225. {
  226. if ((*compare) (list, SUCCESSOR (list)) > 0)
  227. {
  228. result = SUCCESSOR (list);
  229. SUCCESSOR (result) = list;
  230. SUCCESSOR (list) = NULL;
  231. return result;
  232. }
  233. return list;
  234. }
  235. first_list = list;
  236. first_length = (length + 1) / 2;
  237. second_length = length / 2;
  238. for (cursor = list, counter = first_length - 1;
  239. counter;
  240. cursor = SUCCESSOR (cursor), counter--)
  241. continue;
  242. second_list = SUCCESSOR (cursor);
  243. SUCCESSOR (cursor) = NULL;
  244. first_list = merge_sort (first_list, first_length, offset, compare);
  245. second_list = merge_sort (second_list, second_length, offset, compare);
  246. merge_point = &result;
  247. while (first_list && second_list)
  248. if ((*compare) (first_list, second_list) < 0)
  249. {
  250. cursor = SUCCESSOR (first_list);
  251. *merge_point = first_list;
  252. merge_point = &SUCCESSOR (first_list);
  253. first_list = cursor;
  254. }
  255. else
  256. {
  257. cursor = SUCCESSOR (second_list);
  258. *merge_point = second_list;
  259. merge_point = &SUCCESSOR (second_list);
  260. second_list = cursor;
  261. }
  262. if (first_list)
  263. *merge_point = first_list;
  264. else
  265. *merge_point = second_list;
  266. return result;
  267. #undef SUCCESSOR
  268. }
  269. /* File handling. */
  270. /* Saved names in case backup needs to be undone. */
  271. static char *before_backup_name = NULL;
  272. static char *after_backup_name = NULL;
  273. /*------------------------------------------------------------------------.
  274. | Returns nonzero if p is `.' or `..'. This could be a macro for speed. |
  275. `------------------------------------------------------------------------*/
  276. /* Early Solaris 2.4 readdir may return d->d_name as `' in NFS-mounted
  277. directories. The workaround here skips `' just like `.'. Without it,
  278. GNU tar would then treat `' much like `.' and loop endlessly. */
  279. int
  280. is_dot_or_dotdot (const char *p)
  281. {
  282. return (p[0] == '\0'
  283. || (p[0] == '.' && (p[1] == '\0'
  284. || (p[1] == '.' && p[2] == '\0'))));
  285. }
  286. /*-------------------------------------------------------------------------.
  287. | Delete PATH, whatever it might be. If RECURSE, first recursively delete |
  288. | the contents of PATH when it is a directory. Return zero on any error, |
  289. | with errno set. As a special case, if we fail to delete a directory |
  290. | when not RECURSE, do not set errno (just be tolerant to this error). |
  291. `-------------------------------------------------------------------------*/
  292. int
  293. remove_any_file (const char *path, int recurse)
  294. {
  295. struct stat stat_buffer;
  296. if (lstat (path, &stat_buffer) < 0)
  297. return 0;
  298. if (S_ISDIR (stat_buffer.st_mode))
  299. if (recurse)
  300. {
  301. DIR *dirp = opendir (path);
  302. struct dirent *dp;
  303. if (dirp == NULL)
  304. return 0;
  305. while (dp = readdir (dirp), dp && !is_dot_or_dotdot (dp->d_name))
  306. {
  307. char *path_buffer = new_name (path, dp->d_name);
  308. if (!remove_any_file (path_buffer, 1))
  309. {
  310. int saved_errno = errno;
  311. free (path_buffer);
  312. closedir (dirp);
  313. errno = saved_errno; /* FIXME: errno should be read-only */
  314. return 0;
  315. }
  316. free (path_buffer);
  317. }
  318. closedir (dirp);
  319. return rmdir (path) >= 0;
  320. }
  321. else
  322. {
  323. /* FIXME: Saving errno might not be needed anymore, now that
  324. extract_archive tests for the special case before recovery. */
  325. int saved_errno = errno;
  326. if (rmdir (path) >= 0)
  327. return 1;
  328. errno = saved_errno; /* FIXME: errno should be read-only */
  329. return 0;
  330. }
  331. return unlink (path) >= 0;
  332. }
  333. /*-------------------------------------------------------------------------.
  334. | Check if PATH already exists and make a backup of it right now. Return |
  335. | success (nonzero) only if the backup in either unneeded, or successful. |
  336. | |
  337. | For now, directories are considered to never need backup. If ARCHIVE is |
  338. | nonzero, this is the archive and so, we do not have to backup block or |
  339. | character devices, nor remote entities. |
  340. `-------------------------------------------------------------------------*/
  341. int
  342. maybe_backup_file (const char *path, int archive)
  343. {
  344. struct stat file_stat;
  345. /* Check if we really need to backup the file. */
  346. if (archive && _remdev (path))
  347. return 1;
  348. if (stat (path, &file_stat))
  349. {
  350. if (errno == ENOENT)
  351. return 1;
  352. ERROR ((0, errno, "%s", path));
  353. return 0;
  354. }
  355. if (S_ISDIR (file_stat.st_mode))
  356. return 1;
  357. #ifdef S_ISBLK
  358. if (archive && S_ISBLK (file_stat.st_mode))
  359. return 1;
  360. #endif
  361. #ifdef S_ISCHR
  362. if (archive && S_ISCHR (file_stat.st_mode))
  363. return 1;
  364. #endif
  365. assign_string (&before_backup_name, path);
  366. /* A run situation may exist between Emacs or other GNU programs trying to
  367. make a backup for the same file simultaneously. If theoretically
  368. possible, real problems are unlikely. Doing any better would require a
  369. convention, GNU-wide, for all programs doing backups. */
  370. assign_string (&after_backup_name, NULL);
  371. after_backup_name = find_backup_file_name (path);
  372. if (after_backup_name == NULL)
  373. FATAL_ERROR ((0, 0, "Virtual memory exhausted"));
  374. if (rename (before_backup_name, after_backup_name) == 0)
  375. {
  376. if (verbose_option)
  377. fprintf (stdlis, _("Renaming previous `%s' to `%s'\n"),
  378. before_backup_name, after_backup_name);
  379. return 1;
  380. }
  381. /* The backup operation failed. */
  382. ERROR ((0, errno, _("%s: Cannot rename for backup"), before_backup_name));
  383. assign_string (&after_backup_name, NULL);
  384. return 0;
  385. }
  386. /*-----------------------------------------------------------------------.
  387. | Try to restore the recently backed up file to its original name. This |
  388. | is usually only needed after a failed extraction. |
  389. `-----------------------------------------------------------------------*/
  390. void
  391. undo_last_backup (void)
  392. {
  393. if (after_backup_name)
  394. {
  395. if (rename (after_backup_name, before_backup_name) != 0)
  396. ERROR ((0, errno, _("%s: Cannot rename from backup"),
  397. before_backup_name));
  398. if (verbose_option)
  399. fprintf (stdlis, _("Renaming `%s' back to `%s'\n"),
  400. after_backup_name, before_backup_name);
  401. assign_string (&after_backup_name, NULL);
  402. }
  403. }