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