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