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; |
f05f993e | 34 | extern int am_root; |
c627d613 AT |
35 | extern int am_server; |
36 | extern int always_checksum; | |
983b1ed3 WD |
37 | extern int module_id; |
38 | extern int ignore_errors; | |
c627d613 AT |
39 | |
40 | extern int cvs_exclude; | |
41 | ||
a06d19e3 | 42 | extern int recurse; |
808c57c3 | 43 | extern char curr_dir[MAXPATHLEN]; |
24d0fcde WD |
44 | extern char *files_from; |
45 | extern int filesfrom_fd; | |
a06d19e3 | 46 | |
c627d613 AT |
47 | extern int one_file_system; |
48 | extern int make_backups; | |
49 | extern int preserve_links; | |
dc5ddbcc | 50 | extern int preserve_hard_links; |
c627d613 AT |
51 | extern int preserve_perms; |
52 | extern int preserve_devices; | |
53 | extern int preserve_uid; | |
54 | extern int preserve_gid; | |
55 | extern int preserve_times; | |
6574b4f7 | 56 | extern int relative_paths; |
24d0fcde | 57 | extern int implied_dirs; |
82306bf6 | 58 | extern int copy_links; |
b5313607 | 59 | extern int copy_unsafe_links; |
d04e9c51 | 60 | extern int protocol_version; |
cb13abfe | 61 | extern int sanitize_paths; |
c627d613 | 62 | |
6902ed17 MP |
63 | extern int read_batch; |
64 | extern int write_batch; | |
65 | ||
429f9828 WD |
66 | extern struct exclude_struct **exclude_list; |
67 | extern struct exclude_struct **server_exclude_list; | |
68 | extern struct exclude_struct **local_exclude_list; | |
c627d613 | 69 | |
06c28400 WD |
70 | int io_error; |
71 | ||
fea4db62 | 72 | static char empty_sum[MD4_SUM_LENGTH]; |
61dec11a | 73 | static unsigned int min_file_struct_len; |
3d382777 | 74 | |
827c37f6 | 75 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups); |
0199b05f | 76 | |
58225000 | 77 | |
61dec11a WD |
78 | void init_flist(void) |
79 | { | |
80 | struct file_struct f; | |
81 | ||
82 | /* Figure out how big the file_struct is without trailing padding */ | |
83 | min_file_struct_len = ((char*)&f.flags - (char*)&f) + sizeof f.flags; | |
84 | } | |
85 | ||
86 | ||
1bbd10fe | 87 | static int show_filelist_p(void) |
58225000 | 88 | { |
24d0fcde | 89 | return verbose && (recurse || files_from) && !am_server; |
1bbd10fe | 90 | } |
ebed4c3a | 91 | |
1bbd10fe DD |
92 | static void start_filelist_progress(char *kind) |
93 | { | |
94 | rprintf(FINFO, "%s ... ", kind); | |
95 | if ((verbose > 1) || do_progress) | |
96 | rprintf(FINFO, "\n"); | |
97 | rflush(FINFO); | |
58225000 MP |
98 | } |
99 | ||
db719fb0 | 100 | |
d27cbec5 | 101 | static void emit_filelist_progress(const struct file_list *flist) |
db719fb0 | 102 | { |
d27cbec5 | 103 | rprintf(FINFO, " %d files...\r", flist->count); |
db719fb0 MP |
104 | } |
105 | ||
106 | ||
d27cbec5 | 107 | static void maybe_emit_filelist_progress(const struct file_list *flist) |
58225000 | 108 | { |
d27cbec5 DD |
109 | if (do_progress && show_filelist_p() && ((flist->count % 100) == 0)) |
110 | emit_filelist_progress(flist); | |
58225000 MP |
111 | } |
112 | ||
113 | ||
1bbd10fe | 114 | static void finish_filelist_progress(const struct file_list *flist) |
58225000 | 115 | { |
1bbd10fe DD |
116 | if (do_progress) { |
117 | /* This overwrites the progress line */ | |
c7b562be MP |
118 | rprintf(FINFO, "%d file%sto consider\n", |
119 | flist->count, flist->count == 1 ? " " : "s "); | |
b7736c79 | 120 | } else |
1bbd10fe | 121 | rprintf(FINFO, "done\n"); |
58225000 MP |
122 | } |
123 | ||
86943126 MP |
124 | void show_flist_stats(void) |
125 | { | |
126 | /* Nothing yet */ | |
127 | } | |
128 | ||
129 | ||
f7632fc6 AT |
130 | static void list_file_entry(struct file_struct *f) |
131 | { | |
740819ef | 132 | char perms[11]; |
f7632fc6 | 133 | |
7212be92 DD |
134 | if (!f->basename) |
135 | /* this can happen if duplicate names were removed */ | |
136 | return; | |
137 | ||
740819ef MP |
138 | permstring(perms, f->mode); |
139 | ||
0d162bd1 | 140 | #if SUPPORT_LINKS |
f7632fc6 | 141 | if (preserve_links && S_ISLNK(f->mode)) { |
ebed4c3a MP |
142 | rprintf(FINFO, "%s %11.0f %s %s -> %s\n", |
143 | perms, | |
144 | (double) f->length, timestring(f->modtime), | |
728d0922 | 145 | f_name(f), f->u.link); |
0d162bd1 WD |
146 | } else |
147 | #endif | |
ebed4c3a MP |
148 | rprintf(FINFO, "%s %11.0f %s %s\n", |
149 | perms, | |
150 | (double) f->length, timestring(f->modtime), | |
151 | f_name(f)); | |
f7632fc6 AT |
152 | } |
153 | ||
154 | ||
bd9e9ecc MP |
155 | /** |
156 | * Stat either a symlink or its referent, depending on the settings of | |
157 | * copy_links, copy_unsafe_links, etc. | |
158 | * | |
4e5db0ad MP |
159 | * @retval -1 on error |
160 | * | |
161 | * @retval 0 for success | |
162 | * | |
163 | * @post If @p path is a symlink, then @p linkbuf (of size @c | |
bd9e9ecc | 164 | * MAXPATHLEN) contains the symlink target. |
4e5db0ad MP |
165 | * |
166 | * @post @p buffer contains information about the link or the | |
167 | * referrent as appropriate, if they exist. | |
bd9e9ecc | 168 | **/ |
b7736c79 | 169 | int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf) |
b5313607 DD |
170 | { |
171 | #if SUPPORT_LINKS | |
b7736c79 | 172 | if (copy_links) |
6f2623fd | 173 | return do_stat(path, buffer); |
b7736c79 | 174 | if (do_lstat(path, buffer) == -1) |
b5313607 | 175 | return -1; |
6f2623fd | 176 | if (S_ISLNK(buffer->st_mode)) { |
b7736c79 | 177 | int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1); |
dbda5fbf | 178 | if (l == -1) |
b5313607 | 179 | return -1; |
6f2623fd | 180 | linkbuf[l] = 0; |
fc638474 DD |
181 | if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) { |
182 | if (verbose > 1) { | |
dbda5fbf | 183 | rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n", |
fc638474 DD |
184 | path, linkbuf); |
185 | } | |
6f2623fd | 186 | return do_stat(path, buffer); |
b5313607 DD |
187 | } |
188 | } | |
189 | return 0; | |
190 | #else | |
6f2623fd | 191 | return do_stat(path, buffer); |
b5313607 DD |
192 | #endif |
193 | } | |
194 | ||
6f2623fd | 195 | int link_stat(const char *path, STRUCT_STAT * buffer) |
82306bf6 AT |
196 | { |
197 | #if SUPPORT_LINKS | |
b7736c79 | 198 | if (copy_links) |
6f2623fd | 199 | return do_stat(path, buffer); |
b7736c79 | 200 | return do_lstat(path, buffer); |
82306bf6 | 201 | #else |
6f2623fd | 202 | return do_stat(path, buffer); |
82306bf6 AT |
203 | #endif |
204 | } | |
205 | ||
c627d613 | 206 | /* |
429f9828 WD |
207 | * This function is used to check if a file should be included/excluded |
208 | * from the list of files based on its name and type etc. The value of | |
209 | * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES. | |
c627d613 | 210 | */ |
429f9828 | 211 | static int check_exclude_file(char *fname, int is_dir, int exclude_level) |
c627d613 | 212 | { |
7d687932 | 213 | #if 0 /* This currently never happens, so avoid a useless compare. */ |
429f9828 WD |
214 | if (exclude_level == NO_EXCLUDES) |
215 | return 0; | |
216 | #endif | |
6931c138 | 217 | if (fname) { |
429f9828 | 218 | /* never exclude '.', even if somebody does --exclude '*' */ |
6931c138 WD |
219 | if (fname[0] == '.' && !fname[1]) |
220 | return 0; | |
221 | /* Handle the -R version of the '.' dir. */ | |
222 | if (fname[0] == '/') { | |
223 | int len = strlen(fname); | |
224 | if (fname[len-1] == '.' && fname[len-2] == '/') | |
225 | return 0; | |
226 | } | |
76e26e10 | 227 | } |
6931c138 WD |
228 | if (server_exclude_list |
229 | && check_exclude(server_exclude_list, fname, is_dir)) | |
429f9828 WD |
230 | return 1; |
231 | if (exclude_level != ALL_EXCLUDES) | |
232 | return 0; | |
233 | if (exclude_list && check_exclude(exclude_list, fname, is_dir)) | |
234 | return 1; | |
6931c138 WD |
235 | if (local_exclude_list |
236 | && check_exclude(local_exclude_list, fname, is_dir)) | |
76e26e10 | 237 | return 1; |
76e26e10 | 238 | return 0; |
c627d613 AT |
239 | } |
240 | ||
241 | /* used by the one_file_system code */ | |
242 | static dev_t filesystem_dev; | |
243 | ||
244 | static void set_filesystem(char *fname) | |
245 | { | |
ebed4c3a MP |
246 | STRUCT_STAT st; |
247 | if (link_stat(fname, &st) != 0) | |
248 | return; | |
249 | filesystem_dev = st.st_dev; | |
c627d613 AT |
250 | } |
251 | ||
252 | ||
b280a1f4 AT |
253 | static int to_wire_mode(mode_t mode) |
254 | { | |
0d162bd1 | 255 | #if SUPPORT_LINKS |
b7736c79 | 256 | if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) |
b280a1f4 | 257 | return (mode & ~(_S_IFMT)) | 0120000; |
0d162bd1 | 258 | #endif |
ebed4c3a | 259 | return (int) mode; |
b280a1f4 AT |
260 | } |
261 | ||
262 | static mode_t from_wire_mode(int mode) | |
263 | { | |
b7736c79 | 264 | if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) |
efe3037c | 265 | return (mode & ~(_S_IFMT)) | _S_IFLNK; |
ebed4c3a | 266 | return (mode_t) mode; |
b280a1f4 AT |
267 | } |
268 | ||
269 | ||
ebed4c3a | 270 | static void send_directory(int f, struct file_list *flist, char *dir); |
c627d613 | 271 | |
3a6a366f | 272 | static char *flist_dir; |
882e6893 | 273 | static int flist_dir_len; |
c627d613 | 274 | |
3ec4dd97 | 275 | |
d9d6bc52 MP |
276 | /** |
277 | * Make sure @p flist is big enough to hold at least @p flist->count | |
278 | * entries. | |
279 | **/ | |
280 | static void flist_expand(struct file_list *flist) | |
281 | { | |
282 | if (flist->count >= flist->malloced) { | |
2e7d1994 | 283 | void *new_ptr; |
dbda5fbf | 284 | |
d9d6bc52 MP |
285 | if (flist->malloced < 1000) |
286 | flist->malloced += 1000; | |
287 | else | |
288 | flist->malloced *= 2; | |
2e7d1994 | 289 | |
58cadc86 WD |
290 | if (flist->files) { |
291 | new_ptr = realloc_array(flist->files, | |
292 | struct file_struct *, | |
293 | flist->malloced); | |
294 | } else { | |
295 | new_ptr = new_array(struct file_struct *, | |
296 | flist->malloced); | |
297 | } | |
2e7d1994 | 298 | |
1d5a1da9 | 299 | if (verbose >= 2) { |
ea847c62 | 300 | rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n", |
bd6abc49 S |
301 | who_am_i(), |
302 | (double) sizeof flist->files[0] * flist->malloced, | |
303 | (new_ptr == flist->files) ? " not" : ""); | |
2e7d1994 | 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 | |
e90cdb8a WD |
770 | /* We only care about directories because we need to avoid recursing |
771 | * into a mount-point directory, not to avoid copying a symlinked | |
772 | * file if -L (or similar) was specified. */ | |
773 | if (one_file_system && st.st_dev != filesystem_dev | |
774 | && S_ISDIR(st.st_mode)) | |
4844449a | 775 | flags |= FLAG_MOUNT_POINT; |
ebed4c3a | 776 | |
1923b1fc | 777 | if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level)) |
76e26e10 DD |
778 | return NULL; |
779 | ||
1923b1fc | 780 | if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0) |
ac1a0994 AT |
781 | return NULL; |
782 | ||
ebed4c3a | 783 | skip_excludes: |
ac1a0994 | 784 | |
ea847c62 WD |
785 | if (verbose > 2) { |
786 | rprintf(FINFO, "[%s] make_file(%s,*,%d)\n", | |
1923b1fc | 787 | who_am_i(), thisname, exclude_level); |
ea847c62 | 788 | } |
ebed4c3a | 789 | |
a289addd WD |
790 | if ((basename = strrchr(thisname, '/')) != NULL) { |
791 | dirname_len = ++basename - thisname; /* counts future '\0' */ | |
792 | if (lastdir_len == dirname_len - 1 | |
793 | && strncmp(thisname, lastdir, lastdir_len) == 0) { | |
794 | dirname = lastdir; | |
795 | dirname_len = 0; /* indicates no copy is needed */ | |
796 | } else | |
797 | dirname = thisname; | |
3ec4dd97 | 798 | } else { |
a289addd WD |
799 | basename = thisname; |
800 | dirname = NULL; | |
801 | dirname_len = 0; | |
3ec4dd97 | 802 | } |
a289addd | 803 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
c627d613 | 804 | |
a289addd WD |
805 | #if SUPPORT_LINKS |
806 | linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0; | |
807 | #else | |
808 | linkname_len = 0; | |
809 | #endif | |
810 | ||
61dec11a | 811 | #if SUPPORT_HARD_LINKS |
b1dab236 | 812 | if (preserve_hard_links) { |
bd6abc49 S |
813 | if (protocol_version < 28) { |
814 | if (S_ISREG(st.st_mode)) | |
815 | idev_len = sizeof (struct idev); | |
b1dab236 WD |
816 | else |
817 | idev_len = 0; | |
bd6abc49 | 818 | } else { |
b1dab236 | 819 | if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) |
bd6abc49 | 820 | idev_len = sizeof (struct idev); |
b1dab236 WD |
821 | else |
822 | idev_len = 0; | |
bd6abc49 | 823 | } |
b1dab236 | 824 | } else |
61dec11a | 825 | #endif |
b1dab236 | 826 | idev_len = 0; |
61dec11a | 827 | |
a289addd | 828 | sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0; |
61dec11a | 829 | file_struct_len = idev_len? sizeof file[0] : min_file_struct_len; |
a289addd | 830 | |
61dec11a WD |
831 | alloc_len = file_struct_len + dirname_len + basename_len |
832 | + linkname_len + sum_len + idev_len; | |
a289addd WD |
833 | if (!(bp = new_array(char, alloc_len))) |
834 | out_of_memory("receive_file_entry"); | |
835 | file = (struct file_struct *)bp; | |
61dec11a WD |
836 | memset(bp, 0, min_file_struct_len); |
837 | bp += file_struct_len; | |
a289addd WD |
838 | |
839 | file->flags = flags; | |
3ec4dd97 AT |
840 | file->modtime = st.st_mtime; |
841 | file->length = st.st_size; | |
842 | file->mode = st.st_mode; | |
843 | file->uid = st.st_uid; | |
844 | file->gid = st.st_gid; | |
0d162bd1 | 845 | |
61dec11a WD |
846 | #if SUPPORT_HARD_LINKS |
847 | if (idev_len) { | |
848 | file->link_u.idev = (struct idev *)bp; | |
849 | bp += idev_len; | |
850 | file->F_DEV = st.st_dev; | |
851 | file->F_INODE = st.st_ino; | |
852 | } | |
853 | #endif | |
854 | ||
a289addd WD |
855 | if (dirname_len) { |
856 | file->dirname = lastdir = bp; | |
857 | lastdir_len = dirname_len - 1; | |
858 | memcpy(bp, dirname, dirname_len - 1); | |
859 | bp += dirname_len; | |
860 | bp[-1] = '\0'; | |
861 | } else if (dirname) | |
862 | file->dirname = dirname; | |
863 | ||
864 | file->basename = bp; | |
865 | memcpy(bp, basename, basename_len); | |
866 | bp += basename_len; | |
867 | ||
0d162bd1 WD |
868 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
869 | if (preserve_devices && IS_DEVICE(st.st_mode)) | |
870 | file->u.rdev = st.st_rdev; | |
871 | #endif | |
872 | ||
873 | #if SUPPORT_LINKS | |
a289addd WD |
874 | if (linkname_len) { |
875 | file->u.link = bp; | |
876 | memcpy(bp, linkname, linkname_len); | |
877 | bp += linkname_len; | |
878 | } | |
0d162bd1 WD |
879 | #endif |
880 | ||
a289addd WD |
881 | if (sum_len) { |
882 | file->u.sum = bp; | |
883 | file_checksum(thisname, bp, st.st_size); | |
884 | /*bp += sum_len;*/ | |
ebed4c3a | 885 | } |
c627d613 | 886 | |
882e6893 | 887 | file->basedir = flist_dir; |
c627d613 | 888 | |
3ec4dd97 | 889 | if (!S_ISDIR(st.st_mode)) |
a800434a | 890 | stats.total_size += st.st_size; |
c627d613 | 891 | |
3ec4dd97 | 892 | return file; |
c627d613 AT |
893 | } |
894 | ||
895 | ||
ebed4c3a | 896 | void send_file_name(int f, struct file_list *flist, char *fname, |
1ef00d20 | 897 | int recursive, unsigned short base_flags) |
c627d613 | 898 | { |
ebed4c3a | 899 | struct file_struct *file; |
b7736c79 | 900 | char fbuf[MAXPATHLEN]; |
429f9828 | 901 | extern int delete_excluded; |
ebed4c3a | 902 | |
429f9828 | 903 | /* f is set to -1 when calculating deletion file list */ |
a289addd | 904 | file = make_file(fname, |
429f9828 WD |
905 | f == -1 && delete_excluded? SERVER_EXCLUDES |
906 | : ALL_EXCLUDES); | |
ebed4c3a MP |
907 | |
908 | if (!file) | |
909 | return; | |
910 | ||
db719fb0 | 911 | maybe_emit_filelist_progress(flist); |
ebed4c3a | 912 | |
d9d6bc52 | 913 | flist_expand(flist); |
ebed4c3a | 914 | |
64c3523a | 915 | if (write_batch) |
d01d15e0 | 916 | file->flags |= FLAG_TOP_DIR; |
ebed4c3a | 917 | |
c120ff37 | 918 | if (file->basename[0]) { |
ebed4c3a MP |
919 | flist->files[flist->count++] = file; |
920 | send_file_entry(file, f, base_flags); | |
921 | } | |
922 | ||
4844449a WD |
923 | if (recursive && S_ISDIR(file->mode) |
924 | && !(file->flags & FLAG_MOUNT_POINT)) { | |
d01d15e0 | 925 | struct exclude_struct **last_exclude_list = local_exclude_list; |
882e6893 | 926 | send_directory(f, flist, f_name_to(file, fbuf)); |
ebed4c3a MP |
927 | local_exclude_list = last_exclude_list; |
928 | return; | |
929 | } | |
c627d613 AT |
930 | } |
931 | ||
932 | ||
ebed4c3a | 933 | static void send_directory(int f, struct file_list *flist, char *dir) |
c627d613 | 934 | { |
3ec4dd97 AT |
935 | DIR *d; |
936 | struct dirent *di; | |
937 | char fname[MAXPATHLEN]; | |
eddd5d12 | 938 | unsigned int offset; |
3ec4dd97 AT |
939 | char *p; |
940 | ||
941 | d = opendir(dir); | |
942 | if (!d) { | |
06c28400 | 943 | io_error |= IOERR_GENERAL; |
ea42541f WD |
944 | rprintf(FERROR, "opendir %s failed: %s\n", |
945 | full_fname(dir), strerror(errno)); | |
3ec4dd97 AT |
946 | return; |
947 | } | |
c627d613 | 948 | |
eddd5d12 WD |
949 | offset = strlcpy(fname, dir, MAXPATHLEN); |
950 | p = fname + offset; | |
951 | if (offset >= MAXPATHLEN || p[-1] != '/') { | |
952 | if (offset >= MAXPATHLEN - 1) { | |
06c28400 | 953 | io_error |= IOERR_GENERAL; |
ea42541f WD |
954 | rprintf(FERROR, "skipping long-named directory: %s\n", |
955 | full_fname(fname)); | |
3ec4dd97 AT |
956 | closedir(d); |
957 | return; | |
958 | } | |
eddd5d12 WD |
959 | *p++ = '/'; |
960 | offset++; | |
3ec4dd97 | 961 | } |
c627d613 | 962 | |
3d913675 AT |
963 | local_exclude_list = NULL; |
964 | ||
3ec4dd97 | 965 | if (cvs_exclude) { |
eddd5d12 WD |
966 | if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset) |
967 | < MAXPATHLEN - offset) | |
57469f6c | 968 | add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE); |
eddd5d12 | 969 | else { |
06c28400 | 970 | io_error |= IOERR_GENERAL; |
ebed4c3a MP |
971 | rprintf(FINFO, |
972 | "cannot cvs-exclude in long-named directory %s\n", | |
ea42541f | 973 | full_fname(fname)); |
3ec4dd97 | 974 | } |
ebed4c3a MP |
975 | } |
976 | ||
6a7cc46c | 977 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
d6e6ecbd | 978 | char *dname = d_name(di); |
6a7cc46c S |
979 | if (dname[0] == '.' && (dname[1] == '\0' |
980 | || (dname[1] == '.' && dname[2] == '\0'))) | |
3ec4dd97 | 981 | continue; |
eddd5d12 WD |
982 | if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset) |
983 | send_file_name(f, flist, fname, recurse, 0); | |
984 | else { | |
985 | io_error |= IOERR_GENERAL; | |
986 | rprintf(FINFO, | |
987 | "cannot send long-named file %s\n", | |
988 | full_fname(fname)); | |
989 | } | |
3ec4dd97 | 990 | } |
6a7cc46c | 991 | if (errno) { |
06c28400 | 992 | io_error |= IOERR_GENERAL; |
6a7cc46c | 993 | rprintf(FERROR, "readdir(%s): (%d) %s\n", |
eddd5d12 | 994 | dir, errno, strerror(errno)); |
6a7cc46c | 995 | } |
c627d613 | 996 | |
429f9828 WD |
997 | if (local_exclude_list) |
998 | free_exclude_list(&local_exclude_list); /* Zeros pointer too */ | |
3d913675 | 999 | |
3ec4dd97 | 1000 | closedir(d); |
c627d613 AT |
1001 | } |
1002 | ||
1003 | ||
c4fea82f | 1004 | /** |
429f9828 WD |
1005 | * The delete_files() function in receiver.c sets f to -1 so that we just |
1006 | * construct the file list in memory without sending it over the wire. It | |
1007 | * also has the side-effect of ignoring user-excludes if delete_excluded | |
1008 | * is set (so that the delete list includes user-excluded files). | |
c4fea82f | 1009 | **/ |
ebed4c3a | 1010 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
c627d613 | 1011 | { |
24d0fcde | 1012 | int l; |
bcacc18b | 1013 | STRUCT_STAT st; |
808c57c3 | 1014 | char *p, *dir, olddir[sizeof curr_dir]; |
ebed4c3a | 1015 | char lastpath[MAXPATHLEN] = ""; |
649d65ed | 1016 | struct file_list *flist; |
a800434a | 1017 | int64 start_write; |
24d0fcde | 1018 | int use_ff_fd = 0; |
649d65ed | 1019 | |
1bbd10fe DD |
1020 | if (show_filelist_p() && f != -1) |
1021 | start_filelist_progress("building file list"); | |
c627d613 | 1022 | |
a800434a AT |
1023 | start_write = stats.total_written; |
1024 | ||
3d382777 | 1025 | flist = flist_new(); |
c627d613 | 1026 | |
d6dead6b | 1027 | if (f != -1) { |
76c21947 | 1028 | io_start_buffering_out(f); |
24d0fcde | 1029 | if (filesfrom_fd >= 0) { |
808c57c3 | 1030 | if (argv[0] && !push_dir(argv[0])) { |
ea42541f WD |
1031 | rprintf(FERROR, "push_dir %s failed: %s\n", |
1032 | full_fname(argv[0]), strerror(errno)); | |
24d0fcde WD |
1033 | exit_cleanup(RERR_FILESELECT); |
1034 | } | |
1035 | use_ff_fd = 1; | |
1036 | } | |
d6dead6b AT |
1037 | } |
1038 | ||
24d0fcde | 1039 | while (1) { |
fc638474 DD |
1040 | char fname2[MAXPATHLEN]; |
1041 | char *fname = fname2; | |
c627d613 | 1042 | |
24d0fcde WD |
1043 | if (use_ff_fd) { |
1044 | if (read_filesfrom_line(filesfrom_fd, fname) == 0) | |
1045 | break; | |
1046 | sanitize_path(fname, NULL); | |
1047 | } else { | |
1048 | if (argc-- == 0) | |
1049 | break; | |
1050 | strlcpy(fname, *argv++, MAXPATHLEN); | |
1051 | if (sanitize_paths) | |
1052 | sanitize_path(fname, NULL); | |
1053 | } | |
c627d613 | 1054 | |
649d65ed | 1055 | l = strlen(fname); |
6931c138 WD |
1056 | if (fname[l - 1] == '/') { |
1057 | if (l == 2 && fname[0] == '.') { | |
1058 | /* Turn "./" into just "." rather than "./." */ | |
1059 | fname[1] = '\0'; | |
eddd5d12 WD |
1060 | } else if (l < MAXPATHLEN) { |
1061 | fname[l++] = '.'; | |
1062 | fname[l] = '\0'; | |
53f821f1 | 1063 | } |
649d65ed | 1064 | } |
c627d613 | 1065 | |
ebed4c3a | 1066 | if (link_stat(fname, &st) != 0) { |
f76933b1 | 1067 | if (f != -1) { |
06c28400 | 1068 | io_error |= IOERR_GENERAL; |
ea42541f WD |
1069 | rprintf(FERROR, "link_stat %s failed: %s\n", |
1070 | full_fname(fname), strerror(errno)); | |
f76933b1 | 1071 | } |
649d65ed AT |
1072 | continue; |
1073 | } | |
c627d613 | 1074 | |
24d0fcde | 1075 | if (S_ISDIR(st.st_mode) && !recurse && !files_from) { |
ebed4c3a | 1076 | rprintf(FINFO, "skipping directory %s\n", fname); |
649d65ed AT |
1077 | continue; |
1078 | } | |
c627d613 | 1079 | |
649d65ed | 1080 | dir = NULL; |
808c57c3 | 1081 | olddir[0] = '\0'; |
649d65ed AT |
1082 | |
1083 | if (!relative_paths) { | |
ebed4c3a | 1084 | p = strrchr(fname, '/'); |
649d65ed AT |
1085 | if (p) { |
1086 | *p = 0; | |
ebed4c3a | 1087 | if (p == fname) |
649d65ed AT |
1088 | dir = "/"; |
1089 | else | |
ebed4c3a MP |
1090 | dir = fname; |
1091 | fname = p + 1; | |
649d65ed | 1092 | } |
24d0fcde | 1093 | } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) { |
649d65ed AT |
1094 | /* this ensures we send the intermediate directories, |
1095 | thus getting their permissions right */ | |
2154309a | 1096 | char *lp = lastpath, *fn = fname, *slash = fname; |
649d65ed | 1097 | *p = 0; |
2154309a WD |
1098 | /* Skip any initial directories in our path that we |
1099 | * have in common with lastpath. */ | |
1100 | while (*fn && *lp == *fn) { | |
1101 | if (*fn == '/') | |
1102 | slash = fn; | |
1103 | lp++, fn++; | |
1104 | } | |
1105 | *p = '/'; | |
1106 | if (fn != p || (*lp && *lp != '/')) { | |
1107 | int copy_links_saved = copy_links; | |
1108 | int recurse_saved = recurse; | |
1109 | copy_links = copy_unsafe_links; | |
1110 | /* set recurse to 1 to prevent make_file | |
1111 | * from ignoring directory, but still | |
1112 | * turn off the recursive parameter to | |
1113 | * send_file_name */ | |
1114 | recurse = 1; | |
1115 | while ((slash = strchr(slash+1, '/')) != 0) { | |
1116 | *slash = 0; | |
1117 | send_file_name(f, flist, fname, 0, 0); | |
1118 | *slash = '/'; | |
649d65ed | 1119 | } |
2154309a WD |
1120 | copy_links = copy_links_saved; |
1121 | recurse = recurse_saved; | |
1122 | *p = 0; | |
1123 | strlcpy(lastpath, fname, sizeof lastpath); | |
649d65ed AT |
1124 | *p = '/'; |
1125 | } | |
1126 | } | |
ebed4c3a | 1127 | |
649d65ed AT |
1128 | if (!*fname) |
1129 | fname = "."; | |
ebed4c3a | 1130 | |
649d65ed | 1131 | if (dir && *dir) { |
882e6893 WD |
1132 | static char *lastdir; |
1133 | static int lastdir_len; | |
1134 | ||
808c57c3 | 1135 | strcpy(olddir, curr_dir); /* can't overflow */ |
5243c216 | 1136 | |
808c57c3 | 1137 | if (!push_dir(dir)) { |
06c28400 | 1138 | io_error |= IOERR_GENERAL; |
ea42541f WD |
1139 | rprintf(FERROR, "push_dir %s failed: %s\n", |
1140 | full_fname(dir), strerror(errno)); | |
649d65ed AT |
1141 | continue; |
1142 | } | |
5243c216 | 1143 | |
882e6893 WD |
1144 | if (lastdir && strcmp(lastdir, dir) == 0) { |
1145 | flist_dir = lastdir; | |
1146 | flist_dir_len = lastdir_len; | |
1147 | } else { | |
882e6893 WD |
1148 | flist_dir = lastdir = strdup(dir); |
1149 | flist_dir_len = lastdir_len = strlen(dir); | |
1150 | } | |
2bca43f6 | 1151 | } |
ebed4c3a | 1152 | |
2bca43f6 DD |
1153 | if (one_file_system) |
1154 | set_filesystem(fname); | |
1155 | ||
d01d15e0 | 1156 | send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR); |
2bca43f6 | 1157 | |
808c57c3 | 1158 | if (olddir[0]) { |
649d65ed | 1159 | flist_dir = NULL; |
882e6893 | 1160 | flist_dir_len = 0; |
808c57c3 | 1161 | if (!pop_dir(olddir)) { |
ea42541f WD |
1162 | rprintf(FERROR, "pop_dir %s failed: %s\n", |
1163 | full_fname(dir), strerror(errno)); | |
65417579 | 1164 | exit_cleanup(RERR_FILESELECT); |
649d65ed | 1165 | } |
649d65ed | 1166 | } |
649d65ed | 1167 | } |
dc5ddbcc | 1168 | |
983b1ed3 | 1169 | if (f != -1) { |
ebed4c3a | 1170 | send_file_entry(NULL, f, 0); |
c627d613 | 1171 | |
983b1ed3 WD |
1172 | if (show_filelist_p()) |
1173 | finish_filelist_progress(flist); | |
1174 | } | |
ebed4c3a | 1175 | |
827c37f6 | 1176 | clean_flist(flist, 0, 0); |
ebed4c3a | 1177 | |
785db4ce WD |
1178 | if (f != -1) { |
1179 | /* Now send the uid/gid list. This was introduced in | |
1180 | * protocol version 15 */ | |
649d65ed | 1181 | send_uid_list(f); |
f6c34742 | 1182 | |
983b1ed3 | 1183 | /* send the io_error flag */ |
ebed4c3a | 1184 | write_int(f, lp_ignore_errors(module_id) ? 0 : io_error); |
6ba9279f | 1185 | |
a261989c | 1186 | io_end_buffering(); |
a800434a AT |
1187 | stats.flist_size = stats.total_written - start_write; |
1188 | stats.num_files = flist->count; | |
64c3523a | 1189 | if (write_batch) |
ebed4c3a | 1190 | write_batch_flist_info(flist->count, flist->files); |
d6dead6b AT |
1191 | } |
1192 | ||
17faa41c | 1193 | if (verbose > 2) |
ebed4c3a | 1194 | rprintf(FINFO, "send_file_list done\n"); |
17faa41c | 1195 | |
649d65ed | 1196 | return flist; |
c627d613 AT |
1197 | } |
1198 | ||
1199 | ||
1200 | struct file_list *recv_file_list(int f) | |
1201 | { | |
ebed4c3a | 1202 | struct file_list *flist; |
1ef00d20 | 1203 | unsigned short flags; |
ebed4c3a MP |
1204 | int64 start_read; |
1205 | extern int list_only; | |
c627d613 | 1206 | |
1bbd10fe DD |
1207 | if (show_filelist_p()) |
1208 | start_filelist_progress("receiving file list"); | |
c627d613 | 1209 | |
ebed4c3a | 1210 | start_read = stats.total_read; |
a800434a | 1211 | |
58cadc86 | 1212 | flist = new(struct file_list); |
ebed4c3a MP |
1213 | if (!flist) |
1214 | goto oom; | |
c627d613 | 1215 | |
ebed4c3a MP |
1216 | flist->count = 0; |
1217 | flist->malloced = 1000; | |
58cadc86 | 1218 | flist->files = new_array(struct file_struct *, flist->malloced); |
ebed4c3a MP |
1219 | if (!flist->files) |
1220 | goto oom; | |
c627d613 AT |
1221 | |
1222 | ||
1ef00d20 | 1223 | while ((flags = read_byte(f)) != 0) { |
5d2c5c4c | 1224 | int i = flist->count; |
dbda5fbf | 1225 | |
d9d6bc52 | 1226 | flist_expand(flist); |
c627d613 | 1227 | |
d01d15e0 | 1228 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
75bc8600 | 1229 | flags |= read_byte(f) << 8; |
ebed4c3a | 1230 | receive_file_entry(&flist->files[i], flags, f); |
c627d613 | 1231 | |
ebed4c3a MP |
1232 | if (S_ISREG(flist->files[i]->mode)) |
1233 | stats.total_size += flist->files[i]->length; | |
c627d613 | 1234 | |
ebed4c3a | 1235 | flist->count++; |
c627d613 | 1236 | |
db719fb0 | 1237 | maybe_emit_filelist_progress(flist); |
1bbd10fe | 1238 | |
8018edd3 | 1239 | if (verbose > 2) { |
ebed4c3a MP |
1240 | rprintf(FINFO, "recv_file_name(%s)\n", |
1241 | f_name(flist->files[i])); | |
8018edd3 | 1242 | } |
ebed4c3a | 1243 | } |
5911fee5 | 1244 | receive_file_entry(NULL, 0, 0); /* Signal that we're done. */ |
c627d613 | 1245 | |
ebed4c3a MP |
1246 | if (verbose > 2) |
1247 | rprintf(FINFO, "received %d names\n", flist->count); | |
c627d613 | 1248 | |
b7736c79 | 1249 | if (show_filelist_p()) |
1bbd10fe | 1250 | finish_filelist_progress(flist); |
a06d19e3 | 1251 | |
983b1ed3 WD |
1252 | clean_flist(flist, relative_paths, 1); |
1253 | ||
785db4ce WD |
1254 | if (f != -1) { |
1255 | /* Now send the uid/gid list. This was introduced in | |
1256 | * protocol version 15 */ | |
ebed4c3a | 1257 | recv_uid_list(f, flist); |
f6c34742 | 1258 | |
785db4ce WD |
1259 | if (!read_batch) { |
1260 | /* Recv the io_error flag */ | |
1261 | if (lp_ignore_errors(module_id) || ignore_errors) | |
1262 | read_int(f); | |
1263 | else | |
1264 | io_error |= read_int(f); | |
1265 | } | |
ebed4c3a | 1266 | } |
6ba9279f | 1267 | |
ebed4c3a MP |
1268 | if (list_only) { |
1269 | int i; | |
b7736c79 | 1270 | for (i = 0; i < flist->count; i++) |
ebed4c3a | 1271 | list_file_entry(flist->files[i]); |
ebed4c3a | 1272 | } |
f7632fc6 | 1273 | |
ebed4c3a MP |
1274 | if (verbose > 2) |
1275 | rprintf(FINFO, "recv_file_list done\n"); | |
17faa41c | 1276 | |
ebed4c3a MP |
1277 | stats.flist_size = stats.total_read - start_read; |
1278 | stats.num_files = flist->count; | |
a800434a | 1279 | |
ebed4c3a | 1280 | return flist; |
c627d613 | 1281 | |
ebed4c3a MP |
1282 | oom: |
1283 | out_of_memory("recv_file_list"); | |
1284 | return NULL; /* not reached */ | |
c627d613 AT |
1285 | } |
1286 | ||
1287 | ||
fa45cda1 | 1288 | int file_compare(struct file_struct **file1, struct file_struct **file2) |
c627d613 | 1289 | { |
fa45cda1 S |
1290 | struct file_struct *f1 = *file1; |
1291 | struct file_struct *f2 = *file2; | |
1292 | ||
1293 | if (!f1->basename && !f2->basename) | |
ebed4c3a | 1294 | return 0; |
fa45cda1 | 1295 | if (!f1->basename) |
ebed4c3a | 1296 | return -1; |
fa45cda1 | 1297 | if (!f2->basename) |
ebed4c3a | 1298 | return 1; |
fa45cda1 S |
1299 | if (f1->dirname == f2->dirname) |
1300 | return u_strcmp(f1->basename, f2->basename); | |
1301 | return f_name_cmp(f1, f2); | |
c627d613 AT |
1302 | } |
1303 | ||
1304 | ||
ebed4c3a | 1305 | int flist_find(struct file_list *flist, struct file_struct *f) |
c627d613 | 1306 | { |
ebed4c3a | 1307 | int low = 0, high = flist->count - 1; |
d966ee25 | 1308 | |
ca23c51a WD |
1309 | while (high >= 0 && !flist->files[high]->basename) high--; |
1310 | ||
1311 | if (high < 0) | |
ebed4c3a | 1312 | return -1; |
d966ee25 AT |
1313 | |
1314 | while (low != high) { | |
ebed4c3a | 1315 | int mid = (low + high) / 2; |
b7736c79 | 1316 | int ret = file_compare(&flist->files[flist_up(flist, mid)],&f); |
ebed4c3a MP |
1317 | if (ret == 0) |
1318 | return flist_up(flist, mid); | |
b7736c79 | 1319 | if (ret > 0) |
ebed4c3a | 1320 | high = mid; |
b7736c79 | 1321 | else |
ebed4c3a | 1322 | low = mid + 1; |
d966ee25 AT |
1323 | } |
1324 | ||
ebed4c3a MP |
1325 | if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0) |
1326 | return flist_up(flist, low); | |
d966ee25 | 1327 | return -1; |
c627d613 AT |
1328 | } |
1329 | ||
1330 | ||
3ec4dd97 | 1331 | /* |
a289addd WD |
1332 | * Free up any resources a file_struct has allocated, and optionally free |
1333 | * it up as well. | |
3ec4dd97 | 1334 | */ |
a289addd | 1335 | void free_file(struct file_struct *file, int free_the_struct) |
c627d613 | 1336 | { |
a289addd WD |
1337 | if (free_the_struct) |
1338 | free(file); | |
1339 | else | |
96eeda03 | 1340 | memset(file, 0, min_file_struct_len); |
3ec4dd97 | 1341 | } |
c627d613 | 1342 | |
c627d613 | 1343 | |
3d382777 AT |
1344 | /* |
1345 | * allocate a new file list | |
1346 | */ | |
1e34e4b7 | 1347 | struct file_list *flist_new(void) |
3d382777 AT |
1348 | { |
1349 | struct file_list *flist; | |
1350 | ||
58cadc86 | 1351 | flist = new(struct file_list); |
ebed4c3a MP |
1352 | if (!flist) |
1353 | out_of_memory("send_file_list"); | |
3d382777 | 1354 | |
ebed4c3a | 1355 | flist->count = 0; |
d9d6bc52 MP |
1356 | flist->malloced = 0; |
1357 | flist->files = NULL; | |
1358 | ||
3d382777 AT |
1359 | return flist; |
1360 | } | |
ebed4c3a | 1361 | |
3ec4dd97 AT |
1362 | /* |
1363 | * free up all elements in a flist | |
1364 | */ | |
1365 | void flist_free(struct file_list *flist) | |
1366 | { | |
1367 | int i; | |
a289addd WD |
1368 | for (i = 1; i < flist->count; i++) |
1369 | free_file(flist->files[i], FREE_STRUCT); | |
3ec4dd97 | 1370 | free(flist->files); |
3ec4dd97 | 1371 | free(flist); |
c627d613 AT |
1372 | } |
1373 | ||
1374 | ||
1375 | /* | |
1376 | * This routine ensures we don't have any duplicate names in our file list. | |
dbda5fbf | 1377 | * duplicate names can cause corruption because of the pipelining |
c627d613 | 1378 | */ |
827c37f6 | 1379 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups) |
c627d613 | 1380 | { |
6931c138 | 1381 | int i, prev_i = 0; |
c627d613 | 1382 | |
ebed4c3a | 1383 | if (!flist || flist->count == 0) |
3ec4dd97 | 1384 | return; |
3ec4dd97 | 1385 | |
ebed4c3a | 1386 | qsort(flist->files, flist->count, |
bd6abc49 | 1387 | sizeof flist->files[0], (int (*)()) file_compare); |
ebed4c3a | 1388 | |
827c37f6 | 1389 | for (i = no_dups? 0 : flist->count; i < flist->count; i++) { |
b91b50c0 | 1390 | if (flist->files[i]->basename) { |
6931c138 | 1391 | prev_i = i; |
b91b50c0 WD |
1392 | break; |
1393 | } | |
1394 | } | |
1395 | while (++i < flist->count) { | |
1396 | if (!flist->files[i]->basename) | |
1397 | continue; | |
8018edd3 | 1398 | if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) { |
b91b50c0 | 1399 | if (verbose > 1 && !am_server) { |
ebed4c3a MP |
1400 | rprintf(FINFO, |
1401 | "removing duplicate name %s from file list %d\n", | |
8018edd3 | 1402 | f_name(flist->files[i]), i); |
b91b50c0 | 1403 | } |
6931c138 WD |
1404 | /* Make sure that if we unduplicate '.', that we don't |
1405 | * lose track of a user-specified starting point (or | |
1406 | * else deletions will mysteriously fail with -R). */ | |
d01d15e0 WD |
1407 | if (flist->files[i]->flags & FLAG_TOP_DIR) |
1408 | flist->files[prev_i]->flags |= FLAG_TOP_DIR; | |
a289addd | 1409 | free_file(flist->files[i], CLEAR_STRUCT); |
728d0922 | 1410 | } else |
6931c138 | 1411 | prev_i = i; |
3ec4dd97 | 1412 | } |
0199b05f AT |
1413 | |
1414 | if (strip_root) { | |
1415 | /* we need to strip off the root directory in the case | |
1416 | of relative paths, but this must be done _after_ | |
1417 | the sorting phase */ | |
ebed4c3a | 1418 | for (i = 0; i < flist->count; i++) { |
0199b05f AT |
1419 | if (flist->files[i]->dirname && |
1420 | flist->files[i]->dirname[0] == '/') { | |
1421 | memmove(&flist->files[i]->dirname[0], | |
1422 | &flist->files[i]->dirname[1], | |
1423 | strlen(flist->files[i]->dirname)); | |
1424 | } | |
ebed4c3a MP |
1425 | |
1426 | if (flist->files[i]->dirname && | |
0199b05f AT |
1427 | !flist->files[i]->dirname[0]) { |
1428 | flist->files[i]->dirname = NULL; | |
1429 | } | |
1430 | } | |
1431 | } | |
1432 | ||
ebed4c3a MP |
1433 | if (verbose <= 3) |
1434 | return; | |
0199b05f | 1435 | |
ebed4c3a | 1436 | for (i = 0; i < flist->count; i++) { |
f05f993e WD |
1437 | char uidbuf[16], gidbuf[16]; |
1438 | struct file_struct *file = flist->files[i]; | |
1439 | if (am_root && preserve_uid) | |
1440 | sprintf(uidbuf, " uid=%ld", (long)file->uid); | |
1441 | else | |
1442 | *uidbuf = '\0'; | |
1443 | if (preserve_gid) | |
1444 | sprintf(gidbuf, " gid=%ld", (long)file->gid); | |
1445 | else | |
1446 | *gidbuf = '\0'; | |
1447 | rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n", | |
1448 | who_am_i(), i, NS(file->basedir), NS(file->dirname), | |
1449 | NS(file->basename), (int) file->mode, | |
1450 | (double) file->length, uidbuf, gidbuf); | |
0199b05f | 1451 | } |
3ec4dd97 AT |
1452 | } |
1453 | ||
1454 | ||
8018edd3 WD |
1455 | enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE }; |
1456 | ||
1457 | /* Compare the names of two file_struct entities, just like strcmp() | |
1458 | * would do if it were operating on the joined strings. We assume | |
1459 | * that there are no 0-length strings. | |
3ec4dd97 | 1460 | */ |
8018edd3 | 1461 | int f_name_cmp(struct file_struct *f1, struct file_struct *f2) |
3ec4dd97 | 1462 | { |
8018edd3 WD |
1463 | int dif; |
1464 | const uchar *c1, *c2; | |
1ef00d20 | 1465 | enum fnc_state state1, state2; |
8018edd3 WD |
1466 | |
1467 | if (!f1 || !f1->basename) { | |
1468 | if (!f2 || !f2->basename) | |
1469 | return 0; | |
1470 | return -1; | |
1471 | } | |
1472 | if (!f2 || !f2->basename) | |
1473 | return 1; | |
1474 | ||
e90b8ace | 1475 | if (!(c1 = (uchar*)f1->dirname)) { |
8018edd3 | 1476 | state1 = fnc_BASE; |
e90b8ace | 1477 | c1 = (uchar*)f1->basename; |
728d0922 | 1478 | } else |
1ef00d20 | 1479 | state1 = fnc_DIR; |
e90b8ace | 1480 | if (!(c2 = (uchar*)f2->dirname)) { |
8018edd3 | 1481 | state2 = fnc_BASE; |
e90b8ace | 1482 | c2 = (uchar*)f2->basename; |
728d0922 | 1483 | } else |
1ef00d20 | 1484 | state2 = fnc_DIR; |
8018edd3 WD |
1485 | |
1486 | while (1) { | |
1487 | if ((dif = (int)*c1 - (int)*c2) != 0) | |
1488 | break; | |
1489 | if (!*++c1) { | |
1490 | switch (state1) { | |
1491 | case fnc_DIR: | |
1492 | state1 = fnc_SLASH; | |
e90b8ace | 1493 | c1 = (uchar*)"/"; |
8018edd3 WD |
1494 | break; |
1495 | case fnc_SLASH: | |
1496 | state1 = fnc_BASE; | |
e90b8ace | 1497 | c1 = (uchar*)f1->basename; |
8018edd3 WD |
1498 | break; |
1499 | case fnc_BASE: | |
1500 | break; | |
1501 | } | |
1502 | } | |
1503 | if (!*++c2) { | |
1504 | switch (state2) { | |
1505 | case fnc_DIR: | |
1506 | state2 = fnc_SLASH; | |
e90b8ace | 1507 | c2 = (uchar*)"/"; |
8018edd3 WD |
1508 | break; |
1509 | case fnc_SLASH: | |
1510 | state2 = fnc_BASE; | |
e90b8ace | 1511 | c2 = (uchar*)f2->basename; |
8018edd3 WD |
1512 | break; |
1513 | case fnc_BASE: | |
1514 | if (!*c1) | |
1515 | return 0; | |
1516 | break; | |
1517 | } | |
1518 | } | |
1519 | } | |
1520 | ||
1521 | return dif; | |
1522 | } | |
1523 | ||
3ec4dd97 | 1524 | |
8018edd3 | 1525 | /* Return a copy of the full filename of a flist entry, using the indicated |
882e6893 WD |
1526 | * buffer. No size-checking is done because we checked the size when creating |
1527 | * the file_struct entry. | |
8018edd3 | 1528 | */ |
882e6893 | 1529 | char *f_name_to(struct file_struct *f, char *fbuf) |
8018edd3 | 1530 | { |
ebed4c3a MP |
1531 | if (!f || !f->basename) |
1532 | return NULL; | |
3ec4dd97 | 1533 | |
3ec4dd97 | 1534 | if (f->dirname) { |
882e6893 WD |
1535 | int len = strlen(f->dirname); |
1536 | memcpy(fbuf, f->dirname, len); | |
1537 | fbuf[len] = '/'; | |
1538 | strcpy(fbuf + len + 1, f->basename); | |
8018edd3 | 1539 | } else |
882e6893 | 1540 | strcpy(fbuf, f->basename); |
b7736c79 | 1541 | return fbuf; |
8018edd3 | 1542 | } |
e03dfae5 | 1543 | |
3ec4dd97 | 1544 | |
8018edd3 WD |
1545 | /* Like f_name_to(), but we rotate through 5 static buffers of our own. |
1546 | */ | |
1547 | char *f_name(struct file_struct *f) | |
1548 | { | |
1549 | static char names[5][MAXPATHLEN]; | |
1550 | static unsigned int n; | |
1551 | ||
1552 | n = (n + 1) % (sizeof names / sizeof names[0]); | |
1553 | ||
882e6893 | 1554 | return f_name_to(f, names[n]); |
c627d613 | 1555 | } |