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