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