1 /*
2  * Copyright (C) 1996, 2000 Andrew Tridgell
3  * Copyright (C) 1996 Paul Mackerras
4  * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
5  * Copyright (C) 2003-2020 Wayne Davison
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, visit the http://fsf.org website.
19  */
20 
21 #define False 0
22 #define True 1
23 #define Unset (-1) /* Our BOOL values are always an int. */
24 
25 #define BLOCK_SIZE 700
26 #define RSYNC_RSH_ENV "RSYNC_RSH"
27 #define RSYNC_RSH_IO_ENV "RSYNC_RSH_IO"
28 
29 #define RSYNC_NAME "rsync"
30 /* RSYNCD_SYSCONF is now set in config.h */
31 #define RSYNCD_USERCONF "rsyncd.conf"
32 
33 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
34 #define URL_PREFIX "rsync://"
35 
36 #define SYMLINK_PREFIX "/rsyncd-munged/"  /* This MUST have a trailing slash! */
37 #define SYMLINK_PREFIX_LEN ((int)sizeof SYMLINK_PREFIX - 1)
38 
39 #define BACKUP_SUFFIX "~"
40 
41 /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
42    incompatible with older versions :-( */
43 #define CHAR_OFFSET 0
44 
45 /* These flags are only used during the flist transfer. */
46 
47 #define XMIT_TOP_DIR (1<<0)
48 #define XMIT_SAME_MODE (1<<1)
49 #define XMIT_SAME_RDEV_pre28 (1<<2)	/* protocols 20 - 27  */
50 #define XMIT_EXTENDED_FLAGS (1<<2)	/* protocols 28 - now */
51 #define XMIT_SAME_UID (1<<3)
52 #define XMIT_SAME_GID (1<<4)
53 #define XMIT_SAME_NAME (1<<5)
54 #define XMIT_LONG_NAME (1<<6)
55 #define XMIT_SAME_TIME (1<<7)
56 
57 #define XMIT_SAME_RDEV_MAJOR (1<<8)	/* protocols 28 - now (devices only) */
58 #define XMIT_NO_CONTENT_DIR (1<<8)	/* protocols 30 - now (dirs only) */
59 #define XMIT_HLINKED (1<<9)		/* protocols 28 - now (non-dirs) */
60 #define XMIT_SAME_DEV_pre30 (1<<10)	/* protocols 28 - 29  */
61 #define XMIT_USER_NAME_FOLLOWS (1<<10)	/* protocols 30 - now */
62 #define XMIT_RDEV_MINOR_8_pre30 (1<<11) /* protocols 28 - 29  */
63 #define XMIT_GROUP_NAME_FOLLOWS (1<<11) /* protocols 30 - now */
64 #define XMIT_HLINK_FIRST (1<<12)	/* protocols 30 - now (HLINKED files only) */
65 #define XMIT_IO_ERROR_ENDLIST (1<<12)	/* protocols 31*- now (w/XMIT_EXTENDED_FLAGS) (also protocol 30 w/'f' compat flag) */
66 #define XMIT_MOD_NSEC (1<<13)		/* protocols 31 - now */
67 #define XMIT_SAME_ATIME (1<<14) 	/* any protocol - restricted by command-line option */
68 #define XMIT_UNUSED_15 (1<<15)  	/* unused flag bit */
69 
70 /* The following XMIT flags require an rsync that uses a varint for the flag values */
71 
72 #define XMIT_SAME_FLAGS (1<<16) 	/* any protocol - restricted by command-line option */
73 #define XMIT_CRTIME_EQ_MTIME (1<<17)	/* any protocol - restricted by command-line option */
74 
75 /* These flags are used in the live flist data. */
76 
77 #define FLAG_TOP_DIR (1<<0)	/* sender/receiver/generator */
78 #define FLAG_OWNED_BY_US (1<<0) /* generator: set by make_file() for aux flists only */
79 #define FLAG_FILE_SENT (1<<1)	/* sender/receiver/generator */
80 #define FLAG_DIR_CREATED (1<<1)	/* generator */
81 #define FLAG_CONTENT_DIR (1<<2)	/* sender/receiver/generator */
82 #define FLAG_MOUNT_DIR (1<<3)	/* sender/generator (dirs only) */
83 #define FLAG_SKIP_HLINK (1<<3)	/* receiver/generator (w/FLAG_HLINKED) */
84 #define FLAG_DUPLICATE (1<<4)	/* sender */
85 #define FLAG_MISSING_DIR (1<<4)	/* generator */
86 #define FLAG_HLINKED (1<<5)	/* receiver/generator (checked on all types) */
87 #define FLAG_HLINK_FIRST (1<<6)	/* receiver/generator (w/FLAG_HLINKED) */
88 #define FLAG_IMPLIED_DIR (1<<6)	/* sender/receiver/generator (dirs only) */
89 #define FLAG_HLINK_LAST (1<<7)	/* receiver/generator */
90 #define FLAG_HLINK_DONE (1<<8)	/* receiver/generator (checked on all types) */
91 #define FLAG_LENGTH64 (1<<9)	/* sender/receiver/generator */
92 #define FLAG_SKIP_GROUP (1<<10)	/* receiver/generator */
93 #define FLAG_TIME_FAILED (1<<11)/* generator */
94 #define FLAG_MOD_NSEC (1<<12)	/* sender/receiver/generator */
95 
96 /* These flags are passed to functions but not stored. */
97 
98 #define FLAG_DIVERT_DIRS (1<<16)   /* sender, but must be unique */
99 #define FLAG_PERHAPS_DIR (1<<17)   /* generator */
100 
101 /* These flags are for get_dirlist(). */
102 #define GDL_IGNORE_FILTER_RULES (1<<0)
103 #define GDL_PERHAPS_DIR (1<<1)
104 
105 /* Some helper macros for matching bits. */
106 #define BITS_SET(val,bits) (((val) & (bits)) == (bits))
107 #define BITS_SETnUNSET(val,onbits,offbits) (((val) & ((onbits)|(offbits))) == (onbits))
108 #define BITS_EQUAL(b1,b2,mask) (((unsigned)(b1) & (unsigned)(mask)) \
109 			     == ((unsigned)(b2) & (unsigned)(mask)))
110 
111 /* update this if you make incompatible changes */
112 #define PROTOCOL_VERSION 31
113 
114 /* This is used when working on a new protocol version in CVS, and should
115  * be a new non-zero value for each CVS change that affects the protocol.
116  * It must ALWAYS be 0 when the protocol goes final (and NEVER before)! */
117 #define SUBPROTOCOL_VERSION 0
118 
119 /* We refuse to interoperate with versions that are not in this range.
120  * Note that we assume we'll work with later versions: the onus is on
121  * people writing them to make sure that they don't send us anything
122  * we won't understand.
123  *
124  * Interoperation with old but supported protocol versions
125  * should cause a warning to be printed.  At a future date
126  * the old protocol will become the minimum and
127  * compatibility code removed.
128  *
129  * There are two possible explanations for the limit at
130  * MAX_PROTOCOL_VERSION: either to allow new major-rev versions that
131  * do not interoperate with us, and (more likely) so that we can
132  * detect an attempt to connect rsync to a non-rsync server, which is
133  * unlikely to begin by sending a byte between MIN_PROTOCL_VERSION and
134  * MAX_PROTOCOL_VERSION. */
135 
136 #define MIN_PROTOCOL_VERSION 20
137 #define OLD_PROTOCOL_VERSION 25
138 #define MAX_PROTOCOL_VERSION 40
139 
140 #define MIN_FILECNT_LOOKAHEAD 1000
141 #define MAX_FILECNT_LOOKAHEAD 10000
142 
143 #define RSYNC_PORT 873
144 
145 #define SPARSE_WRITE_SIZE (1024)
146 #define WRITE_SIZE (32*1024)
147 #define CHUNK_SIZE (32*1024)
148 #define MAX_MAP_SIZE (256*1024)
149 #define IO_BUFFER_SIZE (32*1024)
150 #define MAX_BLOCK_SIZE ((int32)1 << 17)
151 
152 /* For compatibility with older rsyncs */
153 #define OLD_MAX_BLOCK_SIZE ((int32)1 << 29)
154 
155 #define ROUND_UP_1024(siz) ((siz) & (1024-1) ? ((siz) | (1024-1)) + 1 : (siz))
156 
157 #define IOERR_GENERAL	(1<<0) /* For backward compatibility, this must == 1 */
158 #define IOERR_VANISHED	(1<<1)
159 #define IOERR_DEL_LIMIT (1<<2)
160 
161 #define MAX_ARGS 1000
162 #define MAX_BASIS_DIRS 20
163 #define MAX_SERVER_ARGS (MAX_BASIS_DIRS*2 + 100)
164 
165 #define COMPARE_DEST 1
166 #define COPY_DEST 2
167 #define LINK_DEST 3
168 
169 #define MPLEX_BASE 7
170 
171 #define NO_FILTERS	0
172 #define SERVER_FILTERS	1
173 #define ALL_FILTERS	2
174 
175 #define XFLG_FATAL_ERRORS	(1<<0)
176 #define XFLG_OLD_PREFIXES	(1<<1)
177 #define XFLG_ANCHORED2ABS	(1<<2) /* leading slash indicates absolute */
178 #define XFLG_ABS_IF_SLASH	(1<<3) /* leading or interior slash is absolute */
179 #define XFLG_DIR2WILD3		(1<<4) /* dir/ match gets trailing *** added */
180 
181 #define ATTRS_REPORT		(1<<0)
182 #define ATTRS_SKIP_MTIME	(1<<1)
183 #define ATTRS_ACCURATE_TIME	(1<<2)
184 #define ATTRS_SKIP_ATIME	(1<<3)
185 #define ATTRS_DELAY_IMMUTABLE	(1<<4)
186 #define ATTRS_SKIP_CRTIME	(1<<5)
187 
188 #define MSG_FLUSH	2
189 #define FULL_FLUSH	1
190 #define NORMAL_FLUSH	0
191 
192 #define PDIR_CREATE	1
193 #define PDIR_DELETE	0
194 
195 /* Note: 0x00 - 0x7F are used for basis_dir[] indexes! */
196 #define FNAMECMP_BASIS_DIR_LOW	0x00 /* Must remain 0! */
197 #define FNAMECMP_BASIS_DIR_HIGH 0x7F
198 #define FNAMECMP_FNAME		0x80
199 #define FNAMECMP_PARTIAL_DIR	0x81
200 #define FNAMECMP_BACKUP 	0x82
201 #define FNAMECMP_FUZZY		0x83
202 
203 /* For use by the itemize_changes code */
204 #define ITEM_REPORT_ATIME (1<<0)
205 #define ITEM_REPORT_CHANGE (1<<1)
206 #define ITEM_REPORT_SIZE (1<<2)     /* regular files only */
207 #define ITEM_REPORT_TIMEFAIL (1<<2) /* symlinks only */
208 #define ITEM_REPORT_TIME (1<<3)
209 #define ITEM_REPORT_PERMS (1<<4)
210 #define ITEM_REPORT_OWNER (1<<5)
211 #define ITEM_REPORT_GROUP (1<<6)
212 #define ITEM_REPORT_ACL (1<<7)
213 #define ITEM_REPORT_XATTR (1<<8)
214 #define ITEM_REPORT_FFLAGS (1<<9)
215 #define ITEM_REPORT_CRTIME (1<<10)
216 #define ITEM_BASIS_TYPE_FOLLOWS (1<<11)
217 #define ITEM_XNAME_FOLLOWS (1<<12)
218 #define ITEM_IS_NEW (1<<13)
219 #define ITEM_LOCAL_CHANGE (1<<14)
220 #define ITEM_TRANSFER (1<<15)
221 /* These are outside the range of the transmitted flags. */
222 #define ITEM_MISSING_DATA (1<<16)	   /* used by log_formatted() */
223 #define ITEM_DELETED (1<<17)		   /* used by log_formatted() */
224 #define ITEM_MATCHED (1<<18)		   /* used by itemize() */
225 
226 #define SIGNIFICANT_ITEM_FLAGS (~(\
227 	ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
228 
229 #define CFN_KEEP_DOT_DIRS (1<<0)
230 #define CFN_KEEP_TRAILING_SLASH (1<<1)
231 #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
232 #define CFN_COLLAPSE_DOT_DOT_DIRS (1<<3)
233 #define CFN_REFUSE_DOT_DOT_DIRS (1<<4)
234 
235 #define SP_DEFAULT 0
236 #define SP_KEEP_DOT_DIRS (1<<0)
237 
238 #define CD_NORMAL 0
239 #define CD_SKIP_CHDIR 1
240 
241 /* Log-message categories.  FLOG only goes to the log file, not the client;
242  * FCLIENT is the opposite. */
243 enum logcode {
244     FNONE=0, /* never sent */
245     FERROR_XFER=1, FINFO=2, /* sent over socket for any protocol */
246     FERROR=3, FWARNING=4, /* sent over socket for protocols >= 30 */
247     FERROR_SOCKET=5, FLOG=6, /* only sent via receiver -> generator pipe */
248     FERROR_UTF8=8, /* only sent via receiver -> generator pipe */
249     FCLIENT=7 /* never transmitted (e.g. server converts to FINFO) */
250 };
251 
252 /* Messages types that are sent over the message channel.  The logcode
253  * values must all be present here with identical numbers. */
254 enum msgcode {
255 	MSG_DATA=0,	/* raw data on the multiplexed stream */
256 	MSG_ERROR_XFER=FERROR_XFER, MSG_INFO=FINFO, /* remote logging */
257 	MSG_ERROR=FERROR, MSG_WARNING=FWARNING, /* protocol-30 remote logging */
258 	MSG_ERROR_SOCKET=FERROR_SOCKET, /* sibling logging */
259 	MSG_ERROR_UTF8=FERROR_UTF8, /* sibling logging */
260 	MSG_LOG=FLOG, MSG_CLIENT=FCLIENT, /* sibling logging */
261 	MSG_REDO=9,	/* reprocess indicated flist index */
262 	MSG_STATS=10,	/* message has stats data for generator */
263 	MSG_IO_ERROR=22,/* the sending side had an I/O error */
264 	MSG_IO_TIMEOUT=33,/* tell client about a daemon's timeout value */
265 	MSG_NOOP=42,	/* a do-nothing message (legacy protocol-30 only) */
266 	MSG_ERROR_EXIT=86, /* synchronize an error exit (siblings and protocol >= 31) */
267 	MSG_SUCCESS=100,/* successfully updated indicated flist index */
268 	MSG_DELETED=101,/* successfully deleted a file on receiving side */
269 	MSG_NO_SEND=102,/* sender failed to open a file we wanted */
270 };
271 
272 #define NDX_DONE -1
273 #define NDX_FLIST_EOF -2
274 #define NDX_DEL_STATS -3
275 #define NDX_FLIST_OFFSET -101
276 
277 /* For calling delete_item() and delete_dir_contents(). */
278 #define DEL_NO_UID_WRITE 	(1<<0) /* file/dir has our uid w/o write perm */
279 #define DEL_RECURSE		(1<<1) /* if dir, delete all contents */
280 #define DEL_DIR_IS_EMPTY	(1<<2) /* internal delete_FUNCTIONS use only */
281 #define DEL_FOR_FILE		(1<<3) /* making room for a replacement file */
282 #define DEL_FOR_DIR		(1<<4) /* making room for a replacement dir */
283 #define DEL_FOR_SYMLINK 	(1<<5) /* making room for a replacement symlink */
284 #define DEL_FOR_DEVICE		(1<<6) /* making room for a replacement device */
285 #define DEL_FOR_SPECIAL 	(1<<7) /* making room for a replacement special */
286 #define DEL_FOR_BACKUP	 	(1<<8) /* the delete is for a backup operation */
287 
288 #define DEL_MAKE_ROOM (DEL_FOR_FILE|DEL_FOR_DIR|DEL_FOR_SYMLINK|DEL_FOR_DEVICE|DEL_FOR_SPECIAL)
289 
290 enum delret {
291 	DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
292 };
293 
294 /* Defines for make_path() */
295 #define MKP_DROP_NAME		(1<<0) /* drop trailing filename or trailing slash */
296 #define MKP_SKIP_SLASH		(1<<1) /* skip one or more leading slashes */
297 
298 /* Defines for maybe_send_keepalive() */
299 #define MSK_ALLOW_FLUSH 	(1<<0)
300 #define MSK_ACTIVE_RECEIVER 	(1<<1)
301 
302 #include "errcode.h"
303 
304 #include "config.h"
305 #include "version.h"
306 
307 /* The default RSYNC_RSH is always set in config.h. */
308 
309 #include <stdio.h>
310 #ifdef HAVE_SYS_TYPES_H
311 # include <sys/types.h>
312 #endif
313 #ifdef HAVE_SYS_STAT_H
314 # include <sys/stat.h>
315 #endif
316 #ifdef STDC_HEADERS
317 # include <stdlib.h>
318 # include <stddef.h>
319 #else
320 # ifdef HAVE_STDLIB_H
321 #  include <stdlib.h>
322 # endif
323 #endif
324 #ifdef HAVE_STRING_H
325 # if !defined STDC_HEADERS && defined HAVE_MEMORY_H
326 #  include <memory.h>
327 # endif
328 # include <string.h>
329 #endif
330 #ifdef HAVE_STRINGS_H
331 # include <strings.h>
332 #endif
333 #ifdef HAVE_INTTYPES_H
334 # include <inttypes.h>
335 #endif
336 #ifdef HAVE_STDINT_H
337 # include <stdint.h>
338 #endif
339 #ifdef HAVE_UNISTD_H
340 # include <unistd.h>
341 #endif
342 
343 #ifdef HAVE_SYS_PARAM_H
344 #include <sys/param.h>
345 #endif
346 
347 #if defined HAVE_MALLOC_H && (defined HAVE_MALLINFO || !defined HAVE_STDLIB_H)
348 #include <malloc.h>
349 #endif
350 
351 #ifdef HAVE_SYS_SOCKET_H
352 #include <sys/socket.h>
353 #endif
354 
355 #ifdef TIME_WITH_SYS_TIME
356 #include <sys/time.h>
357 #include <time.h>
358 #else
359 #ifdef HAVE_SYS_TIME_H
360 #include <sys/time.h>
361 #else
362 #include <time.h>
363 #endif
364 #endif
365 
366 #ifdef HAVE_FCNTL_H
367 #include <fcntl.h>
368 #else
369 #ifdef HAVE_SYS_FCNTL_H
370 #include <sys/fcntl.h>
371 #endif
372 #endif
373 
374 #ifdef HAVE_SYS_IOCTL_H
375 #include <sys/ioctl.h>
376 #endif
377 
378 #ifdef HAVE_SYS_FILIO_H
379 #include <sys/filio.h>
380 #endif
381 
382 #include <signal.h>
383 #ifdef HAVE_SYS_WAIT_H
384 #include <sys/wait.h>
385 #endif
386 #ifdef HAVE_CTYPE_H
387 #include <ctype.h>
388 #endif
389 #ifdef HAVE_GRP_H
390 #include <grp.h>
391 #endif
392 #include <errno.h>
393 
394 #ifdef HAVE_UTIME_H
395 #include <utime.h>
396 #endif
397 
398 #if defined HAVE_UTIMENSAT || defined HAVE_LUTIMES || defined HAVE_SETATTRLIST
399 #define CAN_SET_SYMLINK_TIMES 1
400 #endif
401 
402 #if defined HAVE_LCHOWN || defined CHOWN_MODIFIES_SYMLINK
403 #define CAN_CHOWN_SYMLINK 1
404 #endif
405 
406 #if defined HAVE_LCHMOD || defined HAVE_SETATTRLIST
407 #define CAN_CHMOD_SYMLINK 1
408 #endif
409 
410 #if defined HAVE_UTIMENSAT || defined HAVE_SETATTRLIST
411 #define CAN_SET_NSEC 1
412 #endif
413 
414 #ifdef CAN_SET_NSEC
415 #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
416 #define ST_MTIME_NSEC st_mtim.tv_nsec
417 #define ST_ATIME_NSEC st_atim.tv_nsec
418 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
419 #define ST_MTIME_NSEC st_mtimensec
420 #define ST_ATIME_NSEC st_atimensec
421 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
422 #define ST_MTIME_NSEC st_mtimespec.tv_nsec
423 #define ST_ATIME_NSEC st_atimespec.tv_nsec
424 #endif
425 #endif
426 
427 #ifdef HAVE_SYS_SELECT_H
428 #include <sys/select.h>
429 #endif
430 
431 #ifdef HAVE_SYS_MODE_H
432 /* apparently AIX needs this for S_ISLNK */
433 #ifndef S_ISLNK
434 #include <sys/mode.h>
435 #endif
436 #endif
437 
438 /* these are needed for the uid/gid mapping code */
439 #include <pwd.h>
440 #include <grp.h>
441 
442 #include <stdarg.h>
443 #include <netinet/in.h>
444 #include <arpa/inet.h>
445 #ifdef HAVE_NETDB_H
446 #include <netdb.h>
447 #endif
448 #include <syslog.h>
449 #ifdef HAVE_SYS_FILE_H
450 #include <sys/file.h>
451 #endif
452 
453 #ifdef HAVE_DIRENT_H
454 # include <dirent.h>
455 #else
456 # define dirent direct
457 # ifdef HAVE_SYS_NDIR_H
458 #  include <sys/ndir.h>
459 # endif
460 # ifdef HAVE_SYS_DIR_H
461 #  include <sys/dir.h>
462 # endif
463 # ifdef HAVE_NDIR_H
464 #  include <ndir.h>
465 # endif
466 #endif
467 
468 #ifdef MAJOR_IN_MKDEV
469 #include <sys/mkdev.h>
470 # if !defined makedev && (defined mkdev || defined _WIN32 || defined __WIN32__)
471 #  define makedev mkdev
472 # endif
473 #elif defined MAJOR_IN_SYSMACROS
474 #include <sys/sysmacros.h>
475 #endif
476 
477 #ifdef MAKEDEV_TAKES_3_ARGS
478 #define MAKEDEV(devmajor,devminor) makedev(0,devmajor,devminor)
479 #else
480 #ifndef __TANDEM
481 #define MAKEDEV(devmajor,devminor) makedev(devmajor,devminor)
482 #else
483 # include <sys/stat.h>
484 # define major DEV_TO_MAJOR
485 # define minor DEV_TO_MINOR
486 # define MAKEDEV MAJORMINOR_TO_DEV
487 #endif
488 #endif
489 
490 #ifdef __TANDEM
491 # include <floss.h(floss_read,floss_write,floss_fork,floss_execvp)>
492 # include <floss.h(floss_getpwuid,floss_select,floss_seteuid)>
493 # define S_IEXEC S_IXUSR
494 # define ROOT_UID 65535
495 #else
496 # define ROOT_UID 0
497 #endif
498 
499 #ifdef HAVE_COMPAT_H
500 #include <compat.h>
501 #endif
502 
503 #ifdef HAVE_LIMITS_H
504 # include <limits.h>
505 #endif
506 
507 #if defined USE_ICONV_OPEN && defined HAVE_ICONV_H
508 #include <iconv.h>
509 #ifndef ICONV_CONST
510 #define ICONV_CONST
511 #endif
512 #else
513 #ifdef ICONV_CONST
514 #undef ICONV_CONST
515 #endif
516 #ifdef ICONV_OPTION
517 #undef ICONV_OPTION
518 #endif
519 #ifdef iconv_t
520 #undef iconv_t
521 #endif
522 #define iconv_t int
523 #endif
524 
525 #include <assert.h>
526 
527 #include "lib/pool_alloc.h"
528 
529 #ifndef HAVE_ID_T
530 typedef unsigned int id_t;
531 #endif
532 #ifndef HAVE_PID_T
533 typedef int pid_t;
534 #endif
535 #ifndef HAVE_MODE_T
536 typedef unsigned int mode_t;
537 #endif
538 #ifndef HAVE_OFF_T
539 typedef long off_t;
540 #undef SIZEOF_OFF_T
541 #define SIZEOF_OFF_T SIZEOF_LONG
542 #endif
543 #ifndef HAVE_SIZE_T
544 typedef unsigned int size_t;
545 #endif
546 
547 #define BOOL int
548 
549 #ifndef uchar
550 #define uchar unsigned char
551 #endif
552 
553 #ifdef SIGNED_CHAR_OK
554 #define schar signed char
555 #else
556 #define schar char
557 #endif
558 
559 #ifndef int16
560 #if SIZEOF_INT16_T == 2
561 # define int16 int16_t
562 #else
563 # define int16 short
564 #endif
565 #endif
566 
567 #ifndef uint16
568 #if SIZEOF_UINT16_T == 2
569 # define uint16 uint16_t
570 #else
571 # define uint16 unsigned int16
572 #endif
573 #endif
574 
575 #ifndef __APPLE__ /* Do we need a configure check for this? */
576 #define SUPPORT_ATIMES 1
577 #endif
578 
579 #ifdef HAVE_GETATTRLIST
580 #define SUPPORT_CRTIMES 1
581 #endif
582 
583 #define NO_FFLAGS ((uint32)-1)
584 
585 #ifdef HAVE_CHFLAGS
586 #define SUPPORT_FILEFLAGS 1
587 #define SUPPORT_FORCE_CHANGE 1
588 #endif
589 
590 #if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
591 #ifndef UF_NOUNLINK
592 #define UF_NOUNLINK 0
593 #endif
594 #ifndef SF_NOUNLINK
595 #define SF_NOUNLINK 0
596 #endif
597 #define USR_IMMUTABLE (UF_IMMUTABLE|UF_NOUNLINK|UF_APPEND)
598 #define SYS_IMMUTABLE (SF_IMMUTABLE|SF_NOUNLINK|SF_APPEND)
599 #define ALL_IMMUTABLE (USR_IMMUTABLE|SYS_IMMUTABLE)
600 #define ST_FLAGS(st) ((st).st_flags)
601 #else
602 #define USR_IMMUTABLE 0
603 #define SYS_IMMUTABLE 0
604 #define ALL_IMMUTABLE 0
605 #define ST_FLAGS(st) NO_FFLAGS
606 #endif
607 
608 /* Find a variable that is either exactly 32-bits or longer.
609  * If some code depends on 32-bit truncation, it will need to
610  * take special action in a "#if SIZEOF_INT32 > 4" section. */
611 #ifndef int32
612 #if SIZEOF_INT32_T == 4
613 # define int32 int32_t
614 # define SIZEOF_INT32 4
615 #elif SIZEOF_INT == 4
616 # define int32 int
617 # define SIZEOF_INT32 4
618 #elif SIZEOF_LONG == 4
619 # define int32 long
620 # define SIZEOF_INT32 4
621 #elif SIZEOF_SHORT == 4
622 # define int32 short
623 # define SIZEOF_INT32 4
624 #elif SIZEOF_INT > 4
625 # define int32 int
626 # define SIZEOF_INT32 SIZEOF_INT
627 #elif SIZEOF_LONG > 4
628 # define int32 long
629 # define SIZEOF_INT32 SIZEOF_LONG
630 #else
631 # error Could not find a 32-bit integer variable
632 #endif
633 #else
634 # define SIZEOF_INT32 4
635 #endif
636 
637 #ifndef uint32
638 #if SIZEOF_UINT32_T == 4
639 # define uint32 uint32_t
640 #else
641 # define uint32 unsigned int32
642 #endif
643 #endif
644 
645 #if SIZEOF_OFF_T == 8 || !SIZEOF_OFF64_T || !defined HAVE_STRUCT_STAT64
646 #define OFF_T off_t
647 #define STRUCT_STAT struct stat
648 #define SIZEOF_CAPITAL_OFF_T SIZEOF_OFF_T
649 #else
650 #define OFF_T off64_t
651 #define STRUCT_STAT struct stat64
652 #define USE_STAT64_FUNCS 1
653 #define SIZEOF_CAPITAL_OFF_T SIZEOF_OFF64_T
654 #endif
655 
656 /* CAVEAT: on some systems, int64 will really be a 32-bit integer IFF
657  * that's the maximum size the file system can handle and there is no
658  * 64-bit type available.  The rsync source must therefore take steps
659  * to ensure that any code that really requires a 64-bit integer has
660  * it (e.g. the checksum code uses two 32-bit integers for its 64-bit
661  * counter). */
662 #if SIZEOF_INT64_T == 8
663 # define int64 int64_t
664 # define SIZEOF_INT64 8
665 #elif SIZEOF_LONG == 8
666 # define int64 long
667 # define SIZEOF_INT64 8
668 #elif SIZEOF_INT == 8
669 # define int64 int
670 # define SIZEOF_INT64 8
671 #elif SIZEOF_LONG_LONG == 8
672 # define int64 long long
673 # define SIZEOF_INT64 8
674 #elif SIZEOF_OFF64_T == 8
675 # define int64 off64_t
676 # define SIZEOF_INT64 8
677 #elif SIZEOF_OFF_T == 8
678 # define int64 off_t
679 # define SIZEOF_INT64 8
680 #elif SIZEOF_INT > 8
681 # define int64 int
682 # define SIZEOF_INT64 SIZEOF_INT
683 #elif SIZEOF_LONG > 8
684 # define int64 long
685 # define SIZEOF_INT64 SIZEOF_LONG
686 #elif SIZEOF_LONG_LONG > 8
687 # define int64 long long
688 # define SIZEOF_INT64 SIZEOF_LONG_LONG
689 #else
690 /* As long as it gets... */
691 # define int64 off_t
692 # define SIZEOF_INT64 SIZEOF_OFF_T
693 #endif
694 
695 #define HT_KEY32 0
696 #define HT_KEY64 1
697 
698 struct hashtable {
699 	void *nodes;
700 	int32 size, entries;
701 	uint32 node_size;
702 	short key64;
703 };
704 
705 struct ht_int32_node {
706 	void *data;
707 	int32 key;
708 };
709 
710 struct ht_int64_node {
711 	void *data;
712 	int64 key;
713 };
714 
715 #define HT_NODE(tbl, bkts, i) ((void*)((char*)(bkts) + (i)*(tbl)->node_size))
716 #define HT_KEY(node, k64) ((k64)? ((struct ht_int64_node*)(node))->key \
717 			 : (int64)((struct ht_int32_node*)(node))->key)
718 
719 #ifndef MIN
720 #define MIN(a,b) ((a)<(b)?(a):(b))
721 #endif
722 
723 #ifndef MAX
724 #define MAX(a,b) ((a)>(b)?(a):(b))
725 #endif
726 
727 #ifndef MAXHOSTNAMELEN
728 #define MAXHOSTNAMELEN 256
729 #endif
730 
731 #define SUM_LENGTH 16
732 #define SHORT_SUM_LENGTH 2
733 #define BLOCKSUM_BIAS 10
734 
735 #ifndef MAXPATHLEN
736 #define MAXPATHLEN 1024
737 #endif
738 
739 /* We want a roomy line buffer that can hold more than MAXPATHLEN,
740  * and significantly more than an overly short MAXPATHLEN. */
741 #if MAXPATHLEN < 4096
742 #define BIGPATHBUFLEN (4096+1024)
743 #else
744 #define BIGPATHBUFLEN (MAXPATHLEN+1024)
745 #endif
746 
747 #ifndef NAME_MAX
748 #define NAME_MAX 255
749 #endif
750 
751 #ifndef SIZE_MAX
752 #define SIZE_MAX ((size_t)-1)
753 #endif
754 
755 #ifndef INADDR_NONE
756 #define INADDR_NONE 0xffffffff
757 #endif
758 
759 #ifndef IN_LOOPBACKNET
760 #define IN_LOOPBACKNET 127
761 #endif
762 
763 #if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
764 #define ACLS_NEED_MASK 1
765 #endif
766 
767 #if defined HAVE_FALLOCATE || HAVE_SYS_FALLOCATE
768 #ifdef HAVE_LINUX_FALLOC_H
769 #include <linux/falloc.h>
770 #endif
771 #ifdef FALLOC_FL_KEEP_SIZE
772 #define SUPPORT_PREALLOCATION 1
773 #elif defined HAVE_FTRUNCATE
774 #define SUPPORT_PREALLOCATION 1
775 #define PREALLOCATE_NEEDS_TRUNCATE 1
776 #endif
777 #else /* !fallocate */
778 #if defined HAVE_EFFICIENT_POSIX_FALLOCATE && defined HAVE_FTRUNCATE
779 #define SUPPORT_PREALLOCATION 1
780 #define PREALLOCATE_NEEDS_TRUNCATE 1
781 #endif
782 #endif
783 
784 #if SIZEOF_CHARP == 4
785 # define PTRS_ARE_32 1
786 # define PTR_EXTRA_CNT 1
787 #elif SIZEOF_CHARP == 8
788 # define PTRS_ARE_64 1
789 # define PTR_EXTRA_CNT EXTRA64_CNT
790 #else
791 # error Character pointers are not 4 or 8 bytes.
792 #endif
793 
794 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
795 #define USE_FLEXIBLE_ARRAY 1
796 #endif
797 
798 union file_extras {
799 	int32 num;
800 	uint32 unum;
801 #ifdef PTRS_ARE_32
802 	const char* ptr;
803 #endif
804 };
805 
806 union file_extras64 {
807 	int64 num;
808 #ifdef PTRS_ARE_64
809 	const char* ptr;
810 #endif
811 };
812 
813 struct file_struct {
814 	const char *dirname;	/* The dir info inside the transfer */
815 	time_t modtime;		/* When the item was last modified */
816 	uint32 len32;		/* Lowest 32 bits of the file's length */
817 	uint16 mode;		/* The item's type and permissions */
818 	uint16 flags;		/* The FLAG_* bits for this item */
819 #ifdef USE_FLEXIBLE_ARRAY
820 	const char basename[];	/* The basename (AKA filename) follows */
821 #else
822 	const char basename[1];	/* A kluge that should work like a flexible array */
823 #endif
824 };
825 
826 extern int file_extra_cnt;
827 extern int inc_recurse;
828 extern int atimes_ndx;
829 extern int crtimes_ndx;
830 extern int pathname_ndx;
831 extern int depth_ndx;
832 extern int uid_ndx;
833 extern int gid_ndx;
834 extern int fileflags_ndx;
835 extern int acls_ndx;
836 extern int xattrs_ndx;
837 
838 #ifdef USE_FLEXIBLE_ARRAY
839 #define FILE_STRUCT_LEN (sizeof (struct file_struct))
840 #else
841 #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
842 #endif
843 #define EXTRA_LEN (sizeof (union file_extras))
844 #define DEV_EXTRA_CNT 2
845 #define DIRNODE_EXTRA_CNT 3
846 #define EXTRA64_CNT ((sizeof (union file_extras64) + EXTRA_LEN - 1) / EXTRA_LEN)
847 #define SUM_EXTRA_CNT ((MAX_DIGEST_LEN + EXTRA_LEN - 1) / EXTRA_LEN)
848 
849 #define REQ_EXTRA(f,ndx) ((union file_extras*)(f) - (ndx))
850 #define OPT_EXTRA(f,bump) ((union file_extras*)(f) - file_extra_cnt - 1 - (bump))
851 
852 /* These are guaranteed to be allocated first in the array so that they
853  * are aligned for direct int64-pointer access. */
854 #define REQ_EXTRA64(f,ndx) ((union file_extras64*)REQ_EXTRA(f,ndx))
855 
856 #define NSEC_BUMP(f) ((f)->flags & FLAG_MOD_NSEC ? 1 : 0)
857 #define LEN64_BUMP(f) ((f)->flags & FLAG_LENGTH64 ? 1 : 0)
858 #define START_BUMP(f) (NSEC_BUMP(f) + LEN64_BUMP(f))
859 #define HLINK_BUMP(f) ((f)->flags & (FLAG_HLINKED|FLAG_HLINK_DONE) ? inc_recurse+1 : 0)
860 #define ACL_BUMP(f) (acls_ndx ? 1 : 0)
861 
862 /* The length applies to all items. */
863 #if SIZEOF_INT64 < 8
864 #define F_LENGTH(f) ((int64)(f)->len32)
865 #else
866 #define F_HIGH_LEN(f) (OPT_EXTRA(f, NSEC_BUMP(f))->unum)
867 #define F_LENGTH(f) ((int64)(f)->len32 + ((f)->flags & FLAG_LENGTH64 ? (int64)F_HIGH_LEN(f) << 32 : 0))
868 #endif
869 
870 #define F_MOD_NSEC(f) OPT_EXTRA(f, 0)->unum
871 #define F_MOD_NSEC_or_0(f) ((f)->flags & FLAG_MOD_NSEC ? F_MOD_NSEC(f) : 0)
872 
873 /* If there is a symlink string, it is always right after the basename */
874 #define F_SYMLINK(f) ((f)->basename + strlen((f)->basename) + 1)
875 
876 /* The sending side always has this available: */
877 #ifdef PTRS_ARE_32
878 #define F_PATHNAME(f) REQ_EXTRA(f, pathname_ndx)->ptr
879 #else
880 #define F_PATHNAME(f) REQ_EXTRA64(f, pathname_ndx)->ptr
881 #endif
882 
883 /* The receiving side always has this available: */
884 #define F_DEPTH(f) REQ_EXTRA(f, depth_ndx)->num
885 
886 /* When the associated option is on, all entries will have these present: */
887 #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum
888 #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum
889 #if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
890 #define F_FFLAGS(f) REQ_EXTRA(f, fileflags_ndx)->unum
891 #else
892 #define F_FFLAGS(f) NO_FFLAGS
893 #endif
894 #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num
895 #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num
896 #define F_NDX(f) REQ_EXTRA(f, unsort_ndx)->num
897 #define F_ATIME(f) REQ_EXTRA64(f, atimes_ndx)->num
898 #define F_CRTIME(f) REQ_EXTRA64(f, crtimes_ndx)->num
899 
900 /* These items are per-entry optional: */
901 #define F_HL_GNUM(f) OPT_EXTRA(f, START_BUMP(f))->num /* non-dirs */
902 #define F_HL_PREV(f) OPT_EXTRA(f, START_BUMP(f)+inc_recurse)->num /* non-dirs */
903 #define F_DIR_NODE_P(f) (&OPT_EXTRA(f, START_BUMP(f) \
904 				+ DIRNODE_EXTRA_CNT - 1)->num) /* sender dirs */
905 #define F_DIR_RELNAMES_P(f) (&OPT_EXTRA(f, START_BUMP(f) + DIRNODE_EXTRA_CNT \
906 				+ PTR_EXTRA_CNT - 1)->num) /* sender dirs */
907 #define F_DIR_DEFACL(f) OPT_EXTRA(f, START_BUMP(f))->unum /* receiver dirs */
908 #define F_DIR_DEV_P(f) (&OPT_EXTRA(f, START_BUMP(f) + ACL_BUMP(f) \
909 				+ DEV_EXTRA_CNT - 1)->unum) /* receiver dirs */
910 
911 /* This optional item might follow an F_HL_*() item. */
912 #define F_RDEV_P(f) (&OPT_EXTRA(f, START_BUMP(f) + HLINK_BUMP(f) + DEV_EXTRA_CNT - 1)->unum)
913 
914 /* The sum is only present on regular files. */
915 #define F_SUM(f) ((char*)OPT_EXTRA(f, START_BUMP(f) + HLINK_BUMP(f) \
916 				    + SUM_EXTRA_CNT - 1))
917 
918 /* Some utility defines: */
919 #define F_IS_ACTIVE(f) (f)->basename[0]
920 #define F_IS_HLINKED(f) ((f)->flags & FLAG_HLINKED)
921 
922 #define F_HLINK_NOT_FIRST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_FIRST)
923 #define F_HLINK_NOT_LAST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_LAST)
924 
925 /* These access the F_DIR_DEV_P() and F_RDEV_P() values: */
926 #define DEV_MAJOR(a) (a)[0]
927 #define DEV_MINOR(a) (a)[1]
928 
929 /* These access the F_DIRS_NODE_P() values: */
930 #define DIR_PARENT(a) (a)[0]
931 #define DIR_FIRST_CHILD(a) (a)[1]
932 #define DIR_NEXT_SIBLING(a) (a)[2]
933 
934 #define IS_MISSING_FILE(statbuf) ((statbuf).st_mode == 0)
935 
936 /*
937  * Start the flist array at FLIST_START entries and grow it
938  * by doubling until FLIST_LINEAR then grow by FLIST_LINEAR
939  */
940 #define FLIST_START	(32 * 1024)
941 #define FLIST_LINEAR	(FLIST_START * 512)
942 
943 /*
944  * Extent size for allocation pools: A minimum size of 128KB
945  * is needed to mmap them so that freeing will release the
946  * space to the OS.
947  *
948  * Larger sizes reduce leftover fragments and speed free calls
949  * (when they happen). Smaller sizes increase the chance of
950  * freed allocations freeing whole extents.
951  */
952 #define NORMAL_EXTENT	(256 * 1024)
953 #define SMALL_EXTENT	(128 * 1024)
954 
955 #define FLIST_TEMP	(1<<1)
956 
957 struct file_list {
958 	struct file_list *next, *prev;
959 	struct file_struct **files, **sorted;
960 	alloc_pool_t file_pool;
961 	void *pool_boundary;
962 	int used, malloced;
963 	int low, high;  /* 0-relative index values excluding empties */
964 	int ndx_start;  /* the start offset for inc_recurse mode */
965 	int flist_num;  /* 1-relative file_list number or 0 */
966 	int parent_ndx; /* dir_flist index of parent directory */
967 	int in_progress, to_redo;
968 };
969 
970 #define SUMFLG_SAME_OFFSET	(1<<0)
971 
972 struct sum_buf {
973 	OFF_T offset;		/**< offset in file of this chunk */
974 	int32 len;		/**< length of chunk of file */
975 	uint32 sum1;	        /**< simple checksum */
976 	int32 chain;		/**< next hash-table collision */
977 	short flags;		/**< flag bits */
978 	char sum2[SUM_LENGTH];	/**< checksum  */
979 };
980 
981 struct sum_struct {
982 	OFF_T flength;		/**< total file length */
983 	struct sum_buf *sums;	/**< points to info for each chunk */
984 	int32 count;		/**< how many chunks */
985 	int32 blength;		/**< block_length */
986 	int32 remainder;	/**< flength % block_length */
987 	int s2length;		/**< sum2_length */
988 };
989 
990 struct map_struct {
991 	OFF_T file_size;	/* File size (from stat)		*/
992 	OFF_T p_offset;		/* Window start				*/
993 	OFF_T p_fd_offset;	/* offset of cursor in fd ala lseek	*/
994 	char *p;		/* Window pointer			*/
995 	int32 p_size;		/* Largest window size we allocated	*/
996 	int32 p_len;		/* Latest (rounded) window size		*/
997 	int32 def_window_size;	/* Default window size			*/
998 	int fd;			/* File Descriptor			*/
999 	int status;		/* first errno from read errors		*/
1000 };
1001 
1002 #define NAME_IS_FILE		(0)    /* filter name as a file */
1003 #define NAME_IS_DIR		(1<<0) /* filter name as a dir */
1004 #define NAME_IS_XATTR		(1<<2) /* filter name as an xattr */
1005 
1006 #define FILTRULE_WILD		(1<<0) /* pattern has '*', '[', and/or '?' */
1007 #define FILTRULE_WILD2		(1<<1) /* pattern has '**' */
1008 #define FILTRULE_WILD2_PREFIX	(1<<2) /* pattern starts with "**" */
1009 #define FILTRULE_WILD3_SUFFIX	(1<<3) /* pattern ends with "***" */
1010 #define FILTRULE_ABS_PATH	(1<<4) /* path-match on absolute path */
1011 #define FILTRULE_INCLUDE	(1<<5) /* this is an include, not an exclude */
1012 #define FILTRULE_DIRECTORY	(1<<6) /* this matches only directories */
1013 #define FILTRULE_WORD_SPLIT	(1<<7) /* split rules on whitespace */
1014 #define FILTRULE_NO_INHERIT	(1<<8) /* don't inherit these rules */
1015 #define FILTRULE_NO_PREFIXES	(1<<9) /* parse no prefixes from patterns */
1016 #define FILTRULE_MERGE_FILE	(1<<10)/* specifies a file to merge */
1017 #define FILTRULE_PERDIR_MERGE	(1<<11)/* merge-file is searched per-dir */
1018 #define FILTRULE_EXCLUDE_SELF	(1<<12)/* merge-file name should be excluded */
1019 #define FILTRULE_FINISH_SETUP	(1<<13)/* per-dir merge file needs setup */
1020 #define FILTRULE_NEGATE 	(1<<14)/* rule matches when pattern does not */
1021 #define FILTRULE_CVS_IGNORE	(1<<15)/* rule was -C or :C */
1022 #define FILTRULE_SENDER_SIDE	(1<<16)/* rule applies to the sending side */
1023 #define FILTRULE_RECEIVER_SIDE	(1<<17)/* rule applies to the receiving side */
1024 #define FILTRULE_CLEAR_LIST	(1<<18)/* this item is the "!" token */
1025 #define FILTRULE_PERISHABLE	(1<<19)/* perishable if parent dir goes away */
1026 #define FILTRULE_XATTR		(1<<20)/* rule only applies to xattr names */
1027 
1028 #define FILTRULES_SIDES (FILTRULE_SENDER_SIDE | FILTRULE_RECEIVER_SIDE)
1029 
1030 typedef struct filter_struct {
1031 	struct filter_struct *next;
1032 	char *pattern;
1033 	uint32 rflags;
1034 	union {
1035 		int slash_cnt;
1036 		struct filter_list_struct *mergelist;
1037 	} u;
1038 } filter_rule;
1039 
1040 typedef struct filter_list_struct {
1041 	filter_rule *head;
1042 	filter_rule *tail;
1043 	char *debug_type;
1044 } filter_rule_list;
1045 
1046 struct stats {
1047 	int64 total_size;
1048 	int64 total_transferred_size;
1049 	int64 total_written;
1050 	int64 total_read;
1051 	int64 literal_data;
1052 	int64 matched_data;
1053 	int64 flist_buildtime;
1054 	int64 flist_xfertime;
1055 	int64 flist_size;
1056 	int num_files, num_dirs, num_symlinks, num_devices, num_specials;
1057 	int created_files, created_dirs, created_symlinks, created_devices, created_specials;
1058 	int deleted_files, deleted_dirs, deleted_symlinks, deleted_devices, deleted_specials;
1059 	int xferred_files;
1060 };
1061 
1062 struct chmod_mode_struct;
1063 
1064 struct flist_ndx_item {
1065 	struct flist_ndx_item *next;
1066 	int ndx;
1067 };
1068 
1069 typedef struct {
1070 	struct flist_ndx_item *head, *tail;
1071 } flist_ndx_list;
1072 
1073 #define EMPTY_ITEM_LIST {NULL, 0, 0}
1074 
1075 typedef struct {
1076 	void *items;
1077 	size_t count;
1078 	size_t malloced;
1079 } item_list;
1080 
1081 #define EXPAND_ITEM_LIST(lp, type, incr) \
1082 	(type*)expand_item_list(lp, sizeof (type), #type, incr)
1083 
1084 #define EMPTY_XBUF {NULL, 0, 0, 0}
1085 
1086 typedef struct {
1087 	char *buf;
1088 	size_t pos;  /* pos = read pos in the buf */
1089 	size_t len;  /* len = chars following pos */
1090 	size_t size; /* size = total space in buf */
1091 } xbuf;
1092 
1093 #define INIT_XBUF(xb, str, ln, sz) (xb).buf = (str), (xb).len = (ln), (xb).size = (sz), (xb).pos = 0
1094 #define INIT_XBUF_STRLEN(xb, str) (xb).buf = (str), (xb).len = strlen((xb).buf), (xb).size = (size_t)-1, (xb).pos = 0
1095 /* This one is used to make an output xbuf based on a char[] buffer: */
1096 #define INIT_CONST_XBUF(xb, bf) (xb).buf = (bf), (xb).size = sizeof (bf), (xb).len = (xb).pos = 0
1097 
1098 #define ICB_EXPAND_OUT (1<<0)
1099 #define ICB_INCLUDE_BAD (1<<1)
1100 #define ICB_INCLUDE_INCOMPLETE (1<<2)
1101 #define ICB_CIRCULAR_OUT (1<<3)
1102 #define ICB_INIT (1<<4)
1103 
1104 #define IOBUF_KEEP_BUFS 0
1105 #define IOBUF_FREE_BUFS 1
1106 
1107 #define MPLX_SWITCHING IOBUF_KEEP_BUFS
1108 #define MPLX_ALL_DONE IOBUF_FREE_BUFS
1109 #define MPLX_TO_BUFFERED 2
1110 
1111 #define RL_EOL_NULLS (1<<0)
1112 #define RL_DUMP_COMMENTS (1<<1)
1113 #define RL_CONVERT (1<<2)
1114 
1115 typedef struct {
1116 	char name_type;
1117 #ifdef USE_FLEXIBLE_ARRAY
1118 	char fname[]; /* has variable size */
1119 #else
1120 	char fname[1]; /* A kluge that should work like a flexible array */
1121 #endif
1122 } relnamecache;
1123 
1124 #ifdef USE_FLEXIBLE_ARRAY
1125 #define RELNAMECACHE_LEN (sizeof (relnamecache))
1126 #else
1127 #define RELNAMECACHE_LEN (offsetof(relnamecache, fname))
1128 #endif
1129 
1130 #include "byteorder.h"
1131 #include "lib/mdigest.h"
1132 #include "lib/wildmatch.h"
1133 #include "lib/permstring.h"
1134 #include "lib/addrinfo.h"
1135 
1136 #ifndef __GNUC__
1137 #define __attribute__(x)
1138 #else
1139 # if __GNUC__ <= 2
1140 # define NORETURN
1141 # endif
1142 #endif
1143 
1144 #define UNUSED(x) x __attribute__((__unused__))
1145 #ifndef NORETURN
1146 #define NORETURN __attribute__((__noreturn__))
1147 #endif
1148 
1149 typedef struct {
1150     STRUCT_STAT st;
1151     time_t crtime;
1152 #ifdef SUPPORT_ACLS
1153     struct rsync_acl *acc_acl; /* access ACL */
1154     struct rsync_acl *def_acl; /* default ACL */
1155 #endif
1156 #ifdef SUPPORT_XATTRS
1157     item_list *xattr;
1158 #endif
1159 } stat_x;
1160 
1161 #define ACL_READY(sx) ((sx).acc_acl != NULL)
1162 #define XATTR_READY(sx) ((sx).xattr != NULL)
1163 
1164 #define CLVL_NOT_SPECIFIED INT_MIN
1165 
1166 #define CPRES_AUTO (-1)
1167 #define CPRES_NONE 0
1168 #define CPRES_ZLIB 1
1169 #define CPRES_ZLIBX 2
1170 #define CPRES_LZ4 3
1171 #define CPRES_ZSTD 4
1172 
1173 #define NSTR_CHECKSUM 0
1174 #define NSTR_COMPRESS 1
1175 
1176 struct name_num_item {
1177 	int num;
1178 	const char *name, *main_name;
1179 };
1180 
1181 struct name_num_obj {
1182 	const char *type;
1183 	const char *negotiated_name;
1184 	uchar *saw;
1185 	int saw_len;
1186 	int negotiated_num;
1187 	struct name_num_item list[8]; /* A big-enough len (we'll get a compile error if it is ever too small) */
1188 };
1189 
1190 #ifndef __cplusplus
1191 #include "proto.h"
1192 #endif
1193 
1194 #ifndef SUPPORT_XATTRS
1195 #define x_stat(fn,fst,xst) do_stat(fn,fst)
1196 #define x_lstat(fn,fst,xst) do_lstat(fn,fst)
1197 #define x_fstat(fd,fst,xst) do_fstat(fd,fst)
1198 #endif
1199 
1200 /* We have replacement versions of these if they're missing. */
1201 #ifndef HAVE_ASPRINTF
1202 int asprintf(char **ptr, const char *format, ...);
1203 #endif
1204 
1205 #ifndef HAVE_VASPRINTF
1206 int vasprintf(char **ptr, const char *format, va_list ap);
1207 #endif
1208 
1209 #if !defined HAVE_VSNPRINTF || !defined HAVE_C99_VSNPRINTF
1210 #define vsnprintf rsync_vsnprintf
1211 int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
1212 #endif
1213 
1214 #if !defined HAVE_SNPRINTF || !defined HAVE_C99_VSNPRINTF
1215 #define snprintf rsync_snprintf
1216 int snprintf(char *str, size_t count, const char *fmt,...);
1217 #endif
1218 
1219 #ifndef HAVE_STRERROR
1220 extern char *sys_errlist[];
1221 #define strerror(i) sys_errlist[i]
1222 #endif
1223 
1224 #ifndef HAVE_STRCHR
1225 # define strchr                 index
1226 # define strrchr                rindex
1227 #endif
1228 
1229 #ifndef HAVE_ERRNO_DECL
1230 extern int errno;
1231 #endif
1232 
1233 #ifdef HAVE_READLINK
1234 #define SUPPORT_LINKS 1
1235 #if !defined NO_SYMLINK_XATTRS && !defined NO_SYMLINK_USER_XATTRS
1236 #define do_readlink(path, buf, bufsiz) readlink(path, buf, bufsiz)
1237 #endif
1238 #endif
1239 #ifdef HAVE_LINK
1240 #define SUPPORT_HARD_LINKS 1
1241 #endif
1242 
1243 #ifdef HAVE_SIGACTION
1244 #define SIGACTION(n,h) sigact.sa_handler=(h), sigaction((n),&sigact,NULL)
1245 #define signal(n,h) we_need_to_call_SIGACTION_not_signal(n,h)
1246 #else
1247 #define SIGACTION(n,h) signal(n,h)
1248 #endif
1249 
1250 #ifndef EWOULDBLOCK
1251 #define EWOULDBLOCK EAGAIN
1252 #endif
1253 
1254 #ifndef STDIN_FILENO
1255 #define STDIN_FILENO 0
1256 #endif
1257 
1258 #ifndef STDOUT_FILENO
1259 #define STDOUT_FILENO 1
1260 #endif
1261 
1262 #ifndef STDERR_FILENO
1263 #define STDERR_FILENO 2
1264 #endif
1265 
1266 #ifndef S_IRUSR
1267 #define S_IRUSR 0400
1268 #endif
1269 
1270 #ifndef S_IWUSR
1271 #define S_IWUSR 0200
1272 #endif
1273 
1274 #ifndef ACCESSPERMS
1275 #define ACCESSPERMS 0777
1276 #endif
1277 
1278 #ifndef S_ISVTX
1279 #define S_ISVTX 0
1280 #endif
1281 
1282 #define CHMOD_BITS (S_ISUID | S_ISGID | S_ISVTX | ACCESSPERMS)
1283 
1284 #ifndef _S_IFMT
1285 #define _S_IFMT        0170000
1286 #endif
1287 
1288 #ifndef _S_IFLNK
1289 #define _S_IFLNK  0120000
1290 #endif
1291 
1292 #ifndef S_ISLNK
1293 #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
1294 #endif
1295 
1296 #ifndef S_ISBLK
1297 #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
1298 #endif
1299 
1300 #ifndef S_ISCHR
1301 #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
1302 #endif
1303 
1304 #ifndef S_ISSOCK
1305 #ifdef _S_IFSOCK
1306 #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
1307 #else
1308 #define S_ISSOCK(mode) (0)
1309 #endif
1310 #endif
1311 
1312 #ifndef S_ISFIFO
1313 #ifdef _S_IFIFO
1314 #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
1315 #else
1316 #define S_ISFIFO(mode) (0)
1317 #endif
1318 #endif
1319 
1320 #ifndef S_ISDIR
1321 #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
1322 #endif
1323 
1324 #ifndef S_ISREG
1325 #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
1326 #endif
1327 
1328 /* work out what fcntl flag to use for non-blocking */
1329 #ifdef O_NONBLOCK
1330 # define NONBLOCK_FLAG O_NONBLOCK
1331 #elif defined SYSV
1332 # define NONBLOCK_FLAG O_NDELAY
1333 #else
1334 # define NONBLOCK_FLAG FNDELAY
1335 #endif
1336 
1337 #ifndef INADDR_LOOPBACK
1338 #define INADDR_LOOPBACK 0x7f000001
1339 #endif
1340 
1341 #ifndef INADDR_NONE
1342 #define INADDR_NONE 0xffffffff
1343 #endif
1344 
1345 #define IS_SPECIAL(mode) (S_ISSOCK(mode) || S_ISFIFO(mode))
1346 #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode))
1347 
1348 #define PRESERVE_FILE_TIMES	(1<<0)
1349 #define PRESERVE_DIR_TIMES	(1<<1)
1350 #define PRESERVE_LINK_TIMES	(1<<2)
1351 
1352 /* Initial mask on permissions given to temporary files.  Mask off setuid
1353      bits and group access because of potential race-condition security
1354      holes, and mask other access because mode 707 is bizarre */
1355 #define INITACCESSPERMS 0700
1356 
1357 /* handler for null strings in printf format */
1358 #define NS(s) ((s)?(s):"<NULL>")
1359 
1360 extern char *do_calloc;
1361 
1362 /* Convenient wrappers for malloc and realloc.  Use them. */
1363 #define new(type) ((type*)my_alloc(NULL, sizeof (type), 1, __FILE__, __LINE__))
1364 #define new0(type) ((type*)my_alloc(do_calloc, sizeof (type), 1, __FILE__, __LINE__))
1365 #define realloc_buf(ptr, num) my_alloc((ptr), (num), 1, __FILE__, __LINE__)
1366 
1367 #define new_array(type, num) ((type*)my_alloc(NULL, (num), sizeof (type), __FILE__, __LINE__))
1368 #define new_array0(type, num) ((type*)my_alloc(do_calloc, (num), sizeof (type), __FILE__, __LINE__))
1369 #define realloc_array(ptr, type, num) ((type*)my_alloc((ptr), (num), sizeof (type), __FILE__, __LINE__))
1370 
1371 #undef strdup
1372 #define strdup(s) my_strdup(s, __FILE__, __LINE__)
1373 
1374 #define out_of_memory(msg) _out_of_memory(msg, __FILE__, __LINE__)
1375 #define overflow_exit(msg) _overflow_exit(msg, __FILE__, __LINE__)
1376 
1377 /* use magic gcc attributes to catch format errors */
1378  void rprintf(enum logcode , const char *, ...)
1379      __attribute__((format (printf, 2, 3)))
1380 ;
1381 
1382 /* This is just like rprintf, but it also tries to print some
1383  * representation of the error code.  Normally errcode = errno. */
1384 void rsyserr(enum logcode, int, const char *, ...)
1385      __attribute__((format (printf, 3, 4)))
1386      ;
1387 
1388 /* Make sure that the O_BINARY flag is defined. */
1389 #ifndef O_BINARY
1390 #define O_BINARY 0
1391 #endif
1392 
1393 #ifndef HAVE_STRLCPY
1394 size_t strlcpy(char *d, const char *s, size_t bufsize);
1395 #endif
1396 
1397 #ifndef HAVE_STRLCAT
1398 size_t strlcat(char *d, const char *s, size_t bufsize);
1399 #endif
1400 
1401 #ifndef WEXITSTATUS
1402 #define	WEXITSTATUS(stat)	((int)(((stat)>>8)&0xFF))
1403 #endif
1404 #ifndef WIFEXITED
1405 #define	WIFEXITED(stat)		((int)((stat)&0xFF) == 0)
1406 #endif
1407 
1408 #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)
1409 
1410 #ifdef HAVE_GETEUID
1411 #define MY_UID() geteuid()
1412 #else
1413 #define MY_UID() getuid()
1414 #endif
1415 
1416 #ifdef HAVE_GETEGID
1417 #define MY_GID() getegid()
1418 #else
1419 #define MY_GID() getgid()
1420 #endif
1421 
1422 #ifdef FORCE_FD_ZERO_MEMSET
1423 #undef FD_ZERO
1424 #define FD_ZERO(fdsetp) memset(fdsetp, 0, sizeof (fd_set))
1425 #endif
1426 
1427 extern short info_levels[], debug_levels[];
1428 
1429 #define INFO_GTE(flag, lvl) (info_levels[INFO_##flag] >= (lvl))
1430 #define INFO_EQ(flag, lvl) (info_levels[INFO_##flag] == (lvl))
1431 #define DEBUG_GTE(flag, lvl) (debug_levels[DEBUG_##flag] >= (lvl))
1432 #define DEBUG_EQ(flag, lvl) (debug_levels[DEBUG_##flag] == (lvl))
1433 
1434 #define INFO_BACKUP 0
1435 #define INFO_COPY (INFO_BACKUP+1)
1436 #define INFO_DEL (INFO_COPY+1)
1437 #define INFO_FLIST (INFO_DEL+1)
1438 #define INFO_MISC (INFO_FLIST+1)
1439 #define INFO_MOUNT (INFO_MISC+1)
1440 #define INFO_NAME (INFO_MOUNT+1)
1441 #define INFO_PROGRESS (INFO_NAME+1)
1442 #define INFO_REMOVE (INFO_PROGRESS+1)
1443 #define INFO_SKIP (INFO_REMOVE+1)
1444 #define INFO_STATS (INFO_SKIP+1)
1445 #define INFO_SYMSAFE (INFO_STATS+1)
1446 
1447 #define COUNT_INFO (INFO_SYMSAFE+1)
1448 
1449 #define DEBUG_ACL 0
1450 #define DEBUG_BACKUP (DEBUG_ACL+1)
1451 #define DEBUG_BIND (DEBUG_BACKUP+1)
1452 #define DEBUG_CHDIR (DEBUG_BIND+1)
1453 #define DEBUG_CONNECT (DEBUG_CHDIR+1)
1454 #define DEBUG_CMD (DEBUG_CONNECT+1)
1455 #define DEBUG_DEL (DEBUG_CMD+1)
1456 #define DEBUG_DELTASUM (DEBUG_DEL+1)
1457 #define DEBUG_DUP (DEBUG_DELTASUM+1)
1458 #define DEBUG_EXIT (DEBUG_DUP+1)
1459 #define DEBUG_FILTER (DEBUG_EXIT+1)
1460 #define DEBUG_FLIST (DEBUG_FILTER+1)
1461 #define DEBUG_FUZZY (DEBUG_FLIST+1)
1462 #define DEBUG_GENR (DEBUG_FUZZY+1)
1463 #define DEBUG_HASH (DEBUG_GENR+1)
1464 #define DEBUG_HLINK (DEBUG_HASH+1)
1465 #define DEBUG_ICONV (DEBUG_HLINK+1)
1466 #define DEBUG_IO (DEBUG_ICONV+1)
1467 #define DEBUG_NSTR (DEBUG_IO+1)
1468 #define DEBUG_OWN (DEBUG_NSTR+1)
1469 #define DEBUG_PROTO (DEBUG_OWN+1)
1470 #define DEBUG_RECV (DEBUG_PROTO+1)
1471 #define DEBUG_SEND (DEBUG_RECV+1)
1472 #define DEBUG_TIME (DEBUG_SEND+1)
1473 
1474 #define COUNT_DEBUG (DEBUG_TIME+1)
1475 
1476 #ifndef HAVE_INET_NTOP
1477 const char *inet_ntop(int af, const void *src, char *dst, size_t size);
1478 #endif
1479 
1480 #ifndef HAVE_INET_PTON
1481 int inet_pton(int af, const char *src, void *dst);
1482 #endif
1483 
1484 #ifndef HAVE_GETPASS
1485 char *getpass(const char *prompt);
1486 #endif
1487 
1488 #ifdef MAINTAINER_MODE
1489 const char *get_panic_action(void);
1490 #endif
1491 
1492 #define NOISY_DEATH(msg) do { \
1493     fprintf(stderr, "%s in %s at line %d\n", msg, __FILE__, __LINE__); \
1494     exit_cleanup(RERR_UNSUPPORTED); \
1495 } while (0)
1496