]>
Dogcows Code - chaz/tar/blob - src/mangle.c
1 /* mangle.c -- encode long filenames
2 Copyright (C) 1988, 1992 Free Software Foundation
4 This file is part of GNU Tar.
6 GNU Tar is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Tar is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Tar; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <sys/types.h>
29 extern PTR
ck_malloc ();
30 void finish_header ();
31 extern PTR
init_buffer ();
32 extern char *quote_copy_string ();
33 extern char *get_buffer ();
34 char *un_quote_string ();
36 extern union record
*start_header ();
38 extern struct stat hstat
; /* Stat struct corresponding */
50 /* Should use a hash table, etc. . */
51 struct mangled
*first_mangle
;
54 #if 0 /* Deleted because there is now a better way to do all this */
60 struct mangled
*munge
;
62 for (munge
= first_mangle
; munge
; munge
= munge
->next
)
63 if (!strcmp (name
, munge
->normal
))
64 return munge
->mangled
;
71 add_symlink_mangle (symlink
, linkto
, buffer
)
76 struct mangled
*munge
, *kludge
;
78 munge
= (struct mangled
*) ck_malloc (sizeof (struct mangled
) + strlen (symlink
) + strlen (linkto
) + 2);
83 for (kludge
= first_mangle
; kludge
->next
; kludge
= kludge
->next
)
89 strcpy (munge
->normal
, symlink
);
90 munge
->linked_to
= munge
->normal
+ strlen (symlink
) + 1;
91 strcpy (munge
->linked_to
, linkto
);
92 sprintf (munge
->mangled
, "@@MaNgLeD.%d", mangled_num
++);
93 strncpy (buffer
, munge
->mangled
, NAMSIZ
);
99 add_mangle (name
, buffer
)
103 struct mangled
*munge
, *kludge
;
105 munge
= (struct mangled
*) ck_malloc (sizeof (struct mangled
) + strlen (name
));
107 first_mangle
= munge
;
110 for (kludge
= first_mangle
; kludge
->next
; kludge
= kludge
->next
)
112 kludge
->next
= munge
;
116 strcpy (munge
->normal
, name
);
117 sprintf (munge
->mangled
, "@@MaNgLeD.%d", mangled_num
++);
118 strncpy (buffer
, munge
->mangled
, NAMSIZ
);
124 struct mangled
*munge
;
126 union record
*header
;
134 the_buffer
= init_buffer ();
135 for (munge
= first_mangle
, size
= 0; munge
; munge
= munge
->next
)
137 ptr1
= quote_copy_string (munge
->normal
);
139 ptr1
= munge
->normal
;
142 add_buffer (the_buffer
, "Symlink ", 8);
143 add_buffer (the_buffer
, ptr1
, strlen (ptr1
));
144 add_buffer (the_buffer
, " to ", 4);
146 if (ptr2
= quote_copy_string (munge
->linked_to
))
148 add_buffer (the_buffer
, ptr2
, strlen (ptr2
));
152 add_buffer (the_buffer
, munge
->linked_to
, strlen (munge
->linked_to
));
156 add_buffer (the_buffer
, "Rename ", 7);
157 add_buffer (the_buffer
, munge
->mangled
, strlen (munge
->mangled
));
158 add_buffer (the_buffer
, " to ", 4);
159 add_buffer (the_buffer
, ptr1
, strlen (ptr1
));
161 add_buffer (the_buffer
, "\n", 1);
162 if (ptr1
!= munge
->normal
)
166 bzero (&hstat
, sizeof (struct stat
));
167 hstat
.st_atime
= hstat
.st_mtime
= hstat
.st_ctime
= time (0);
168 ptr1
= get_buffer (the_buffer
);
169 hstat
.st_size
= strlen (ptr1
);
171 header
= start_header ("././@MaNgLeD_NaMeS", &hstat
);
172 header
->header
.linkflag
= LF_NAMES
;
173 finish_header (header
);
174 size
= hstat
.st_size
;
176 bufsize
= endofrecs ()->charptr
- header
->charptr
;
178 while (bufsize
< size
)
180 bcopy (ptr1
, header
->charptr
, bufsize
);
183 userec (header
+ (bufsize
- 1) / RECORDSIZE
);
185 bufsize
= endofrecs ()->charptr
- header
->charptr
;
187 bcopy (ptr1
, header
->charptr
, size
);
188 bzero (header
->charptr
+ size
, bufsize
- size
);
189 userec (header
+ (size
- 1) / RECORDSIZE
);
195 extract_mangle (head
)
202 char *nam1
, *nam1end
;
206 size
= hstat
.st_size
;
207 buf
= to
= ck_malloc (size
+ 1);
211 fromtape
= findrec ()->charptr
;
214 msg ("Unexpected EOF in mangled names!");
217 copied
= endofrecs ()->charptr
- fromtape
;
220 bcopy (fromtape
, to
, copied
);
223 userec ((union record
*) (fromtape
+ copied
- 1));
225 for (ptr
= buf
; *ptr
; ptr
= ptrend
)
227 ptrend
= index (ptr
, '\n');
230 if (!strncmp (ptr
, "Rename ", 7))
233 nam1end
= index (nam1
, ' ');
234 while (strncmp (nam1end
, " to ", 4))
237 nam1end
= index (nam1end
, ' ');
240 if (ptrend
[-2] == '/')
242 un_quote_string (nam1end
+ 4);
243 if (rename (nam1
, nam1end
+ 4))
244 msg_perror ("Can't rename %s to %s", nam1
, nam1end
+ 4);
246 msg ("Renamed %s to %s", nam1
, nam1end
+ 4);
249 else if (!strncmp (ptr
, "Symlink ", 8))
252 nam1end
= index (nam1
, ' ');
253 while (strncmp (nam1end
, " to ", 4))
256 nam1end
= index (nam1end
, ' ');
259 un_quote_string (nam1
);
260 un_quote_string (nam1end
+ 4);
261 if (symlink (nam1
, nam1end
+ 4) && (unlink (nam1end
+ 4) || symlink (nam1
, nam1end
+ 4)))
262 msg_perror ("Can't symlink %s to %s", nam1
, nam1end
+ 4);
264 msg ("Symlinkd %s to %s", nam1
, nam1end
+ 4);
268 msg ("Unknown demangling command %s", ptr
);
This page took 0.046464 seconds and 5 git commands to generate.