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 | ||
0c5f37d9 AT |
319 | /* determine if a file in a different filesstem should be skipped |
320 | when one_file_system is set. We bascally only want to include | |
321 | the mount points - but they can be hard to find! */ | |
322 | static int skip_filesystem(char *fname, struct stat *st) | |
323 | { | |
324 | struct stat st2; | |
325 | char *p = strrchr(fname, '/'); | |
326 | ||
327 | /* skip all but directories */ | |
328 | if (!S_ISDIR(st->st_mode)) return 1; | |
329 | ||
330 | /* if its not a subdirectory then allow */ | |
331 | if (!p) return 0; | |
332 | ||
333 | *p = 0; | |
334 | if (link_stat(fname, &st2)) { | |
335 | *p = '/'; | |
336 | return 0; | |
337 | } | |
338 | *p = '/'; | |
339 | ||
340 | return (st2.st_dev != filesystem_dev); | |
341 | } | |
4fe159a8 | 342 | |
a06d19e3 | 343 | static struct file_struct *make_file(char *fname) |
c627d613 | 344 | { |
3ec4dd97 AT |
345 | struct file_struct *file; |
346 | struct stat st; | |
347 | char sum[SUM_LENGTH]; | |
348 | char *p; | |
349 | char cleaned_name[MAXPATHLEN]; | |
350 | ||
351 | strncpy(cleaned_name, fname, MAXPATHLEN-1); | |
352 | cleaned_name[MAXPATHLEN-1] = 0; | |
353 | clean_fname(cleaned_name); | |
354 | fname = cleaned_name; | |
355 | ||
356 | bzero(sum,SUM_LENGTH); | |
357 | ||
358 | if (link_stat(fname,&st) != 0) { | |
359 | fprintf(FERROR,"%s: %s\n", | |
360 | fname,strerror(errno)); | |
361 | return NULL; | |
362 | } | |
c627d613 | 363 | |
3ec4dd97 AT |
364 | if (S_ISDIR(st.st_mode) && !recurse) { |
365 | fprintf(FERROR,"skipping directory %s\n",fname); | |
366 | return NULL; | |
367 | } | |
368 | ||
0c5f37d9 AT |
369 | if (one_file_system && st.st_dev != filesystem_dev) { |
370 | if (skip_filesystem(fname, &st)) | |
371 | return NULL; | |
372 | } | |
3ec4dd97 AT |
373 | |
374 | if (!match_file_name(fname,&st)) | |
375 | return NULL; | |
376 | ||
377 | if (verbose > 2) | |
378 | fprintf(FERROR,"make_file(%s)\n",fname); | |
379 | ||
380 | file = (struct file_struct *)malloc(sizeof(*file)); | |
381 | if (!file) out_of_memory("make_file"); | |
382 | bzero((char *)file,sizeof(*file)); | |
383 | ||
384 | if ((p = strrchr(fname,'/'))) { | |
385 | static char *lastdir; | |
386 | *p = 0; | |
387 | if (lastdir && strcmp(fname, lastdir)==0) { | |
388 | file->dirname = lastdir; | |
389 | } else { | |
390 | file->dirname = strdup(fname); | |
391 | lastdir = file->dirname; | |
392 | } | |
393 | file->basename = strdup(p+1); | |
394 | *p = '/'; | |
395 | } else { | |
396 | file->dirname = NULL; | |
397 | file->basename = strdup(fname); | |
398 | } | |
c627d613 | 399 | |
3ec4dd97 AT |
400 | file->modtime = st.st_mtime; |
401 | file->length = st.st_size; | |
402 | file->mode = st.st_mode; | |
403 | file->uid = st.st_uid; | |
404 | file->gid = st.st_gid; | |
405 | file->dev = st.st_dev; | |
406 | file->inode = st.st_ino; | |
c627d613 | 407 | #ifdef HAVE_ST_RDEV |
3ec4dd97 | 408 | file->rdev = st.st_rdev; |
c627d613 AT |
409 | #endif |
410 | ||
411 | #if SUPPORT_LINKS | |
3ec4dd97 AT |
412 | if (S_ISLNK(st.st_mode)) { |
413 | int l; | |
414 | char lnk[MAXPATHLEN]; | |
415 | if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) { | |
416 | fprintf(FERROR,"readlink %s : %s\n", | |
417 | fname,strerror(errno)); | |
418 | return NULL; | |
419 | } | |
420 | lnk[l] = 0; | |
421 | file->link = strdup(lnk); | |
422 | } | |
c627d613 AT |
423 | #endif |
424 | ||
3ec4dd97 | 425 | if (always_checksum && S_ISREG(st.st_mode)) { |
2d0bb8eb AT |
426 | file->sum = (char *)malloc(MD4_SUM_LENGTH); |
427 | if (!file->sum) out_of_memory("md4 sum"); | |
3ec4dd97 AT |
428 | file_checksum(fname,file->sum,st.st_size); |
429 | } | |
c627d613 | 430 | |
3ec4dd97 AT |
431 | if (flist_dir) { |
432 | static char *lastdir; | |
433 | if (lastdir && strcmp(lastdir, flist_dir)==0) { | |
434 | file->basedir = lastdir; | |
435 | } else { | |
436 | file->basedir = strdup(flist_dir); | |
437 | lastdir = file->basedir; | |
438 | } | |
439 | } else { | |
440 | file->basedir = NULL; | |
441 | } | |
c627d613 | 442 | |
3ec4dd97 AT |
443 | if (!S_ISDIR(st.st_mode)) |
444 | total_size += st.st_size; | |
c627d613 | 445 | |
3ec4dd97 | 446 | return file; |
c627d613 AT |
447 | } |
448 | ||
449 | ||
450 | ||
649d65ed AT |
451 | static void send_file_name(int f,struct file_list *flist,char *fname, |
452 | int recursive) | |
c627d613 AT |
453 | { |
454 | struct file_struct *file; | |
455 | ||
a06d19e3 | 456 | file = make_file(fname); |
c627d613 AT |
457 | |
458 | if (!file) return; | |
459 | ||
460 | if (flist->count >= flist->malloced) { | |
3ec4dd97 AT |
461 | if (flist->malloced < 1000) |
462 | flist->malloced += 1000; | |
f9c51620 | 463 | else |
3ec4dd97 AT |
464 | flist->malloced *= 2; |
465 | flist->files = (struct file_struct **)realloc(flist->files, | |
466 | sizeof(flist->files[0])* | |
467 | flist->malloced); | |
f9c51620 AT |
468 | if (!flist->files) |
469 | out_of_memory("send_file_name"); | |
c627d613 AT |
470 | } |
471 | ||
3ec4dd97 AT |
472 | if (strcmp(file->basename,"")) { |
473 | flist->files[flist->count++] = file; | |
c627d613 AT |
474 | send_file_entry(file,f); |
475 | } | |
476 | ||
649d65ed | 477 | if (S_ISDIR(file->mode) && recursive) { |
c627d613 | 478 | char **last_exclude_list = local_exclude_list; |
3ec4dd97 | 479 | send_directory(f,flist,f_name(file)); |
c627d613 AT |
480 | local_exclude_list = last_exclude_list; |
481 | return; | |
482 | } | |
483 | } | |
484 | ||
485 | ||
486 | ||
487 | static void send_directory(int f,struct file_list *flist,char *dir) | |
488 | { | |
3ec4dd97 AT |
489 | DIR *d; |
490 | struct dirent *di; | |
491 | char fname[MAXPATHLEN]; | |
492 | int l; | |
493 | char *p; | |
494 | ||
495 | d = opendir(dir); | |
496 | if (!d) { | |
497 | fprintf(FERROR,"%s: %s\n", | |
498 | dir,strerror(errno)); | |
499 | return; | |
500 | } | |
c627d613 | 501 | |
3ec4dd97 AT |
502 | strncpy(fname,dir,MAXPATHLEN-1); |
503 | fname[MAXPATHLEN-1]=0; | |
504 | l = strlen(fname); | |
505 | if (fname[l-1] != '/') { | |
506 | if (l == MAXPATHLEN-1) { | |
507 | fprintf(FERROR,"skipping long-named directory %s\n",fname); | |
508 | closedir(d); | |
509 | return; | |
510 | } | |
511 | strcat(fname,"/"); | |
512 | l++; | |
513 | } | |
514 | p = fname + strlen(fname); | |
c627d613 | 515 | |
3ec4dd97 AT |
516 | if (cvs_exclude) { |
517 | if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) { | |
518 | strcpy(p,".cvsignore"); | |
519 | local_exclude_list = make_exclude_list(fname,NULL,0); | |
520 | } else { | |
521 | fprintf(FERROR,"cannot cvs-exclude in long-named directory %s\n",fname); | |
522 | } | |
523 | } | |
524 | ||
525 | for (di=readdir(d); di; di=readdir(d)) { | |
526 | if (strcmp(di->d_name,".")==0 || | |
527 | strcmp(di->d_name,"..")==0) | |
528 | continue; | |
529 | strncpy(p,di->d_name,MAXPATHLEN-(l+1)); | |
649d65ed | 530 | send_file_name(f,flist,fname,recurse); |
3ec4dd97 | 531 | } |
c627d613 | 532 | |
3ec4dd97 | 533 | closedir(d); |
c627d613 AT |
534 | } |
535 | ||
536 | ||
537 | ||
a06d19e3 | 538 | struct file_list *send_file_list(int f,int argc,char *argv[]) |
c627d613 | 539 | { |
649d65ed AT |
540 | int i,l; |
541 | struct stat st; | |
542 | char *p,*dir; | |
543 | char dbuf[MAXPATHLEN]; | |
544 | char lastpath[MAXPATHLEN]=""; | |
545 | struct file_list *flist; | |
546 | ||
547 | if (verbose && recurse && !am_server && f != -1) { | |
548 | fprintf(FINFO,"building file list ... "); | |
549 | fflush(FINFO); | |
550 | } | |
c627d613 | 551 | |
649d65ed AT |
552 | flist = (struct file_list *)malloc(sizeof(flist[0])); |
553 | if (!flist) out_of_memory("send_file_list"); | |
c627d613 | 554 | |
649d65ed AT |
555 | flist->count=0; |
556 | flist->malloced = 1000; | |
557 | flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])* | |
558 | flist->malloced); | |
559 | if (!flist->files) out_of_memory("send_file_list"); | |
c627d613 | 560 | |
649d65ed AT |
561 | for (i=0;i<argc;i++) { |
562 | char fname2[MAXPATHLEN]; | |
563 | char *fname = fname2; | |
c627d613 | 564 | |
649d65ed AT |
565 | strncpy(fname,argv[i],MAXPATHLEN-1); |
566 | fname[MAXPATHLEN-1] = 0; | |
c627d613 | 567 | |
649d65ed AT |
568 | l = strlen(fname); |
569 | if (l != 1 && fname[l-1] == '/') { | |
570 | strcat(fname,"."); | |
571 | } | |
c627d613 | 572 | |
649d65ed AT |
573 | if (link_stat(fname,&st) != 0) { |
574 | fprintf(FERROR,"%s : %s\n",fname,strerror(errno)); | |
575 | continue; | |
576 | } | |
c627d613 | 577 | |
649d65ed AT |
578 | if (S_ISDIR(st.st_mode) && !recurse) { |
579 | fprintf(FERROR,"skipping directory %s\n",fname); | |
580 | continue; | |
581 | } | |
c627d613 | 582 | |
649d65ed AT |
583 | dir = NULL; |
584 | ||
585 | if (!relative_paths) { | |
586 | p = strrchr(fname,'/'); | |
587 | if (p) { | |
588 | *p = 0; | |
589 | if (p == fname) | |
590 | dir = "/"; | |
591 | else | |
592 | dir = fname; | |
593 | fname = p+1; | |
594 | } | |
595 | } else if (f != -1 && (p=strrchr(fname,'/'))) { | |
596 | /* this ensures we send the intermediate directories, | |
597 | thus getting their permissions right */ | |
598 | *p = 0; | |
599 | if (strcmp(lastpath,fname)) { | |
600 | strcpy(lastpath, fname); | |
601 | *p = '/'; | |
602 | for (p=fname+1; (p=strchr(p,'/')); p++) { | |
603 | *p = 0; | |
604 | send_file_name(f, flist, fname, 0); | |
605 | *p = '/'; | |
606 | } | |
607 | } else { | |
608 | *p = '/'; | |
609 | } | |
610 | } | |
611 | ||
612 | if (!*fname) | |
613 | fname = "."; | |
614 | ||
615 | if (dir && *dir) { | |
616 | if (getcwd(dbuf,MAXPATHLEN-1) == NULL) { | |
617 | fprintf(FERROR,"getwd : %s\n",strerror(errno)); | |
618 | exit_cleanup(1); | |
619 | } | |
620 | if (chdir(dir) != 0) { | |
621 | fprintf(FERROR,"chdir %s : %s\n", | |
622 | dir,strerror(errno)); | |
623 | continue; | |
624 | } | |
625 | flist_dir = dir; | |
626 | if (one_file_system) | |
627 | set_filesystem(fname); | |
628 | send_file_name(f,flist,fname,recurse); | |
629 | flist_dir = NULL; | |
630 | if (chdir(dbuf) != 0) { | |
631 | fprintf(FERROR,"chdir %s : %s\n", | |
632 | dbuf,strerror(errno)); | |
633 | exit_cleanup(1); | |
634 | } | |
635 | continue; | |
636 | } | |
637 | ||
638 | if (one_file_system) | |
639 | set_filesystem(fname); | |
640 | send_file_name(f,flist,fname,recurse); | |
641 | } | |
dc5ddbcc | 642 | |
649d65ed AT |
643 | if (f != -1) { |
644 | send_file_entry(NULL,f); | |
645 | write_flush(f); | |
646 | } | |
c627d613 | 647 | |
649d65ed AT |
648 | if (verbose && recurse && !am_server && f != -1) |
649 | fprintf(FINFO,"done\n"); | |
650 | ||
651 | clean_flist(flist); | |
652 | ||
653 | /* now send the uid/gid list. This was introduced in protocol | |
654 | version 15 */ | |
655 | if (f != -1 && remote_version >= 15) { | |
656 | send_uid_list(f); | |
657 | } | |
f6c34742 | 658 | |
649d65ed | 659 | return flist; |
c627d613 AT |
660 | } |
661 | ||
662 | ||
663 | struct file_list *recv_file_list(int f) | |
664 | { | |
c627d613 | 665 | struct file_list *flist; |
182dca5c | 666 | unsigned char flags; |
c627d613 | 667 | |
a06d19e3 AT |
668 | if (verbose && recurse && !am_server) { |
669 | fprintf(FINFO,"receiving file list ... "); | |
670 | fflush(FINFO); | |
671 | } | |
c627d613 AT |
672 | |
673 | flist = (struct file_list *)malloc(sizeof(flist[0])); | |
674 | if (!flist) | |
675 | goto oom; | |
676 | ||
677 | flist->count=0; | |
3ec4dd97 AT |
678 | flist->malloced=1000; |
679 | flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])* | |
680 | flist->malloced); | |
c627d613 AT |
681 | if (!flist->files) |
682 | goto oom; | |
683 | ||
684 | ||
182dca5c | 685 | for (flags=read_byte(f); flags; flags=read_byte(f)) { |
c627d613 AT |
686 | int i = flist->count; |
687 | ||
688 | if (i >= flist->malloced) { | |
3ec4dd97 AT |
689 | if (flist->malloced < 1000) |
690 | flist->malloced += 1000; | |
f9c51620 | 691 | else |
3ec4dd97 AT |
692 | flist->malloced *= 2; |
693 | flist->files =(struct file_struct **)realloc(flist->files, | |
694 | sizeof(flist->files[0])* | |
695 | flist->malloced); | |
f9c51620 AT |
696 | if (!flist->files) |
697 | goto oom; | |
c627d613 AT |
698 | } |
699 | ||
182dca5c | 700 | receive_file_entry(&flist->files[i],flags,f); |
c627d613 | 701 | |
3ec4dd97 AT |
702 | if (S_ISREG(flist->files[i]->mode)) |
703 | total_size += flist->files[i]->length; | |
c627d613 AT |
704 | |
705 | flist->count++; | |
706 | ||
707 | if (verbose > 2) | |
3ec4dd97 | 708 | fprintf(FERROR,"recv_file_name(%s)\n",f_name(flist->files[i])); |
c627d613 AT |
709 | } |
710 | ||
711 | ||
712 | if (verbose > 2) | |
dc5ddbcc | 713 | fprintf(FERROR,"received %d names\n",flist->count); |
c627d613 AT |
714 | |
715 | clean_flist(flist); | |
716 | ||
a06d19e3 AT |
717 | if (verbose && recurse && !am_server) { |
718 | fprintf(FINFO,"done\n"); | |
719 | } | |
720 | ||
f6c34742 AT |
721 | /* now recv the uid/gid list. This was introduced in protocol version 15 */ |
722 | if (f != -1 && remote_version >= 15) { | |
723 | recv_uid_list(f, flist); | |
724 | } | |
725 | ||
c627d613 AT |
726 | return flist; |
727 | ||
728 | oom: | |
729 | out_of_memory("recv_file_list"); | |
730 | return NULL; /* not reached */ | |
731 | } | |
732 | ||
733 | ||
3ec4dd97 | 734 | int file_compare(struct file_struct **f1,struct file_struct **f2) |
c627d613 | 735 | { |
3ec4dd97 AT |
736 | if (!(*f1)->basename && !(*f2)->basename) return 0; |
737 | if (!(*f1)->basename) return -1; | |
738 | if (!(*f2)->basename) return 1; | |
739 | if ((*f1)->dirname == (*f2)->dirname) | |
740 | return strcmp((*f1)->basename, (*f2)->basename); | |
741 | return strcmp(f_name(*f1),f_name(*f2)); | |
c627d613 AT |
742 | } |
743 | ||
744 | ||
745 | int flist_find(struct file_list *flist,struct file_struct *f) | |
746 | { | |
d966ee25 AT |
747 | int low=0,high=flist->count-1; |
748 | ||
749 | if (flist->count <= 0) return -1; | |
750 | ||
751 | while (low != high) { | |
752 | int mid = (low+high)/2; | |
3ec4dd97 | 753 | int ret = file_compare(&flist->files[flist_up(flist, mid)],&f); |
d966ee25 AT |
754 | if (ret == 0) return flist_up(flist, mid); |
755 | if (ret > 0) { | |
756 | high=mid; | |
757 | } else { | |
758 | low=mid+1; | |
759 | } | |
760 | } | |
761 | ||
3ec4dd97 | 762 | if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0) |
d966ee25 AT |
763 | return flist_up(flist,low); |
764 | return -1; | |
c627d613 AT |
765 | } |
766 | ||
767 | ||
3ec4dd97 AT |
768 | /* |
769 | * free up one file | |
770 | */ | |
771 | static void free_file(struct file_struct *file) | |
c627d613 | 772 | { |
3ec4dd97 AT |
773 | if (!file) return; |
774 | if (file->basename) free(file->basename); | |
775 | if (file->link) free(file->link); | |
0b910560 | 776 | if (file->sum) free(file->sum); |
3ec4dd97 | 777 | bzero((char *)file, sizeof(*file)); |
3ec4dd97 | 778 | } |
c627d613 | 779 | |
c627d613 | 780 | |
3ec4dd97 AT |
781 | /* |
782 | * free up all elements in a flist | |
783 | */ | |
784 | void flist_free(struct file_list *flist) | |
785 | { | |
786 | int i; | |
787 | for (i=1;i<flist->count;i++) { | |
788 | free_file(flist->files[i]); | |
649d65ed | 789 | free(flist->files[i]); |
3ec4dd97 AT |
790 | } |
791 | bzero((char *)flist->files, sizeof(flist->files[0])*flist->count); | |
792 | free(flist->files); | |
793 | bzero((char *)flist, sizeof(*flist)); | |
794 | free(flist); | |
c627d613 AT |
795 | } |
796 | ||
797 | ||
798 | /* | |
799 | * This routine ensures we don't have any duplicate names in our file list. | |
800 | * duplicate names can cause corruption because of the pipelining | |
801 | */ | |
802 | void clean_flist(struct file_list *flist) | |
803 | { | |
3ec4dd97 | 804 | int i; |
c627d613 | 805 | |
3ec4dd97 AT |
806 | if (!flist || flist->count == 0) |
807 | return; | |
c627d613 | 808 | |
3ec4dd97 AT |
809 | qsort(flist->files,flist->count, |
810 | sizeof(flist->files[0]), | |
811 | (int (*)())file_compare); | |
812 | ||
813 | for (i=1;i<flist->count;i++) { | |
814 | if (flist->files[i]->basename && | |
649d65ed | 815 | flist->files[i-1]->basename && |
3ec4dd97 AT |
816 | strcmp(f_name(flist->files[i]), |
817 | f_name(flist->files[i-1])) == 0) { | |
818 | if (verbose > 1 && !am_server) | |
819 | fprintf(FERROR,"removing duplicate name %s from file list %d\n", | |
820 | f_name(flist->files[i-1]),i-1); | |
821 | free_file(flist->files[i]); | |
3ec4dd97 AT |
822 | } |
823 | } | |
824 | } | |
825 | ||
826 | ||
827 | /* | |
828 | * return the full filename of a flist entry | |
829 | */ | |
830 | char *f_name(struct file_struct *f) | |
831 | { | |
832 | static char names[10][MAXPATHLEN]; | |
833 | static int n; | |
834 | char *p = names[n]; | |
835 | ||
649d65ed | 836 | if (!f || !f->basename) return NULL; |
3ec4dd97 AT |
837 | |
838 | n = (n+1)%10; | |
839 | ||
840 | if (f->dirname) { | |
841 | sprintf(p, "%s/%s", f->dirname, f->basename); | |
842 | } else { | |
843 | strcpy(p, f->basename); | |
844 | } | |
845 | ||
846 | return p; | |
c627d613 AT |
847 | } |
848 |