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