Commit | Line | Data |
---|---|---|
c627d613 AT |
1 | /* |
2 | Copyright (C) Andrew Tridgell 1996 | |
3 | Copyright (C) Paul Mackerras 1996 | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | /* generate and receive file lists */ | |
21 | ||
22 | #include "rsync.h" | |
23 | ||
43a481dc AT |
24 | extern int csum_length; |
25 | ||
c627d613 AT |
26 | extern int verbose; |
27 | extern int am_server; | |
28 | extern int always_checksum; | |
71c46176 | 29 | extern int64 total_size; |
c627d613 AT |
30 | |
31 | extern int cvs_exclude; | |
32 | ||
a06d19e3 AT |
33 | extern int recurse; |
34 | ||
c627d613 AT |
35 | extern int one_file_system; |
36 | extern int make_backups; | |
37 | extern int preserve_links; | |
dc5ddbcc | 38 | extern int preserve_hard_links; |
c627d613 AT |
39 | extern int preserve_perms; |
40 | extern int preserve_devices; | |
41 | extern int preserve_uid; | |
42 | extern int preserve_gid; | |
43 | extern int preserve_times; | |
6574b4f7 | 44 | extern int relative_paths; |
82306bf6 | 45 | extern int copy_links; |
f6c34742 | 46 | extern int remote_version; |
c627d613 | 47 | |
3a6a366f | 48 | static char **local_exclude_list; |
c627d613 | 49 | |
82306bf6 AT |
50 | int link_stat(const char *Path, struct stat *Buffer) |
51 | { | |
52 | #if SUPPORT_LINKS | |
53 | if (copy_links) { | |
54 | return stat(Path, Buffer); | |
55 | } else { | |
56 | return lstat(Path, Buffer); | |
57 | } | |
58 | #else | |
59 | return stat(Path, Buffer); | |
60 | #endif | |
61 | } | |
62 | ||
c627d613 AT |
63 | /* |
64 | This function is used to check if a file should be included/excluded | |
65 | from the list of files based on its name and type etc | |
66 | */ | |
67 | static int match_file_name(char *fname,struct stat *st) | |
68 | { | |
69 | if (check_exclude(fname,local_exclude_list)) { | |
70 | if (verbose > 2) | |
dc5ddbcc | 71 | fprintf(FERROR,"excluding file %s\n",fname); |
c627d613 AT |
72 | return 0; |
73 | } | |
74 | return 1; | |
75 | } | |
76 | ||
77 | /* used by the one_file_system code */ | |
78 | static dev_t filesystem_dev; | |
79 | ||
80 | static void set_filesystem(char *fname) | |
81 | { | |
82 | struct stat st; | |
82306bf6 | 83 | if (link_stat(fname,&st) != 0) return; |
c627d613 AT |
84 | filesystem_dev = st.st_dev; |
85 | } | |
86 | ||
87 | ||
88 | static void send_directory(int f,struct file_list *flist,char *dir); | |
89 | ||
3a6a366f | 90 | static char *flist_dir; |
c627d613 | 91 | |
3ec4dd97 AT |
92 | static void clean_fname(char *name) |
93 | { | |
94 | char *p; | |
95 | int l; | |
96 | int modified = 1; | |
97 | ||
98 | if (!name) return; | |
99 | ||
100 | while (modified) { | |
101 | modified = 0; | |
102 | ||
103 | if ((p=strstr(name,"/./"))) { | |
104 | modified = 1; | |
105 | while (*p) { | |
106 | p[0] = p[2]; | |
107 | p++; | |
108 | } | |
109 | } | |
110 | ||
111 | if ((p=strstr(name,"//"))) { | |
112 | modified = 1; | |
113 | while (*p) { | |
114 | p[0] = p[1]; | |
115 | p++; | |
116 | } | |
117 | } | |
4fe159a8 | 118 | |
3ec4dd97 AT |
119 | if (strncmp(p=name,"./",2) == 0) { |
120 | modified = 1; | |
121 | do { | |
122 | p[0] = p[2]; | |
123 | } while (*p++); | |
124 | } | |
4fe159a8 | 125 | |
3ec4dd97 AT |
126 | l = strlen(p=name); |
127 | if (l > 1 && p[l-1] == '/') { | |
128 | modified = 1; | |
129 | p[l-1] = 0; | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | ||
135 | ||
136 | void send_file_entry(struct file_struct *file,int f) | |
c627d613 | 137 | { |
182dca5c | 138 | unsigned char flags; |
3a6a366f AT |
139 | static time_t last_time; |
140 | static mode_t last_mode; | |
141 | static dev_t last_rdev; | |
142 | static uid_t last_uid; | |
143 | static gid_t last_gid; | |
144 | static char lastname[MAXPATHLEN]; | |
3ec4dd97 | 145 | char *fname; |
4fe159a8 | 146 | int l1,l2; |
182dca5c | 147 | |
c627d613 AT |
148 | if (f == -1) return; |
149 | ||
182dca5c AT |
150 | if (!file) { |
151 | write_byte(f,0); | |
152 | return; | |
153 | } | |
154 | ||
3ec4dd97 AT |
155 | fname = f_name(file); |
156 | ||
182dca5c AT |
157 | flags = FILE_VALID; |
158 | ||
159 | if (file->mode == last_mode) flags |= SAME_MODE; | |
dc5ddbcc | 160 | if (file->rdev == last_rdev) flags |= SAME_RDEV; |
182dca5c AT |
161 | if (file->uid == last_uid) flags |= SAME_UID; |
162 | if (file->gid == last_gid) flags |= SAME_GID; | |
4fe159a8 | 163 | if (file->modtime == last_time) flags |= SAME_TIME; |
182dca5c | 164 | |
3ec4dd97 AT |
165 | for (l1=0;lastname[l1] && fname[l1] == lastname[l1];l1++) ; |
166 | l2 = strlen(fname) - l1; | |
4fe159a8 AT |
167 | |
168 | if (l1 > 0) flags |= SAME_NAME; | |
169 | if (l2 > 255) flags |= LONG_NAME; | |
d89322c4 | 170 | |
4fe159a8 AT |
171 | write_byte(f,flags); |
172 | if (flags & SAME_NAME) | |
173 | write_byte(f,l1); | |
174 | if (flags & LONG_NAME) | |
175 | write_int(f,l2); | |
9f3541e6 | 176 | else |
4fe159a8 | 177 | write_byte(f,l2); |
3ec4dd97 | 178 | write_buf(f,fname+l1,l2); |
4fe159a8 | 179 | |
3a6a366f | 180 | write_longint(f,file->length); |
4fe159a8 AT |
181 | if (!(flags & SAME_TIME)) |
182 | write_int(f,(int)file->modtime); | |
182dca5c AT |
183 | if (!(flags & SAME_MODE)) |
184 | write_int(f,(int)file->mode); | |
f6c34742 AT |
185 | if (preserve_uid && !(flags & SAME_UID)) { |
186 | add_uid(file->uid); | |
187 | write_int(f,(int)file->uid); | |
188 | } | |
189 | if (preserve_gid && !(flags & SAME_GID)) { | |
190 | add_gid(file->gid); | |
191 | write_int(f,(int)file->gid); | |
192 | } | |
dc5ddbcc AT |
193 | if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV)) |
194 | write_int(f,(int)file->rdev); | |
c627d613 AT |
195 | |
196 | #if SUPPORT_LINKS | |
197 | if (preserve_links && S_ISLNK(file->mode)) { | |
198 | write_int(f,strlen(file->link)); | |
199 | write_buf(f,file->link,strlen(file->link)); | |
200 | } | |
201 | #endif | |
202 | ||
dc5ddbcc AT |
203 | #if SUPPORT_HARD_LINKS |
204 | if (preserve_hard_links && S_ISREG(file->mode)) { | |
3a6a366f AT |
205 | write_int(f,(int)file->dev); |
206 | write_int(f,(int)file->inode); | |
dc5ddbcc AT |
207 | } |
208 | #endif | |
209 | ||
c627d613 | 210 | if (always_checksum) { |
43a481dc | 211 | write_buf(f,file->sum,csum_length); |
c627d613 | 212 | } |
182dca5c AT |
213 | |
214 | last_mode = file->mode; | |
dc5ddbcc | 215 | last_rdev = file->rdev; |
182dca5c AT |
216 | last_uid = file->uid; |
217 | last_gid = file->gid; | |
4fe159a8 AT |
218 | last_time = file->modtime; |
219 | ||
3ec4dd97 | 220 | strncpy(lastname,fname,MAXPATHLEN-1); |
13a1f792 | 221 | lastname[MAXPATHLEN-1] = 0; |
182dca5c AT |
222 | } |
223 | ||
224 | ||
225 | ||
3ec4dd97 AT |
226 | void receive_file_entry(struct file_struct **fptr, |
227 | unsigned char flags,int f) | |
182dca5c | 228 | { |
3a6a366f AT |
229 | static time_t last_time; |
230 | static mode_t last_mode; | |
231 | static dev_t last_rdev; | |
232 | static uid_t last_uid; | |
233 | static gid_t last_gid; | |
3ec4dd97 AT |
234 | static char lastname[MAXPATHLEN]; |
235 | char thisname[MAXPATHLEN]; | |
4fe159a8 | 236 | int l1=0,l2=0; |
3ec4dd97 AT |
237 | char *p; |
238 | struct file_struct *file; | |
182dca5c | 239 | |
4fe159a8 | 240 | if (flags & SAME_NAME) |
9f3541e6 | 241 | l1 = read_byte(f); |
4fe159a8 AT |
242 | |
243 | if (flags & LONG_NAME) | |
244 | l2 = read_int(f); | |
245 | else | |
246 | l2 = read_byte(f); | |
182dca5c | 247 | |
3ec4dd97 AT |
248 | file = (struct file_struct *)malloc(sizeof(*file)); |
249 | if (!file) out_of_memory("receive_file_entry"); | |
dc5ddbcc | 250 | bzero((char *)file,sizeof(*file)); |
3ec4dd97 AT |
251 | (*fptr) = file; |
252 | ||
253 | strncpy(thisname,lastname,l1); | |
254 | read_buf(f,&thisname[l1],l2); | |
255 | thisname[l1+l2] = 0; | |
256 | ||
257 | strncpy(lastname,thisname,MAXPATHLEN-1); | |
258 | lastname[MAXPATHLEN-1] = 0; | |
259 | ||
260 | clean_fname(thisname); | |
261 | ||
262 | if ((p = strrchr(thisname,'/'))) { | |
263 | static char *lastdir; | |
264 | *p = 0; | |
265 | if (lastdir && strcmp(thisname, lastdir)==0) { | |
266 | file->dirname = lastdir; | |
267 | } else { | |
268 | file->dirname = strdup(thisname); | |
269 | lastdir = file->dirname; | |
270 | } | |
271 | file->basename = strdup(p+1); | |
272 | } else { | |
273 | file->dirname = NULL; | |
274 | file->basename = strdup(thisname); | |
275 | } | |
dc5ddbcc | 276 | |
3ec4dd97 | 277 | if (!file->basename) out_of_memory("receive_file_entry 1"); |
182dca5c | 278 | |
182dca5c | 279 | |
3a6a366f | 280 | file->length = read_longint(f); |
4fe159a8 | 281 | file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f); |
182dca5c AT |
282 | file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f); |
283 | if (preserve_uid) | |
284 | file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f); | |
285 | if (preserve_gid) | |
286 | file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f); | |
287 | if (preserve_devices && IS_DEVICE(file->mode)) | |
dc5ddbcc | 288 | file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f); |
182dca5c | 289 | |
182dca5c AT |
290 | if (preserve_links && S_ISLNK(file->mode)) { |
291 | int l = read_int(f); | |
292 | file->link = (char *)malloc(l+1); | |
6dd1782c | 293 | if (!file->link) out_of_memory("receive_file_entry 2"); |
182dca5c AT |
294 | read_buf(f,file->link,l); |
295 | file->link[l] = 0; | |
296 | } | |
dc5ddbcc AT |
297 | |
298 | #if SUPPORT_HARD_LINKS | |
299 | if (preserve_hard_links && S_ISREG(file->mode)) { | |
300 | file->dev = read_int(f); | |
301 | file->inode = read_int(f); | |
302 | } | |
303 | #endif | |
182dca5c | 304 | |
2d0bb8eb AT |
305 | if (always_checksum) { |
306 | file->sum = (char *)malloc(MD4_SUM_LENGTH); | |
307 | if (!file->sum) out_of_memory("md4 sum"); | |
308 | read_buf(f,file->sum,csum_length); | |
309 | } | |
182dca5c AT |
310 | |
311 | last_mode = file->mode; | |
dc5ddbcc | 312 | last_rdev = file->rdev; |
182dca5c AT |
313 | last_uid = file->uid; |
314 | last_gid = file->gid; | |
4fe159a8 | 315 | last_time = file->modtime; |
c627d613 AT |
316 | } |
317 | ||
318 | ||
4fe159a8 | 319 | |
a06d19e3 | 320 | static struct file_struct *make_file(char *fname) |
c627d613 | 321 | { |
3ec4dd97 AT |
322 | struct file_struct *file; |
323 | struct stat st; | |
324 | char sum[SUM_LENGTH]; | |
325 | char *p; | |
326 | char cleaned_name[MAXPATHLEN]; | |
327 | ||
328 | strncpy(cleaned_name, fname, MAXPATHLEN-1); | |
329 | cleaned_name[MAXPATHLEN-1] = 0; | |
330 | clean_fname(cleaned_name); | |
331 | fname = cleaned_name; | |
332 | ||
333 | bzero(sum,SUM_LENGTH); | |
334 | ||
335 | if (link_stat(fname,&st) != 0) { | |
336 | fprintf(FERROR,"%s: %s\n", | |
337 | fname,strerror(errno)); | |
338 | return NULL; | |
339 | } | |
c627d613 | 340 | |
3ec4dd97 AT |
341 | if (S_ISDIR(st.st_mode) && !recurse) { |
342 | fprintf(FERROR,"skipping directory %s\n",fname); | |
343 | return NULL; | |
344 | } | |
345 | ||
346 | if (one_file_system && st.st_dev != filesystem_dev) | |
347 | return NULL; | |
348 | ||
349 | if (!match_file_name(fname,&st)) | |
350 | return NULL; | |
351 | ||
352 | if (verbose > 2) | |
353 | fprintf(FERROR,"make_file(%s)\n",fname); | |
354 | ||
355 | file = (struct file_struct *)malloc(sizeof(*file)); | |
356 | if (!file) out_of_memory("make_file"); | |
357 | bzero((char *)file,sizeof(*file)); | |
358 | ||
359 | if ((p = strrchr(fname,'/'))) { | |
360 | static char *lastdir; | |
361 | *p = 0; | |
362 | if (lastdir && strcmp(fname, lastdir)==0) { | |
363 | file->dirname = lastdir; | |
364 | } else { | |
365 | file->dirname = strdup(fname); | |
366 | lastdir = file->dirname; | |
367 | } | |
368 | file->basename = strdup(p+1); | |
369 | *p = '/'; | |
370 | } else { | |
371 | file->dirname = NULL; | |
372 | file->basename = strdup(fname); | |
373 | } | |
c627d613 | 374 | |
3ec4dd97 AT |
375 | file->modtime = st.st_mtime; |
376 | file->length = st.st_size; | |
377 | file->mode = st.st_mode; | |
378 | file->uid = st.st_uid; | |
379 | file->gid = st.st_gid; | |
380 | file->dev = st.st_dev; | |
381 | file->inode = st.st_ino; | |
c627d613 | 382 | #ifdef HAVE_ST_RDEV |
3ec4dd97 | 383 | file->rdev = st.st_rdev; |
c627d613 AT |
384 | #endif |
385 | ||
386 | #if SUPPORT_LINKS | |
3ec4dd97 AT |
387 | if (S_ISLNK(st.st_mode)) { |
388 | int l; | |
389 | char lnk[MAXPATHLEN]; | |
390 | if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) { | |
391 | fprintf(FERROR,"readlink %s : %s\n", | |
392 | fname,strerror(errno)); | |
393 | return NULL; | |
394 | } | |
395 | lnk[l] = 0; | |
396 | file->link = strdup(lnk); | |
397 | } | |
c627d613 AT |
398 | #endif |
399 | ||
3ec4dd97 | 400 | if (always_checksum && S_ISREG(st.st_mode)) { |
2d0bb8eb AT |
401 | file->sum = (char *)malloc(MD4_SUM_LENGTH); |
402 | if (!file->sum) out_of_memory("md4 sum"); | |
3ec4dd97 AT |
403 | file_checksum(fname,file->sum,st.st_size); |
404 | } | |
c627d613 | 405 | |
3ec4dd97 AT |
406 | if (flist_dir) { |
407 | static char *lastdir; | |
408 | if (lastdir && strcmp(lastdir, flist_dir)==0) { | |
409 | file->basedir = lastdir; | |
410 | } else { | |
411 | file->basedir = strdup(flist_dir); | |
412 | lastdir = file->basedir; | |
413 | } | |
414 | } else { | |
415 | file->basedir = NULL; | |
416 | } | |
c627d613 | 417 | |
3ec4dd97 AT |
418 | if (!S_ISDIR(st.st_mode)) |
419 | total_size += st.st_size; | |
c627d613 | 420 | |
3ec4dd97 | 421 | return file; |
c627d613 AT |
422 | } |
423 | ||
424 | ||
425 | ||
649d65ed AT |
426 | static void send_file_name(int f,struct file_list *flist,char *fname, |
427 | int recursive) | |
c627d613 AT |
428 | { |
429 | struct file_struct *file; | |
430 | ||
a06d19e3 | 431 | file = make_file(fname); |
c627d613 AT |
432 | |
433 | if (!file) return; | |
434 | ||
435 | if (flist->count >= flist->malloced) { | |
3ec4dd97 AT |
436 | if (flist->malloced < 1000) |
437 | flist->malloced += 1000; | |
f9c51620 | 438 | else |
3ec4dd97 AT |
439 | flist->malloced *= 2; |
440 | flist->files = (struct file_struct **)realloc(flist->files, | |
441 | sizeof(flist->files[0])* | |
442 | flist->malloced); | |
f9c51620 AT |
443 | if (!flist->files) |
444 | out_of_memory("send_file_name"); | |
c627d613 AT |
445 | } |
446 | ||
3ec4dd97 AT |
447 | if (strcmp(file->basename,"")) { |
448 | flist->files[flist->count++] = file; | |
c627d613 AT |
449 | send_file_entry(file,f); |
450 | } | |
451 | ||
649d65ed | 452 | if (S_ISDIR(file->mode) && recursive) { |
c627d613 | 453 | char **last_exclude_list = local_exclude_list; |
3ec4dd97 | 454 | send_directory(f,flist,f_name(file)); |
c627d613 AT |
455 | local_exclude_list = last_exclude_list; |
456 | return; | |
457 | } | |
458 | } | |
459 | ||
460 | ||
461 | ||
462 | static void send_directory(int f,struct file_list *flist,char *dir) | |
463 | { | |
3ec4dd97 AT |
464 | DIR *d; |
465 | struct dirent *di; | |
466 | char fname[MAXPATHLEN]; | |
467 | int l; | |
468 | char *p; | |
469 | ||
470 | d = opendir(dir); | |
471 | if (!d) { | |
472 | fprintf(FERROR,"%s: %s\n", | |
473 | dir,strerror(errno)); | |
474 | return; | |
475 | } | |
c627d613 | 476 | |
3ec4dd97 AT |
477 | strncpy(fname,dir,MAXPATHLEN-1); |
478 | fname[MAXPATHLEN-1]=0; | |
479 | l = strlen(fname); | |
480 | if (fname[l-1] != '/') { | |
481 | if (l == MAXPATHLEN-1) { | |
482 | fprintf(FERROR,"skipping long-named directory %s\n",fname); | |
483 | closedir(d); | |
484 | return; | |
485 | } | |
486 | strcat(fname,"/"); | |
487 | l++; | |
488 | } | |
489 | p = fname + strlen(fname); | |
c627d613 | 490 | |
3ec4dd97 AT |
491 | if (cvs_exclude) { |
492 | if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) { | |
493 | strcpy(p,".cvsignore"); | |
494 | local_exclude_list = make_exclude_list(fname,NULL,0); | |
495 | } else { | |
496 | fprintf(FERROR,"cannot cvs-exclude in long-named directory %s\n",fname); | |
497 | } | |
498 | } | |
499 | ||
500 | for (di=readdir(d); di; di=readdir(d)) { | |
501 | if (strcmp(di->d_name,".")==0 || | |
502 | strcmp(di->d_name,"..")==0) | |
503 | continue; | |
504 | strncpy(p,di->d_name,MAXPATHLEN-(l+1)); | |
649d65ed | 505 | send_file_name(f,flist,fname,recurse); |
3ec4dd97 | 506 | } |
c627d613 | 507 | |
3ec4dd97 | 508 | closedir(d); |
c627d613 AT |
509 | } |
510 | ||
511 | ||
512 | ||
a06d19e3 | 513 | struct file_list *send_file_list(int f,int argc,char *argv[]) |
c627d613 | 514 | { |
649d65ed AT |
515 | int i,l; |
516 | struct stat st; | |
517 | char *p,*dir; | |
518 | char dbuf[MAXPATHLEN]; | |
519 | char lastpath[MAXPATHLEN]=""; | |
520 | struct file_list *flist; | |
521 | ||
522 | if (verbose && recurse && !am_server && f != -1) { | |
523 | fprintf(FINFO,"building file list ... "); | |
524 | fflush(FINFO); | |
525 | } | |
c627d613 | 526 | |
649d65ed AT |
527 | flist = (struct file_list *)malloc(sizeof(flist[0])); |
528 | if (!flist) out_of_memory("send_file_list"); | |
c627d613 | 529 | |
649d65ed AT |
530 | flist->count=0; |
531 | flist->malloced = 1000; | |
532 | flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])* | |
533 | flist->malloced); | |
534 | if (!flist->files) out_of_memory("send_file_list"); | |
c627d613 | 535 | |
649d65ed AT |
536 | for (i=0;i<argc;i++) { |
537 | char fname2[MAXPATHLEN]; | |
538 | char *fname = fname2; | |
c627d613 | 539 | |
649d65ed AT |
540 | strncpy(fname,argv[i],MAXPATHLEN-1); |
541 | fname[MAXPATHLEN-1] = 0; | |
c627d613 | 542 | |
649d65ed AT |
543 | l = strlen(fname); |
544 | if (l != 1 && fname[l-1] == '/') { | |
545 | strcat(fname,"."); | |
546 | } | |
c627d613 | 547 | |
649d65ed AT |
548 | if (link_stat(fname,&st) != 0) { |
549 | fprintf(FERROR,"%s : %s\n",fname,strerror(errno)); | |
550 | continue; | |
551 | } | |
c627d613 | 552 | |
649d65ed AT |
553 | if (S_ISDIR(st.st_mode) && !recurse) { |
554 | fprintf(FERROR,"skipping directory %s\n",fname); | |
555 | continue; | |
556 | } | |
c627d613 | 557 | |
649d65ed AT |
558 | dir = NULL; |
559 | ||
560 | if (!relative_paths) { | |
561 | p = strrchr(fname,'/'); | |
562 | if (p) { | |
563 | *p = 0; | |
564 | if (p == fname) | |
565 | dir = "/"; | |
566 | else | |
567 | dir = fname; | |
568 | fname = p+1; | |
569 | } | |
570 | } else if (f != -1 && (p=strrchr(fname,'/'))) { | |
571 | /* this ensures we send the intermediate directories, | |
572 | thus getting their permissions right */ | |
573 | *p = 0; | |
574 | if (strcmp(lastpath,fname)) { | |
575 | strcpy(lastpath, fname); | |
576 | *p = '/'; | |
577 | for (p=fname+1; (p=strchr(p,'/')); p++) { | |
578 | *p = 0; | |
579 | send_file_name(f, flist, fname, 0); | |
580 | *p = '/'; | |
581 | } | |
582 | } else { | |
583 | *p = '/'; | |
584 | } | |
585 | } | |
586 | ||
587 | if (!*fname) | |
588 | fname = "."; | |
589 | ||
590 | if (dir && *dir) { | |
591 | if (getcwd(dbuf,MAXPATHLEN-1) == NULL) { | |
592 | fprintf(FERROR,"getwd : %s\n",strerror(errno)); | |
593 | exit_cleanup(1); | |
594 | } | |
595 | if (chdir(dir) != 0) { | |
596 | fprintf(FERROR,"chdir %s : %s\n", | |
597 | dir,strerror(errno)); | |
598 | continue; | |
599 | } | |
600 | flist_dir = dir; | |
601 | if (one_file_system) | |
602 | set_filesystem(fname); | |
603 | send_file_name(f,flist,fname,recurse); | |
604 | flist_dir = NULL; | |
605 | if (chdir(dbuf) != 0) { | |
606 | fprintf(FERROR,"chdir %s : %s\n", | |
607 | dbuf,strerror(errno)); | |
608 | exit_cleanup(1); | |
609 | } | |
610 | continue; | |
611 | } | |
612 | ||
613 | if (one_file_system) | |
614 | set_filesystem(fname); | |
615 | send_file_name(f,flist,fname,recurse); | |
616 | } | |
dc5ddbcc | 617 | |
649d65ed AT |
618 | if (f != -1) { |
619 | send_file_entry(NULL,f); | |
620 | write_flush(f); | |
621 | } | |
c627d613 | 622 | |
649d65ed AT |
623 | if (verbose && recurse && !am_server && f != -1) |
624 | fprintf(FINFO,"done\n"); | |
625 | ||
626 | clean_flist(flist); | |
627 | ||
628 | /* now send the uid/gid list. This was introduced in protocol | |
629 | version 15 */ | |
630 | if (f != -1 && remote_version >= 15) { | |
631 | send_uid_list(f); | |
632 | } | |
f6c34742 | 633 | |
649d65ed | 634 | return flist; |
c627d613 AT |
635 | } |
636 | ||
637 | ||
638 | struct file_list *recv_file_list(int f) | |
639 | { | |
c627d613 | 640 | struct file_list *flist; |
182dca5c | 641 | unsigned char flags; |
c627d613 | 642 | |
a06d19e3 AT |
643 | if (verbose && recurse && !am_server) { |
644 | fprintf(FINFO,"receiving file list ... "); | |
645 | fflush(FINFO); | |
646 | } | |
c627d613 AT |
647 | |
648 | flist = (struct file_list *)malloc(sizeof(flist[0])); | |
649 | if (!flist) | |
650 | goto oom; | |
651 | ||
652 | flist->count=0; | |
3ec4dd97 AT |
653 | flist->malloced=1000; |
654 | flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])* | |
655 | flist->malloced); | |
c627d613 AT |
656 | if (!flist->files) |
657 | goto oom; | |
658 | ||
659 | ||
182dca5c | 660 | for (flags=read_byte(f); flags; flags=read_byte(f)) { |
c627d613 AT |
661 | int i = flist->count; |
662 | ||
663 | if (i >= flist->malloced) { | |
3ec4dd97 AT |
664 | if (flist->malloced < 1000) |
665 | flist->malloced += 1000; | |
f9c51620 | 666 | else |
3ec4dd97 AT |
667 | flist->malloced *= 2; |
668 | flist->files =(struct file_struct **)realloc(flist->files, | |
669 | sizeof(flist->files[0])* | |
670 | flist->malloced); | |
f9c51620 AT |
671 | if (!flist->files) |
672 | goto oom; | |
c627d613 AT |
673 | } |
674 | ||
182dca5c | 675 | receive_file_entry(&flist->files[i],flags,f); |
c627d613 | 676 | |
3ec4dd97 AT |
677 | if (S_ISREG(flist->files[i]->mode)) |
678 | total_size += flist->files[i]->length; | |
c627d613 AT |
679 | |
680 | flist->count++; | |
681 | ||
682 | if (verbose > 2) | |
3ec4dd97 | 683 | fprintf(FERROR,"recv_file_name(%s)\n",f_name(flist->files[i])); |
c627d613 AT |
684 | } |
685 | ||
686 | ||
687 | if (verbose > 2) | |
dc5ddbcc | 688 | fprintf(FERROR,"received %d names\n",flist->count); |
c627d613 AT |
689 | |
690 | clean_flist(flist); | |
691 | ||
a06d19e3 AT |
692 | if (verbose && recurse && !am_server) { |
693 | fprintf(FINFO,"done\n"); | |
694 | } | |
695 | ||
f6c34742 AT |
696 | /* now recv the uid/gid list. This was introduced in protocol version 15 */ |
697 | if (f != -1 && remote_version >= 15) { | |
698 | recv_uid_list(f, flist); | |
699 | } | |
700 | ||
c627d613 AT |
701 | return flist; |
702 | ||
703 | oom: | |
704 | out_of_memory("recv_file_list"); | |
705 | return NULL; /* not reached */ | |
706 | } | |
707 | ||
708 | ||
3ec4dd97 | 709 | int file_compare(struct file_struct **f1,struct file_struct **f2) |
c627d613 | 710 | { |
3ec4dd97 AT |
711 | if (!(*f1)->basename && !(*f2)->basename) return 0; |
712 | if (!(*f1)->basename) return -1; | |
713 | if (!(*f2)->basename) return 1; | |
714 | if ((*f1)->dirname == (*f2)->dirname) | |
715 | return strcmp((*f1)->basename, (*f2)->basename); | |
716 | return strcmp(f_name(*f1),f_name(*f2)); | |
c627d613 AT |
717 | } |
718 | ||
719 | ||
720 | int flist_find(struct file_list *flist,struct file_struct *f) | |
721 | { | |
d966ee25 AT |
722 | int low=0,high=flist->count-1; |
723 | ||
724 | if (flist->count <= 0) return -1; | |
725 | ||
726 | while (low != high) { | |
727 | int mid = (low+high)/2; | |
3ec4dd97 | 728 | int ret = file_compare(&flist->files[flist_up(flist, mid)],&f); |
d966ee25 AT |
729 | if (ret == 0) return flist_up(flist, mid); |
730 | if (ret > 0) { | |
731 | high=mid; | |
732 | } else { | |
733 | low=mid+1; | |
734 | } | |
735 | } | |
736 | ||
3ec4dd97 | 737 | if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0) |
d966ee25 AT |
738 | return flist_up(flist,low); |
739 | return -1; | |
c627d613 AT |
740 | } |
741 | ||
742 | ||
3ec4dd97 AT |
743 | /* |
744 | * free up one file | |
745 | */ | |
746 | static void free_file(struct file_struct *file) | |
c627d613 | 747 | { |
3ec4dd97 AT |
748 | if (!file) return; |
749 | if (file->basename) free(file->basename); | |
750 | if (file->link) free(file->link); | |
0b910560 | 751 | if (file->sum) free(file->sum); |
3ec4dd97 | 752 | bzero((char *)file, sizeof(*file)); |
3ec4dd97 | 753 | } |
c627d613 | 754 | |
c627d613 | 755 | |
3ec4dd97 AT |
756 | /* |
757 | * free up all elements in a flist | |
758 | */ | |
759 | void flist_free(struct file_list *flist) | |
760 | { | |
761 | int i; | |
762 | for (i=1;i<flist->count;i++) { | |
763 | free_file(flist->files[i]); | |
649d65ed | 764 | free(flist->files[i]); |
3ec4dd97 AT |
765 | } |
766 | bzero((char *)flist->files, sizeof(flist->files[0])*flist->count); | |
767 | free(flist->files); | |
768 | bzero((char *)flist, sizeof(*flist)); | |
769 | free(flist); | |
c627d613 AT |
770 | } |
771 | ||
772 | ||
773 | /* | |
774 | * This routine ensures we don't have any duplicate names in our file list. | |
775 | * duplicate names can cause corruption because of the pipelining | |
776 | */ | |
777 | void clean_flist(struct file_list *flist) | |
778 | { | |
3ec4dd97 | 779 | int i; |
c627d613 | 780 | |
3ec4dd97 AT |
781 | if (!flist || flist->count == 0) |
782 | return; | |
c627d613 | 783 | |
3ec4dd97 AT |
784 | qsort(flist->files,flist->count, |
785 | sizeof(flist->files[0]), | |
786 | (int (*)())file_compare); | |
787 | ||
788 | for (i=1;i<flist->count;i++) { | |
789 | if (flist->files[i]->basename && | |
649d65ed | 790 | flist->files[i-1]->basename && |
3ec4dd97 AT |
791 | strcmp(f_name(flist->files[i]), |
792 | f_name(flist->files[i-1])) == 0) { | |
793 | if (verbose > 1 && !am_server) | |
794 | fprintf(FERROR,"removing duplicate name %s from file list %d\n", | |
795 | f_name(flist->files[i-1]),i-1); | |
796 | free_file(flist->files[i]); | |
3ec4dd97 AT |
797 | } |
798 | } | |
799 | } | |
800 | ||
801 | ||
802 | /* | |
803 | * return the full filename of a flist entry | |
804 | */ | |
805 | char *f_name(struct file_struct *f) | |
806 | { | |
807 | static char names[10][MAXPATHLEN]; | |
808 | static int n; | |
809 | char *p = names[n]; | |
810 | ||
649d65ed | 811 | if (!f || !f->basename) return NULL; |
3ec4dd97 AT |
812 | |
813 | n = (n+1)%10; | |
814 | ||
815 | if (f->dirname) { | |
816 | sprintf(p, "%s/%s", f->dirname, f->basename); | |
817 | } else { | |
818 | strcpy(p, f->basename); | |
819 | } | |
820 | ||
821 | return p; | |
c627d613 AT |
822 | } |
823 |