Commit | Line | Data |
---|---|---|
dbda5fbf | 1 | /* |
c627d613 AT |
2 | Copyright (C) Andrew Tridgell 1996 |
3 | Copyright (C) Paul Mackerras 1996 | |
736a6a29 | 4 | Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org> |
dbda5fbf | 5 | |
c627d613 AT |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
dbda5fbf | 10 | |
c627d613 AT |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
dbda5fbf | 15 | |
c627d613 AT |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
736a6a29 MP |
21 | /** @file flist.c |
22 | * Generate and receive file lists | |
23 | * | |
24 | * @todo Get rid of the string_area optimization. Efficiently | |
25 | * allocating blocks is the responsibility of the system's malloc | |
26 | * library, not of rsync. | |
27 | * | |
172875cf MP |
28 | * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html |
29 | * | |
736a6a29 | 30 | **/ |
c627d613 AT |
31 | |
32 | #include "rsync.h" | |
33 | ||
a800434a AT |
34 | extern struct stats stats; |
35 | ||
c627d613 | 36 | extern int verbose; |
1bbd10fe | 37 | extern int do_progress; |
c627d613 AT |
38 | extern int am_server; |
39 | extern int always_checksum; | |
983b1ed3 WD |
40 | extern int module_id; |
41 | extern int ignore_errors; | |
c627d613 AT |
42 | |
43 | extern int cvs_exclude; | |
44 | ||
a06d19e3 | 45 | extern int recurse; |
808c57c3 | 46 | extern char curr_dir[MAXPATHLEN]; |
24d0fcde WD |
47 | extern char *files_from; |
48 | extern int filesfrom_fd; | |
a06d19e3 | 49 | |
c627d613 AT |
50 | extern int one_file_system; |
51 | extern int make_backups; | |
52 | extern int preserve_links; | |
dc5ddbcc | 53 | extern int preserve_hard_links; |
c627d613 AT |
54 | extern int preserve_perms; |
55 | extern int preserve_devices; | |
56 | extern int preserve_uid; | |
57 | extern int preserve_gid; | |
58 | extern int preserve_times; | |
6574b4f7 | 59 | extern int relative_paths; |
24d0fcde | 60 | extern int implied_dirs; |
82306bf6 | 61 | extern int copy_links; |
b5313607 | 62 | extern int copy_unsafe_links; |
d04e9c51 | 63 | extern int protocol_version; |
cb13abfe | 64 | extern int sanitize_paths; |
c627d613 | 65 | |
6902ed17 MP |
66 | extern int read_batch; |
67 | extern int write_batch; | |
68 | ||
429f9828 WD |
69 | extern struct exclude_struct **exclude_list; |
70 | extern struct exclude_struct **server_exclude_list; | |
71 | extern struct exclude_struct **local_exclude_list; | |
c627d613 | 72 | |
06c28400 WD |
73 | int io_error; |
74 | ||
3d382777 | 75 | static struct file_struct null_file; |
fea4db62 | 76 | static char empty_sum[MD4_SUM_LENGTH]; |
3d382777 | 77 | |
827c37f6 | 78 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups); |
0199b05f | 79 | |
58225000 | 80 | |
1bbd10fe | 81 | static int show_filelist_p(void) |
58225000 | 82 | { |
24d0fcde | 83 | return verbose && (recurse || files_from) && !am_server; |
1bbd10fe | 84 | } |
ebed4c3a | 85 | |
1bbd10fe DD |
86 | static void start_filelist_progress(char *kind) |
87 | { | |
88 | rprintf(FINFO, "%s ... ", kind); | |
89 | if ((verbose > 1) || do_progress) | |
90 | rprintf(FINFO, "\n"); | |
91 | rflush(FINFO); | |
58225000 MP |
92 | } |
93 | ||
db719fb0 | 94 | |
d27cbec5 | 95 | static void emit_filelist_progress(const struct file_list *flist) |
db719fb0 | 96 | { |
d27cbec5 | 97 | rprintf(FINFO, " %d files...\r", flist->count); |
db719fb0 MP |
98 | } |
99 | ||
100 | ||
d27cbec5 | 101 | static void maybe_emit_filelist_progress(const struct file_list *flist) |
58225000 | 102 | { |
d27cbec5 DD |
103 | if (do_progress && show_filelist_p() && ((flist->count % 100) == 0)) |
104 | emit_filelist_progress(flist); | |
58225000 MP |
105 | } |
106 | ||
107 | ||
1bbd10fe | 108 | static void finish_filelist_progress(const struct file_list *flist) |
58225000 | 109 | { |
1bbd10fe DD |
110 | if (do_progress) { |
111 | /* This overwrites the progress line */ | |
c7b562be MP |
112 | rprintf(FINFO, "%d file%sto consider\n", |
113 | flist->count, flist->count == 1 ? " " : "s "); | |
b7736c79 | 114 | } else |
1bbd10fe | 115 | rprintf(FINFO, "done\n"); |
58225000 MP |
116 | } |
117 | ||
86943126 MP |
118 | void show_flist_stats(void) |
119 | { | |
120 | /* Nothing yet */ | |
121 | } | |
122 | ||
123 | ||
5c66303a | 124 | static struct string_area *string_area_new(int size) |
3d382777 AT |
125 | { |
126 | struct string_area *a; | |
127 | ||
ebed4c3a MP |
128 | if (size <= 0) |
129 | size = ARENA_SIZE; | |
58cadc86 | 130 | a = new(struct string_area); |
ebed4c3a MP |
131 | if (!a) |
132 | out_of_memory("string_area_new"); | |
58cadc86 | 133 | a->current = a->base = new_array(char, size); |
ebed4c3a MP |
134 | if (!a->current) |
135 | out_of_memory("string_area_new buffer"); | |
3d382777 | 136 | a->end = a->base + size; |
5c66303a | 137 | a->next = NULL; |
3d382777 AT |
138 | |
139 | return a; | |
140 | } | |
141 | ||
5c66303a | 142 | static void string_area_free(struct string_area *a) |
3d382777 AT |
143 | { |
144 | struct string_area *next; | |
145 | ||
ebed4c3a | 146 | for (; a; a = next) { |
3d382777 AT |
147 | next = a->next; |
148 | free(a->base); | |
149 | } | |
150 | } | |
151 | ||
5c66303a | 152 | static char *string_area_malloc(struct string_area **ap, int size) |
3d382777 AT |
153 | { |
154 | char *p; | |
155 | struct string_area *a; | |
156 | ||
157 | /* does the request fit into the current space? */ | |
158 | a = *ap; | |
159 | if (a->current + size >= a->end) { | |
160 | /* no; get space, move new string_area to front of the list */ | |
161 | a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE); | |
162 | a->next = *ap; | |
163 | *ap = a; | |
164 | } | |
165 | ||
166 | /* have space; do the "allocation." */ | |
167 | p = a->current; | |
168 | a->current += size; | |
169 | return p; | |
170 | } | |
171 | ||
5c66303a | 172 | static char *string_area_strdup(struct string_area **ap, const char *src) |
3d382777 | 173 | { |
ebed4c3a | 174 | char *dest = string_area_malloc(ap, strlen(src) + 1); |
3d382777 AT |
175 | return strcpy(dest, src); |
176 | } | |
f7632fc6 AT |
177 | |
178 | static void list_file_entry(struct file_struct *f) | |
179 | { | |
740819ef | 180 | char perms[11]; |
f7632fc6 | 181 | |
7212be92 DD |
182 | if (!f->basename) |
183 | /* this can happen if duplicate names were removed */ | |
184 | return; | |
185 | ||
740819ef MP |
186 | permstring(perms, f->mode); |
187 | ||
f7632fc6 | 188 | if (preserve_links && S_ISLNK(f->mode)) { |
ebed4c3a MP |
189 | rprintf(FINFO, "%s %11.0f %s %s -> %s\n", |
190 | perms, | |
191 | (double) f->length, timestring(f->modtime), | |
728d0922 | 192 | f_name(f), f->u.link); |
f7632fc6 | 193 | } else { |
ebed4c3a MP |
194 | rprintf(FINFO, "%s %11.0f %s %s\n", |
195 | perms, | |
196 | (double) f->length, timestring(f->modtime), | |
197 | f_name(f)); | |
f7632fc6 AT |
198 | } |
199 | } | |
200 | ||
201 | ||
bd9e9ecc MP |
202 | /** |
203 | * Stat either a symlink or its referent, depending on the settings of | |
204 | * copy_links, copy_unsafe_links, etc. | |
205 | * | |
4e5db0ad MP |
206 | * @retval -1 on error |
207 | * | |
208 | * @retval 0 for success | |
209 | * | |
210 | * @post If @p path is a symlink, then @p linkbuf (of size @c | |
bd9e9ecc | 211 | * MAXPATHLEN) contains the symlink target. |
4e5db0ad MP |
212 | * |
213 | * @post @p buffer contains information about the link or the | |
214 | * referrent as appropriate, if they exist. | |
bd9e9ecc | 215 | **/ |
b7736c79 | 216 | int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf) |
b5313607 DD |
217 | { |
218 | #if SUPPORT_LINKS | |
b7736c79 | 219 | if (copy_links) |
6f2623fd | 220 | return do_stat(path, buffer); |
b7736c79 | 221 | if (do_lstat(path, buffer) == -1) |
b5313607 | 222 | return -1; |
6f2623fd | 223 | if (S_ISLNK(buffer->st_mode)) { |
b7736c79 | 224 | int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1); |
dbda5fbf | 225 | if (l == -1) |
b5313607 | 226 | return -1; |
6f2623fd | 227 | linkbuf[l] = 0; |
fc638474 DD |
228 | if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) { |
229 | if (verbose > 1) { | |
dbda5fbf | 230 | rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n", |
fc638474 DD |
231 | path, linkbuf); |
232 | } | |
6f2623fd | 233 | return do_stat(path, buffer); |
b5313607 DD |
234 | } |
235 | } | |
236 | return 0; | |
237 | #else | |
6f2623fd | 238 | return do_stat(path, buffer); |
b5313607 DD |
239 | #endif |
240 | } | |
241 | ||
6f2623fd | 242 | int link_stat(const char *path, STRUCT_STAT * buffer) |
82306bf6 AT |
243 | { |
244 | #if SUPPORT_LINKS | |
b7736c79 | 245 | if (copy_links) |
6f2623fd | 246 | return do_stat(path, buffer); |
b7736c79 | 247 | return do_lstat(path, buffer); |
82306bf6 | 248 | #else |
6f2623fd | 249 | return do_stat(path, buffer); |
82306bf6 AT |
250 | #endif |
251 | } | |
252 | ||
c627d613 | 253 | /* |
429f9828 WD |
254 | * This function is used to check if a file should be included/excluded |
255 | * from the list of files based on its name and type etc. The value of | |
256 | * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES. | |
c627d613 | 257 | */ |
429f9828 | 258 | static int check_exclude_file(char *fname, int is_dir, int exclude_level) |
c627d613 | 259 | { |
7d687932 | 260 | #if 0 /* This currently never happens, so avoid a useless compare. */ |
429f9828 WD |
261 | if (exclude_level == NO_EXCLUDES) |
262 | return 0; | |
263 | #endif | |
6931c138 | 264 | if (fname) { |
429f9828 | 265 | /* never exclude '.', even if somebody does --exclude '*' */ |
6931c138 WD |
266 | if (fname[0] == '.' && !fname[1]) |
267 | return 0; | |
268 | /* Handle the -R version of the '.' dir. */ | |
269 | if (fname[0] == '/') { | |
270 | int len = strlen(fname); | |
271 | if (fname[len-1] == '.' && fname[len-2] == '/') | |
272 | return 0; | |
273 | } | |
76e26e10 | 274 | } |
6931c138 WD |
275 | if (server_exclude_list |
276 | && check_exclude(server_exclude_list, fname, is_dir)) | |
429f9828 WD |
277 | return 1; |
278 | if (exclude_level != ALL_EXCLUDES) | |
279 | return 0; | |
280 | if (exclude_list && check_exclude(exclude_list, fname, is_dir)) | |
281 | return 1; | |
6931c138 WD |
282 | if (local_exclude_list |
283 | && check_exclude(local_exclude_list, fname, is_dir)) | |
76e26e10 | 284 | return 1; |
76e26e10 | 285 | return 0; |
c627d613 AT |
286 | } |
287 | ||
288 | /* used by the one_file_system code */ | |
289 | static dev_t filesystem_dev; | |
290 | ||
291 | static void set_filesystem(char *fname) | |
292 | { | |
ebed4c3a MP |
293 | STRUCT_STAT st; |
294 | if (link_stat(fname, &st) != 0) | |
295 | return; | |
296 | filesystem_dev = st.st_dev; | |
c627d613 AT |
297 | } |
298 | ||
299 | ||
b280a1f4 AT |
300 | static int to_wire_mode(mode_t mode) |
301 | { | |
b7736c79 | 302 | if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) |
b280a1f4 | 303 | return (mode & ~(_S_IFMT)) | 0120000; |
ebed4c3a | 304 | return (int) mode; |
b280a1f4 AT |
305 | } |
306 | ||
307 | static mode_t from_wire_mode(int mode) | |
308 | { | |
b7736c79 | 309 | if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) |
efe3037c | 310 | return (mode & ~(_S_IFMT)) | _S_IFLNK; |
ebed4c3a | 311 | return (mode_t) mode; |
b280a1f4 AT |
312 | } |
313 | ||
314 | ||
ebed4c3a | 315 | static void send_directory(int f, struct file_list *flist, char *dir); |
c627d613 | 316 | |
3a6a366f | 317 | static char *flist_dir; |
882e6893 | 318 | static int flist_dir_len; |
c627d613 | 319 | |
3ec4dd97 | 320 | |
d9d6bc52 MP |
321 | /** |
322 | * Make sure @p flist is big enough to hold at least @p flist->count | |
323 | * entries. | |
324 | **/ | |
325 | static void flist_expand(struct file_list *flist) | |
326 | { | |
327 | if (flist->count >= flist->malloced) { | |
2e7d1994 | 328 | void *new_ptr; |
dbda5fbf | 329 | |
d9d6bc52 MP |
330 | if (flist->malloced < 1000) |
331 | flist->malloced += 1000; | |
332 | else | |
333 | flist->malloced *= 2; | |
2e7d1994 | 334 | |
58cadc86 WD |
335 | if (flist->files) { |
336 | new_ptr = realloc_array(flist->files, | |
337 | struct file_struct *, | |
338 | flist->malloced); | |
339 | } else { | |
340 | new_ptr = new_array(struct file_struct *, | |
341 | flist->malloced); | |
342 | } | |
2e7d1994 | 343 | |
1d5a1da9 | 344 | if (verbose >= 2) { |
1bbd10fe | 345 | rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n", |
58cadc86 WD |
346 | (double)sizeof(flist->files[0]) |
347 | * flist->malloced, | |
2e7d1994 MP |
348 | (new_ptr == flist->files) ? " not" : ""); |
349 | } | |
dbda5fbf | 350 | |
2e7d1994 MP |
351 | flist->files = (struct file_struct **) new_ptr; |
352 | ||
d9d6bc52 MP |
353 | if (!flist->files) |
354 | out_of_memory("flist_expand"); | |
355 | } | |
356 | } | |
357 | ||
7b1a0c19 | 358 | void send_file_entry(struct file_struct *file, int f, unsigned short base_flags) |
c627d613 | 359 | { |
1ef00d20 | 360 | unsigned short flags; |
5911fee5 WD |
361 | static time_t modtime; |
362 | static mode_t mode; | |
363 | static DEV64_T rdev; /* just high bytes in p28 onward */ | |
364 | static DEV64_T dev; | |
365 | static uid_t uid; | |
366 | static gid_t gid; | |
367 | static char lastname[MAXPATHLEN]; | |
b7736c79 | 368 | char *fname, fbuf[MAXPATHLEN]; |
ebed4c3a | 369 | int l1, l2; |
72914a60 | 370 | |
ebed4c3a MP |
371 | if (f == -1) |
372 | return; | |
72914a60 AT |
373 | |
374 | if (!file) { | |
ebed4c3a | 375 | write_byte(f, 0); |
5911fee5 WD |
376 | modtime = 0, mode = 0; |
377 | rdev = 0, dev = 0; | |
378 | uid = 0, gid = 0; | |
379 | *lastname = '\0'; | |
72914a60 AT |
380 | return; |
381 | } | |
382 | ||
eca2adb4 MP |
383 | io_write_phase = "send_file_entry"; |
384 | ||
882e6893 | 385 | fname = f_name_to(file, fbuf); |
72914a60 AT |
386 | |
387 | flags = base_flags; | |
388 | ||
30f337c9 | 389 | if (file->mode == mode) |
d01d15e0 | 390 | flags |= XMIT_SAME_MODE; |
1ef00d20 | 391 | else |
30f337c9 | 392 | mode = file->mode; |
75bc8600 WD |
393 | if (preserve_devices) { |
394 | if (protocol_version < 28) { | |
728d0922 WD |
395 | if (IS_DEVICE(mode)) { |
396 | if (file->u.rdev == rdev) { | |
d01d15e0 WD |
397 | /* Set both flags to simplify the test |
398 | * when writing the data. */ | |
399 | flags |= XMIT_SAME_RDEV_pre28 | |
400 | | XMIT_SAME_HIGH_RDEV; | |
728d0922 WD |
401 | } else |
402 | rdev = file->u.rdev; | |
403 | } else | |
404 | rdev = 0; | |
405 | } else if (IS_DEVICE(mode)) { | |
406 | if ((file->u.rdev & ~0xFF) == rdev) | |
d01d15e0 | 407 | flags |= XMIT_SAME_HIGH_RDEV; |
30f337c9 | 408 | else |
728d0922 | 409 | rdev = file->u.rdev & ~0xFF; |
75bc8600 WD |
410 | } |
411 | } | |
30f337c9 | 412 | if (file->uid == uid) |
d01d15e0 | 413 | flags |= XMIT_SAME_UID; |
1ef00d20 | 414 | else |
30f337c9 WD |
415 | uid = file->uid; |
416 | if (file->gid == gid) | |
d01d15e0 | 417 | flags |= XMIT_SAME_GID; |
1ef00d20 | 418 | else |
30f337c9 WD |
419 | gid = file->gid; |
420 | if (file->modtime == modtime) | |
d01d15e0 | 421 | flags |= XMIT_SAME_TIME; |
1ef00d20 | 422 | else |
30f337c9 | 423 | modtime = file->modtime; |
92cc9dd7 WD |
424 | if (file->link_u.idev) { |
425 | if (file->F_DEV == dev) { | |
c4b4df4f | 426 | if (protocol_version >= 28) |
d01d15e0 | 427 | flags |= XMIT_SAME_DEV; |
728d0922 | 428 | } else |
92cc9dd7 | 429 | dev = file->F_DEV; |
d01d15e0 | 430 | flags |= XMIT_HAS_IDEV_DATA; |
c4b4df4f | 431 | } |
ebed4c3a MP |
432 | |
433 | for (l1 = 0; | |
434 | lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255); | |
dbda5fbf | 435 | l1++) {} |
eddd5d12 | 436 | l2 = strlen(fname+l1); |
72914a60 | 437 | |
ebed4c3a | 438 | if (l1 > 0) |
d01d15e0 | 439 | flags |= XMIT_SAME_NAME; |
ebed4c3a | 440 | if (l2 > 255) |
d01d15e0 | 441 | flags |= XMIT_LONG_NAME; |
72914a60 | 442 | |
75bc8600 WD |
443 | /* We must make sure we don't send a zero flags byte or |
444 | * the other end will terminate the flist transfer. */ | |
30f337c9 | 445 | if (flags == 0 && !S_ISDIR(mode)) |
9e4c32ea | 446 | flags |= XMIT_TOP_DIR; /* NOTE: no meaning for non-dir */ |
75bc8600 WD |
447 | if (protocol_version >= 28) { |
448 | if ((flags & 0xFF00) || flags == 0) { | |
d01d15e0 | 449 | flags |= XMIT_EXTENDED_FLAGS; |
75bc8600 WD |
450 | write_byte(f, flags); |
451 | write_byte(f, flags >> 8); | |
452 | } else | |
453 | write_byte(f, flags); | |
454 | } else { | |
455 | if (flags == 0) | |
d01d15e0 | 456 | flags |= XMIT_LONG_NAME; |
75bc8600 WD |
457 | write_byte(f, flags); |
458 | } | |
d01d15e0 | 459 | if (flags & XMIT_SAME_NAME) |
ebed4c3a | 460 | write_byte(f, l1); |
d01d15e0 | 461 | if (flags & XMIT_LONG_NAME) |
ebed4c3a | 462 | write_int(f, l2); |
72914a60 | 463 | else |
ebed4c3a MP |
464 | write_byte(f, l2); |
465 | write_buf(f, fname + l1, l2); | |
72914a60 | 466 | |
ebed4c3a | 467 | write_longint(f, file->length); |
d01d15e0 | 468 | if (!(flags & XMIT_SAME_TIME)) |
30f337c9 | 469 | write_int(f, modtime); |
d01d15e0 | 470 | if (!(flags & XMIT_SAME_MODE)) |
30f337c9 | 471 | write_int(f, to_wire_mode(mode)); |
d01d15e0 | 472 | if (preserve_uid && !(flags & XMIT_SAME_UID)) { |
30f337c9 WD |
473 | add_uid(uid); |
474 | write_int(f, uid); | |
72914a60 | 475 | } |
d01d15e0 | 476 | if (preserve_gid && !(flags & XMIT_SAME_GID)) { |
30f337c9 WD |
477 | add_gid(gid); |
478 | write_int(f, gid); | |
72914a60 | 479 | } |
30f337c9 | 480 | if (preserve_devices && IS_DEVICE(mode)) { |
d01d15e0 WD |
481 | /* If XMIT_SAME_HIGH_RDEV is off, XMIT_SAME_RDEV_pre28 is |
482 | * also off. Also, avoid using "rdev" because it may be | |
483 | * incomplete. */ | |
484 | if (!(flags & XMIT_SAME_HIGH_RDEV)) | |
728d0922 | 485 | write_int(f, file->u.rdev); |
75bc8600 | 486 | else if (protocol_version >= 28) |
728d0922 | 487 | write_byte(f, file->u.rdev); |
75bc8600 | 488 | } |
c627d613 AT |
489 | |
490 | #if SUPPORT_LINKS | |
30f337c9 | 491 | if (preserve_links && S_ISLNK(mode)) { |
728d0922 WD |
492 | write_int(f, strlen(file->u.link)); |
493 | write_buf(f, file->u.link, strlen(file->u.link)); | |
72914a60 | 494 | } |
c627d613 AT |
495 | #endif |
496 | ||
dc5ddbcc | 497 | #if SUPPORT_HARD_LINKS |
d01d15e0 | 498 | if (flags & XMIT_HAS_IDEV_DATA) { |
d04e9c51 | 499 | if (protocol_version < 26) { |
736a6a29 | 500 | /* 32-bit dev_t and ino_t */ |
30f337c9 | 501 | write_int(f, dev); |
92cc9dd7 | 502 | write_int(f, file->F_INODE); |
736a6a29 MP |
503 | } else { |
504 | /* 64-bit dev_t and ino_t */ | |
d01d15e0 | 505 | if (!(flags & XMIT_SAME_DEV)) |
30f337c9 | 506 | write_longint(f, dev); |
92cc9dd7 | 507 | write_longint(f, file->F_INODE); |
736a6a29 | 508 | } |
72914a60 | 509 | } |
dc5ddbcc AT |
510 | #endif |
511 | ||
728d0922 WD |
512 | if (always_checksum) { |
513 | char *sum; | |
514 | if (S_ISREG(mode)) | |
515 | sum = file->u.sum; | |
516 | else if (protocol_version < 28) { | |
517 | /* Prior to 28, we sent a useless set of nulls. */ | |
518 | sum = empty_sum; | |
519 | } else | |
520 | sum = NULL; | |
521 | if (sum) { | |
522 | write_buf(f, sum, protocol_version < 21? 2 | |
523 | : MD4_SUM_LENGTH); | |
524 | } | |
ebed4c3a | 525 | } |
182dca5c | 526 | |
ebed4c3a | 527 | strlcpy(lastname, fname, MAXPATHLEN); |
eca2adb4 MP |
528 | |
529 | io_write_phase = "unknown"; | |
182dca5c AT |
530 | } |
531 | ||
532 | ||
533 | ||
7b1a0c19 | 534 | void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f) |
182dca5c | 535 | { |
5911fee5 WD |
536 | static time_t modtime; |
537 | static mode_t mode; | |
538 | static DEV64_T rdev; /* just high bytes in p28 onward */ | |
539 | static DEV64_T dev; | |
540 | static uid_t uid; | |
541 | static gid_t gid; | |
542 | static char lastname[MAXPATHLEN]; | |
72914a60 | 543 | char thisname[MAXPATHLEN]; |
ebed4c3a | 544 | unsigned int l1 = 0, l2 = 0; |
72914a60 AT |
545 | char *p; |
546 | struct file_struct *file; | |
547 | ||
5911fee5 WD |
548 | if (!fptr) { |
549 | modtime = 0, mode = 0; | |
550 | rdev = 0, dev = 0; | |
551 | uid = 0, gid = 0; | |
552 | *lastname = '\0'; | |
553 | return; | |
554 | } | |
555 | ||
d01d15e0 | 556 | if (flags & XMIT_SAME_NAME) |
72914a60 | 557 | l1 = read_byte(f); |
ebed4c3a | 558 | |
d01d15e0 | 559 | if (flags & XMIT_LONG_NAME) |
72914a60 AT |
560 | l2 = read_int(f); |
561 | else | |
562 | l2 = read_byte(f); | |
563 | ||
58cadc86 | 564 | file = new(struct file_struct); |
ebed4c3a MP |
565 | if (!file) |
566 | out_of_memory("receive_file_entry"); | |
567 | memset((char *) file, 0, sizeof(*file)); | |
72914a60 AT |
568 | (*fptr) = file; |
569 | ||
ebed4c3a MP |
570 | if (l2 >= MAXPATHLEN - l1) { |
571 | rprintf(FERROR, | |
572 | "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n", | |
d0fd26aa AT |
573 | flags, l1, l2, lastname); |
574 | overflow("receive_file_entry"); | |
575 | } | |
72914a60 | 576 | |
ebed4c3a MP |
577 | strlcpy(thisname, lastname, l1 + 1); |
578 | read_sbuf(f, &thisname[l1], l2); | |
579 | thisname[l1 + l2] = 0; | |
72914a60 | 580 | |
ebed4c3a MP |
581 | strlcpy(lastname, thisname, MAXPATHLEN); |
582 | lastname[MAXPATHLEN - 1] = 0; | |
72914a60 AT |
583 | |
584 | clean_fname(thisname); | |
585 | ||
cb13abfe DD |
586 | if (sanitize_paths) { |
587 | sanitize_path(thisname, NULL); | |
588 | } | |
589 | ||
ebed4c3a | 590 | if ((p = strrchr(thisname, '/'))) { |
72914a60 AT |
591 | static char *lastdir; |
592 | *p = 0; | |
b7736c79 | 593 | if (lastdir && strcmp(thisname, lastdir) == 0) |
72914a60 | 594 | file->dirname = lastdir; |
b7736c79 | 595 | else { |
72914a60 AT |
596 | file->dirname = strdup(thisname); |
597 | lastdir = file->dirname; | |
598 | } | |
ebed4c3a | 599 | file->basename = strdup(p + 1); |
72914a60 AT |
600 | } else { |
601 | file->dirname = NULL; | |
602 | file->basename = strdup(thisname); | |
603 | } | |
604 | ||
ebed4c3a MP |
605 | if (!file->basename) |
606 | out_of_memory("receive_file_entry 1"); | |
72914a60 | 607 | |
d01d15e0 | 608 | file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0; |
72914a60 | 609 | file->length = read_longint(f); |
d01d15e0 | 610 | if (!(flags & XMIT_SAME_TIME)) |
30f337c9 WD |
611 | modtime = (time_t)read_int(f); |
612 | file->modtime = modtime; | |
d01d15e0 | 613 | if (!(flags & XMIT_SAME_MODE)) |
30f337c9 WD |
614 | mode = from_wire_mode(read_int(f)); |
615 | file->mode = mode; | |
1ef00d20 WD |
616 | |
617 | if (preserve_uid) { | |
d01d15e0 | 618 | if (!(flags & XMIT_SAME_UID)) |
30f337c9 WD |
619 | uid = (uid_t)read_int(f); |
620 | file->uid = uid; | |
1ef00d20 WD |
621 | } |
622 | if (preserve_gid) { | |
d01d15e0 | 623 | if (!(flags & XMIT_SAME_GID)) |
30f337c9 WD |
624 | gid = (gid_t)read_int(f); |
625 | file->gid = gid; | |
1ef00d20 WD |
626 | } |
627 | if (preserve_devices) { | |
75bc8600 | 628 | if (protocol_version < 28) { |
30f337c9 | 629 | if (IS_DEVICE(mode)) { |
d01d15e0 | 630 | if (!(flags & XMIT_SAME_RDEV_pre28)) |
30f337c9 | 631 | rdev = (DEV64_T)read_int(f); |
728d0922 | 632 | file->u.rdev = rdev; |
75bc8600 | 633 | } else |
30f337c9 WD |
634 | rdev = 0; |
635 | } else if (IS_DEVICE(mode)) { | |
d01d15e0 | 636 | if (!(flags & XMIT_SAME_HIGH_RDEV)) { |
728d0922 WD |
637 | file->u.rdev = (DEV64_T)read_int(f); |
638 | rdev = file->u.rdev & ~0xFF; | |
30f337c9 | 639 | } else |
728d0922 | 640 | file->u.rdev = rdev | (DEV64_T)read_byte(f); |
1ef00d20 | 641 | } |
b7736c79 | 642 | } |
72914a60 | 643 | |
30f337c9 | 644 | if (preserve_links && S_ISLNK(mode)) { |
72914a60 | 645 | int l = read_int(f); |
9dd891bb | 646 | if (l < 0) { |
ebed4c3a | 647 | rprintf(FERROR, "overflow: l=%d\n", l); |
9dd891bb MP |
648 | overflow("receive_file_entry"); |
649 | } | |
728d0922 | 650 | if (!(file->u.link = new_array(char, l + 1))) |
ebed4c3a | 651 | out_of_memory("receive_file_entry 2"); |
728d0922 | 652 | read_sbuf(f, file->u.link, l); |
b7736c79 | 653 | if (sanitize_paths) |
728d0922 | 654 | sanitize_path(file->u.link, file->dirname); |
72914a60 | 655 | } |
dc5ddbcc | 656 | #if SUPPORT_HARD_LINKS |
92cc9dd7 | 657 | if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode)) |
d01d15e0 WD |
658 | flags |= XMIT_HAS_IDEV_DATA; |
659 | if (flags & XMIT_HAS_IDEV_DATA) { | |
92cc9dd7 WD |
660 | if (!(file->link_u.idev = new(struct idev))) |
661 | out_of_memory("file inode data"); | |
d04e9c51 | 662 | if (protocol_version < 26) { |
30f337c9 | 663 | dev = read_int(f); |
92cc9dd7 | 664 | file->F_INODE = read_int(f); |
736a6a29 | 665 | } else { |
d01d15e0 | 666 | if (!(flags & XMIT_SAME_DEV)) |
30f337c9 | 667 | dev = read_longint(f); |
92cc9dd7 | 668 | file->F_INODE = read_longint(f); |
736a6a29 | 669 | } |
92cc9dd7 | 670 | file->F_DEV = dev; |
72914a60 | 671 | } |
dc5ddbcc | 672 | #endif |
ebed4c3a | 673 | |
72914a60 | 674 | if (always_checksum) { |
7c4f063b | 675 | char *sum; |
fea4db62 | 676 | if (S_ISREG(mode)) { |
728d0922 | 677 | sum = file->u.sum = new_array(char, MD4_SUM_LENGTH); |
fea4db62 WD |
678 | if (!sum) |
679 | out_of_memory("md4 sum"); | |
680 | } else if (protocol_version < 28) { | |
681 | /* Prior to 28, we get a useless set of nulls. */ | |
7c4f063b | 682 | sum = empty_sum; |
fea4db62 WD |
683 | } else |
684 | sum = NULL; | |
685 | if (sum) { | |
686 | read_buf(f, sum, protocol_version < 21? 2 | |
687 | : MD4_SUM_LENGTH); | |
688 | } | |
72914a60 | 689 | } |
ebed4c3a | 690 | |
72914a60 AT |
691 | if (!preserve_perms) { |
692 | extern int orig_umask; | |
693 | /* set an appropriate set of permissions based on original | |
fea4db62 | 694 | * permissions and umask. This emulates what GNU cp does */ |
72914a60 AT |
695 | file->mode &= ~orig_umask; |
696 | } | |
c627d613 AT |
697 | } |
698 | ||
699 | ||
34fada30 | 700 | /* determine if a file in a different filesystem should be skipped |
0c5f37d9 AT |
701 | when one_file_system is set. We bascally only want to include |
702 | the mount points - but they can be hard to find! */ | |
ebed4c3a | 703 | static int skip_filesystem(char *fname, STRUCT_STAT * st) |
0c5f37d9 | 704 | { |
bcacc18b | 705 | STRUCT_STAT st2; |
f673ea84 | 706 | char *p = strrchr(fname, '/'); |
0c5f37d9 AT |
707 | |
708 | /* skip all but directories */ | |
ebed4c3a MP |
709 | if (!S_ISDIR(st->st_mode)) |
710 | return 1; | |
0c5f37d9 AT |
711 | |
712 | /* if its not a subdirectory then allow */ | |
f673ea84 | 713 | if (!p) |
ebed4c3a | 714 | return 0; |
0c5f37d9 | 715 | |
f673ea84 S |
716 | *p = 0; |
717 | if (link_stat(fname, &st2)) { | |
718 | *p = '/'; | |
0c5f37d9 AT |
719 | return 0; |
720 | } | |
f673ea84 | 721 | *p = '/'; |
ebed4c3a | 722 | |
0c5f37d9 AT |
723 | return (st2.st_dev != filesystem_dev); |
724 | } | |
4fe159a8 | 725 | |
3d382777 | 726 | #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p)) |
87a819ed MP |
727 | /* IRIX cc cares that the operands to the ternary have the same type. */ |
728 | #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i)) | |
3d382777 | 729 | |
db719fb0 MP |
730 | /** |
731 | * Create a file_struct for a named file by reading its stat() | |
732 | * information and performing extensive checks against global | |
733 | * options. | |
734 | * | |
735 | * @return the new file, or NULL if there was an error or this file | |
736 | * should be excluded. | |
737 | * | |
738 | * @todo There is a small optimization opportunity here to avoid | |
739 | * stat()ing the file in some circumstances, which has a certain cost. | |
740 | * We are called immediately after doing readdir(), and so we may | |
741 | * already know the d_type of the file. We could for example avoid | |
742 | * statting directories if we're not recursing, but this is not a very | |
743 | * important case. Some systems may not have d_type. | |
744 | **/ | |
429f9828 WD |
745 | struct file_struct *make_file(char *fname, struct string_area **ap, |
746 | int exclude_level) | |
c627d613 | 747 | { |
3ec4dd97 | 748 | struct file_struct *file; |
bcacc18b | 749 | STRUCT_STAT st; |
3ec4dd97 AT |
750 | char sum[SUM_LENGTH]; |
751 | char *p; | |
752 | char cleaned_name[MAXPATHLEN]; | |
b5313607 | 753 | char linkbuf[MAXPATHLEN]; |
3ec4dd97 | 754 | |
882e6893 WD |
755 | if (strlcpy(cleaned_name, fname, sizeof cleaned_name) |
756 | >= sizeof cleaned_name - flist_dir_len) { | |
757 | rprintf(FINFO, "skipping overly long name: %s\n", fname); | |
758 | return NULL; | |
759 | } | |
3ec4dd97 | 760 | clean_fname(cleaned_name); |
b7736c79 | 761 | if (sanitize_paths) |
cb13abfe | 762 | sanitize_path(cleaned_name, NULL); |
3ec4dd97 AT |
763 | fname = cleaned_name; |
764 | ||
ebed4c3a | 765 | memset(sum, 0, SUM_LENGTH); |
3ec4dd97 | 766 | |
ebed4c3a | 767 | if (readlink_stat(fname, &st, linkbuf) != 0) { |
76e26e10 | 768 | int save_errno = errno; |
429f9828 | 769 | if (errno == ENOENT && exclude_level != NO_EXCLUDES) { |
dbda5fbf | 770 | /* either symlink pointing nowhere or file that |
1b85e3f1 DD |
771 | * was removed during rsync run; see if excluded |
772 | * before reporting an error */ | |
429f9828 | 773 | if (check_exclude_file(fname, 0, exclude_level)) { |
76e26e10 DD |
774 | /* file is excluded anyway, ignore silently */ |
775 | return NULL; | |
776 | } | |
777 | } | |
06c28400 | 778 | io_error |= IOERR_GENERAL; |
ea42541f WD |
779 | rprintf(FERROR, "readlink %s failed: %s\n", |
780 | full_fname(fname), strerror(save_errno)); | |
3ec4dd97 AT |
781 | return NULL; |
782 | } | |
c627d613 | 783 | |
429f9828 WD |
784 | /* backup.c calls us with exclude_level set to NO_EXCLUDES. */ |
785 | if (exclude_level == NO_EXCLUDES) | |
ebed4c3a | 786 | goto skip_excludes; |
ac1a0994 | 787 | |
24d0fcde | 788 | if (S_ISDIR(st.st_mode) && !recurse && !files_from) { |
ebed4c3a | 789 | rprintf(FINFO, "skipping directory %s\n", fname); |
3ec4dd97 AT |
790 | return NULL; |
791 | } | |
ebed4c3a | 792 | |
0c5f37d9 AT |
793 | if (one_file_system && st.st_dev != filesystem_dev) { |
794 | if (skip_filesystem(fname, &st)) | |
795 | return NULL; | |
796 | } | |
ebed4c3a | 797 | |
429f9828 | 798 | if (check_exclude_file(fname, S_ISDIR(st.st_mode) != 0, exclude_level)) |
76e26e10 DD |
799 | return NULL; |
800 | ||
ebed4c3a | 801 | if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0) |
ac1a0994 AT |
802 | return NULL; |
803 | ||
ebed4c3a | 804 | skip_excludes: |
ac1a0994 | 805 | |
3ec4dd97 | 806 | if (verbose > 2) |
429f9828 | 807 | rprintf(FINFO, "make_file(%s,*,%d)\n", fname, exclude_level); |
ebed4c3a | 808 | |
58cadc86 | 809 | file = new(struct file_struct); |
ebed4c3a MP |
810 | if (!file) |
811 | out_of_memory("make_file"); | |
812 | memset((char *) file, 0, sizeof(*file)); | |
3ec4dd97 | 813 | |
ebed4c3a | 814 | if ((p = strrchr(fname, '/'))) { |
3ec4dd97 AT |
815 | static char *lastdir; |
816 | *p = 0; | |
b7736c79 | 817 | if (lastdir && strcmp(fname, lastdir) == 0) |
3ec4dd97 | 818 | file->dirname = lastdir; |
b7736c79 | 819 | else { |
a20aa42a | 820 | file->dirname = strdup(fname); |
3ec4dd97 AT |
821 | lastdir = file->dirname; |
822 | } | |
ebed4c3a | 823 | file->basename = STRDUP(ap, p + 1); |
3ec4dd97 AT |
824 | *p = '/'; |
825 | } else { | |
826 | file->dirname = NULL; | |
3d382777 | 827 | file->basename = STRDUP(ap, fname); |
3ec4dd97 | 828 | } |
c627d613 | 829 | |
3ec4dd97 AT |
830 | file->modtime = st.st_mtime; |
831 | file->length = st.st_size; | |
832 | file->mode = st.st_mode; | |
833 | file->uid = st.st_uid; | |
834 | file->gid = st.st_gid; | |
c4b4df4f | 835 | if (preserve_hard_links) { |
92cc9dd7 | 836 | if (protocol_version < 28 ? S_ISREG(st.st_mode) |
c4b4df4f | 837 | : !S_ISDIR(st.st_mode) && st.st_nlink > 1) { |
92cc9dd7 WD |
838 | if (!(file->link_u.idev = new(struct idev))) |
839 | out_of_memory("file inode data"); | |
840 | file->F_DEV = st.st_dev; | |
841 | file->F_INODE = st.st_ino; | |
c4b4df4f WD |
842 | } |
843 | } | |
5f78da20 | 844 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
728d0922 WD |
845 | if (IS_DEVICE(st.st_mode)) |
846 | file->u.rdev = st.st_rdev; | |
c627d613 AT |
847 | #endif |
848 | ||
849 | #if SUPPORT_LINKS | |
b7736c79 | 850 | if (S_ISLNK(st.st_mode)) |
728d0922 | 851 | file->u.link = STRDUP(ap, linkbuf); |
c627d613 AT |
852 | #endif |
853 | ||
fea4db62 | 854 | if (always_checksum && S_ISREG(st.st_mode)) { |
728d0922 | 855 | if (!(file->u.sum = (char*)MALLOC(ap, MD4_SUM_LENGTH))) |
ebed4c3a | 856 | out_of_memory("md4 sum"); |
728d0922 | 857 | file_checksum(fname, file->u.sum, st.st_size); |
ebed4c3a | 858 | } |
c627d613 | 859 | |
882e6893 | 860 | file->basedir = flist_dir; |
c627d613 | 861 | |
3ec4dd97 | 862 | if (!S_ISDIR(st.st_mode)) |
a800434a | 863 | stats.total_size += st.st_size; |
c627d613 | 864 | |
3ec4dd97 | 865 | return file; |
c627d613 AT |
866 | } |
867 | ||
868 | ||
ebed4c3a | 869 | void send_file_name(int f, struct file_list *flist, char *fname, |
1ef00d20 | 870 | int recursive, unsigned short base_flags) |
c627d613 | 871 | { |
ebed4c3a | 872 | struct file_struct *file; |
b7736c79 | 873 | char fbuf[MAXPATHLEN]; |
429f9828 | 874 | extern int delete_excluded; |
ebed4c3a | 875 | |
429f9828 WD |
876 | /* f is set to -1 when calculating deletion file list */ |
877 | file = make_file(fname, &flist->string_area, | |
878 | f == -1 && delete_excluded? SERVER_EXCLUDES | |
879 | : ALL_EXCLUDES); | |
ebed4c3a MP |
880 | |
881 | if (!file) | |
882 | return; | |
883 | ||
db719fb0 | 884 | maybe_emit_filelist_progress(flist); |
ebed4c3a | 885 | |
d9d6bc52 | 886 | flist_expand(flist); |
ebed4c3a | 887 | |
64c3523a | 888 | if (write_batch) |
d01d15e0 | 889 | file->flags |= FLAG_TOP_DIR; |
ebed4c3a | 890 | |
c120ff37 | 891 | if (file->basename[0]) { |
ebed4c3a MP |
892 | flist->files[flist->count++] = file; |
893 | send_file_entry(file, f, base_flags); | |
894 | } | |
895 | ||
896 | if (S_ISDIR(file->mode) && recursive) { | |
d01d15e0 | 897 | struct exclude_struct **last_exclude_list = local_exclude_list; |
882e6893 | 898 | send_directory(f, flist, f_name_to(file, fbuf)); |
ebed4c3a MP |
899 | local_exclude_list = last_exclude_list; |
900 | return; | |
901 | } | |
c627d613 AT |
902 | } |
903 | ||
904 | ||
ebed4c3a | 905 | static void send_directory(int f, struct file_list *flist, char *dir) |
c627d613 | 906 | { |
3ec4dd97 AT |
907 | DIR *d; |
908 | struct dirent *di; | |
909 | char fname[MAXPATHLEN]; | |
eddd5d12 | 910 | unsigned int offset; |
3ec4dd97 AT |
911 | char *p; |
912 | ||
913 | d = opendir(dir); | |
914 | if (!d) { | |
06c28400 | 915 | io_error |= IOERR_GENERAL; |
ea42541f WD |
916 | rprintf(FERROR, "opendir %s failed: %s\n", |
917 | full_fname(dir), strerror(errno)); | |
3ec4dd97 AT |
918 | return; |
919 | } | |
c627d613 | 920 | |
eddd5d12 WD |
921 | offset = strlcpy(fname, dir, MAXPATHLEN); |
922 | p = fname + offset; | |
923 | if (offset >= MAXPATHLEN || p[-1] != '/') { | |
924 | if (offset >= MAXPATHLEN - 1) { | |
06c28400 | 925 | io_error |= IOERR_GENERAL; |
ea42541f WD |
926 | rprintf(FERROR, "skipping long-named directory: %s\n", |
927 | full_fname(fname)); | |
3ec4dd97 AT |
928 | closedir(d); |
929 | return; | |
930 | } | |
eddd5d12 WD |
931 | *p++ = '/'; |
932 | offset++; | |
3ec4dd97 | 933 | } |
c627d613 | 934 | |
3d913675 AT |
935 | local_exclude_list = NULL; |
936 | ||
3ec4dd97 | 937 | if (cvs_exclude) { |
eddd5d12 WD |
938 | if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset) |
939 | < MAXPATHLEN - offset) | |
57469f6c | 940 | add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE); |
eddd5d12 | 941 | else { |
06c28400 | 942 | io_error |= IOERR_GENERAL; |
ebed4c3a MP |
943 | rprintf(FINFO, |
944 | "cannot cvs-exclude in long-named directory %s\n", | |
ea42541f | 945 | full_fname(fname)); |
3ec4dd97 | 946 | } |
ebed4c3a MP |
947 | } |
948 | ||
6a7cc46c | 949 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
d6e6ecbd | 950 | char *dname = d_name(di); |
6a7cc46c S |
951 | if (dname[0] == '.' && (dname[1] == '\0' |
952 | || (dname[1] == '.' && dname[2] == '\0'))) | |
3ec4dd97 | 953 | continue; |
eddd5d12 WD |
954 | if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset) |
955 | send_file_name(f, flist, fname, recurse, 0); | |
956 | else { | |
957 | io_error |= IOERR_GENERAL; | |
958 | rprintf(FINFO, | |
959 | "cannot send long-named file %s\n", | |
960 | full_fname(fname)); | |
961 | } | |
3ec4dd97 | 962 | } |
6a7cc46c | 963 | if (errno) { |
06c28400 | 964 | io_error |= IOERR_GENERAL; |
6a7cc46c | 965 | rprintf(FERROR, "readdir(%s): (%d) %s\n", |
eddd5d12 | 966 | dir, errno, strerror(errno)); |
6a7cc46c | 967 | } |
c627d613 | 968 | |
429f9828 WD |
969 | if (local_exclude_list) |
970 | free_exclude_list(&local_exclude_list); /* Zeros pointer too */ | |
3d913675 | 971 | |
3ec4dd97 | 972 | closedir(d); |
c627d613 AT |
973 | } |
974 | ||
975 | ||
c4fea82f | 976 | /** |
429f9828 WD |
977 | * The delete_files() function in receiver.c sets f to -1 so that we just |
978 | * construct the file list in memory without sending it over the wire. It | |
979 | * also has the side-effect of ignoring user-excludes if delete_excluded | |
980 | * is set (so that the delete list includes user-excluded files). | |
c4fea82f | 981 | **/ |
ebed4c3a | 982 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
c627d613 | 983 | { |
24d0fcde | 984 | int l; |
bcacc18b | 985 | STRUCT_STAT st; |
808c57c3 | 986 | char *p, *dir, olddir[sizeof curr_dir]; |
ebed4c3a | 987 | char lastpath[MAXPATHLEN] = ""; |
649d65ed | 988 | struct file_list *flist; |
a800434a | 989 | int64 start_write; |
24d0fcde | 990 | int use_ff_fd = 0; |
649d65ed | 991 | |
1bbd10fe DD |
992 | if (show_filelist_p() && f != -1) |
993 | start_filelist_progress("building file list"); | |
c627d613 | 994 | |
a800434a AT |
995 | start_write = stats.total_written; |
996 | ||
3d382777 | 997 | flist = flist_new(); |
c627d613 | 998 | |
d6dead6b | 999 | if (f != -1) { |
76c21947 | 1000 | io_start_buffering_out(f); |
24d0fcde | 1001 | if (filesfrom_fd >= 0) { |
808c57c3 | 1002 | if (argv[0] && !push_dir(argv[0])) { |
ea42541f WD |
1003 | rprintf(FERROR, "push_dir %s failed: %s\n", |
1004 | full_fname(argv[0]), strerror(errno)); | |
24d0fcde WD |
1005 | exit_cleanup(RERR_FILESELECT); |
1006 | } | |
1007 | use_ff_fd = 1; | |
1008 | } | |
d6dead6b AT |
1009 | } |
1010 | ||
24d0fcde | 1011 | while (1) { |
fc638474 DD |
1012 | char fname2[MAXPATHLEN]; |
1013 | char *fname = fname2; | |
c627d613 | 1014 | |
24d0fcde WD |
1015 | if (use_ff_fd) { |
1016 | if (read_filesfrom_line(filesfrom_fd, fname) == 0) | |
1017 | break; | |
1018 | sanitize_path(fname, NULL); | |
1019 | } else { | |
1020 | if (argc-- == 0) | |
1021 | break; | |
1022 | strlcpy(fname, *argv++, MAXPATHLEN); | |
1023 | if (sanitize_paths) | |
1024 | sanitize_path(fname, NULL); | |
1025 | } | |
c627d613 | 1026 | |
649d65ed | 1027 | l = strlen(fname); |
6931c138 WD |
1028 | if (fname[l - 1] == '/') { |
1029 | if (l == 2 && fname[0] == '.') { | |
1030 | /* Turn "./" into just "." rather than "./." */ | |
1031 | fname[1] = '\0'; | |
eddd5d12 WD |
1032 | } else if (l < MAXPATHLEN) { |
1033 | fname[l++] = '.'; | |
1034 | fname[l] = '\0'; | |
53f821f1 | 1035 | } |
649d65ed | 1036 | } |
c627d613 | 1037 | |
ebed4c3a | 1038 | if (link_stat(fname, &st) != 0) { |
f76933b1 | 1039 | if (f != -1) { |
06c28400 | 1040 | io_error |= IOERR_GENERAL; |
ea42541f WD |
1041 | rprintf(FERROR, "link_stat %s failed: %s\n", |
1042 | full_fname(fname), strerror(errno)); | |
f76933b1 | 1043 | } |
649d65ed AT |
1044 | continue; |
1045 | } | |
c627d613 | 1046 | |
24d0fcde | 1047 | if (S_ISDIR(st.st_mode) && !recurse && !files_from) { |
ebed4c3a | 1048 | rprintf(FINFO, "skipping directory %s\n", fname); |
649d65ed AT |
1049 | continue; |
1050 | } | |
c627d613 | 1051 | |
649d65ed | 1052 | dir = NULL; |
808c57c3 | 1053 | olddir[0] = '\0'; |
649d65ed AT |
1054 | |
1055 | if (!relative_paths) { | |
ebed4c3a | 1056 | p = strrchr(fname, '/'); |
649d65ed AT |
1057 | if (p) { |
1058 | *p = 0; | |
ebed4c3a | 1059 | if (p == fname) |
649d65ed AT |
1060 | dir = "/"; |
1061 | else | |
ebed4c3a MP |
1062 | dir = fname; |
1063 | fname = p + 1; | |
649d65ed | 1064 | } |
24d0fcde | 1065 | } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) { |
649d65ed AT |
1066 | /* this ensures we send the intermediate directories, |
1067 | thus getting their permissions right */ | |
2154309a | 1068 | char *lp = lastpath, *fn = fname, *slash = fname; |
649d65ed | 1069 | *p = 0; |
2154309a WD |
1070 | /* Skip any initial directories in our path that we |
1071 | * have in common with lastpath. */ | |
1072 | while (*fn && *lp == *fn) { | |
1073 | if (*fn == '/') | |
1074 | slash = fn; | |
1075 | lp++, fn++; | |
1076 | } | |
1077 | *p = '/'; | |
1078 | if (fn != p || (*lp && *lp != '/')) { | |
1079 | int copy_links_saved = copy_links; | |
1080 | int recurse_saved = recurse; | |
1081 | copy_links = copy_unsafe_links; | |
1082 | /* set recurse to 1 to prevent make_file | |
1083 | * from ignoring directory, but still | |
1084 | * turn off the recursive parameter to | |
1085 | * send_file_name */ | |
1086 | recurse = 1; | |
1087 | while ((slash = strchr(slash+1, '/')) != 0) { | |
1088 | *slash = 0; | |
1089 | send_file_name(f, flist, fname, 0, 0); | |
1090 | *slash = '/'; | |
649d65ed | 1091 | } |
2154309a WD |
1092 | copy_links = copy_links_saved; |
1093 | recurse = recurse_saved; | |
1094 | *p = 0; | |
1095 | strlcpy(lastpath, fname, sizeof lastpath); | |
649d65ed AT |
1096 | *p = '/'; |
1097 | } | |
1098 | } | |
ebed4c3a | 1099 | |
649d65ed AT |
1100 | if (!*fname) |
1101 | fname = "."; | |
ebed4c3a | 1102 | |
649d65ed | 1103 | if (dir && *dir) { |
882e6893 WD |
1104 | static char *lastdir; |
1105 | static int lastdir_len; | |
1106 | ||
808c57c3 | 1107 | strcpy(olddir, curr_dir); /* can't overflow */ |
5243c216 | 1108 | |
808c57c3 | 1109 | if (!push_dir(dir)) { |
06c28400 | 1110 | io_error |= IOERR_GENERAL; |
ea42541f WD |
1111 | rprintf(FERROR, "push_dir %s failed: %s\n", |
1112 | full_fname(dir), strerror(errno)); | |
649d65ed AT |
1113 | continue; |
1114 | } | |
5243c216 | 1115 | |
882e6893 WD |
1116 | if (lastdir && strcmp(lastdir, dir) == 0) { |
1117 | flist_dir = lastdir; | |
1118 | flist_dir_len = lastdir_len; | |
1119 | } else { | |
1120 | if (lastdir) | |
1121 | free(lastdir); | |
1122 | flist_dir = lastdir = strdup(dir); | |
1123 | flist_dir_len = lastdir_len = strlen(dir); | |
1124 | } | |
2bca43f6 | 1125 | } |
ebed4c3a | 1126 | |
2bca43f6 DD |
1127 | if (one_file_system) |
1128 | set_filesystem(fname); | |
1129 | ||
d01d15e0 | 1130 | send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR); |
2bca43f6 | 1131 | |
808c57c3 | 1132 | if (olddir[0]) { |
649d65ed | 1133 | flist_dir = NULL; |
882e6893 | 1134 | flist_dir_len = 0; |
808c57c3 | 1135 | if (!pop_dir(olddir)) { |
ea42541f WD |
1136 | rprintf(FERROR, "pop_dir %s failed: %s\n", |
1137 | full_fname(dir), strerror(errno)); | |
65417579 | 1138 | exit_cleanup(RERR_FILESELECT); |
649d65ed | 1139 | } |
649d65ed | 1140 | } |
649d65ed | 1141 | } |
dc5ddbcc | 1142 | |
983b1ed3 | 1143 | if (f != -1) { |
ebed4c3a | 1144 | send_file_entry(NULL, f, 0); |
c627d613 | 1145 | |
983b1ed3 WD |
1146 | if (show_filelist_p()) |
1147 | finish_filelist_progress(flist); | |
1148 | } | |
ebed4c3a | 1149 | |
827c37f6 | 1150 | clean_flist(flist, 0, 0); |
ebed4c3a | 1151 | |
785db4ce WD |
1152 | if (f != -1) { |
1153 | /* Now send the uid/gid list. This was introduced in | |
1154 | * protocol version 15 */ | |
649d65ed | 1155 | send_uid_list(f); |
f6c34742 | 1156 | |
983b1ed3 | 1157 | /* send the io_error flag */ |
ebed4c3a | 1158 | write_int(f, lp_ignore_errors(module_id) ? 0 : io_error); |
6ba9279f | 1159 | |
a261989c | 1160 | io_end_buffering(); |
a800434a AT |
1161 | stats.flist_size = stats.total_written - start_write; |
1162 | stats.num_files = flist->count; | |
64c3523a | 1163 | if (write_batch) |
ebed4c3a | 1164 | write_batch_flist_info(flist->count, flist->files); |
d6dead6b AT |
1165 | } |
1166 | ||
17faa41c | 1167 | if (verbose > 2) |
ebed4c3a | 1168 | rprintf(FINFO, "send_file_list done\n"); |
17faa41c | 1169 | |
649d65ed | 1170 | return flist; |
c627d613 AT |
1171 | } |
1172 | ||
1173 | ||
1174 | struct file_list *recv_file_list(int f) | |
1175 | { | |
ebed4c3a | 1176 | struct file_list *flist; |
1ef00d20 | 1177 | unsigned short flags; |
ebed4c3a MP |
1178 | int64 start_read; |
1179 | extern int list_only; | |
c627d613 | 1180 | |
1bbd10fe DD |
1181 | if (show_filelist_p()) |
1182 | start_filelist_progress("receiving file list"); | |
c627d613 | 1183 | |
ebed4c3a | 1184 | start_read = stats.total_read; |
a800434a | 1185 | |
58cadc86 | 1186 | flist = new(struct file_list); |
ebed4c3a MP |
1187 | if (!flist) |
1188 | goto oom; | |
c627d613 | 1189 | |
ebed4c3a MP |
1190 | flist->count = 0; |
1191 | flist->malloced = 1000; | |
58cadc86 | 1192 | flist->files = new_array(struct file_struct *, flist->malloced); |
ebed4c3a MP |
1193 | if (!flist->files) |
1194 | goto oom; | |
c627d613 AT |
1195 | |
1196 | ||
1ef00d20 | 1197 | while ((flags = read_byte(f)) != 0) { |
5d2c5c4c | 1198 | int i = flist->count; |
dbda5fbf | 1199 | |
d9d6bc52 | 1200 | flist_expand(flist); |
c627d613 | 1201 | |
d01d15e0 | 1202 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
75bc8600 | 1203 | flags |= read_byte(f) << 8; |
ebed4c3a | 1204 | receive_file_entry(&flist->files[i], flags, f); |
c627d613 | 1205 | |
ebed4c3a MP |
1206 | if (S_ISREG(flist->files[i]->mode)) |
1207 | stats.total_size += flist->files[i]->length; | |
c627d613 | 1208 | |
ebed4c3a | 1209 | flist->count++; |
c627d613 | 1210 | |
db719fb0 | 1211 | maybe_emit_filelist_progress(flist); |
1bbd10fe | 1212 | |
8018edd3 | 1213 | if (verbose > 2) { |
ebed4c3a MP |
1214 | rprintf(FINFO, "recv_file_name(%s)\n", |
1215 | f_name(flist->files[i])); | |
8018edd3 | 1216 | } |
ebed4c3a | 1217 | } |
5911fee5 | 1218 | receive_file_entry(NULL, 0, 0); /* Signal that we're done. */ |
c627d613 | 1219 | |
ebed4c3a MP |
1220 | if (verbose > 2) |
1221 | rprintf(FINFO, "received %d names\n", flist->count); | |
c627d613 | 1222 | |
b7736c79 | 1223 | if (show_filelist_p()) |
1bbd10fe | 1224 | finish_filelist_progress(flist); |
a06d19e3 | 1225 | |
983b1ed3 WD |
1226 | clean_flist(flist, relative_paths, 1); |
1227 | ||
785db4ce WD |
1228 | if (f != -1) { |
1229 | /* Now send the uid/gid list. This was introduced in | |
1230 | * protocol version 15 */ | |
ebed4c3a | 1231 | recv_uid_list(f, flist); |
f6c34742 | 1232 | |
785db4ce WD |
1233 | if (!read_batch) { |
1234 | /* Recv the io_error flag */ | |
1235 | if (lp_ignore_errors(module_id) || ignore_errors) | |
1236 | read_int(f); | |
1237 | else | |
1238 | io_error |= read_int(f); | |
1239 | } | |
ebed4c3a | 1240 | } |
6ba9279f | 1241 | |
ebed4c3a MP |
1242 | if (list_only) { |
1243 | int i; | |
b7736c79 | 1244 | for (i = 0; i < flist->count; i++) |
ebed4c3a | 1245 | list_file_entry(flist->files[i]); |
ebed4c3a | 1246 | } |
f7632fc6 | 1247 | |
ebed4c3a MP |
1248 | if (verbose > 2) |
1249 | rprintf(FINFO, "recv_file_list done\n"); | |
17faa41c | 1250 | |
ebed4c3a MP |
1251 | stats.flist_size = stats.total_read - start_read; |
1252 | stats.num_files = flist->count; | |
a800434a | 1253 | |
ebed4c3a | 1254 | return flist; |
c627d613 | 1255 | |
ebed4c3a MP |
1256 | oom: |
1257 | out_of_memory("recv_file_list"); | |
1258 | return NULL; /* not reached */ | |
c627d613 AT |
1259 | } |
1260 | ||
1261 | ||
fa45cda1 | 1262 | int file_compare(struct file_struct **file1, struct file_struct **file2) |
c627d613 | 1263 | { |
fa45cda1 S |
1264 | struct file_struct *f1 = *file1; |
1265 | struct file_struct *f2 = *file2; | |
1266 | ||
1267 | if (!f1->basename && !f2->basename) | |
ebed4c3a | 1268 | return 0; |
fa45cda1 | 1269 | if (!f1->basename) |
ebed4c3a | 1270 | return -1; |
fa45cda1 | 1271 | if (!f2->basename) |
ebed4c3a | 1272 | return 1; |
fa45cda1 S |
1273 | if (f1->dirname == f2->dirname) |
1274 | return u_strcmp(f1->basename, f2->basename); | |
1275 | return f_name_cmp(f1, f2); | |
c627d613 AT |
1276 | } |
1277 | ||
1278 | ||
ebed4c3a | 1279 | int flist_find(struct file_list *flist, struct file_struct *f) |
c627d613 | 1280 | { |
ebed4c3a | 1281 | int low = 0, high = flist->count - 1; |
d966ee25 | 1282 | |
ca23c51a WD |
1283 | while (high >= 0 && !flist->files[high]->basename) high--; |
1284 | ||
1285 | if (high < 0) | |
ebed4c3a | 1286 | return -1; |
d966ee25 AT |
1287 | |
1288 | while (low != high) { | |
ebed4c3a | 1289 | int mid = (low + high) / 2; |
b7736c79 | 1290 | int ret = file_compare(&flist->files[flist_up(flist, mid)],&f); |
ebed4c3a MP |
1291 | if (ret == 0) |
1292 | return flist_up(flist, mid); | |
b7736c79 | 1293 | if (ret > 0) |
ebed4c3a | 1294 | high = mid; |
b7736c79 | 1295 | else |
ebed4c3a | 1296 | low = mid + 1; |
d966ee25 AT |
1297 | } |
1298 | ||
ebed4c3a MP |
1299 | if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0) |
1300 | return flist_up(flist, low); | |
d966ee25 | 1301 | return -1; |
c627d613 AT |
1302 | } |
1303 | ||
1304 | ||
3ec4dd97 AT |
1305 | /* |
1306 | * free up one file | |
1307 | */ | |
66203a98 | 1308 | void free_file(struct file_struct *file) |
c627d613 | 1309 | { |
ebed4c3a MP |
1310 | if (!file) |
1311 | return; | |
1312 | if (file->basename) | |
1313 | free(file->basename); | |
728d0922 WD |
1314 | if (!IS_DEVICE(file->mode) && file->u.link) |
1315 | free(file->u.link); /* Handles u.sum too. */ | |
92cc9dd7 WD |
1316 | if (file->link_u.idev) |
1317 | free((char*)file->link_u.idev); /* Handles link_u.links too. */ | |
3d382777 | 1318 | *file = null_file; |
3ec4dd97 | 1319 | } |
c627d613 | 1320 | |
c627d613 | 1321 | |
3d382777 AT |
1322 | /* |
1323 | * allocate a new file list | |
1324 | */ | |
1e34e4b7 | 1325 | struct file_list *flist_new(void) |
3d382777 AT |
1326 | { |
1327 | struct file_list *flist; | |
1328 | ||
58cadc86 | 1329 | flist = new(struct file_list); |
ebed4c3a MP |
1330 | if (!flist) |
1331 | out_of_memory("send_file_list"); | |
3d382777 | 1332 | |
ebed4c3a | 1333 | flist->count = 0; |
d9d6bc52 MP |
1334 | flist->malloced = 0; |
1335 | flist->files = NULL; | |
1336 | ||
3d382777 AT |
1337 | #if ARENA_SIZE > 0 |
1338 | flist->string_area = string_area_new(0); | |
1339 | #else | |
5c66303a | 1340 | flist->string_area = NULL; |
3d382777 AT |
1341 | #endif |
1342 | return flist; | |
1343 | } | |
ebed4c3a | 1344 | |
3ec4dd97 AT |
1345 | /* |
1346 | * free up all elements in a flist | |
1347 | */ | |
1348 | void flist_free(struct file_list *flist) | |
1349 | { | |
1350 | int i; | |
ebed4c3a | 1351 | for (i = 1; i < flist->count; i++) { |
3d382777 AT |
1352 | if (!flist->string_area) |
1353 | free_file(flist->files[i]); | |
649d65ed | 1354 | free(flist->files[i]); |
ebed4c3a | 1355 | } |
d9d6bc52 MP |
1356 | /* FIXME: I don't think we generally need to blank the flist |
1357 | * since it's about to be freed. This will just cause more | |
1358 | * memory traffic. If you want a freed-memory debugger, you | |
1359 | * know where to get it. */ | |
ebed4c3a MP |
1360 | memset((char *) flist->files, 0, |
1361 | sizeof(flist->files[0]) * flist->count); | |
3ec4dd97 | 1362 | free(flist->files); |
3d382777 AT |
1363 | if (flist->string_area) |
1364 | string_area_free(flist->string_area); | |
ebed4c3a | 1365 | memset((char *) flist, 0, sizeof(*flist)); |
3ec4dd97 | 1366 | free(flist); |
c627d613 AT |
1367 | } |
1368 | ||
1369 | ||
1370 | /* | |
1371 | * This routine ensures we don't have any duplicate names in our file list. | |
dbda5fbf | 1372 | * duplicate names can cause corruption because of the pipelining |
c627d613 | 1373 | */ |
827c37f6 | 1374 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups) |
c627d613 | 1375 | { |
6931c138 | 1376 | int i, prev_i = 0; |
c627d613 | 1377 | |
ebed4c3a | 1378 | if (!flist || flist->count == 0) |
3ec4dd97 | 1379 | return; |
3ec4dd97 | 1380 | |
ebed4c3a MP |
1381 | qsort(flist->files, flist->count, |
1382 | sizeof(flist->files[0]), (int (*)()) file_compare); | |
1383 | ||
827c37f6 | 1384 | for (i = no_dups? 0 : flist->count; i < flist->count; i++) { |
b91b50c0 | 1385 | if (flist->files[i]->basename) { |
6931c138 | 1386 | prev_i = i; |
b91b50c0 WD |
1387 | break; |
1388 | } | |
1389 | } | |
1390 | while (++i < flist->count) { | |
1391 | if (!flist->files[i]->basename) | |
1392 | continue; | |
8018edd3 | 1393 | if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) { |
b91b50c0 | 1394 | if (verbose > 1 && !am_server) { |
ebed4c3a MP |
1395 | rprintf(FINFO, |
1396 | "removing duplicate name %s from file list %d\n", | |
8018edd3 | 1397 | f_name(flist->files[i]), i); |
b91b50c0 | 1398 | } |
6931c138 WD |
1399 | /* Make sure that if we unduplicate '.', that we don't |
1400 | * lose track of a user-specified starting point (or | |
1401 | * else deletions will mysteriously fail with -R). */ | |
d01d15e0 WD |
1402 | if (flist->files[i]->flags & FLAG_TOP_DIR) |
1403 | flist->files[prev_i]->flags |= FLAG_TOP_DIR; | |
b91b50c0 WD |
1404 | /* it's not great that the flist knows the semantics of |
1405 | * the file memory usage, but i'd rather not add a flag | |
1406 | * byte to that struct. | |
1407 | * XXX can i use a bit in the flags field? */ | |
3d382777 AT |
1408 | if (flist->string_area) |
1409 | flist->files[i][0] = null_file; | |
1410 | else | |
1411 | free_file(flist->files[i]); | |
728d0922 | 1412 | } else |
6931c138 | 1413 | prev_i = i; |
3ec4dd97 | 1414 | } |
0199b05f AT |
1415 | |
1416 | if (strip_root) { | |
1417 | /* we need to strip off the root directory in the case | |
1418 | of relative paths, but this must be done _after_ | |
1419 | the sorting phase */ | |
ebed4c3a | 1420 | for (i = 0; i < flist->count; i++) { |
0199b05f AT |
1421 | if (flist->files[i]->dirname && |
1422 | flist->files[i]->dirname[0] == '/') { | |
1423 | memmove(&flist->files[i]->dirname[0], | |
1424 | &flist->files[i]->dirname[1], | |
1425 | strlen(flist->files[i]->dirname)); | |
1426 | } | |
ebed4c3a MP |
1427 | |
1428 | if (flist->files[i]->dirname && | |
0199b05f AT |
1429 | !flist->files[i]->dirname[0]) { |
1430 | flist->files[i]->dirname = NULL; | |
1431 | } | |
1432 | } | |
1433 | } | |
1434 | ||
ebed4c3a MP |
1435 | if (verbose <= 3) |
1436 | return; | |
0199b05f | 1437 | |
ebed4c3a | 1438 | for (i = 0; i < flist->count; i++) { |
1ef00d20 WD |
1439 | rprintf(FINFO, "[%ld] i=%d %s %s mode=0%o len=%.0f\n", |
1440 | (long) getpid(), i, | |
74e708d8 AT |
1441 | NS(flist->files[i]->dirname), |
1442 | NS(flist->files[i]->basename), | |
08a740ff | 1443 | (int) flist->files[i]->mode, |
ebed4c3a | 1444 | (double) flist->files[i]->length); |
0199b05f | 1445 | } |
3ec4dd97 AT |
1446 | } |
1447 | ||
1448 | ||
8018edd3 WD |
1449 | enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE }; |
1450 | ||
1451 | /* Compare the names of two file_struct entities, just like strcmp() | |
1452 | * would do if it were operating on the joined strings. We assume | |
1453 | * that there are no 0-length strings. | |
3ec4dd97 | 1454 | */ |
8018edd3 | 1455 | int f_name_cmp(struct file_struct *f1, struct file_struct *f2) |
3ec4dd97 | 1456 | { |
8018edd3 WD |
1457 | int dif; |
1458 | const uchar *c1, *c2; | |
1ef00d20 | 1459 | enum fnc_state state1, state2; |
8018edd3 WD |
1460 | |
1461 | if (!f1 || !f1->basename) { | |
1462 | if (!f2 || !f2->basename) | |
1463 | return 0; | |
1464 | return -1; | |
1465 | } | |
1466 | if (!f2 || !f2->basename) | |
1467 | return 1; | |
1468 | ||
e90b8ace | 1469 | if (!(c1 = (uchar*)f1->dirname)) { |
8018edd3 | 1470 | state1 = fnc_BASE; |
e90b8ace | 1471 | c1 = (uchar*)f1->basename; |
728d0922 | 1472 | } else |
1ef00d20 | 1473 | state1 = fnc_DIR; |
e90b8ace | 1474 | if (!(c2 = (uchar*)f2->dirname)) { |
8018edd3 | 1475 | state2 = fnc_BASE; |
e90b8ace | 1476 | c2 = (uchar*)f2->basename; |
728d0922 | 1477 | } else |
1ef00d20 | 1478 | state2 = fnc_DIR; |
8018edd3 WD |
1479 | |
1480 | while (1) { | |
1481 | if ((dif = (int)*c1 - (int)*c2) != 0) | |
1482 | break; | |
1483 | if (!*++c1) { | |
1484 | switch (state1) { | |
1485 | case fnc_DIR: | |
1486 | state1 = fnc_SLASH; | |
e90b8ace | 1487 | c1 = (uchar*)"/"; |
8018edd3 WD |
1488 | break; |
1489 | case fnc_SLASH: | |
1490 | state1 = fnc_BASE; | |
e90b8ace | 1491 | c1 = (uchar*)f1->basename; |
8018edd3 WD |
1492 | break; |
1493 | case fnc_BASE: | |
1494 | break; | |
1495 | } | |
1496 | } | |
1497 | if (!*++c2) { | |
1498 | switch (state2) { | |
1499 | case fnc_DIR: | |
1500 | state2 = fnc_SLASH; | |
e90b8ace | 1501 | c2 = (uchar*)"/"; |
8018edd3 WD |
1502 | break; |
1503 | case fnc_SLASH: | |
1504 | state2 = fnc_BASE; | |
e90b8ace | 1505 | c2 = (uchar*)f2->basename; |
8018edd3 WD |
1506 | break; |
1507 | case fnc_BASE: | |
1508 | if (!*c1) | |
1509 | return 0; | |
1510 | break; | |
1511 | } | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | return dif; | |
1516 | } | |
1517 | ||
3ec4dd97 | 1518 | |
8018edd3 | 1519 | /* Return a copy of the full filename of a flist entry, using the indicated |
882e6893 WD |
1520 | * buffer. No size-checking is done because we checked the size when creating |
1521 | * the file_struct entry. | |
8018edd3 | 1522 | */ |
882e6893 | 1523 | char *f_name_to(struct file_struct *f, char *fbuf) |
8018edd3 | 1524 | { |
ebed4c3a MP |
1525 | if (!f || !f->basename) |
1526 | return NULL; | |
3ec4dd97 | 1527 | |
3ec4dd97 | 1528 | if (f->dirname) { |
882e6893 WD |
1529 | int len = strlen(f->dirname); |
1530 | memcpy(fbuf, f->dirname, len); | |
1531 | fbuf[len] = '/'; | |
1532 | strcpy(fbuf + len + 1, f->basename); | |
8018edd3 | 1533 | } else |
882e6893 | 1534 | strcpy(fbuf, f->basename); |
b7736c79 | 1535 | return fbuf; |
8018edd3 | 1536 | } |
e03dfae5 | 1537 | |
3ec4dd97 | 1538 | |
8018edd3 WD |
1539 | /* Like f_name_to(), but we rotate through 5 static buffers of our own. |
1540 | */ | |
1541 | char *f_name(struct file_struct *f) | |
1542 | { | |
1543 | static char names[5][MAXPATHLEN]; | |
1544 | static unsigned int n; | |
1545 | ||
1546 | n = (n + 1) % (sizeof names / sizeof names[0]); | |
1547 | ||
882e6893 | 1548 | return f_name_to(f, names[n]); |
c627d613 | 1549 | } |