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