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