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