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) { |
ea847c62 WD |
345 | rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n", |
346 | who_am_i(), | |
58cadc86 WD |
347 | (double)sizeof(flist->files[0]) |
348 | * flist->malloced, | |
2e7d1994 MP |
349 | (new_ptr == flist->files) ? " not" : ""); |
350 | } | |
dbda5fbf | 351 | |
2e7d1994 MP |
352 | flist->files = (struct file_struct **) new_ptr; |
353 | ||
d9d6bc52 MP |
354 | if (!flist->files) |
355 | out_of_memory("flist_expand"); | |
356 | } | |
357 | } | |
358 | ||
7b1a0c19 | 359 | void send_file_entry(struct file_struct *file, int f, unsigned short base_flags) |
c627d613 | 360 | { |
1ef00d20 | 361 | unsigned short flags; |
5911fee5 WD |
362 | static time_t modtime; |
363 | static mode_t mode; | |
364 | static DEV64_T rdev; /* just high bytes in p28 onward */ | |
365 | static DEV64_T dev; | |
366 | static uid_t uid; | |
367 | static gid_t gid; | |
368 | static char lastname[MAXPATHLEN]; | |
b7736c79 | 369 | char *fname, fbuf[MAXPATHLEN]; |
ebed4c3a | 370 | int l1, l2; |
72914a60 | 371 | |
ebed4c3a MP |
372 | if (f == -1) |
373 | return; | |
72914a60 AT |
374 | |
375 | if (!file) { | |
ebed4c3a | 376 | write_byte(f, 0); |
5911fee5 WD |
377 | modtime = 0, mode = 0; |
378 | rdev = 0, dev = 0; | |
379 | uid = 0, gid = 0; | |
380 | *lastname = '\0'; | |
72914a60 AT |
381 | return; |
382 | } | |
383 | ||
eca2adb4 MP |
384 | io_write_phase = "send_file_entry"; |
385 | ||
882e6893 | 386 | fname = f_name_to(file, fbuf); |
72914a60 AT |
387 | |
388 | flags = base_flags; | |
389 | ||
30f337c9 | 390 | if (file->mode == mode) |
d01d15e0 | 391 | flags |= XMIT_SAME_MODE; |
1ef00d20 | 392 | else |
30f337c9 | 393 | mode = file->mode; |
75bc8600 WD |
394 | if (preserve_devices) { |
395 | if (protocol_version < 28) { | |
728d0922 WD |
396 | if (IS_DEVICE(mode)) { |
397 | if (file->u.rdev == rdev) { | |
d01d15e0 WD |
398 | /* Set both flags to simplify the test |
399 | * when writing the data. */ | |
400 | flags |= XMIT_SAME_RDEV_pre28 | |
401 | | XMIT_SAME_HIGH_RDEV; | |
728d0922 WD |
402 | } else |
403 | rdev = file->u.rdev; | |
404 | } else | |
405 | rdev = 0; | |
406 | } else if (IS_DEVICE(mode)) { | |
407 | if ((file->u.rdev & ~0xFF) == rdev) | |
d01d15e0 | 408 | flags |= XMIT_SAME_HIGH_RDEV; |
30f337c9 | 409 | else |
728d0922 | 410 | rdev = file->u.rdev & ~0xFF; |
75bc8600 WD |
411 | } |
412 | } | |
30f337c9 | 413 | if (file->uid == uid) |
d01d15e0 | 414 | flags |= XMIT_SAME_UID; |
1ef00d20 | 415 | else |
30f337c9 WD |
416 | uid = file->uid; |
417 | if (file->gid == gid) | |
d01d15e0 | 418 | flags |= XMIT_SAME_GID; |
1ef00d20 | 419 | else |
30f337c9 WD |
420 | gid = file->gid; |
421 | if (file->modtime == modtime) | |
d01d15e0 | 422 | flags |= XMIT_SAME_TIME; |
1ef00d20 | 423 | else |
30f337c9 | 424 | modtime = file->modtime; |
92cc9dd7 WD |
425 | if (file->link_u.idev) { |
426 | if (file->F_DEV == dev) { | |
c4b4df4f | 427 | if (protocol_version >= 28) |
d01d15e0 | 428 | flags |= XMIT_SAME_DEV; |
728d0922 | 429 | } else |
92cc9dd7 | 430 | dev = file->F_DEV; |
d01d15e0 | 431 | flags |= XMIT_HAS_IDEV_DATA; |
c4b4df4f | 432 | } |
ebed4c3a MP |
433 | |
434 | for (l1 = 0; | |
435 | lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255); | |
dbda5fbf | 436 | l1++) {} |
eddd5d12 | 437 | l2 = strlen(fname+l1); |
72914a60 | 438 | |
ebed4c3a | 439 | if (l1 > 0) |
d01d15e0 | 440 | flags |= XMIT_SAME_NAME; |
ebed4c3a | 441 | if (l2 > 255) |
d01d15e0 | 442 | flags |= XMIT_LONG_NAME; |
72914a60 | 443 | |
0a982011 | 444 | /* We must make sure we don't send a zero flag-byte or |
75bc8600 | 445 | * the other end will terminate the flist transfer. */ |
0a982011 | 446 | if (!(flags & 0xFF) && !S_ISDIR(mode)) |
9e4c32ea | 447 | flags |= XMIT_TOP_DIR; /* NOTE: no meaning for non-dir */ |
75bc8600 | 448 | if (protocol_version >= 28) { |
0a982011 | 449 | if ((flags & 0xFF00) || !(flags & 0xFF)) { |
d01d15e0 | 450 | flags |= XMIT_EXTENDED_FLAGS; |
75bc8600 WD |
451 | write_byte(f, flags); |
452 | write_byte(f, flags >> 8); | |
453 | } else | |
454 | write_byte(f, flags); | |
455 | } else { | |
0a982011 | 456 | if (!(flags & 0xFF)) |
d01d15e0 | 457 | flags |= XMIT_LONG_NAME; |
75bc8600 WD |
458 | write_byte(f, flags); |
459 | } | |
d01d15e0 | 460 | if (flags & XMIT_SAME_NAME) |
ebed4c3a | 461 | write_byte(f, l1); |
d01d15e0 | 462 | if (flags & XMIT_LONG_NAME) |
ebed4c3a | 463 | write_int(f, l2); |
72914a60 | 464 | else |
ebed4c3a MP |
465 | write_byte(f, l2); |
466 | write_buf(f, fname + l1, l2); | |
72914a60 | 467 | |
ebed4c3a | 468 | write_longint(f, file->length); |
d01d15e0 | 469 | if (!(flags & XMIT_SAME_TIME)) |
30f337c9 | 470 | write_int(f, modtime); |
d01d15e0 | 471 | if (!(flags & XMIT_SAME_MODE)) |
30f337c9 | 472 | write_int(f, to_wire_mode(mode)); |
d01d15e0 | 473 | if (preserve_uid && !(flags & XMIT_SAME_UID)) { |
30f337c9 WD |
474 | add_uid(uid); |
475 | write_int(f, uid); | |
72914a60 | 476 | } |
d01d15e0 | 477 | if (preserve_gid && !(flags & XMIT_SAME_GID)) { |
30f337c9 WD |
478 | add_gid(gid); |
479 | write_int(f, gid); | |
72914a60 | 480 | } |
30f337c9 | 481 | if (preserve_devices && IS_DEVICE(mode)) { |
d01d15e0 WD |
482 | /* If XMIT_SAME_HIGH_RDEV is off, XMIT_SAME_RDEV_pre28 is |
483 | * also off. Also, avoid using "rdev" because it may be | |
484 | * incomplete. */ | |
485 | if (!(flags & XMIT_SAME_HIGH_RDEV)) | |
728d0922 | 486 | write_int(f, file->u.rdev); |
75bc8600 | 487 | else if (protocol_version >= 28) |
728d0922 | 488 | write_byte(f, file->u.rdev); |
75bc8600 | 489 | } |
c627d613 AT |
490 | |
491 | #if SUPPORT_LINKS | |
30f337c9 | 492 | if (preserve_links && S_ISLNK(mode)) { |
306ffb8c WD |
493 | int len = strlen(file->u.link); |
494 | write_int(f, len); | |
495 | write_buf(f, file->u.link, len); | |
72914a60 | 496 | } |
c627d613 AT |
497 | #endif |
498 | ||
dc5ddbcc | 499 | #if SUPPORT_HARD_LINKS |
d01d15e0 | 500 | if (flags & XMIT_HAS_IDEV_DATA) { |
d04e9c51 | 501 | if (protocol_version < 26) { |
736a6a29 | 502 | /* 32-bit dev_t and ino_t */ |
30f337c9 | 503 | write_int(f, dev); |
92cc9dd7 | 504 | write_int(f, file->F_INODE); |
736a6a29 MP |
505 | } else { |
506 | /* 64-bit dev_t and ino_t */ | |
d01d15e0 | 507 | if (!(flags & XMIT_SAME_DEV)) |
30f337c9 | 508 | write_longint(f, dev); |
92cc9dd7 | 509 | write_longint(f, file->F_INODE); |
736a6a29 | 510 | } |
72914a60 | 511 | } |
dc5ddbcc AT |
512 | #endif |
513 | ||
728d0922 WD |
514 | if (always_checksum) { |
515 | char *sum; | |
516 | if (S_ISREG(mode)) | |
517 | sum = file->u.sum; | |
518 | else if (protocol_version < 28) { | |
519 | /* Prior to 28, we sent a useless set of nulls. */ | |
520 | sum = empty_sum; | |
521 | } else | |
522 | sum = NULL; | |
523 | if (sum) { | |
524 | write_buf(f, sum, protocol_version < 21? 2 | |
525 | : MD4_SUM_LENGTH); | |
526 | } | |
ebed4c3a | 527 | } |
182dca5c | 528 | |
ebed4c3a | 529 | strlcpy(lastname, fname, MAXPATHLEN); |
eca2adb4 MP |
530 | |
531 | io_write_phase = "unknown"; | |
182dca5c AT |
532 | } |
533 | ||
534 | ||
535 | ||
7b1a0c19 | 536 | void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f) |
182dca5c | 537 | { |
5911fee5 WD |
538 | static time_t modtime; |
539 | static mode_t mode; | |
540 | static DEV64_T rdev; /* just high bytes in p28 onward */ | |
541 | static DEV64_T dev; | |
542 | static uid_t uid; | |
543 | static gid_t gid; | |
544 | static char lastname[MAXPATHLEN]; | |
72914a60 | 545 | char thisname[MAXPATHLEN]; |
ebed4c3a | 546 | unsigned int l1 = 0, l2 = 0; |
72914a60 AT |
547 | char *p; |
548 | struct file_struct *file; | |
549 | ||
5911fee5 WD |
550 | if (!fptr) { |
551 | modtime = 0, mode = 0; | |
552 | rdev = 0, dev = 0; | |
553 | uid = 0, gid = 0; | |
554 | *lastname = '\0'; | |
555 | return; | |
556 | } | |
557 | ||
d01d15e0 | 558 | if (flags & XMIT_SAME_NAME) |
72914a60 | 559 | l1 = read_byte(f); |
ebed4c3a | 560 | |
d01d15e0 | 561 | if (flags & XMIT_LONG_NAME) |
72914a60 AT |
562 | l2 = read_int(f); |
563 | else | |
564 | l2 = read_byte(f); | |
565 | ||
58cadc86 | 566 | file = new(struct file_struct); |
ebed4c3a MP |
567 | if (!file) |
568 | out_of_memory("receive_file_entry"); | |
569 | memset((char *) file, 0, sizeof(*file)); | |
72914a60 AT |
570 | (*fptr) = file; |
571 | ||
ebed4c3a MP |
572 | if (l2 >= MAXPATHLEN - l1) { |
573 | rprintf(FERROR, | |
574 | "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n", | |
d0fd26aa AT |
575 | flags, l1, l2, lastname); |
576 | overflow("receive_file_entry"); | |
577 | } | |
72914a60 | 578 | |
ebed4c3a MP |
579 | strlcpy(thisname, lastname, l1 + 1); |
580 | read_sbuf(f, &thisname[l1], l2); | |
581 | thisname[l1 + l2] = 0; | |
72914a60 | 582 | |
ebed4c3a MP |
583 | strlcpy(lastname, thisname, MAXPATHLEN); |
584 | lastname[MAXPATHLEN - 1] = 0; | |
72914a60 AT |
585 | |
586 | clean_fname(thisname); | |
587 | ||
cb13abfe DD |
588 | if (sanitize_paths) { |
589 | sanitize_path(thisname, NULL); | |
590 | } | |
591 | ||
ebed4c3a | 592 | if ((p = strrchr(thisname, '/'))) { |
72914a60 AT |
593 | static char *lastdir; |
594 | *p = 0; | |
b7736c79 | 595 | if (lastdir && strcmp(thisname, lastdir) == 0) |
72914a60 | 596 | file->dirname = lastdir; |
b7736c79 | 597 | else { |
72914a60 AT |
598 | file->dirname = strdup(thisname); |
599 | lastdir = file->dirname; | |
600 | } | |
ebed4c3a | 601 | file->basename = strdup(p + 1); |
72914a60 AT |
602 | } else { |
603 | file->dirname = NULL; | |
604 | file->basename = strdup(thisname); | |
605 | } | |
606 | ||
ebed4c3a MP |
607 | if (!file->basename) |
608 | out_of_memory("receive_file_entry 1"); | |
72914a60 | 609 | |
d01d15e0 | 610 | file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0; |
72914a60 | 611 | file->length = read_longint(f); |
d01d15e0 | 612 | if (!(flags & XMIT_SAME_TIME)) |
30f337c9 WD |
613 | modtime = (time_t)read_int(f); |
614 | file->modtime = modtime; | |
d01d15e0 | 615 | if (!(flags & XMIT_SAME_MODE)) |
30f337c9 WD |
616 | mode = from_wire_mode(read_int(f)); |
617 | file->mode = mode; | |
1ef00d20 WD |
618 | |
619 | if (preserve_uid) { | |
d01d15e0 | 620 | if (!(flags & XMIT_SAME_UID)) |
30f337c9 WD |
621 | uid = (uid_t)read_int(f); |
622 | file->uid = uid; | |
1ef00d20 WD |
623 | } |
624 | if (preserve_gid) { | |
d01d15e0 | 625 | if (!(flags & XMIT_SAME_GID)) |
30f337c9 WD |
626 | gid = (gid_t)read_int(f); |
627 | file->gid = gid; | |
1ef00d20 WD |
628 | } |
629 | if (preserve_devices) { | |
75bc8600 | 630 | if (protocol_version < 28) { |
30f337c9 | 631 | if (IS_DEVICE(mode)) { |
d01d15e0 | 632 | if (!(flags & XMIT_SAME_RDEV_pre28)) |
30f337c9 | 633 | rdev = (DEV64_T)read_int(f); |
728d0922 | 634 | file->u.rdev = rdev; |
75bc8600 | 635 | } else |
30f337c9 WD |
636 | rdev = 0; |
637 | } else if (IS_DEVICE(mode)) { | |
d01d15e0 | 638 | if (!(flags & XMIT_SAME_HIGH_RDEV)) { |
728d0922 WD |
639 | file->u.rdev = (DEV64_T)read_int(f); |
640 | rdev = file->u.rdev & ~0xFF; | |
30f337c9 | 641 | } else |
728d0922 | 642 | file->u.rdev = rdev | (DEV64_T)read_byte(f); |
1ef00d20 | 643 | } |
b7736c79 | 644 | } |
72914a60 | 645 | |
30f337c9 | 646 | if (preserve_links && S_ISLNK(mode)) { |
306ffb8c WD |
647 | int len = read_int(f); |
648 | if (len < 0 || len >= MAXPATHLEN) { | |
649 | rprintf(FERROR, "overflow: len=%d\n", len); | |
9dd891bb MP |
650 | overflow("receive_file_entry"); |
651 | } | |
306ffb8c | 652 | if (!(file->u.link = new_array(char, len + 1))) |
ebed4c3a | 653 | out_of_memory("receive_file_entry 2"); |
306ffb8c | 654 | read_sbuf(f, file->u.link, len); |
b7736c79 | 655 | if (sanitize_paths) |
728d0922 | 656 | sanitize_path(file->u.link, file->dirname); |
72914a60 | 657 | } |
dc5ddbcc | 658 | #if SUPPORT_HARD_LINKS |
92cc9dd7 | 659 | if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode)) |
d01d15e0 WD |
660 | flags |= XMIT_HAS_IDEV_DATA; |
661 | if (flags & XMIT_HAS_IDEV_DATA) { | |
92cc9dd7 WD |
662 | if (!(file->link_u.idev = new(struct idev))) |
663 | out_of_memory("file inode data"); | |
d04e9c51 | 664 | if (protocol_version < 26) { |
30f337c9 | 665 | dev = read_int(f); |
92cc9dd7 | 666 | file->F_INODE = read_int(f); |
736a6a29 | 667 | } else { |
d01d15e0 | 668 | if (!(flags & XMIT_SAME_DEV)) |
30f337c9 | 669 | dev = read_longint(f); |
92cc9dd7 | 670 | file->F_INODE = read_longint(f); |
736a6a29 | 671 | } |
92cc9dd7 | 672 | file->F_DEV = dev; |
72914a60 | 673 | } |
dc5ddbcc | 674 | #endif |
ebed4c3a | 675 | |
72914a60 | 676 | if (always_checksum) { |
7c4f063b | 677 | char *sum; |
fea4db62 | 678 | if (S_ISREG(mode)) { |
728d0922 | 679 | sum = file->u.sum = new_array(char, MD4_SUM_LENGTH); |
fea4db62 WD |
680 | if (!sum) |
681 | out_of_memory("md4 sum"); | |
682 | } else if (protocol_version < 28) { | |
683 | /* Prior to 28, we get a useless set of nulls. */ | |
7c4f063b | 684 | sum = empty_sum; |
fea4db62 WD |
685 | } else |
686 | sum = NULL; | |
687 | if (sum) { | |
688 | read_buf(f, sum, protocol_version < 21? 2 | |
689 | : MD4_SUM_LENGTH); | |
690 | } | |
72914a60 | 691 | } |
ebed4c3a | 692 | |
72914a60 AT |
693 | if (!preserve_perms) { |
694 | extern int orig_umask; | |
695 | /* set an appropriate set of permissions based on original | |
fea4db62 | 696 | * permissions and umask. This emulates what GNU cp does */ |
72914a60 AT |
697 | file->mode &= ~orig_umask; |
698 | } | |
c627d613 AT |
699 | } |
700 | ||
701 | ||
3d382777 | 702 | #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p)) |
87a819ed MP |
703 | /* IRIX cc cares that the operands to the ternary have the same type. */ |
704 | #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i)) | |
3d382777 | 705 | |
db719fb0 MP |
706 | /** |
707 | * Create a file_struct for a named file by reading its stat() | |
708 | * information and performing extensive checks against global | |
709 | * options. | |
710 | * | |
711 | * @return the new file, or NULL if there was an error or this file | |
712 | * should be excluded. | |
713 | * | |
714 | * @todo There is a small optimization opportunity here to avoid | |
715 | * stat()ing the file in some circumstances, which has a certain cost. | |
716 | * We are called immediately after doing readdir(), and so we may | |
717 | * already know the d_type of the file. We could for example avoid | |
718 | * statting directories if we're not recursing, but this is not a very | |
719 | * important case. Some systems may not have d_type. | |
720 | **/ | |
429f9828 WD |
721 | struct file_struct *make_file(char *fname, struct string_area **ap, |
722 | int exclude_level) | |
c627d613 | 723 | { |
3ec4dd97 | 724 | struct file_struct *file; |
bcacc18b | 725 | STRUCT_STAT st; |
3ec4dd97 AT |
726 | char sum[SUM_LENGTH]; |
727 | char *p; | |
1923b1fc | 728 | char thisname[MAXPATHLEN]; |
e0870f1d | 729 | char linkname[MAXPATHLEN]; |
4844449a | 730 | unsigned short flags = 0; |
3ec4dd97 | 731 | |
1923b1fc WD |
732 | if (strlcpy(thisname, fname, sizeof thisname) |
733 | >= sizeof thisname - flist_dir_len) { | |
882e6893 WD |
734 | rprintf(FINFO, "skipping overly long name: %s\n", fname); |
735 | return NULL; | |
736 | } | |
1923b1fc | 737 | clean_fname(thisname); |
b7736c79 | 738 | if (sanitize_paths) |
1923b1fc | 739 | sanitize_path(thisname, NULL); |
3ec4dd97 | 740 | |
ebed4c3a | 741 | memset(sum, 0, SUM_LENGTH); |
3ec4dd97 | 742 | |
e0870f1d | 743 | if (readlink_stat(thisname, &st, linkname) != 0) { |
76e26e10 | 744 | int save_errno = errno; |
429f9828 | 745 | if (errno == ENOENT && exclude_level != NO_EXCLUDES) { |
dbda5fbf | 746 | /* either symlink pointing nowhere or file that |
1b85e3f1 DD |
747 | * was removed during rsync run; see if excluded |
748 | * before reporting an error */ | |
1923b1fc | 749 | if (check_exclude_file(thisname, 0, exclude_level)) { |
76e26e10 DD |
750 | /* file is excluded anyway, ignore silently */ |
751 | return NULL; | |
752 | } | |
753 | } | |
06c28400 | 754 | io_error |= IOERR_GENERAL; |
ea42541f | 755 | rprintf(FERROR, "readlink %s failed: %s\n", |
1923b1fc | 756 | full_fname(thisname), strerror(save_errno)); |
3ec4dd97 AT |
757 | return NULL; |
758 | } | |
c627d613 | 759 | |
429f9828 WD |
760 | /* backup.c calls us with exclude_level set to NO_EXCLUDES. */ |
761 | if (exclude_level == NO_EXCLUDES) | |
ebed4c3a | 762 | goto skip_excludes; |
ac1a0994 | 763 | |
24d0fcde | 764 | if (S_ISDIR(st.st_mode) && !recurse && !files_from) { |
1923b1fc | 765 | rprintf(FINFO, "skipping directory %s\n", thisname); |
3ec4dd97 AT |
766 | return NULL; |
767 | } | |
ebed4c3a | 768 | |
0c5f37d9 | 769 | if (one_file_system && st.st_dev != filesystem_dev) { |
4844449a WD |
770 | /* We allow a directory though to preserve the mount point. |
771 | * However, flag it so that we don't recurse. */ | |
772 | if (!S_ISDIR(st.st_mode)) | |
0c5f37d9 | 773 | return NULL; |
4844449a | 774 | flags |= FLAG_MOUNT_POINT; |
0c5f37d9 | 775 | } |
ebed4c3a | 776 | |
1923b1fc | 777 | if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level)) |
76e26e10 DD |
778 | return NULL; |
779 | ||
1923b1fc | 780 | if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0) |
ac1a0994 AT |
781 | return NULL; |
782 | ||
ebed4c3a | 783 | skip_excludes: |
ac1a0994 | 784 | |
ea847c62 WD |
785 | if (verbose > 2) { |
786 | rprintf(FINFO, "[%s] make_file(%s,*,%d)\n", | |
1923b1fc | 787 | who_am_i(), thisname, exclude_level); |
ea847c62 | 788 | } |
ebed4c3a | 789 | |
58cadc86 | 790 | file = new(struct file_struct); |
ebed4c3a MP |
791 | if (!file) |
792 | out_of_memory("make_file"); | |
793 | memset((char *) file, 0, sizeof(*file)); | |
4844449a | 794 | file->flags = flags; |
3ec4dd97 | 795 | |
1923b1fc | 796 | if ((p = strrchr(thisname, '/'))) { |
3ec4dd97 AT |
797 | static char *lastdir; |
798 | *p = 0; | |
1923b1fc | 799 | if (lastdir && strcmp(thisname, lastdir) == 0) |
3ec4dd97 | 800 | file->dirname = lastdir; |
b7736c79 | 801 | else { |
1923b1fc | 802 | file->dirname = strdup(thisname); |
3ec4dd97 AT |
803 | lastdir = file->dirname; |
804 | } | |
ebed4c3a | 805 | file->basename = STRDUP(ap, p + 1); |
3ec4dd97 AT |
806 | *p = '/'; |
807 | } else { | |
808 | file->dirname = NULL; | |
1923b1fc | 809 | file->basename = STRDUP(ap, thisname); |
3ec4dd97 | 810 | } |
c627d613 | 811 | |
3ec4dd97 AT |
812 | file->modtime = st.st_mtime; |
813 | file->length = st.st_size; | |
814 | file->mode = st.st_mode; | |
815 | file->uid = st.st_uid; | |
816 | file->gid = st.st_gid; | |
c4b4df4f | 817 | if (preserve_hard_links) { |
92cc9dd7 | 818 | if (protocol_version < 28 ? S_ISREG(st.st_mode) |
c4b4df4f | 819 | : !S_ISDIR(st.st_mode) && st.st_nlink > 1) { |
92cc9dd7 WD |
820 | if (!(file->link_u.idev = new(struct idev))) |
821 | out_of_memory("file inode data"); | |
822 | file->F_DEV = st.st_dev; | |
823 | file->F_INODE = st.st_ino; | |
c4b4df4f WD |
824 | } |
825 | } | |
5f78da20 | 826 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
728d0922 WD |
827 | if (IS_DEVICE(st.st_mode)) |
828 | file->u.rdev = st.st_rdev; | |
c627d613 AT |
829 | #endif |
830 | ||
831 | #if SUPPORT_LINKS | |
b7736c79 | 832 | if (S_ISLNK(st.st_mode)) |
e0870f1d | 833 | file->u.link = STRDUP(ap, linkname); |
c627d613 AT |
834 | #endif |
835 | ||
fea4db62 | 836 | if (always_checksum && S_ISREG(st.st_mode)) { |
728d0922 | 837 | if (!(file->u.sum = (char*)MALLOC(ap, MD4_SUM_LENGTH))) |
ebed4c3a | 838 | out_of_memory("md4 sum"); |
1923b1fc | 839 | file_checksum(thisname, file->u.sum, st.st_size); |
ebed4c3a | 840 | } |
c627d613 | 841 | |
882e6893 | 842 | file->basedir = flist_dir; |
c627d613 | 843 | |
3ec4dd97 | 844 | if (!S_ISDIR(st.st_mode)) |
a800434a | 845 | stats.total_size += st.st_size; |
c627d613 | 846 | |
3ec4dd97 | 847 | return file; |
c627d613 AT |
848 | } |
849 | ||
850 | ||
ebed4c3a | 851 | void send_file_name(int f, struct file_list *flist, char *fname, |
1ef00d20 | 852 | int recursive, unsigned short base_flags) |
c627d613 | 853 | { |
ebed4c3a | 854 | struct file_struct *file; |
b7736c79 | 855 | char fbuf[MAXPATHLEN]; |
429f9828 | 856 | extern int delete_excluded; |
ebed4c3a | 857 | |
429f9828 WD |
858 | /* f is set to -1 when calculating deletion file list */ |
859 | file = make_file(fname, &flist->string_area, | |
860 | f == -1 && delete_excluded? SERVER_EXCLUDES | |
861 | : ALL_EXCLUDES); | |
ebed4c3a MP |
862 | |
863 | if (!file) | |
864 | return; | |
865 | ||
db719fb0 | 866 | maybe_emit_filelist_progress(flist); |
ebed4c3a | 867 | |
d9d6bc52 | 868 | flist_expand(flist); |
ebed4c3a | 869 | |
64c3523a | 870 | if (write_batch) |
d01d15e0 | 871 | file->flags |= FLAG_TOP_DIR; |
ebed4c3a | 872 | |
c120ff37 | 873 | if (file->basename[0]) { |
ebed4c3a MP |
874 | flist->files[flist->count++] = file; |
875 | send_file_entry(file, f, base_flags); | |
876 | } | |
877 | ||
4844449a WD |
878 | if (recursive && S_ISDIR(file->mode) |
879 | && !(file->flags & FLAG_MOUNT_POINT)) { | |
d01d15e0 | 880 | struct exclude_struct **last_exclude_list = local_exclude_list; |
882e6893 | 881 | send_directory(f, flist, f_name_to(file, fbuf)); |
ebed4c3a MP |
882 | local_exclude_list = last_exclude_list; |
883 | return; | |
884 | } | |
c627d613 AT |
885 | } |
886 | ||
887 | ||
ebed4c3a | 888 | static void send_directory(int f, struct file_list *flist, char *dir) |
c627d613 | 889 | { |
3ec4dd97 AT |
890 | DIR *d; |
891 | struct dirent *di; | |
892 | char fname[MAXPATHLEN]; | |
eddd5d12 | 893 | unsigned int offset; |
3ec4dd97 AT |
894 | char *p; |
895 | ||
896 | d = opendir(dir); | |
897 | if (!d) { | |
06c28400 | 898 | io_error |= IOERR_GENERAL; |
ea42541f WD |
899 | rprintf(FERROR, "opendir %s failed: %s\n", |
900 | full_fname(dir), strerror(errno)); | |
3ec4dd97 AT |
901 | return; |
902 | } | |
c627d613 | 903 | |
eddd5d12 WD |
904 | offset = strlcpy(fname, dir, MAXPATHLEN); |
905 | p = fname + offset; | |
906 | if (offset >= MAXPATHLEN || p[-1] != '/') { | |
907 | if (offset >= MAXPATHLEN - 1) { | |
06c28400 | 908 | io_error |= IOERR_GENERAL; |
ea42541f WD |
909 | rprintf(FERROR, "skipping long-named directory: %s\n", |
910 | full_fname(fname)); | |
3ec4dd97 AT |
911 | closedir(d); |
912 | return; | |
913 | } | |
eddd5d12 WD |
914 | *p++ = '/'; |
915 | offset++; | |
3ec4dd97 | 916 | } |
c627d613 | 917 | |
3d913675 AT |
918 | local_exclude_list = NULL; |
919 | ||
3ec4dd97 | 920 | if (cvs_exclude) { |
eddd5d12 WD |
921 | if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset) |
922 | < MAXPATHLEN - offset) | |
57469f6c | 923 | add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE); |
eddd5d12 | 924 | else { |
06c28400 | 925 | io_error |= IOERR_GENERAL; |
ebed4c3a MP |
926 | rprintf(FINFO, |
927 | "cannot cvs-exclude in long-named directory %s\n", | |
ea42541f | 928 | full_fname(fname)); |
3ec4dd97 | 929 | } |
ebed4c3a MP |
930 | } |
931 | ||
6a7cc46c | 932 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
d6e6ecbd | 933 | char *dname = d_name(di); |
6a7cc46c S |
934 | if (dname[0] == '.' && (dname[1] == '\0' |
935 | || (dname[1] == '.' && dname[2] == '\0'))) | |
3ec4dd97 | 936 | continue; |
eddd5d12 WD |
937 | if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset) |
938 | send_file_name(f, flist, fname, recurse, 0); | |
939 | else { | |
940 | io_error |= IOERR_GENERAL; | |
941 | rprintf(FINFO, | |
942 | "cannot send long-named file %s\n", | |
943 | full_fname(fname)); | |
944 | } | |
3ec4dd97 | 945 | } |
6a7cc46c | 946 | if (errno) { |
06c28400 | 947 | io_error |= IOERR_GENERAL; |
6a7cc46c | 948 | rprintf(FERROR, "readdir(%s): (%d) %s\n", |
eddd5d12 | 949 | dir, errno, strerror(errno)); |
6a7cc46c | 950 | } |
c627d613 | 951 | |
429f9828 WD |
952 | if (local_exclude_list) |
953 | free_exclude_list(&local_exclude_list); /* Zeros pointer too */ | |
3d913675 | 954 | |
3ec4dd97 | 955 | closedir(d); |
c627d613 AT |
956 | } |
957 | ||
958 | ||
c4fea82f | 959 | /** |
429f9828 WD |
960 | * The delete_files() function in receiver.c sets f to -1 so that we just |
961 | * construct the file list in memory without sending it over the wire. It | |
962 | * also has the side-effect of ignoring user-excludes if delete_excluded | |
963 | * is set (so that the delete list includes user-excluded files). | |
c4fea82f | 964 | **/ |
ebed4c3a | 965 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
c627d613 | 966 | { |
24d0fcde | 967 | int l; |
bcacc18b | 968 | STRUCT_STAT st; |
808c57c3 | 969 | char *p, *dir, olddir[sizeof curr_dir]; |
ebed4c3a | 970 | char lastpath[MAXPATHLEN] = ""; |
649d65ed | 971 | struct file_list *flist; |
a800434a | 972 | int64 start_write; |
24d0fcde | 973 | int use_ff_fd = 0; |
649d65ed | 974 | |
1bbd10fe DD |
975 | if (show_filelist_p() && f != -1) |
976 | start_filelist_progress("building file list"); | |
c627d613 | 977 | |
a800434a AT |
978 | start_write = stats.total_written; |
979 | ||
3d382777 | 980 | flist = flist_new(); |
c627d613 | 981 | |
d6dead6b | 982 | if (f != -1) { |
76c21947 | 983 | io_start_buffering_out(f); |
24d0fcde | 984 | if (filesfrom_fd >= 0) { |
808c57c3 | 985 | if (argv[0] && !push_dir(argv[0])) { |
ea42541f WD |
986 | rprintf(FERROR, "push_dir %s failed: %s\n", |
987 | full_fname(argv[0]), strerror(errno)); | |
24d0fcde WD |
988 | exit_cleanup(RERR_FILESELECT); |
989 | } | |
990 | use_ff_fd = 1; | |
991 | } | |
d6dead6b AT |
992 | } |
993 | ||
24d0fcde | 994 | while (1) { |
fc638474 DD |
995 | char fname2[MAXPATHLEN]; |
996 | char *fname = fname2; | |
c627d613 | 997 | |
24d0fcde WD |
998 | if (use_ff_fd) { |
999 | if (read_filesfrom_line(filesfrom_fd, fname) == 0) | |
1000 | break; | |
1001 | sanitize_path(fname, NULL); | |
1002 | } else { | |
1003 | if (argc-- == 0) | |
1004 | break; | |
1005 | strlcpy(fname, *argv++, MAXPATHLEN); | |
1006 | if (sanitize_paths) | |
1007 | sanitize_path(fname, NULL); | |
1008 | } | |
c627d613 | 1009 | |
649d65ed | 1010 | l = strlen(fname); |
6931c138 WD |
1011 | if (fname[l - 1] == '/') { |
1012 | if (l == 2 && fname[0] == '.') { | |
1013 | /* Turn "./" into just "." rather than "./." */ | |
1014 | fname[1] = '\0'; | |
eddd5d12 WD |
1015 | } else if (l < MAXPATHLEN) { |
1016 | fname[l++] = '.'; | |
1017 | fname[l] = '\0'; | |
53f821f1 | 1018 | } |
649d65ed | 1019 | } |
c627d613 | 1020 | |
ebed4c3a | 1021 | if (link_stat(fname, &st) != 0) { |
f76933b1 | 1022 | if (f != -1) { |
06c28400 | 1023 | io_error |= IOERR_GENERAL; |
ea42541f WD |
1024 | rprintf(FERROR, "link_stat %s failed: %s\n", |
1025 | full_fname(fname), strerror(errno)); | |
f76933b1 | 1026 | } |
649d65ed AT |
1027 | continue; |
1028 | } | |
c627d613 | 1029 | |
24d0fcde | 1030 | if (S_ISDIR(st.st_mode) && !recurse && !files_from) { |
ebed4c3a | 1031 | rprintf(FINFO, "skipping directory %s\n", fname); |
649d65ed AT |
1032 | continue; |
1033 | } | |
c627d613 | 1034 | |
649d65ed | 1035 | dir = NULL; |
808c57c3 | 1036 | olddir[0] = '\0'; |
649d65ed AT |
1037 | |
1038 | if (!relative_paths) { | |
ebed4c3a | 1039 | p = strrchr(fname, '/'); |
649d65ed AT |
1040 | if (p) { |
1041 | *p = 0; | |
ebed4c3a | 1042 | if (p == fname) |
649d65ed AT |
1043 | dir = "/"; |
1044 | else | |
ebed4c3a MP |
1045 | dir = fname; |
1046 | fname = p + 1; | |
649d65ed | 1047 | } |
24d0fcde | 1048 | } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) { |
649d65ed AT |
1049 | /* this ensures we send the intermediate directories, |
1050 | thus getting their permissions right */ | |
2154309a | 1051 | char *lp = lastpath, *fn = fname, *slash = fname; |
649d65ed | 1052 | *p = 0; |
2154309a WD |
1053 | /* Skip any initial directories in our path that we |
1054 | * have in common with lastpath. */ | |
1055 | while (*fn && *lp == *fn) { | |
1056 | if (*fn == '/') | |
1057 | slash = fn; | |
1058 | lp++, fn++; | |
1059 | } | |
1060 | *p = '/'; | |
1061 | if (fn != p || (*lp && *lp != '/')) { | |
1062 | int copy_links_saved = copy_links; | |
1063 | int recurse_saved = recurse; | |
1064 | copy_links = copy_unsafe_links; | |
1065 | /* set recurse to 1 to prevent make_file | |
1066 | * from ignoring directory, but still | |
1067 | * turn off the recursive parameter to | |
1068 | * send_file_name */ | |
1069 | recurse = 1; | |
1070 | while ((slash = strchr(slash+1, '/')) != 0) { | |
1071 | *slash = 0; | |
1072 | send_file_name(f, flist, fname, 0, 0); | |
1073 | *slash = '/'; | |
649d65ed | 1074 | } |
2154309a WD |
1075 | copy_links = copy_links_saved; |
1076 | recurse = recurse_saved; | |
1077 | *p = 0; | |
1078 | strlcpy(lastpath, fname, sizeof lastpath); | |
649d65ed AT |
1079 | *p = '/'; |
1080 | } | |
1081 | } | |
ebed4c3a | 1082 | |
649d65ed AT |
1083 | if (!*fname) |
1084 | fname = "."; | |
ebed4c3a | 1085 | |
649d65ed | 1086 | if (dir && *dir) { |
882e6893 WD |
1087 | static char *lastdir; |
1088 | static int lastdir_len; | |
1089 | ||
808c57c3 | 1090 | strcpy(olddir, curr_dir); /* can't overflow */ |
5243c216 | 1091 | |
808c57c3 | 1092 | if (!push_dir(dir)) { |
06c28400 | 1093 | io_error |= IOERR_GENERAL; |
ea42541f WD |
1094 | rprintf(FERROR, "push_dir %s failed: %s\n", |
1095 | full_fname(dir), strerror(errno)); | |
649d65ed AT |
1096 | continue; |
1097 | } | |
5243c216 | 1098 | |
882e6893 WD |
1099 | if (lastdir && strcmp(lastdir, dir) == 0) { |
1100 | flist_dir = lastdir; | |
1101 | flist_dir_len = lastdir_len; | |
1102 | } else { | |
882e6893 WD |
1103 | flist_dir = lastdir = strdup(dir); |
1104 | flist_dir_len = lastdir_len = strlen(dir); | |
1105 | } | |
2bca43f6 | 1106 | } |
ebed4c3a | 1107 | |
2bca43f6 DD |
1108 | if (one_file_system) |
1109 | set_filesystem(fname); | |
1110 | ||
d01d15e0 | 1111 | send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR); |
2bca43f6 | 1112 | |
808c57c3 | 1113 | if (olddir[0]) { |
649d65ed | 1114 | flist_dir = NULL; |
882e6893 | 1115 | flist_dir_len = 0; |
808c57c3 | 1116 | if (!pop_dir(olddir)) { |
ea42541f WD |
1117 | rprintf(FERROR, "pop_dir %s failed: %s\n", |
1118 | full_fname(dir), strerror(errno)); | |
65417579 | 1119 | exit_cleanup(RERR_FILESELECT); |
649d65ed | 1120 | } |
649d65ed | 1121 | } |
649d65ed | 1122 | } |
dc5ddbcc | 1123 | |
983b1ed3 | 1124 | if (f != -1) { |
ebed4c3a | 1125 | send_file_entry(NULL, f, 0); |
c627d613 | 1126 | |
983b1ed3 WD |
1127 | if (show_filelist_p()) |
1128 | finish_filelist_progress(flist); | |
1129 | } | |
ebed4c3a | 1130 | |
827c37f6 | 1131 | clean_flist(flist, 0, 0); |
ebed4c3a | 1132 | |
785db4ce WD |
1133 | if (f != -1) { |
1134 | /* Now send the uid/gid list. This was introduced in | |
1135 | * protocol version 15 */ | |
649d65ed | 1136 | send_uid_list(f); |
f6c34742 | 1137 | |
983b1ed3 | 1138 | /* send the io_error flag */ |
ebed4c3a | 1139 | write_int(f, lp_ignore_errors(module_id) ? 0 : io_error); |
6ba9279f | 1140 | |
a261989c | 1141 | io_end_buffering(); |
a800434a AT |
1142 | stats.flist_size = stats.total_written - start_write; |
1143 | stats.num_files = flist->count; | |
64c3523a | 1144 | if (write_batch) |
ebed4c3a | 1145 | write_batch_flist_info(flist->count, flist->files); |
d6dead6b AT |
1146 | } |
1147 | ||
17faa41c | 1148 | if (verbose > 2) |
ebed4c3a | 1149 | rprintf(FINFO, "send_file_list done\n"); |
17faa41c | 1150 | |
649d65ed | 1151 | return flist; |
c627d613 AT |
1152 | } |
1153 | ||
1154 | ||
1155 | struct file_list *recv_file_list(int f) | |
1156 | { | |
ebed4c3a | 1157 | struct file_list *flist; |
1ef00d20 | 1158 | unsigned short flags; |
ebed4c3a MP |
1159 | int64 start_read; |
1160 | extern int list_only; | |
c627d613 | 1161 | |
1bbd10fe DD |
1162 | if (show_filelist_p()) |
1163 | start_filelist_progress("receiving file list"); | |
c627d613 | 1164 | |
ebed4c3a | 1165 | start_read = stats.total_read; |
a800434a | 1166 | |
58cadc86 | 1167 | flist = new(struct file_list); |
ebed4c3a MP |
1168 | if (!flist) |
1169 | goto oom; | |
c627d613 | 1170 | |
ebed4c3a MP |
1171 | flist->count = 0; |
1172 | flist->malloced = 1000; | |
58cadc86 | 1173 | flist->files = new_array(struct file_struct *, flist->malloced); |
ebed4c3a MP |
1174 | if (!flist->files) |
1175 | goto oom; | |
c627d613 AT |
1176 | |
1177 | ||
1ef00d20 | 1178 | while ((flags = read_byte(f)) != 0) { |
5d2c5c4c | 1179 | int i = flist->count; |
dbda5fbf | 1180 | |
d9d6bc52 | 1181 | flist_expand(flist); |
c627d613 | 1182 | |
d01d15e0 | 1183 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
75bc8600 | 1184 | flags |= read_byte(f) << 8; |
ebed4c3a | 1185 | receive_file_entry(&flist->files[i], flags, f); |
c627d613 | 1186 | |
ebed4c3a MP |
1187 | if (S_ISREG(flist->files[i]->mode)) |
1188 | stats.total_size += flist->files[i]->length; | |
c627d613 | 1189 | |
ebed4c3a | 1190 | flist->count++; |
c627d613 | 1191 | |
db719fb0 | 1192 | maybe_emit_filelist_progress(flist); |
1bbd10fe | 1193 | |
8018edd3 | 1194 | if (verbose > 2) { |
ebed4c3a MP |
1195 | rprintf(FINFO, "recv_file_name(%s)\n", |
1196 | f_name(flist->files[i])); | |
8018edd3 | 1197 | } |
ebed4c3a | 1198 | } |
5911fee5 | 1199 | receive_file_entry(NULL, 0, 0); /* Signal that we're done. */ |
c627d613 | 1200 | |
ebed4c3a MP |
1201 | if (verbose > 2) |
1202 | rprintf(FINFO, "received %d names\n", flist->count); | |
c627d613 | 1203 | |
b7736c79 | 1204 | if (show_filelist_p()) |
1bbd10fe | 1205 | finish_filelist_progress(flist); |
a06d19e3 | 1206 | |
983b1ed3 WD |
1207 | clean_flist(flist, relative_paths, 1); |
1208 | ||
785db4ce WD |
1209 | if (f != -1) { |
1210 | /* Now send the uid/gid list. This was introduced in | |
1211 | * protocol version 15 */ | |
ebed4c3a | 1212 | recv_uid_list(f, flist); |
f6c34742 | 1213 | |
785db4ce WD |
1214 | if (!read_batch) { |
1215 | /* Recv the io_error flag */ | |
1216 | if (lp_ignore_errors(module_id) || ignore_errors) | |
1217 | read_int(f); | |
1218 | else | |
1219 | io_error |= read_int(f); | |
1220 | } | |
ebed4c3a | 1221 | } |
6ba9279f | 1222 | |
ebed4c3a MP |
1223 | if (list_only) { |
1224 | int i; | |
b7736c79 | 1225 | for (i = 0; i < flist->count; i++) |
ebed4c3a | 1226 | list_file_entry(flist->files[i]); |
ebed4c3a | 1227 | } |
f7632fc6 | 1228 | |
ebed4c3a MP |
1229 | if (verbose > 2) |
1230 | rprintf(FINFO, "recv_file_list done\n"); | |
17faa41c | 1231 | |
ebed4c3a MP |
1232 | stats.flist_size = stats.total_read - start_read; |
1233 | stats.num_files = flist->count; | |
a800434a | 1234 | |
ebed4c3a | 1235 | return flist; |
c627d613 | 1236 | |
ebed4c3a MP |
1237 | oom: |
1238 | out_of_memory("recv_file_list"); | |
1239 | return NULL; /* not reached */ | |
c627d613 AT |
1240 | } |
1241 | ||
1242 | ||
fa45cda1 | 1243 | int file_compare(struct file_struct **file1, struct file_struct **file2) |
c627d613 | 1244 | { |
fa45cda1 S |
1245 | struct file_struct *f1 = *file1; |
1246 | struct file_struct *f2 = *file2; | |
1247 | ||
1248 | if (!f1->basename && !f2->basename) | |
ebed4c3a | 1249 | return 0; |
fa45cda1 | 1250 | if (!f1->basename) |
ebed4c3a | 1251 | return -1; |
fa45cda1 | 1252 | if (!f2->basename) |
ebed4c3a | 1253 | return 1; |
fa45cda1 S |
1254 | if (f1->dirname == f2->dirname) |
1255 | return u_strcmp(f1->basename, f2->basename); | |
1256 | return f_name_cmp(f1, f2); | |
c627d613 AT |
1257 | } |
1258 | ||
1259 | ||
ebed4c3a | 1260 | int flist_find(struct file_list *flist, struct file_struct *f) |
c627d613 | 1261 | { |
ebed4c3a | 1262 | int low = 0, high = flist->count - 1; |
d966ee25 | 1263 | |
ca23c51a WD |
1264 | while (high >= 0 && !flist->files[high]->basename) high--; |
1265 | ||
1266 | if (high < 0) | |
ebed4c3a | 1267 | return -1; |
d966ee25 AT |
1268 | |
1269 | while (low != high) { | |
ebed4c3a | 1270 | int mid = (low + high) / 2; |
b7736c79 | 1271 | int ret = file_compare(&flist->files[flist_up(flist, mid)],&f); |
ebed4c3a MP |
1272 | if (ret == 0) |
1273 | return flist_up(flist, mid); | |
b7736c79 | 1274 | if (ret > 0) |
ebed4c3a | 1275 | high = mid; |
b7736c79 | 1276 | else |
ebed4c3a | 1277 | low = mid + 1; |
d966ee25 AT |
1278 | } |
1279 | ||
ebed4c3a MP |
1280 | if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0) |
1281 | return flist_up(flist, low); | |
d966ee25 | 1282 | return -1; |
c627d613 AT |
1283 | } |
1284 | ||
1285 | ||
3ec4dd97 AT |
1286 | /* |
1287 | * free up one file | |
1288 | */ | |
66203a98 | 1289 | void free_file(struct file_struct *file) |
c627d613 | 1290 | { |
ebed4c3a MP |
1291 | if (!file) |
1292 | return; | |
1293 | if (file->basename) | |
1294 | free(file->basename); | |
728d0922 WD |
1295 | if (!IS_DEVICE(file->mode) && file->u.link) |
1296 | free(file->u.link); /* Handles u.sum too. */ | |
92cc9dd7 WD |
1297 | if (file->link_u.idev) |
1298 | free((char*)file->link_u.idev); /* Handles link_u.links too. */ | |
3d382777 | 1299 | *file = null_file; |
3ec4dd97 | 1300 | } |
c627d613 | 1301 | |
c627d613 | 1302 | |
3d382777 AT |
1303 | /* |
1304 | * allocate a new file list | |
1305 | */ | |
1e34e4b7 | 1306 | struct file_list *flist_new(void) |
3d382777 AT |
1307 | { |
1308 | struct file_list *flist; | |
1309 | ||
58cadc86 | 1310 | flist = new(struct file_list); |
ebed4c3a MP |
1311 | if (!flist) |
1312 | out_of_memory("send_file_list"); | |
3d382777 | 1313 | |
ebed4c3a | 1314 | flist->count = 0; |
d9d6bc52 MP |
1315 | flist->malloced = 0; |
1316 | flist->files = NULL; | |
1317 | ||
3d382777 AT |
1318 | #if ARENA_SIZE > 0 |
1319 | flist->string_area = string_area_new(0); | |
1320 | #else | |
5c66303a | 1321 | flist->string_area = NULL; |
3d382777 AT |
1322 | #endif |
1323 | return flist; | |
1324 | } | |
ebed4c3a | 1325 | |
3ec4dd97 AT |
1326 | /* |
1327 | * free up all elements in a flist | |
1328 | */ | |
1329 | void flist_free(struct file_list *flist) | |
1330 | { | |
1331 | int i; | |
ebed4c3a | 1332 | for (i = 1; i < flist->count; i++) { |
3d382777 AT |
1333 | if (!flist->string_area) |
1334 | free_file(flist->files[i]); | |
649d65ed | 1335 | free(flist->files[i]); |
ebed4c3a | 1336 | } |
d9d6bc52 MP |
1337 | /* FIXME: I don't think we generally need to blank the flist |
1338 | * since it's about to be freed. This will just cause more | |
1339 | * memory traffic. If you want a freed-memory debugger, you | |
1340 | * know where to get it. */ | |
ebed4c3a MP |
1341 | memset((char *) flist->files, 0, |
1342 | sizeof(flist->files[0]) * flist->count); | |
3ec4dd97 | 1343 | free(flist->files); |
3d382777 AT |
1344 | if (flist->string_area) |
1345 | string_area_free(flist->string_area); | |
ebed4c3a | 1346 | memset((char *) flist, 0, sizeof(*flist)); |
3ec4dd97 | 1347 | free(flist); |
c627d613 AT |
1348 | } |
1349 | ||
1350 | ||
1351 | /* | |
1352 | * This routine ensures we don't have any duplicate names in our file list. | |
dbda5fbf | 1353 | * duplicate names can cause corruption because of the pipelining |
c627d613 | 1354 | */ |
827c37f6 | 1355 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups) |
c627d613 | 1356 | { |
6931c138 | 1357 | int i, prev_i = 0; |
c627d613 | 1358 | |
ebed4c3a | 1359 | if (!flist || flist->count == 0) |
3ec4dd97 | 1360 | return; |
3ec4dd97 | 1361 | |
ebed4c3a MP |
1362 | qsort(flist->files, flist->count, |
1363 | sizeof(flist->files[0]), (int (*)()) file_compare); | |
1364 | ||
827c37f6 | 1365 | for (i = no_dups? 0 : flist->count; i < flist->count; i++) { |
b91b50c0 | 1366 | if (flist->files[i]->basename) { |
6931c138 | 1367 | prev_i = i; |
b91b50c0 WD |
1368 | break; |
1369 | } | |
1370 | } | |
1371 | while (++i < flist->count) { | |
1372 | if (!flist->files[i]->basename) | |
1373 | continue; | |
8018edd3 | 1374 | if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) { |
b91b50c0 | 1375 | if (verbose > 1 && !am_server) { |
ebed4c3a MP |
1376 | rprintf(FINFO, |
1377 | "removing duplicate name %s from file list %d\n", | |
8018edd3 | 1378 | f_name(flist->files[i]), i); |
b91b50c0 | 1379 | } |
6931c138 WD |
1380 | /* Make sure that if we unduplicate '.', that we don't |
1381 | * lose track of a user-specified starting point (or | |
1382 | * else deletions will mysteriously fail with -R). */ | |
d01d15e0 WD |
1383 | if (flist->files[i]->flags & FLAG_TOP_DIR) |
1384 | flist->files[prev_i]->flags |= FLAG_TOP_DIR; | |
b91b50c0 WD |
1385 | /* it's not great that the flist knows the semantics of |
1386 | * the file memory usage, but i'd rather not add a flag | |
1387 | * byte to that struct. | |
1388 | * XXX can i use a bit in the flags field? */ | |
3d382777 AT |
1389 | if (flist->string_area) |
1390 | flist->files[i][0] = null_file; | |
1391 | else | |
1392 | free_file(flist->files[i]); | |
728d0922 | 1393 | } else |
6931c138 | 1394 | prev_i = i; |
3ec4dd97 | 1395 | } |
0199b05f AT |
1396 | |
1397 | if (strip_root) { | |
1398 | /* we need to strip off the root directory in the case | |
1399 | of relative paths, but this must be done _after_ | |
1400 | the sorting phase */ | |
ebed4c3a | 1401 | for (i = 0; i < flist->count; i++) { |
0199b05f AT |
1402 | if (flist->files[i]->dirname && |
1403 | flist->files[i]->dirname[0] == '/') { | |
1404 | memmove(&flist->files[i]->dirname[0], | |
1405 | &flist->files[i]->dirname[1], | |
1406 | strlen(flist->files[i]->dirname)); | |
1407 | } | |
ebed4c3a MP |
1408 | |
1409 | if (flist->files[i]->dirname && | |
0199b05f AT |
1410 | !flist->files[i]->dirname[0]) { |
1411 | flist->files[i]->dirname = NULL; | |
1412 | } | |
1413 | } | |
1414 | } | |
1415 | ||
ebed4c3a MP |
1416 | if (verbose <= 3) |
1417 | return; | |
0199b05f | 1418 | |
ebed4c3a | 1419 | for (i = 0; i < flist->count; i++) { |
ea847c62 WD |
1420 | rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f\n", |
1421 | who_am_i(), i, | |
4c7e4607 | 1422 | NS(flist->files[i]->basedir), |
74e708d8 AT |
1423 | NS(flist->files[i]->dirname), |
1424 | NS(flist->files[i]->basename), | |
08a740ff | 1425 | (int) flist->files[i]->mode, |
ebed4c3a | 1426 | (double) flist->files[i]->length); |
0199b05f | 1427 | } |
3ec4dd97 AT |
1428 | } |
1429 | ||
1430 | ||
8018edd3 WD |
1431 | enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE }; |
1432 | ||
1433 | /* Compare the names of two file_struct entities, just like strcmp() | |
1434 | * would do if it were operating on the joined strings. We assume | |
1435 | * that there are no 0-length strings. | |
3ec4dd97 | 1436 | */ |
8018edd3 | 1437 | int f_name_cmp(struct file_struct *f1, struct file_struct *f2) |
3ec4dd97 | 1438 | { |
8018edd3 WD |
1439 | int dif; |
1440 | const uchar *c1, *c2; | |
1ef00d20 | 1441 | enum fnc_state state1, state2; |
8018edd3 WD |
1442 | |
1443 | if (!f1 || !f1->basename) { | |
1444 | if (!f2 || !f2->basename) | |
1445 | return 0; | |
1446 | return -1; | |
1447 | } | |
1448 | if (!f2 || !f2->basename) | |
1449 | return 1; | |
1450 | ||
e90b8ace | 1451 | if (!(c1 = (uchar*)f1->dirname)) { |
8018edd3 | 1452 | state1 = fnc_BASE; |
e90b8ace | 1453 | c1 = (uchar*)f1->basename; |
728d0922 | 1454 | } else |
1ef00d20 | 1455 | state1 = fnc_DIR; |
e90b8ace | 1456 | if (!(c2 = (uchar*)f2->dirname)) { |
8018edd3 | 1457 | state2 = fnc_BASE; |
e90b8ace | 1458 | c2 = (uchar*)f2->basename; |
728d0922 | 1459 | } else |
1ef00d20 | 1460 | state2 = fnc_DIR; |
8018edd3 WD |
1461 | |
1462 | while (1) { | |
1463 | if ((dif = (int)*c1 - (int)*c2) != 0) | |
1464 | break; | |
1465 | if (!*++c1) { | |
1466 | switch (state1) { | |
1467 | case fnc_DIR: | |
1468 | state1 = fnc_SLASH; | |
e90b8ace | 1469 | c1 = (uchar*)"/"; |
8018edd3 WD |
1470 | break; |
1471 | case fnc_SLASH: | |
1472 | state1 = fnc_BASE; | |
e90b8ace | 1473 | c1 = (uchar*)f1->basename; |
8018edd3 WD |
1474 | break; |
1475 | case fnc_BASE: | |
1476 | break; | |
1477 | } | |
1478 | } | |
1479 | if (!*++c2) { | |
1480 | switch (state2) { | |
1481 | case fnc_DIR: | |
1482 | state2 = fnc_SLASH; | |
e90b8ace | 1483 | c2 = (uchar*)"/"; |
8018edd3 WD |
1484 | break; |
1485 | case fnc_SLASH: | |
1486 | state2 = fnc_BASE; | |
e90b8ace | 1487 | c2 = (uchar*)f2->basename; |
8018edd3 WD |
1488 | break; |
1489 | case fnc_BASE: | |
1490 | if (!*c1) | |
1491 | return 0; | |
1492 | break; | |
1493 | } | |
1494 | } | |
1495 | } | |
1496 | ||
1497 | return dif; | |
1498 | } | |
1499 | ||
3ec4dd97 | 1500 | |
8018edd3 | 1501 | /* Return a copy of the full filename of a flist entry, using the indicated |
882e6893 WD |
1502 | * buffer. No size-checking is done because we checked the size when creating |
1503 | * the file_struct entry. | |
8018edd3 | 1504 | */ |
882e6893 | 1505 | char *f_name_to(struct file_struct *f, char *fbuf) |
8018edd3 | 1506 | { |
ebed4c3a MP |
1507 | if (!f || !f->basename) |
1508 | return NULL; | |
3ec4dd97 | 1509 | |
3ec4dd97 | 1510 | if (f->dirname) { |
882e6893 WD |
1511 | int len = strlen(f->dirname); |
1512 | memcpy(fbuf, f->dirname, len); | |
1513 | fbuf[len] = '/'; | |
1514 | strcpy(fbuf + len + 1, f->basename); | |
8018edd3 | 1515 | } else |
882e6893 | 1516 | strcpy(fbuf, f->basename); |
b7736c79 | 1517 | return fbuf; |
8018edd3 | 1518 | } |
e03dfae5 | 1519 | |
3ec4dd97 | 1520 | |
8018edd3 WD |
1521 | /* Like f_name_to(), but we rotate through 5 static buffers of our own. |
1522 | */ | |
1523 | char *f_name(struct file_struct *f) | |
1524 | { | |
1525 | static char names[5][MAXPATHLEN]; | |
1526 | static unsigned int n; | |
1527 | ||
1528 | n = (n + 1) % (sizeof names / sizeof names[0]); | |
1529 | ||
882e6893 | 1530 | return f_name_to(f, names[n]); |
c627d613 | 1531 | } |