mangle.c 3.5 KB

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