mangle.c 3.6 KB

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