mangle.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Encode long filenames for GNU tar.
  2. Copyright 1988, 92, 94, 96, 97, 99, 2000 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. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #include <system.h>
  15. #include "common.h"
  16. #include <quotearg.h>
  17. struct mangled
  18. {
  19. struct mangled *next;
  20. int type;
  21. char mangled[NAME_FIELD_SIZE];
  22. char *linked_to;
  23. char normal[1];
  24. };
  25. /* Extract a GNUTYPE_NAMES record contents. It seems that such are
  26. not produced anymore by GNU tar, but we leave the reading code
  27. around nevertheless, for salvaging old tapes. */
  28. void
  29. extract_mangle (void)
  30. {
  31. off_t size = current_stat_info.stat.st_size;
  32. char *buffer = xmalloc ((size_t) (size + 1));
  33. char *copy = buffer;
  34. char *cursor = buffer;
  35. if (size != (size_t) size || size == (size_t) -1)
  36. xalloc_die ();
  37. buffer[size] = '\0';
  38. while (size > 0)
  39. {
  40. union block *block = find_next_block ();
  41. size_t available;
  42. if (!block)
  43. {
  44. ERROR ((0, 0, _("Unexpected EOF in mangled names")));
  45. return;
  46. }
  47. available = available_space_after (block);
  48. if (available > size)
  49. available = size;
  50. memcpy (copy, block->buffer, available);
  51. copy += available;
  52. size -= available;
  53. set_next_block_after ((union block *) (block->buffer + available - 1));
  54. }
  55. while (*cursor)
  56. {
  57. char *next_cursor;
  58. char *name;
  59. char *name_end;
  60. next_cursor = strchr (cursor, '\n');
  61. *next_cursor++ = '\0';
  62. if (!strncmp (cursor, "Rename ", 7))
  63. {
  64. name = cursor + 7;
  65. name_end = strchr (name, ' ');
  66. while (strncmp (name_end, " to ", 4))
  67. {
  68. name_end++;
  69. name_end = strchr (name_end, ' ');
  70. }
  71. *name_end = '\0';
  72. if (next_cursor[-2] == '/')
  73. next_cursor[-2] = '\0';
  74. unquote_string (name_end + 4);
  75. if (rename (name, name_end + 4))
  76. ERROR ((0, errno, _("%s: Cannot rename to %s"),
  77. quotearg_colon (name), quote_n (1, name_end + 4)));
  78. else if (verbose_option)
  79. WARN ((0, 0, _("Renamed %s to %s"), name, name_end + 4));
  80. }
  81. #ifdef HAVE_SYMLINK
  82. else if (!strncmp (cursor, "Symlink ", 8))
  83. {
  84. name = cursor + 8;
  85. name_end = strchr (name, ' ');
  86. while (strncmp (name_end, " to ", 4))
  87. {
  88. name_end++;
  89. name_end = strchr (name_end, ' ');
  90. }
  91. *name_end = '\0';
  92. unquote_string (name);
  93. unquote_string (name_end + 4);
  94. if (symlink (name, name_end + 4)
  95. && (unlink (name_end + 4) || symlink (name, name_end + 4)))
  96. ERROR ((0, errno, _("%s: Cannot symlink to %s"),
  97. quotearg_colon (name), quote_n (1, name_end + 4)));
  98. else if (verbose_option)
  99. WARN ((0, 0, _("Symlinked %s to %s"), name, name_end + 4));
  100. }
  101. #endif
  102. else
  103. ERROR ((0, 0, _("Unknown demangling command %s"), cursor));
  104. cursor = next_cursor;
  105. }
  106. }