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