]> Dogcows Code - chaz/tar/blob - src/mangle.c
2f3e7df448eebaf9dbee365920e4cb8c1f055613
[chaz/tar] / src / mangle.c
1 /* Encode long filenames for GNU tar.
2 Copyright 1988, 1992, 1994, 1996, 1997, 1999 Free Software Foundation, Inc.
3
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
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18 #include "system.h"
19 #include "common.h"
20 #include <quotearg.h>
21
22 struct mangled
23 {
24 struct mangled *next;
25 int type;
26 char mangled[NAME_FIELD_SIZE];
27 char *linked_to;
28 char normal[1];
29 };
30
31 /*---------------------------------------------------------------------.
32 | Extract a GNUTYPE_NAMES record contents. It seems that such are not |
33 | produced anymore by GNU tar, but we leave the reading code around |
34 | nevertheless, for salvaging old tapes. |
35 `---------------------------------------------------------------------*/
36
37 void
38 extract_mangle (void)
39 {
40 off_t size = current_stat.st_size;
41 char *buffer = xmalloc ((size_t) (size + 1));
42 char *copy = buffer;
43 char *cursor = buffer;
44
45 if (size != (size_t) size || size == (size_t) -1)
46 xalloc_die ();
47
48 buffer[size] = '\0';
49
50 while (size > 0)
51 {
52 union block *block = find_next_block ();
53 size_t available;
54
55 if (!block)
56 {
57 ERROR ((0, 0, _("Unexpected EOF in mangled names")));
58 return;
59 }
60 available = available_space_after (block);
61 if (available > size)
62 available = size;
63 memcpy (copy, block->buffer, available);
64 copy += available;
65 size -= available;
66 set_next_block_after ((union block *) (block->buffer + available - 1));
67 }
68
69 while (*cursor)
70 {
71 char *next_cursor;
72 char *name;
73 char *name_end;
74
75 next_cursor = strchr (cursor, '\n');
76 *next_cursor++ = '\0';
77
78 if (!strncmp (cursor, "Rename ", 7))
79 {
80
81 name = cursor + 7;
82 name_end = strchr (name, ' ');
83 while (strncmp (name_end, " to ", 4))
84 {
85 name_end++;
86 name_end = strchr (name_end, ' ');
87 }
88 *name_end = '\0';
89 if (next_cursor[-2] == '/')
90 next_cursor[-2] = '\0';
91 unquote_string (name_end + 4);
92 if (rename (name, name_end + 4))
93 ERROR ((0, errno, _("%s: Cannot rename to %s"),
94 quotearg_colon (name), quote_n (1, name_end + 4)));
95 else if (verbose_option)
96 WARN ((0, 0, _("Renamed %s to %s"), name, name_end + 4));
97 }
98 #ifdef HAVE_SYMLINK
99 else if (!strncmp (cursor, "Symlink ", 8))
100 {
101 name = cursor + 8;
102 name_end = strchr (name, ' ');
103 while (strncmp (name_end, " to ", 4))
104 {
105 name_end++;
106 name_end = strchr (name_end, ' ');
107 }
108 *name_end = '\0';
109 unquote_string (name);
110 unquote_string (name_end + 4);
111 if (symlink (name, name_end + 4)
112 && (unlink (name_end + 4) || symlink (name, name_end + 4)))
113 ERROR ((0, errno, _("%s: Cannot symlink %s %s"),
114 quotearg_colon (name), quote_n (1, name_end + 4)));
115 else if (verbose_option)
116 WARN ((0, 0, _("Symlinked %s to %s"), name, name_end + 4));
117 }
118 #endif
119 else
120 ERROR ((0, 0, _("Unknown demangling command %s"), cursor));
121
122 cursor = next_cursor;
123 }
124 }
This page took 0.040009 seconds and 3 git commands to generate.