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