]>
Dogcows Code - chaz/tar/blob - src/mangle.c
1 /* mangle.c -- encode long filenames
2 Copyright (C) 1988 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();
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 */
49 /* Should use a hash table, etc. . */
50 struct mangled
*first_mangle
;
57 struct mangled
*munge
;
59 for(munge
=first_mangle
;munge
;munge
=munge
->next
)
60 if(!strcmp(name
,munge
->normal
))
61 return munge
->mangled
;
68 add_symlink_mangle(symlink
, linkto
, buffer
)
73 struct mangled
*munge
,*kludge
;
75 munge
=(struct mangled
*)ck_malloc(sizeof(struct mangled
)+strlen(symlink
)+strlen(linkto
)+2);
79 for(kludge
=first_mangle
;kludge
->next
;kludge
=kludge
->next
)
85 strcpy(munge
->normal
,symlink
);
86 munge
->linked_to
=munge
->normal
+strlen(symlink
)+1;
87 strcpy(munge
->linked_to
,linkto
);
88 sprintf(munge
->mangled
,"@@MaNgLeD.%d",mangled_num
++);
89 strncpy(buffer
,munge
->mangled
,NAMSIZ
);
94 add_mangle (name
, buffer
)
98 struct mangled
*munge
,*kludge
;
100 munge
=(struct mangled
*)ck_malloc(sizeof(struct mangled
)+strlen(name
));
104 for(kludge
=first_mangle
;kludge
->next
;kludge
=kludge
->next
)
110 strcpy(munge
->normal
,name
);
111 sprintf(munge
->mangled
,"@@MaNgLeD.%d",mangled_num
++);
112 strncpy(buffer
,munge
->mangled
,NAMSIZ
);
118 struct mangled
*munge
;
120 union record
*header
;
128 the_buffer
=init_buffer();
129 for(munge
=first_mangle
,size
=0;munge
;munge
=munge
->next
) {
130 ptr1
=quote_copy_string(munge
->normal
);
134 add_buffer(the_buffer
,"Symlink ",8);
135 add_buffer(the_buffer
,ptr1
,strlen(ptr1
));
136 add_buffer(the_buffer
," to ",4);
138 if(ptr2
=quote_copy_string(munge
->linked_to
)) {
139 add_buffer(the_buffer
,ptr2
,strlen(ptr2
));
142 add_buffer(the_buffer
,munge
->linked_to
,strlen(munge
->linked_to
));
144 add_buffer(the_buffer
,"Rename ",7);
145 add_buffer(the_buffer
,munge
->mangled
,strlen(munge
->mangled
));
146 add_buffer(the_buffer
," to ",4);
147 add_buffer(the_buffer
,ptr1
,strlen(ptr1
));
149 add_buffer(the_buffer
,"\n",1);
150 if(ptr1
!=munge
->normal
)
154 bzero(&hstat
,sizeof(struct stat
));
155 hstat
.st_atime
=hstat
.st_mtime
=hstat
.st_ctime
=time(0);
156 ptr1
=get_buffer(the_buffer
);
157 hstat
.st_size
=strlen(ptr1
);
159 header
=start_header("././@MaNgLeD_NaMeS",&hstat
);
160 header
->header
.linkflag
=LF_NAMES
;
161 finish_header(header
);
164 bufsize
= endofrecs()->charptr
- header
->charptr
;
166 while(bufsize
<size
) {
167 bcopy(ptr1
,header
->charptr
,bufsize
);
170 userec(header
+(bufsize
-1)/RECORDSIZE
);
172 bufsize
= endofrecs()->charptr
- header
->charptr
;
174 bcopy(ptr1
,header
->charptr
,size
);
175 bzero(header
->charptr
+size
,bufsize
-size
);
176 userec(header
+(size
-1)/RECORDSIZE
);
192 buf
=to
=ck_malloc(size
+1);
195 fromtape
=findrec()->charptr
;
197 msg("Unexpected EOF in mangled names!");
200 copied
=endofrecs()->charptr
-fromtape
;
203 bcopy(fromtape
,to
,copied
);
206 userec((union record
*)(fromtape
+copied
-1));
208 for(ptr
=buf
;*ptr
;ptr
=ptrend
) {
209 ptrend
=index(ptr
,'\n');
212 if(!strncmp(ptr
,"Rename ",7)) {
214 nam1end
=index(nam1
,' ');
215 while(strncmp(nam1end
," to ",4)) {
217 nam1end
=index(nam1end
,' ');
222 un_quote_string(nam1end
+4);
223 if(rename(nam1
,nam1end
+4))
224 msg_perror("Can't rename %s to %s",nam1
,nam1end
+4);
226 msg("Renamed %s to %s",nam1
,nam1end
+4);
229 else if(!strncmp(ptr
,"Symlink ",8)) {
231 nam1end
=index(nam1
,' ');
232 while(strncmp(nam1end
," to ",4)) {
234 nam1end
=index(nam1end
,' ');
236 un_quote_string(nam1
);
237 un_quote_string(nam1end
+4);
238 if(symlink(nam1
,nam1end
+4) && (unlink(nam1end
+4) || symlink(nam1
,nam1end
+4)))
239 msg_perror("Can't symlink %s to %s",nam1
,nam1end
+4);
241 msg("Symlinkd %s to %s",nam1
,nam1end
+4);
245 msg("Unknown demangling command %s",ptr
);
This page took 0.048924 seconds and 5 git commands to generate.