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