1/*
2 * Generate and receive file lists.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23
24#include "rsync.h"
25#ifdef HAVE_COPYFILE_H
26#include <copyfile.h>
27#endif	/* HAVE_COPYFILE_H */
28
29extern int verbose;
30extern int list_only;
31extern int am_root;
32extern int am_server;
33extern int am_daemon;
34extern int am_sender;
35extern int do_progress;
36extern int always_checksum;
37extern int module_id;
38extern int ignore_errors;
39extern int numeric_ids;
40extern int recurse;
41extern int xfer_dirs;
42extern int filesfrom_fd;
43extern int one_file_system;
44extern int copy_dirlinks;
45extern int keep_dirlinks;
46extern int preserve_links;
47extern int preserve_hard_links;
48extern int preserve_devices;
49extern int preserve_specials;
50extern int preserve_uid;
51extern int preserve_gid;
52extern int relative_paths;
53extern int implied_dirs;
54extern int prune_empty_dirs;
55extern int copy_links;
56extern int copy_unsafe_links;
57extern int protocol_version;
58extern int sanitize_paths;
59extern int munge_symlinks;
60extern struct stats stats;
61extern struct file_list *the_file_list;
62
63extern char curr_dir[MAXPATHLEN];
64
65extern struct chmod_mode_struct *chmod_modes;
66
67extern struct filter_list_struct filter_list;
68extern struct filter_list_struct server_filter_list;
69#ifdef EA_SUPPORT
70extern int extended_attributes;
71#endif	/* EA_SUPPORT */
72int io_error;
73int checksum_len;
74dev_t filesystem_dev; /* used to implement -x */
75unsigned int file_struct_len;
76
77static char empty_sum[MD4_SUM_LENGTH];
78static int flist_count_offset;
79
80static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
81static void output_flist(struct file_list *flist);
82
83void init_flist(void)
84{
85	struct file_struct f;
86
87	/* Figure out how big the file_struct is without trailing padding */
88	file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
89	checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
90}
91
92static int show_filelist_p(void)
93{
94	return verbose && xfer_dirs && !am_server;
95}
96
97static void start_filelist_progress(char *kind)
98{
99	rprintf(FCLIENT, "%s ... ", kind);
100	if (verbose > 1 || do_progress)
101		rprintf(FCLIENT, "\n");
102	rflush(FINFO);
103}
104
105static void emit_filelist_progress(int count)
106{
107	rprintf(FCLIENT, " %d files...\r", count);
108}
109
110static void maybe_emit_filelist_progress(int count)
111{
112	if (do_progress && show_filelist_p() && (count % 100) == 0)
113		emit_filelist_progress(count);
114}
115
116static void finish_filelist_progress(const struct file_list *flist)
117{
118	if (do_progress) {
119		/* This overwrites the progress line */
120		rprintf(FINFO, "%d file%sto consider\n",
121			flist->count, flist->count == 1 ? " " : "s ");
122	} else
123		rprintf(FINFO, "done\n");
124}
125
126void show_flist_stats(void)
127{
128	/* Nothing yet */
129}
130
131static void list_file_entry(struct file_struct *f)
132{
133	char permbuf[PERMSTRING_SIZE];
134
135	if (!f->basename) {
136		/* this can happen if duplicate names were removed */
137		return;
138	}
139
140	permstring(permbuf, f->mode);
141
142#ifdef SUPPORT_LINKS
143	if (preserve_links && S_ISLNK(f->mode)) {
144		rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
145			permbuf,
146			(double)f->length, timestring(f->modtime),
147			f_name(f, NULL), f->u.link);
148	} else
149#endif
150	{
151		rprintf(FINFO, "%s %11.0f %s %s\n",
152			permbuf,
153			(double)f->length, timestring(f->modtime),
154			f_name(f, NULL));
155	}
156}
157
158/* Stat either a symlink or its referent, depending on the settings of
159 * copy_links, copy_unsafe_links, etc.  Returns -1 on error, 0 on success.
160 *
161 * If path is the name of a symlink, then the linkbuf buffer (which must hold
162 * MAXPATHLEN chars) will be set to the symlink's target string.
163 *
164 * The stat structure pointed to by stp will contain information about the
165 * link or the referent as appropriate, if they exist. */
166static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
167{
168#ifdef SUPPORT_LINKS
169	if (link_stat(path, stp, copy_dirlinks) < 0)
170		return -1;
171	if (S_ISLNK(stp->st_mode)) {
172		int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
173		if (llen < 0)
174			return -1;
175		linkbuf[llen] = '\0';
176		if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
177			if (verbose > 1) {
178				rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
179					path, linkbuf);
180			}
181			return do_stat(path, stp);
182		}
183		if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
184		 && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
185			memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
186				llen - SYMLINK_PREFIX_LEN + 1);
187		}
188	}
189	return 0;
190#else
191	return do_stat(path, stp);
192#endif
193}
194
195int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
196{
197#ifdef SUPPORT_LINKS
198	if (copy_links)
199		return do_stat(path, stp);
200	if (do_lstat(path, stp) < 0)
201		return -1;
202	if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
203		STRUCT_STAT st;
204		if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
205			*stp = st;
206	}
207	return 0;
208#else
209	return do_stat(path, stp);
210#endif
211}
212
213/* This function is used to check if a file should be included/excluded
214 * from the list of files based on its name and type etc.  The value of
215 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
216static int is_excluded(char *fname, int is_dir, int filter_level)
217{
218#if 0 /* This currently never happens, so avoid a useless compare. */
219	if (filter_level == NO_FILTERS)
220		return 0;
221#endif
222	if (fname) {
223		/* never exclude '.', even if somebody does --exclude '*' */
224		if (fname[0] == '.' && !fname[1])
225			return 0;
226		/* Handle the -R version of the '.' dir. */
227		if (fname[0] == '/') {
228			int len = strlen(fname);
229			if (fname[len-1] == '.' && fname[len-2] == '/')
230				return 0;
231		}
232	}
233	if (server_filter_list.head
234	    && check_filter(&server_filter_list, fname, is_dir) < 0)
235		return 1;
236	if (filter_level != ALL_FILTERS)
237		return 0;
238	if (filter_list.head
239	    && check_filter(&filter_list, fname, is_dir) < 0)
240		return 1;
241	return 0;
242}
243
244static int to_wire_mode(mode_t mode)
245{
246#ifdef SUPPORT_LINKS
247#if _S_IFLNK != 0120000
248	if (S_ISLNK(mode))
249		return (mode & ~(_S_IFMT)) | 0120000;
250#endif
251#endif
252	return mode;
253}
254
255static mode_t from_wire_mode(int mode)
256{
257#if _S_IFLNK != 0120000
258	if ((mode & (_S_IFMT)) == 0120000)
259		return (mode & ~(_S_IFMT)) | _S_IFLNK;
260#endif
261	return mode;
262}
263
264static void send_directory(int f, struct file_list *flist,
265			   char *fbuf, int len);
266
267static char *flist_dir;
268static int flist_dir_len;
269
270
271/**
272 * Make sure @p flist is big enough to hold at least @p flist->count
273 * entries.
274 **/
275void flist_expand(struct file_list *flist)
276{
277	struct file_struct **new_ptr;
278
279	if (flist->count < flist->malloced)
280		return;
281
282	if (flist->malloced < FLIST_START)
283		flist->malloced = FLIST_START;
284	else if (flist->malloced >= FLIST_LINEAR)
285		flist->malloced += FLIST_LINEAR;
286	else
287		flist->malloced *= 2;
288
289	/*
290	 * In case count jumped or we are starting the list
291	 * with a known size just set it.
292	 */
293	if (flist->malloced < flist->count)
294		flist->malloced = flist->count;
295
296	new_ptr = realloc_array(flist->files, struct file_struct *,
297				flist->malloced);
298
299	if (verbose >= 2 && flist->malloced != FLIST_START) {
300		rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
301		    who_am_i(),
302		    (double)sizeof flist->files[0] * flist->malloced,
303		    (new_ptr == flist->files) ? " not" : "");
304	}
305
306	flist->files = new_ptr;
307
308	if (!flist->files)
309		out_of_memory("flist_expand");
310}
311
312static void send_file_entry(struct file_struct *file, int f)
313{
314	unsigned short flags;
315	static time_t modtime;
316	static mode_t mode;
317	static int64 dev;
318	static dev_t rdev;
319	static uint32 rdev_major;
320	static uid_t uid;
321	static gid_t gid;
322	static char lastname[MAXPATHLEN];
323	char fname[MAXPATHLEN];
324	int l1, l2;
325
326	if (f < 0)
327		return;
328
329	if (!file) {
330		write_byte(f, 0);
331		modtime = 0, mode = 0;
332		dev = 0, rdev = MAKEDEV(0, 0);
333		rdev_major = 0;
334		uid = 0, gid = 0;
335		*lastname = '\0';
336		return;
337	}
338
339	f_name(file, fname);
340
341	flags = file->flags & XMIT_TOP_DIR;
342
343	if (file->mode == mode)
344		flags |= XMIT_SAME_MODE;
345	else
346		mode = file->mode;
347	if ((preserve_devices && IS_DEVICE(mode))
348	 || (preserve_specials && IS_SPECIAL(mode))) {
349		if (protocol_version < 28) {
350			if (file->u.rdev == rdev)
351				flags |= XMIT_SAME_RDEV_pre28;
352			else
353				rdev = file->u.rdev;
354		} else {
355			rdev = file->u.rdev;
356			if ((uint32)major(rdev) == rdev_major)
357				flags |= XMIT_SAME_RDEV_MAJOR;
358			else
359				rdev_major = major(rdev);
360			if ((uint32)minor(rdev) <= 0xFFu)
361				flags |= XMIT_RDEV_MINOR_IS_SMALL;
362		}
363	} else if (protocol_version < 28)
364		rdev = MAKEDEV(0, 0);
365	if (file->uid == uid)
366		flags |= XMIT_SAME_UID;
367	else
368		uid = file->uid;
369	if (file->gid == gid)
370		flags |= XMIT_SAME_GID;
371	else
372		gid = file->gid;
373	if (file->modtime == modtime)
374		flags |= XMIT_SAME_TIME;
375	else
376		modtime = file->modtime;
377
378#ifdef SUPPORT_HARD_LINKS
379	if (file->link_u.idev) {
380		if (file->F_DEV == dev) {
381			if (protocol_version >= 28)
382				flags |= XMIT_SAME_DEV;
383		} else
384			dev = file->F_DEV;
385		flags |= XMIT_HAS_IDEV_DATA;
386	}
387#endif
388
389	for (l1 = 0;
390	    lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
391	    l1++) {}
392	l2 = strlen(fname+l1);
393
394	if (l1 > 0)
395		flags |= XMIT_SAME_NAME;
396	if (l2 > 255)
397		flags |= XMIT_LONG_NAME;
398
399	/* We must make sure we don't send a zero flag byte or the
400	 * other end will terminate the flist transfer.  Note that
401	 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
402	 * it's harmless way to add a bit to the first flag byte. */
403	if (protocol_version >= 28) {
404		if (!flags && !S_ISDIR(mode))
405			flags |= XMIT_TOP_DIR;
406		if ((flags & 0xFF00) || !flags) {
407			flags |= XMIT_EXTENDED_FLAGS;
408			write_byte(f, flags);
409			write_byte(f, flags >> 8);
410		} else
411			write_byte(f, flags);
412	} else {
413		if (!(flags & 0xFF))
414			flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
415		write_byte(f, flags);
416	}
417	if (flags & XMIT_SAME_NAME)
418		write_byte(f, l1);
419	if (flags & XMIT_LONG_NAME)
420		write_int(f, l2);
421	else
422		write_byte(f, l2);
423	write_buf(f, fname + l1, l2);
424
425	write_longint(f, file->length);
426	if (!(flags & XMIT_SAME_TIME))
427		write_int(f, modtime);
428	if (!(flags & XMIT_SAME_MODE))
429		write_int(f, to_wire_mode(mode));
430	if (preserve_uid && !(flags & XMIT_SAME_UID)) {
431		if (!numeric_ids)
432			add_uid(uid);
433		write_int(f, uid);
434	}
435	if (preserve_gid && !(flags & XMIT_SAME_GID)) {
436		if (!numeric_ids)
437			add_gid(gid);
438		write_int(f, gid);
439	}
440	if ((preserve_devices && IS_DEVICE(mode))
441	 || (preserve_specials && IS_SPECIAL(mode))) {
442		if (protocol_version < 28) {
443			if (!(flags & XMIT_SAME_RDEV_pre28))
444				write_int(f, (int)rdev);
445		} else {
446			if (!(flags & XMIT_SAME_RDEV_MAJOR))
447				write_int(f, major(rdev));
448			if (flags & XMIT_RDEV_MINOR_IS_SMALL)
449				write_byte(f, minor(rdev));
450			else
451				write_int(f, minor(rdev));
452		}
453	}
454
455#ifdef SUPPORT_LINKS
456	if (preserve_links && S_ISLNK(mode)) {
457		int len = strlen(file->u.link);
458		write_int(f, len);
459		write_buf(f, file->u.link, len);
460	}
461#endif
462
463#ifdef SUPPORT_HARD_LINKS
464	if (file->link_u.idev) {
465		if (protocol_version < 26) {
466			/* 32-bit dev_t and ino_t */
467			write_int(f, dev);
468			write_int(f, file->F_INODE);
469		} else {
470			/* 64-bit dev_t and ino_t */
471			if (!(flags & XMIT_SAME_DEV))
472				write_longint(f, dev);
473			write_longint(f, file->F_INODE);
474		}
475	}
476#endif
477
478	if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
479		char *sum;
480		if (S_ISREG(mode))
481			sum = file->u.sum;
482		else {
483			/* Prior to 28, we sent a useless set of nulls. */
484			sum = empty_sum;
485		}
486		write_buf(f, sum, checksum_len);
487	}
488
489	strlcpy(lastname, fname, MAXPATHLEN);
490}
491
492static struct file_struct *receive_file_entry(struct file_list *flist,
493					      unsigned short flags, int f)
494{
495	static time_t modtime;
496	static mode_t mode;
497	static int64 dev;
498	static dev_t rdev;
499	static uint32 rdev_major;
500	static uid_t uid;
501	static gid_t gid;
502	static char lastname[MAXPATHLEN], *lastdir;
503	static int lastdir_depth, lastdir_len = -1;
504	static unsigned int del_hier_name_len = 0;
505	static int in_del_hier = 0;
506	char thisname[MAXPATHLEN];
507	unsigned int l1 = 0, l2 = 0;
508	int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
509	OFF_T file_length;
510	char *basename, *dirname, *bp;
511	struct file_struct *file;
512
513	if (!flist) {
514		modtime = 0, mode = 0;
515		dev = 0, rdev = MAKEDEV(0, 0);
516		rdev_major = 0;
517		uid = 0, gid = 0;
518		*lastname = '\0';
519		lastdir_len = -1;
520		in_del_hier = 0;
521		return NULL;
522	}
523
524	if (flags & XMIT_SAME_NAME)
525		l1 = read_byte(f);
526
527	if (flags & XMIT_LONG_NAME)
528		l2 = read_int(f);
529	else
530		l2 = read_byte(f);
531
532	if (l2 >= MAXPATHLEN - l1) {
533		rprintf(FERROR,
534			"overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
535			flags, l1, l2, lastname);
536		overflow_exit("receive_file_entry");
537	}
538
539	strlcpy(thisname, lastname, l1 + 1);
540	read_sbuf(f, &thisname[l1], l2);
541	thisname[l1 + l2] = 0;
542
543	strlcpy(lastname, thisname, MAXPATHLEN);
544
545	clean_fname(thisname, 0);
546
547	if (sanitize_paths)
548		sanitize_path(thisname, thisname, "", 0, NULL);
549
550	if ((basename = strrchr(thisname, '/')) != NULL) {
551		dirname_len = ++basename - thisname; /* counts future '\0' */
552		if (lastdir_len == dirname_len - 1
553		    && strncmp(thisname, lastdir, lastdir_len) == 0) {
554			dirname = lastdir;
555			dirname_len = 0; /* indicates no copy is needed */
556		} else
557			dirname = thisname;
558	} else {
559		basename = thisname;
560		dirname = NULL;
561		dirname_len = 0;
562	}
563	basename_len = strlen(basename) + 1; /* count the '\0' */
564
565	file_length = read_longint(f);
566	if (!(flags & XMIT_SAME_TIME))
567		modtime = (time_t)read_int(f);
568	if (!(flags & XMIT_SAME_MODE))
569		mode = from_wire_mode(read_int(f));
570
571	if (chmod_modes && !S_ISLNK(mode))
572		mode = tweak_mode(mode, chmod_modes);
573
574	if (preserve_uid && !(flags & XMIT_SAME_UID))
575		uid = (uid_t)read_int(f);
576	if (preserve_gid && !(flags & XMIT_SAME_GID))
577		gid = (gid_t)read_int(f);
578
579	if ((preserve_devices && IS_DEVICE(mode))
580	 || (preserve_specials && IS_SPECIAL(mode))) {
581		if (protocol_version < 28) {
582			if (!(flags & XMIT_SAME_RDEV_pre28))
583				rdev = (dev_t)read_int(f);
584		} else {
585			uint32 rdev_minor;
586			if (!(flags & XMIT_SAME_RDEV_MAJOR))
587				rdev_major = read_int(f);
588			if (flags & XMIT_RDEV_MINOR_IS_SMALL)
589				rdev_minor = read_byte(f);
590			else
591				rdev_minor = read_int(f);
592			rdev = MAKEDEV(rdev_major, rdev_minor);
593		}
594	} else if (protocol_version < 28)
595		rdev = MAKEDEV(0, 0);
596
597#ifdef SUPPORT_LINKS
598	if (preserve_links && S_ISLNK(mode)) {
599		linkname_len = read_int(f) + 1; /* count the '\0' */
600		if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
601			rprintf(FERROR, "overflow: linkname_len=%d\n",
602				linkname_len - 1);
603			overflow_exit("receive_file_entry");
604		}
605		if (munge_symlinks)
606			linkname_len += SYMLINK_PREFIX_LEN;
607	}
608	else
609#endif
610		linkname_len = 0;
611
612	sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
613
614	alloc_len = file_struct_len + dirname_len + basename_len
615		  + linkname_len + sum_len;
616	bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
617
618	file = (struct file_struct *)bp;
619	memset(bp, 0, file_struct_len);
620	bp += file_struct_len;
621
622	file->modtime = modtime;
623	file->length = file_length;
624	file->mode = mode;
625	file->uid = uid;
626	file->gid = gid;
627
628	if (dirname_len) {
629		file->dirname = lastdir = bp;
630		lastdir_len = dirname_len - 1;
631		memcpy(bp, dirname, dirname_len - 1);
632		bp += dirname_len;
633		bp[-1] = '\0';
634		lastdir_depth = count_dir_elements(lastdir);
635		file->dir.depth = lastdir_depth + 1;
636	} else if (dirname) {
637		file->dirname = dirname; /* we're reusing lastname */
638		file->dir.depth = lastdir_depth + 1;
639	} else
640		file->dir.depth = 1;
641
642	if (S_ISDIR(mode)) {
643		if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
644			file->dir.depth--;
645		if (flags & XMIT_TOP_DIR) {
646			in_del_hier = recurse;
647			del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
648			if (relative_paths && del_hier_name_len > 2
649			    && lastname[del_hier_name_len-1] == '.'
650			    && lastname[del_hier_name_len-2] == '/')
651				del_hier_name_len -= 2;
652			file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
653		} else if (in_del_hier) {
654			if (!relative_paths || !del_hier_name_len
655			 || (l1 >= del_hier_name_len
656			  && lastname[del_hier_name_len] == '/'))
657				file->flags |= FLAG_DEL_HERE;
658			else
659				in_del_hier = 0;
660		}
661	}
662
663	file->basename = bp;
664	memcpy(bp, basename, basename_len);
665	bp += basename_len;
666
667	if ((preserve_devices && IS_DEVICE(mode))
668	 || (preserve_specials && IS_SPECIAL(mode)))
669		file->u.rdev = rdev;
670
671#ifdef SUPPORT_LINKS
672	if (linkname_len) {
673		file->u.link = bp;
674		if (munge_symlinks) {
675			strlcpy(bp, SYMLINK_PREFIX, linkname_len);
676			bp += SYMLINK_PREFIX_LEN;
677			linkname_len -= SYMLINK_PREFIX_LEN;
678		}
679		read_sbuf(f, bp, linkname_len - 1);
680		if (sanitize_paths && !munge_symlinks) {
681			sanitize_path(bp, bp, "", lastdir_depth, NULL);
682			bp += strlen(bp) + 1;
683		} else
684			bp += linkname_len;
685	}
686#endif
687
688#ifdef SUPPORT_HARD_LINKS
689	if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
690		flags |= XMIT_HAS_IDEV_DATA;
691	if (flags & XMIT_HAS_IDEV_DATA) {
692		int64 inode;
693		if (protocol_version < 26) {
694			dev = read_int(f);
695			inode = read_int(f);
696		} else {
697			if (!(flags & XMIT_SAME_DEV))
698				dev = read_longint(f);
699			inode = read_longint(f);
700		}
701		if (flist->hlink_pool) {
702			file->link_u.idev = pool_talloc(flist->hlink_pool,
703			    struct idev, 1, "inode_table");
704			file->F_INODE = inode;
705			file->F_DEV = dev;
706		}
707	}
708#endif
709
710	if (always_checksum && (sum_len || protocol_version < 28)) {
711		char *sum;
712		if (sum_len) {
713			file->u.sum = sum = bp;
714			/*bp += sum_len;*/
715		} else {
716			/* Prior to 28, we get a useless set of nulls. */
717			sum = empty_sum;
718		}
719		read_buf(f, sum, checksum_len);
720	}
721
722	return file;
723}
724
725/**
726 * Create a file_struct for a named file by reading its stat()
727 * information and performing extensive checks against global
728 * options.
729 *
730 * @return the new file, or NULL if there was an error or this file
731 * should be excluded.
732 *
733 * @todo There is a small optimization opportunity here to avoid
734 * stat()ing the file in some circumstances, which has a certain cost.
735 * We are called immediately after doing readdir(), and so we may
736 * already know the d_type of the file.  We could for example avoid
737 * statting directories if we're not recursing, but this is not a very
738 * important case.  Some systems may not have d_type.
739 **/
740struct file_struct *make_file(char *fname, struct file_list *flist,
741			      STRUCT_STAT *stp, unsigned short flags,
742			      int filter_level)
743{
744	static char *lastdir;
745	static int lastdir_len = -1;
746	struct file_struct *file;
747	STRUCT_STAT st;
748	char sum[SUM_LENGTH];
749	char thisname[MAXPATHLEN];
750	char linkname[MAXPATHLEN];
751	int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
752	char *basename, *dirname, *bp;
753
754	if (!flist || !flist->count)	/* Ignore lastdir when invalid. */
755		lastdir_len = -1;
756
757	if (strlcpy(thisname, fname, sizeof thisname)
758	    >= sizeof thisname - flist_dir_len) {
759		rprintf(FINFO, "skipping overly long name: %s\n", fname);
760		return NULL;
761	}
762	clean_fname(thisname, 0);
763	if (sanitize_paths)
764		sanitize_path(thisname, thisname, "", 0, NULL);
765
766	memset(sum, 0, SUM_LENGTH);
767
768	if (stp && S_ISDIR(stp->st_mode)) {
769		st = *stp; /* Needed for "symlink/." with --relative. */
770		*linkname = '\0'; /* make IBM code checker happy */
771	} else if (readlink_stat(thisname, &st, linkname) != 0) {
772		int save_errno = errno;
773		/* See if file is excluded before reporting an error. */
774		if (filter_level != NO_FILTERS
775		    && is_excluded(thisname, 0, filter_level))
776			return NULL;
777		if (save_errno == ENOENT) {
778#ifdef SUPPORT_LINKS
779			/* Avoid "vanished" error if symlink points nowhere. */
780			if (copy_links && do_lstat(thisname, &st) == 0
781			    && S_ISLNK(st.st_mode)) {
782				io_error |= IOERR_GENERAL;
783				rprintf(FERROR, "symlink has no referent: %s\n",
784					full_fname(thisname));
785			} else
786#endif
787			{
788				enum logcode c = am_daemon && protocol_version < 28
789				    ? FERROR : FINFO;
790				io_error |= IOERR_VANISHED;
791				rprintf(c, "file has vanished: %s\n",
792					full_fname(thisname));
793			}
794		} else {
795			io_error |= IOERR_GENERAL;
796			rsyserr(FERROR, save_errno, "readlink %s failed",
797				full_fname(thisname));
798		}
799		return NULL;
800	}
801
802	/* backup.c calls us with filter_level set to NO_FILTERS. */
803	if (filter_level == NO_FILTERS)
804		goto skip_filters;
805
806	if (S_ISDIR(st.st_mode) && !xfer_dirs) {
807		rprintf(FINFO, "skipping directory %s\n", thisname);
808		return NULL;
809	}
810
811	/* We only care about directories because we need to avoid recursing
812	 * into a mount-point directory, not to avoid copying a symlinked
813	 * file if -L (or similar) was specified. */
814	if (one_file_system && st.st_dev != filesystem_dev
815	 && S_ISDIR(st.st_mode)) {
816		if (one_file_system > 1) {
817			if (verbose > 2) {
818				rprintf(FINFO, "skipping mount-point dir %s\n",
819					thisname);
820			}
821			return NULL;
822		}
823		flags |= FLAG_MOUNT_POINT;
824	}
825
826	if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
827		return NULL;
828
829	if (lp_ignore_nonreadable(module_id)) {
830#ifdef SUPPORT_LINKS
831		if (!S_ISLNK(st.st_mode))
832#endif
833			if (access(thisname, R_OK) != 0)
834				return NULL;
835	}
836
837  skip_filters:
838
839	if (verbose > 2) {
840		rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
841			who_am_i(), thisname, filter_level);
842	}
843
844	if ((basename = strrchr(thisname, '/')) != NULL) {
845		dirname_len = ++basename - thisname; /* counts future '\0' */
846		if (lastdir_len == dirname_len - 1
847		    && strncmp(thisname, lastdir, lastdir_len) == 0) {
848			dirname = lastdir;
849			dirname_len = 0; /* indicates no copy is needed */
850		} else
851			dirname = thisname;
852	} else {
853		basename = thisname;
854		dirname = NULL;
855		dirname_len = 0;
856	}
857	basename_len = strlen(basename) + 1; /* count the '\0' */
858
859#ifdef SUPPORT_LINKS
860	linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
861#else
862	linkname_len = 0;
863#endif
864
865	sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
866	        ? MD4_SUM_LENGTH : 0;
867
868	alloc_len = file_struct_len + dirname_len + basename_len
869		  + linkname_len + sum_len;
870	if (flist)
871		bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
872	else {
873		if (!(bp = new_array(char, alloc_len)))
874			out_of_memory("make_file");
875	}
876
877	file = (struct file_struct *)bp;
878	memset(bp, 0, file_struct_len);
879	bp += file_struct_len;
880
881	file->flags = flags;
882	file->modtime = st.st_mtime;
883	file->length = st.st_size;
884	file->mode = st.st_mode;
885	file->uid = st.st_uid;
886	file->gid = st.st_gid;
887
888#ifdef SUPPORT_HARD_LINKS
889	if (flist && flist->hlink_pool) {
890		if (protocol_version < 28) {
891			if (S_ISREG(st.st_mode))
892				file->link_u.idev = pool_talloc(
893				    flist->hlink_pool, struct idev, 1,
894				    "inode_table");
895		} else {
896			if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
897				file->link_u.idev = pool_talloc(
898				    flist->hlink_pool, struct idev, 1,
899				    "inode_table");
900		}
901	}
902	if (file->link_u.idev) {
903		file->F_DEV = st.st_dev;
904		file->F_INODE = st.st_ino;
905	}
906#endif
907
908	if (dirname_len) {
909		file->dirname = lastdir = bp;
910		lastdir_len = dirname_len - 1;
911		memcpy(bp, dirname, dirname_len - 1);
912		bp += dirname_len;
913		bp[-1] = '\0';
914	} else if (dirname)
915		file->dirname = dirname;
916
917	file->basename = bp;
918	memcpy(bp, basename, basename_len);
919	bp += basename_len;
920
921#ifdef HAVE_STRUCT_STAT_ST_RDEV
922	if ((preserve_devices && IS_DEVICE(st.st_mode))
923	 || (preserve_specials && IS_SPECIAL(st.st_mode)))
924		file->u.rdev = st.st_rdev;
925#endif
926
927#ifdef SUPPORT_LINKS
928	if (linkname_len) {
929		file->u.link = bp;
930		memcpy(bp, linkname, linkname_len);
931		bp += linkname_len;
932	}
933#endif
934
935	if (sum_len) {
936		file->u.sum = bp;
937		file_checksum(thisname, bp, st.st_size);
938		/*bp += sum_len;*/
939	}
940
941	file->dir.root = flist_dir;
942
943	/* This code is only used by the receiver when it is building
944	 * a list of files for a delete pass. */
945	if (keep_dirlinks && linkname_len && flist) {
946		STRUCT_STAT st2;
947		int save_mode = file->mode;
948		file->mode = S_IFDIR; /* Find a directory with our name. */
949		if (flist_find(the_file_list, file) >= 0
950		    && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
951			file->modtime = st2.st_mtime;
952			file->length = st2.st_size;
953			file->mode = st2.st_mode;
954			file->uid = st2.st_uid;
955			file->gid = st2.st_gid;
956			file->u.link = NULL;
957		} else
958			file->mode = save_mode;
959	}
960
961	if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
962		stats.total_size += st.st_size;
963
964	return file;
965}
966
967static struct file_struct *send_file_name(int f, struct file_list *flist,
968					  char *fname, STRUCT_STAT *stp,
969					  unsigned short flags)
970{
971	struct file_struct *file;
972
973	file = make_file(fname, flist, stp, flags,
974			 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
975	if (!file)
976		return NULL;
977
978	if (chmod_modes && !S_ISLNK(file->mode))
979		file->mode = tweak_mode(file->mode, chmod_modes);
980
981	maybe_emit_filelist_progress(flist->count + flist_count_offset);
982
983	flist_expand(flist);
984
985	if (file->basename[0]) {
986		flist->files[flist->count++] = file;
987		send_file_entry(file, f);
988#ifdef EA_SUPPORT
989		/* If the file doesn't begin with "._", and has
990		 * either acls or extended attributes, serialize
991		 * the data out and add a fake file to the file
992		 * list  with the "._" prepended to the basename.
993		 * of the original source.
994		 */
995		if (extended_attributes) {
996#ifdef HAVE_COPYFILE
997		    if(strncmp(file->basename, "._", 2)
998			&& copyfile(fname, NULL, 0,
999				    COPYFILE_CHECK | COPYFILE_ACL | COPYFILE_XATTR | (preserve_links ? COPYFILE_NOFOLLOW : 0))) {
1000			char *bp;
1001			struct file_struct *file2 = NULL;
1002			int alloc_len;
1003
1004			if (verbose > 4)
1005			    rprintf(FINFO, "added synthetic file for: %s\n", fname);
1006
1007			alloc_len = file_struct_len
1008			    + strlen(file->basename) + 1 + 2 // + strlen("._")
1009			    + MD4_SUM_LENGTH;
1010
1011			bp = pool_alloc(flist->file_pool, alloc_len, "receive_metadata_entry");
1012
1013			file2 = (struct file_struct *)bp;
1014			memcpy(bp, file, file_struct_len);
1015 			bp += file_struct_len;
1016
1017			file2->basename = bp;
1018			memcpy(bp + 2, file->basename, strlen(file->basename) + 1);
1019			file2->basename[0] = '.';
1020			file2->basename[1] = '_';
1021			file2->length = 1;
1022			file2->mode = S_IFREG | S_IRUSR;
1023			file2->modtime = file->modtime;
1024			file2->u.sum = empty_sum;
1025
1026			flist_expand(flist);
1027			flist->files[flist->count++] = file2;
1028
1029			if (f != -1)
1030			    write_byte(f, 1);
1031			send_file_entry(file2, f);
1032		    }
1033#endif	/* HAVE_COPYFILE */
1034			if (f != -1)
1035			    write_byte(f, 0);
1036		}
1037#endif	/* EA_SUPPORT */
1038	}
1039	return file;
1040}
1041
1042static void send_if_directory(int f, struct file_list *flist,
1043			      struct file_struct *file,
1044			      char *fbuf, unsigned int ol)
1045{
1046	char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1047
1048	if (S_ISDIR(file->mode)
1049	    && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
1050		void *save_filters;
1051		unsigned int len = strlen(fbuf);
1052		if (len > 1 && fbuf[len-1] == '/')
1053			fbuf[--len] = '\0';
1054		if (len >= MAXPATHLEN - 1) {
1055			io_error |= IOERR_GENERAL;
1056			rprintf(FERROR, "skipping long-named directory: %s\n",
1057				full_fname(fbuf));
1058			return;
1059		}
1060		save_filters = push_local_filters(fbuf, len);
1061		send_directory(f, flist, fbuf, len);
1062		pop_local_filters(save_filters);
1063		fbuf[ol] = '\0';
1064		if (is_dot_dir)
1065			fbuf[ol-1] = '.';
1066	}
1067}
1068
1069/* This function is normally called by the sender, but the receiving side also
1070 * calls it from get_dirlist() with f set to -1 so that we just construct the
1071 * file list in memory without sending it over the wire.  Also, get_dirlist()
1072 * might call this with f set to -2, which also indicates that local filter
1073 * rules should be ignored. */
1074static void send_directory(int f, struct file_list *flist,
1075			   char *fbuf, int len)
1076{
1077	struct dirent *di;
1078	unsigned remainder;
1079	char *p;
1080	DIR *d;
1081	int start = flist->count;
1082
1083	if (!(d = opendir(fbuf))) {
1084		io_error |= IOERR_GENERAL;
1085		rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1086		return;
1087	}
1088
1089	p = fbuf + len;
1090	if (len != 1 || *fbuf != '/')
1091		*p++ = '/';
1092	*p = '\0';
1093	remainder = MAXPATHLEN - (p - fbuf);
1094
1095	for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1096		char *dname = d_name(di);
1097		if (dname[0] == '.' && (dname[1] == '\0'
1098		    || (dname[1] == '.' && dname[2] == '\0')))
1099			continue;
1100		if (strlcpy(p, dname, remainder) >= remainder) {
1101			io_error |= IOERR_GENERAL;
1102			rprintf(FINFO,
1103				"cannot send long-named file %s\n",
1104				full_fname(fbuf));
1105			continue;
1106		}
1107
1108		send_file_name(f, flist, fbuf, NULL, 0);
1109	}
1110
1111	fbuf[len] = '\0';
1112
1113	if (errno) {
1114		io_error |= IOERR_GENERAL;
1115		rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1116	}
1117
1118	closedir(d);
1119
1120	if (recurse) {
1121		int i, end = flist->count - 1;
1122		for (i = start; i <= end; i++)
1123			send_if_directory(f, flist, flist->files[i], fbuf, len);
1124	}
1125}
1126
1127struct file_list *send_file_list(int f, int argc, char *argv[])
1128{
1129	int len;
1130	STRUCT_STAT st;
1131	char *p, *dir, olddir[sizeof curr_dir];
1132	char lastpath[MAXPATHLEN] = "";
1133	struct file_list *flist;
1134	struct timeval start_tv, end_tv;
1135	int64 start_write;
1136	int use_ff_fd = 0;
1137
1138	rprintf(FLOG, "building file list\n");
1139	if (show_filelist_p())
1140		start_filelist_progress("building file list");
1141
1142	start_write = stats.total_written;
1143	gettimeofday(&start_tv, NULL);
1144
1145	flist = flist_new(WITH_HLINK, "send_file_list");
1146
1147	io_start_buffering_out();
1148	if (filesfrom_fd >= 0) {
1149		if (argv[0] && !push_dir(argv[0], 0)) {
1150			rsyserr(FERROR, errno, "push_dir %s failed",
1151				full_fname(argv[0]));
1152			exit_cleanup(RERR_FILESELECT);
1153		}
1154		use_ff_fd = 1;
1155	}
1156
1157	while (1) {
1158		char fbuf[MAXPATHLEN];
1159		char *fn;
1160		int is_dot_dir;
1161
1162		if (use_ff_fd) {
1163			if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1164				break;
1165			sanitize_path(fbuf, fbuf, "", 0, NULL);
1166		} else {
1167			if (argc-- == 0)
1168				break;
1169			strlcpy(fbuf, *argv++, MAXPATHLEN);
1170			if (sanitize_paths)
1171				sanitize_path(fbuf, fbuf, "", 0, NULL);
1172		}
1173
1174		len = strlen(fbuf);
1175		if (relative_paths) {
1176			/* We clean up fbuf below. */
1177			is_dot_dir = 0;
1178		} else if (!len || fbuf[len - 1] == '/') {
1179			if (len == 2 && fbuf[0] == '.') {
1180				/* Turn "./" into just "." rather than "./." */
1181				fbuf[1] = '\0';
1182			} else {
1183				if (len + 1 >= MAXPATHLEN)
1184					overflow_exit("send_file_list");
1185				fbuf[len++] = '.';
1186				fbuf[len] = '\0';
1187			}
1188			is_dot_dir = 1;
1189		} else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1190		    && (len == 2 || fbuf[len-3] == '/')) {
1191			if (len + 2 >= MAXPATHLEN)
1192				overflow_exit("send_file_list");
1193			fbuf[len++] = '/';
1194			fbuf[len++] = '.';
1195			fbuf[len] = '\0';
1196			is_dot_dir = 1;
1197		} else {
1198			is_dot_dir = fbuf[len-1] == '.'
1199				   && (len == 1 || fbuf[len-2] == '/');
1200		}
1201
1202		if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1203			io_error |= IOERR_GENERAL;
1204			rsyserr(FERROR, errno, "link_stat %s failed",
1205				full_fname(fbuf));
1206			continue;
1207		}
1208
1209		if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1210			rprintf(FINFO, "skipping directory %s\n", fbuf);
1211			continue;
1212		}
1213
1214		dir = NULL;
1215		olddir[0] = '\0';
1216
1217		if (!relative_paths) {
1218			p = strrchr(fbuf, '/');
1219			if (p) {
1220				*p = '\0';
1221				if (p == fbuf)
1222					dir = "/";
1223				else
1224					dir = fbuf;
1225				len -= p - fbuf + 1;
1226				fn = p + 1;
1227			} else
1228				fn = fbuf;
1229		} else {
1230			if ((p = strstr(fbuf, "/./")) != NULL) {
1231				*p = '\0';
1232				if (p == fbuf)
1233					dir = "/";
1234				else
1235					dir = fbuf;
1236				len -= p - fbuf + 3;
1237				fn = p + 3;
1238			} else
1239				fn = fbuf;
1240			/* Get rid of trailing "/" and "/.". */
1241			while (len) {
1242				if (fn[len - 1] == '/') {
1243					is_dot_dir = 1;
1244					if (!--len && !dir) {
1245						len++;
1246						break;
1247					}
1248				}
1249				else if (len >= 2 && fn[len - 1] == '.'
1250						  && fn[len - 2] == '/') {
1251					is_dot_dir = 1;
1252					if (!(len -= 2) && !dir) {
1253						len++;
1254						break;
1255					}
1256				} else
1257					break;
1258			}
1259			if (len == 1 && fn[0] == '/')
1260				fn[len++] = '.';
1261			fn[len] = '\0';
1262			/* Reject a ".." dir in the active part of the path. */
1263			for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1264				if ((p[2] == '/' || p[2] == '\0')
1265				 && (p == fn || p[-1] == '/')) {
1266					rprintf(FERROR,
1267					    "found \"..\" dir in relative path: %s\n",
1268					    fbuf);
1269					exit_cleanup(RERR_SYNTAX);
1270				}
1271			}
1272		}
1273
1274		if (!*fn) {
1275			len = 1;
1276			fn = ".";
1277		}
1278
1279		if (dir && *dir) {
1280			static char *lastdir;
1281			static int lastdir_len;
1282
1283			strlcpy(olddir, curr_dir, sizeof olddir);
1284
1285			if (!push_dir(dir, 0)) {
1286				io_error |= IOERR_GENERAL;
1287				rsyserr(FERROR, errno, "push_dir %s failed",
1288					full_fname(dir));
1289				continue;
1290			}
1291
1292			if (lastdir && strcmp(lastdir, dir) == 0) {
1293				flist_dir = lastdir;
1294				flist_dir_len = lastdir_len;
1295			} else {
1296				flist_dir = lastdir = strdup(dir);
1297				flist_dir_len = lastdir_len = strlen(dir);
1298			}
1299		}
1300
1301		if (fn != fbuf)
1302			memmove(fbuf, fn, len + 1);
1303
1304		if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1305			/* Send the implied directories at the start of the
1306			 * source spec, so we get their permissions right. */
1307			char *lp = lastpath, *slash = fbuf;
1308			*p = '\0';
1309			/* Skip any initial directories in our path that we
1310			 * have in common with lastpath. */
1311			for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1312				if (*fn == '/')
1313					slash = fn;
1314			}
1315			*p = '/';
1316			if (fn != p || (*lp && *lp != '/')) {
1317				int save_copy_links = copy_links;
1318				int save_xfer_dirs = xfer_dirs;
1319				copy_links |= copy_unsafe_links;
1320				xfer_dirs = 1;
1321				while ((slash = strchr(slash+1, '/')) != 0) {
1322					*slash = '\0';
1323					send_file_name(f, flist, fbuf, NULL, 0);
1324					*slash = '/';
1325				}
1326				copy_links = save_copy_links;
1327				xfer_dirs = save_xfer_dirs;
1328				*p = '\0';
1329				strlcpy(lastpath, fbuf, sizeof lastpath);
1330				*p = '/';
1331			}
1332		}
1333
1334		if (one_file_system)
1335			filesystem_dev = st.st_dev;
1336
1337		if (recurse || (xfer_dirs && is_dot_dir)) {
1338			struct file_struct *file;
1339			file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1340			if (file)
1341				send_if_directory(f, flist, file, fbuf, len);
1342		} else
1343			send_file_name(f, flist, fbuf, &st, 0);
1344
1345		if (olddir[0]) {
1346			flist_dir = NULL;
1347			flist_dir_len = 0;
1348			if (!pop_dir(olddir)) {
1349				rsyserr(FERROR, errno, "pop_dir %s failed",
1350					full_fname(olddir));
1351				exit_cleanup(RERR_FILESELECT);
1352			}
1353		}
1354	}
1355
1356	gettimeofday(&end_tv, NULL);
1357	stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1358			      + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1359	if (stats.flist_buildtime == 0)
1360		stats.flist_buildtime = 1;
1361	start_tv = end_tv;
1362
1363	send_file_entry(NULL, f);
1364
1365	if (show_filelist_p())
1366		finish_filelist_progress(flist);
1367
1368	gettimeofday(&end_tv, NULL);
1369	stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1370			     + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1371
1372	if (flist->hlink_pool) {
1373		pool_destroy(flist->hlink_pool);
1374		flist->hlink_pool = NULL;
1375	}
1376
1377	/* Sort the list without removing any duplicates.  This allows the
1378	 * receiving side to ask for any name they like, which gives us the
1379	 * flexibility to change the way we unduplicate names in the future
1380	 * without causing a compatibility problem with older versions. */
1381	clean_flist(flist, 0, 0);
1382
1383	send_uid_list(f);
1384
1385	/* send the io_error flag */
1386	write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1387
1388	io_end_buffering();
1389	stats.flist_size = stats.total_written - start_write;
1390	stats.num_files = flist->count;
1391
1392	if (verbose > 3)
1393		output_flist(flist);
1394
1395	if (verbose > 2)
1396		rprintf(FINFO, "send_file_list done\n");
1397
1398	return flist;
1399}
1400
1401struct file_list *recv_file_list(int f)
1402{
1403	struct file_list *flist;
1404	unsigned short flags;
1405	int64 start_read;
1406
1407	rprintf(FLOG, "receiving file list\n");
1408	if (show_filelist_p())
1409		start_filelist_progress("receiving file list");
1410
1411	start_read = stats.total_read;
1412
1413	flist = flist_new(WITH_HLINK, "recv_file_list");
1414
1415	flist->count = 0;
1416	flist->malloced = 1000;
1417	flist->files = new_array(struct file_struct *, flist->malloced);
1418	if (!flist->files)
1419		goto oom;
1420
1421	while ((flags = read_byte(f)) != 0) {
1422		struct file_struct *file;
1423
1424		flist_expand(flist);
1425
1426		if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1427			flags |= read_byte(f) << 8;
1428		file = receive_file_entry(flist, flags, f);
1429#ifdef EA_SUPPORT
1430		if (extended_attributes) {
1431			if (read_byte(f)) {
1432				file->flags &= ~FLAG_CLEAR_METADATA;
1433				if (verbose > 4)
1434					rprintf(FINFO, "receiving synthetic file entry for: %s/%s\n",
1435						file->dirname ? file->dirname : ".",
1436						file->basename);
1437			} else if (strncmp(file->basename, "._", 2)) {
1438				file->flags |= FLAG_CLEAR_METADATA;
1439			}
1440		}
1441#endif
1442
1443		if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1444			stats.total_size += file->length;
1445
1446		flist->files[flist->count++] = file;
1447
1448		maybe_emit_filelist_progress(flist->count);
1449
1450		if (verbose > 2) {
1451			rprintf(FINFO, "recv_file_name(%s)\n",
1452				f_name(file, NULL));
1453		}
1454	}
1455	receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1456
1457	if (verbose > 2)
1458		rprintf(FINFO, "received %d names\n", flist->count);
1459
1460	if (show_filelist_p())
1461		finish_filelist_progress(flist);
1462
1463	clean_flist(flist, relative_paths, 1);
1464
1465	if (f >= 0) {
1466		recv_uid_list(f, flist);
1467
1468		/* Recv the io_error flag */
1469		if (lp_ignore_errors(module_id) || ignore_errors)
1470			read_int(f);
1471		else
1472			io_error |= read_int(f);
1473	}
1474
1475	if (verbose > 3)
1476		output_flist(flist);
1477
1478	if (list_only) {
1479		int i;
1480		for (i = 0; i < flist->count; i++)
1481			list_file_entry(flist->files[i]);
1482	}
1483
1484	if (verbose > 2)
1485		rprintf(FINFO, "recv_file_list done\n");
1486
1487	stats.flist_size = stats.total_read - start_read;
1488	stats.num_files = flist->count;
1489
1490	return flist;
1491
1492  oom:
1493	out_of_memory("recv_file_list");
1494	return NULL;		/* not reached */
1495}
1496
1497static int file_compare(struct file_struct **file1, struct file_struct **file2)
1498{
1499	return f_name_cmp(*file1, *file2);
1500}
1501
1502/* Search for an identically-named item in the file list.  Note that the
1503 * items must agree in their directory-ness, or no match is returned. */
1504int flist_find(struct file_list *flist, struct file_struct *f)
1505{
1506	int low = flist->low, high = flist->high;
1507	int diff, mid, mid_up;
1508
1509	while (low <= high) {
1510		mid = (low + high) / 2;
1511		if (flist->files[mid]->basename)
1512			mid_up = mid;
1513		else {
1514			/* Scan for the next non-empty entry using the cached
1515			 * distance values.  If the value isn't fully up-to-
1516			 * date, update it. */
1517			mid_up = mid + flist->files[mid]->dir.depth;
1518			if (!flist->files[mid_up]->basename) {
1519				do {
1520				    mid_up += flist->files[mid_up]->dir.depth;
1521				} while (!flist->files[mid_up]->basename);
1522				flist->files[mid]->dir.depth = mid_up - mid;
1523			}
1524			if (mid_up > high) {
1525				/* If there's nothing left above us, set high to
1526				 * a non-empty entry below us and continue. */
1527				high = mid - flist->files[mid]->length;
1528				if (!flist->files[high]->basename) {
1529					do {
1530					    high -= flist->files[high]->length;
1531					} while (!flist->files[high]->basename);
1532					flist->files[mid]->length = mid - high;
1533				}
1534				continue;
1535			}
1536		}
1537		diff = f_name_cmp(flist->files[mid_up], f);
1538		if (diff == 0) {
1539			if (protocol_version < 29
1540			    && S_ISDIR(flist->files[mid_up]->mode)
1541			    != S_ISDIR(f->mode))
1542				return -1;
1543			return mid_up;
1544		}
1545		if (diff < 0)
1546			low = mid_up + 1;
1547		else
1548			high = mid - 1;
1549	}
1550	return -1;
1551}
1552
1553/*
1554 * Free up any resources a file_struct has allocated
1555 * and clear the file.
1556 */
1557void clear_file(struct file_struct *file, struct file_list *flist)
1558{
1559	if (flist->hlink_pool && file->link_u.idev)
1560		pool_free(flist->hlink_pool, 0, file->link_u.idev);
1561	memset(file, 0, file_struct_len);
1562	/* In an empty entry, dir.depth is an offset to the next non-empty
1563	 * entry.  Likewise for length in the opposite direction.  We assume
1564	 * that we're alone for now since flist_find() will adjust the counts
1565	 * it runs into that aren't up-to-date. */
1566	file->length = file->dir.depth = 1;
1567}
1568
1569/*
1570 * allocate a new file list
1571 */
1572struct file_list *flist_new(int with_hlink, char *msg)
1573{
1574	struct file_list *flist;
1575
1576	flist = new(struct file_list);
1577	if (!flist)
1578		out_of_memory(msg);
1579
1580	memset(flist, 0, sizeof (struct file_list));
1581
1582	if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1583	    out_of_memory, POOL_INTERN)))
1584		out_of_memory(msg);
1585
1586#ifdef SUPPORT_HARD_LINKS
1587	if (with_hlink && preserve_hard_links) {
1588		if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1589		    sizeof (struct idev), out_of_memory, POOL_INTERN)))
1590			out_of_memory(msg);
1591	}
1592#endif
1593
1594	return flist;
1595}
1596
1597/*
1598 * free up all elements in a flist
1599 */
1600void flist_free(struct file_list *flist)
1601{
1602	pool_destroy(flist->file_pool);
1603	pool_destroy(flist->hlink_pool);
1604	free(flist->files);
1605	free(flist);
1606}
1607
1608/*
1609 * This routine ensures we don't have any duplicate names in our file list.
1610 * duplicate names can cause corruption because of the pipelining
1611 */
1612static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1613{
1614	char fbuf[MAXPATHLEN];
1615	int i, prev_i = 0;
1616
1617	if (!flist)
1618		return;
1619	if (flist->count == 0) {
1620		flist->high = -1;
1621		return;
1622	}
1623
1624	qsort(flist->files, flist->count,
1625	    sizeof flist->files[0], (int (*)())file_compare);
1626
1627	for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1628		if (flist->files[i]->basename) {
1629			prev_i = i;
1630			break;
1631		}
1632	}
1633	flist->low = prev_i;
1634	while (++i < flist->count) {
1635		int j;
1636		struct file_struct *file = flist->files[i];
1637
1638		if (!file->basename)
1639			continue;
1640		if (f_name_cmp(file, flist->files[prev_i]) == 0)
1641			j = prev_i;
1642		else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1643			int save_mode = file->mode;
1644			/* Make sure that this directory doesn't duplicate a
1645			 * non-directory earlier in the list. */
1646			flist->high = prev_i;
1647			file->mode = S_IFREG;
1648			j = flist_find(flist, file);
1649			file->mode = save_mode;
1650		} else
1651			j = -1;
1652		if (j >= 0) {
1653			struct file_struct *fp = flist->files[j];
1654			int keep, drop;
1655			/* If one is a dir and the other is not, we want to
1656			 * keep the dir because it might have contents in the
1657			 * list. */
1658			if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1659				if (S_ISDIR(file->mode))
1660					keep = i, drop = j;
1661				else
1662					keep = j, drop = i;
1663			} else
1664				keep = j, drop = i;
1665			if (verbose > 1 && !am_server) {
1666				rprintf(FINFO,
1667					"removing duplicate name %s from file list (%d)\n",
1668					f_name(file, fbuf), drop);
1669			}
1670			/* Make sure we don't lose track of a user-specified
1671			 * top directory. */
1672			flist->files[keep]->flags |= flist->files[drop]->flags
1673						   & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1674
1675			clear_file(flist->files[drop], flist);
1676
1677			if (keep == i) {
1678				if (flist->low == drop) {
1679					for (j = drop + 1;
1680					     j < i && !flist->files[j]->basename;
1681					     j++) {}
1682					flist->low = j;
1683				}
1684				prev_i = i;
1685			}
1686		} else
1687			prev_i = i;
1688	}
1689	flist->high = no_dups ? prev_i : flist->count - 1;
1690
1691	if (strip_root) {
1692		/* We need to strip off the leading slashes for relative
1693		 * paths, but this must be done _after_ the sorting phase. */
1694		for (i = flist->low; i <= flist->high; i++) {
1695			struct file_struct *file = flist->files[i];
1696
1697			if (!file->dirname)
1698				continue;
1699			while (*file->dirname == '/')
1700				file->dirname++;
1701			if (!*file->dirname)
1702				file->dirname = NULL;
1703		}
1704	}
1705
1706	if (prune_empty_dirs && no_dups) {
1707		int j, prev_depth = 0;
1708
1709		prev_i = 0; /* It's OK that this isn't really true. */
1710
1711		for (i = flist->low; i <= flist->high; i++) {
1712			struct file_struct *fp, *file = flist->files[i];
1713
1714			/* This temporarily abuses the dir.depth value for a
1715			 * directory that is in a chain that might get pruned.
1716			 * We restore the old value if it gets a reprieve. */
1717			if (S_ISDIR(file->mode) && file->dir.depth) {
1718				/* Dump empty dirs when coming back down. */
1719				for (j = prev_depth; j >= file->dir.depth; j--) {
1720					fp = flist->files[prev_i];
1721					if (fp->dir.depth >= 0)
1722						break;
1723					prev_i = -fp->dir.depth-1;
1724					clear_file(fp, flist);
1725				}
1726				prev_depth = file->dir.depth;
1727				if (is_excluded(f_name(file, fbuf), 1,
1728						       ALL_FILTERS)) {
1729					/* Keep dirs through this dir. */
1730					for (j = prev_depth-1; ; j--) {
1731						fp = flist->files[prev_i];
1732						if (fp->dir.depth >= 0)
1733							break;
1734						prev_i = -fp->dir.depth-1;
1735						fp->dir.depth = j;
1736					}
1737				} else
1738					file->dir.depth = -prev_i-1;
1739				prev_i = i;
1740			} else {
1741				/* Keep dirs through this non-dir. */
1742				for (j = prev_depth; ; j--) {
1743					fp = flist->files[prev_i];
1744					if (fp->dir.depth >= 0)
1745						break;
1746					prev_i = -fp->dir.depth-1;
1747					fp->dir.depth = j;
1748				}
1749			}
1750		}
1751		/* Dump empty all remaining empty dirs. */
1752		while (1) {
1753			struct file_struct *fp = flist->files[prev_i];
1754			if (fp->dir.depth >= 0)
1755				break;
1756			prev_i = -fp->dir.depth-1;
1757			clear_file(fp, flist);
1758		}
1759
1760		for (i = flist->low; i <= flist->high; i++) {
1761			if (flist->files[i]->basename)
1762				break;
1763		}
1764		flist->low = i;
1765		for (i = flist->high; i >= flist->low; i--) {
1766			if (flist->files[i]->basename)
1767				break;
1768		}
1769		flist->high = i;
1770	}
1771}
1772
1773static void output_flist(struct file_list *flist)
1774{
1775	char uidbuf[16], gidbuf[16], depthbuf[16];
1776	struct file_struct *file;
1777	const char *who = who_am_i();
1778	int i;
1779
1780	for (i = 0; i < flist->count; i++) {
1781		file = flist->files[i];
1782		if ((am_root || am_sender) && preserve_uid)
1783			snprintf(uidbuf, sizeof uidbuf, " uid=%ld", (long)file->uid);
1784		else
1785			*uidbuf = '\0';
1786		if (preserve_gid && file->gid != GID_NONE)
1787			snprintf(gidbuf, sizeof gidbuf, " gid=%ld", (long)file->gid);
1788		else
1789			*gidbuf = '\0';
1790		if (!am_sender)
1791			snprintf(depthbuf, sizeof depthbuf, "%d", file->dir.depth);
1792		rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1793			who, i, am_sender ? NS(file->dir.root) : depthbuf,
1794			file->dirname ? file->dirname : "",
1795			file->dirname ? "/" : "", NS(file->basename),
1796			S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1797			(double)file->length, uidbuf, gidbuf, file->flags);
1798	}
1799}
1800
1801enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1802enum fnc_type { t_PATH, t_ITEM };
1803
1804/* Compare the names of two file_struct entities, similar to how strcmp()
1805 * would do if it were operating on the joined strings.
1806 *
1807 * Some differences beginning with protocol_version 29: (1) directory names
1808 * are compared with an assumed trailing slash so that they compare in a
1809 * way that would cause them to sort immediately prior to any content they
1810 * may have; (2) a directory of any name compares after a non-directory of
1811 * any name at the same depth; (3) a directory with name "." compares prior
1812 * to anything else.  These changes mean that a directory and a non-dir
1813 * with the same name will not compare as equal (protocol_version >= 29).
1814 *
1815 * The dirname component can be an empty string, but the basename component
1816 * cannot (and never is in the current codebase).  The basename component
1817 * may be NULL (for a removed item), in which case it is considered to be
1818 * after any existing item. */
1819int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1820{
1821	int dif;
1822	const uchar *c1, *c2;
1823	enum fnc_state state1, state2;
1824	enum fnc_type type1, type2;
1825	enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1826
1827	if (!f1 || !f1->basename) {
1828		if (!f2 || !f2->basename)
1829			return 0;
1830		return -1;
1831	}
1832	if (!f2 || !f2->basename)
1833		return 1;
1834
1835	c1 = (uchar*)f1->dirname;
1836	c2 = (uchar*)f2->dirname;
1837	if (c1 == c2)
1838		c1 = c2 = NULL;
1839	if (!c1) {
1840		type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1841		c1 = (uchar*)f1->basename;
1842		if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1843			type1 = t_ITEM;
1844			state1 = s_TRAILING;
1845			c1 = (uchar*)"";
1846		} else
1847			state1 = s_BASE;
1848	} else {
1849		type1 = t_path;
1850		state1 = s_DIR;
1851	}
1852	if (!c2) {
1853		type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1854		c2 = (uchar*)f2->basename;
1855		if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1856			type2 = t_ITEM;
1857			state2 = s_TRAILING;
1858			c2 = (uchar*)"";
1859		} else
1860			state2 = s_BASE;
1861	} else {
1862		type2 = t_path;
1863		state2 = s_DIR;
1864	}
1865
1866	if (type1 != type2)
1867		return type1 == t_PATH ? 1 : -1;
1868
1869	do {
1870		if (!*c1) {
1871			switch (state1) {
1872			case s_DIR:
1873				state1 = s_SLASH;
1874				c1 = (uchar*)"/";
1875				break;
1876			case s_SLASH:
1877				type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1878				c1 = (uchar*)f1->basename;
1879				if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1880					type1 = t_ITEM;
1881					state1 = s_TRAILING;
1882					c1 = (uchar*)"";
1883				} else
1884					state1 = s_BASE;
1885				break;
1886			case s_BASE:
1887				state1 = s_TRAILING;
1888				if (type1 == t_PATH) {
1889					c1 = (uchar*)"/";
1890					break;
1891				}
1892				/* FALL THROUGH */
1893			case s_TRAILING:
1894				type1 = t_ITEM;
1895				break;
1896			}
1897			if (*c2 && type1 != type2)
1898				return type1 == t_PATH ? 1 : -1;
1899		}
1900		if (!*c2) {
1901			switch (state2) {
1902			case s_DIR:
1903				state2 = s_SLASH;
1904				c2 = (uchar*)"/";
1905				break;
1906			case s_SLASH:
1907				type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1908				c2 = (uchar*)f2->basename;
1909				if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1910					type2 = t_ITEM;
1911					state2 = s_TRAILING;
1912					c2 = (uchar*)"";
1913				} else
1914					state2 = s_BASE;
1915				break;
1916			case s_BASE:
1917				state2 = s_TRAILING;
1918				if (type2 == t_PATH) {
1919					c2 = (uchar*)"/";
1920					break;
1921				}
1922				/* FALL THROUGH */
1923			case s_TRAILING:
1924				if (!*c1)
1925					return 0;
1926				type2 = t_ITEM;
1927				break;
1928			}
1929			if (type1 != type2)
1930				return type1 == t_PATH ? 1 : -1;
1931		}
1932	} while ((dif = (int)*c1++ - (int)*c2++) == 0);
1933
1934	return dif;
1935}
1936
1937/* Return a copy of the full filename of a flist entry, using the indicated
1938 * buffer or one of 5 static buffers if fbuf is NULL.  No size-checking is
1939 * done because we checked the size when creating the file_struct entry.
1940 */
1941char *f_name(struct file_struct *f, char *fbuf)
1942{
1943	if (!f || !f->basename)
1944		return NULL;
1945
1946	if (!fbuf) {
1947		static char names[5][MAXPATHLEN];
1948		static unsigned int n;
1949
1950		n = (n + 1) % (sizeof names / sizeof names[0]);
1951
1952		fbuf = names[n];
1953	}
1954
1955	if (f->dirname) {
1956		int len = strlen(f->dirname);
1957		memcpy(fbuf, f->dirname, len);
1958		fbuf[len] = '/';
1959		strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
1960	} else
1961		strlcpy(fbuf, f->basename, MAXPATHLEN);
1962
1963	return fbuf;
1964}
1965
1966/* Do a non-recursive scan of the named directory, possibly ignoring all
1967 * exclude rules except for the daemon's.  If "dlen" is >=0, it is the length
1968 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1969 * buffer (the functions we call will append names onto the end, but the old
1970 * dir value will be restored on exit). */
1971struct file_list *get_dirlist(char *dirname, int dlen,
1972			      int ignore_filter_rules)
1973{
1974	struct file_list *dirlist;
1975	char dirbuf[MAXPATHLEN];
1976	int save_recurse = recurse;
1977	int save_xfer_dirs = xfer_dirs;
1978
1979	if (dlen < 0) {
1980		dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1981		if (dlen >= MAXPATHLEN)
1982			return NULL;
1983		dirname = dirbuf;
1984	}
1985
1986	dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1987
1988	recurse = 0;
1989	xfer_dirs = 1;
1990	send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1991	xfer_dirs = save_xfer_dirs;
1992	recurse = save_recurse;
1993	if (do_progress)
1994		flist_count_offset += dirlist->count;
1995
1996	clean_flist(dirlist, 0, 0);
1997
1998	if (verbose > 3)
1999		output_flist(dirlist);
2000
2001	return dirlist;
2002}
2003