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