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