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