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