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