]>
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();
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
;
53 #if 0 /* Deleted because there is now a better way to do all this */
59 struct mangled
*munge
;
61 for(munge
=first_mangle
;munge
;munge
=munge
->next
)
62 if(!strcmp(name
,munge
->normal
))
63 return munge
->mangled
;
70 add_symlink_mangle(symlink
, linkto
, buffer
)
75 struct mangled
*munge
,*kludge
;
77 munge
=(struct mangled
*)ck_malloc(sizeof(struct mangled
)+strlen(symlink
)+strlen(linkto
)+2);
81 for(kludge
=first_mangle
;kludge
->next
;kludge
=kludge
->next
)
87 strcpy(munge
->normal
,symlink
);
88 munge
->linked_to
=munge
->normal
+strlen(symlink
)+1;
89 strcpy(munge
->linked_to
,linkto
);
90 sprintf(munge
->mangled
,"@@MaNgLeD.%d",mangled_num
++);
91 strncpy(buffer
,munge
->mangled
,NAMSIZ
);
96 add_mangle (name
, buffer
)
100 struct mangled
*munge
,*kludge
;
102 munge
=(struct mangled
*)ck_malloc(sizeof(struct mangled
)+strlen(name
));
106 for(kludge
=first_mangle
;kludge
->next
;kludge
=kludge
->next
)
112 strcpy(munge
->normal
,name
);
113 sprintf(munge
->mangled
,"@@MaNgLeD.%d",mangled_num
++);
114 strncpy(buffer
,munge
->mangled
,NAMSIZ
);
120 struct mangled
*munge
;
122 union record
*header
;
130 the_buffer
=init_buffer();
131 for(munge
=first_mangle
,size
=0;munge
;munge
=munge
->next
) {
132 ptr1
=quote_copy_string(munge
->normal
);
136 add_buffer(the_buffer
,"Symlink ",8);
137 add_buffer(the_buffer
,ptr1
,strlen(ptr1
));
138 add_buffer(the_buffer
," to ",4);
140 if(ptr2
=quote_copy_string(munge
->linked_to
)) {
141 add_buffer(the_buffer
,ptr2
,strlen(ptr2
));
144 add_buffer(the_buffer
,munge
->linked_to
,strlen(munge
->linked_to
));
146 add_buffer(the_buffer
,"Rename ",7);
147 add_buffer(the_buffer
,munge
->mangled
,strlen(munge
->mangled
));
148 add_buffer(the_buffer
," to ",4);
149 add_buffer(the_buffer
,ptr1
,strlen(ptr1
));
151 add_buffer(the_buffer
,"\n",1);
152 if(ptr1
!=munge
->normal
)
156 bzero(&hstat
,sizeof(struct stat
));
157 hstat
.st_atime
=hstat
.st_mtime
=hstat
.st_ctime
=time(0);
158 ptr1
=get_buffer(the_buffer
);
159 hstat
.st_size
=strlen(ptr1
);
161 header
=start_header("././@MaNgLeD_NaMeS",&hstat
);
162 header
->header
.linkflag
=LF_NAMES
;
163 finish_header(header
);
166 bufsize
= endofrecs()->charptr
- header
->charptr
;
168 while(bufsize
<size
) {
169 bcopy(ptr1
,header
->charptr
,bufsize
);
172 userec(header
+(bufsize
-1)/RECORDSIZE
);
174 bufsize
= endofrecs()->charptr
- header
->charptr
;
176 bcopy(ptr1
,header
->charptr
,size
);
177 bzero(header
->charptr
+size
,bufsize
-size
);
178 userec(header
+(size
-1)/RECORDSIZE
);
196 buf
=to
=ck_malloc(size
+1);
199 fromtape
=findrec()->charptr
;
201 msg("Unexpected EOF in mangled names!");
204 copied
=endofrecs()->charptr
-fromtape
;
207 bcopy(fromtape
,to
,copied
);
210 userec((union record
*)(fromtape
+copied
-1));
212 for(ptr
=buf
;*ptr
;ptr
=ptrend
) {
213 ptrend
=index(ptr
,'\n');
216 if(!strncmp(ptr
,"Rename ",7)) {
218 nam1end
=index(nam1
,' ');
219 while(strncmp(nam1end
," to ",4)) {
221 nam1end
=index(nam1end
,' ');
226 un_quote_string(nam1end
+4);
227 if(rename(nam1
,nam1end
+4))
228 msg_perror("Can't rename %s to %s",nam1
,nam1end
+4);
230 msg("Renamed %s to %s",nam1
,nam1end
+4);
233 else if(!strncmp(ptr
,"Symlink ",8)) {
235 nam1end
=index(nam1
,' ');
236 while(strncmp(nam1end
," to ",4)) {
238 nam1end
=index(nam1end
,' ');
240 un_quote_string(nam1
);
241 un_quote_string(nam1end
+4);
242 if(symlink(nam1
,nam1end
+4) && (unlink(nam1end
+4) || symlink(nam1
,nam1end
+4)))
243 msg_perror("Can't symlink %s to %s",nam1
,nam1end
+4);
245 msg("Symlinkd %s to %s",nam1
,nam1end
+4);
249 msg("Unknown demangling command %s",ptr
);
This page took 0.044133 seconds and 5 git commands to generate.