Expand the max name_num_item list size.
[rsync.git] / rsync.h
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_RESERVED_16 (1<<16)        /* reserved for future fileflags use */
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 and ALSO update the
112  * SUBPROTOCOL_VERSION if it is not a final (offical) release. */
113 #define PROTOCOL_VERSION 31
114
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
128
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.
133  *
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.
138  *
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. */
145
146 #define MIN_PROTOCOL_VERSION 20
147 #define OLD_PROTOCOL_VERSION 25
148 #define MAX_PROTOCOL_VERSION 40
149
150 #define MIN_FILECNT_LOOKAHEAD 1000
151 #define MAX_FILECNT_LOOKAHEAD 10000
152
153 #define RSYNC_PORT 873
154
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)
161
162 /* For compatibility with older rsyncs */
163 #define OLD_MAX_BLOCK_SIZE ((int32)1 << 29)
164
165 #define ROUND_UP_1024(siz) ((siz) & (1024-1) ? ((siz) | (1024-1)) + 1 : (siz))
166
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)
170
171 #define MAX_ARGS 1000
172 #define MAX_BASIS_DIRS 20
173 #define MAX_SERVER_ARGS (MAX_BASIS_DIRS*2 + 100)
174
175 #define COMPARE_DEST 1
176 #define COPY_DEST 2
177 #define LINK_DEST 3
178
179 #define MPLEX_BASE 7
180
181 #define NO_FILTERS      0
182 #define SERVER_FILTERS  1
183 #define ALL_FILTERS     2
184
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 */
190
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)
196
197 #define MSG_FLUSH       2
198 #define FULL_FLUSH      1
199 #define NORMAL_FLUSH    0
200
201 #define PDIR_CREATE     1
202 #define PDIR_DELETE     0
203
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
211
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() */
233
234 #define SIGNIFICANT_ITEM_FLAGS (~(\
235         ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
236
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)
242
243 #define SP_DEFAULT 0
244 #define SP_KEEP_DOT_DIRS (1<<0)
245
246 #define CD_NORMAL 0
247 #define CD_SKIP_CHDIR 1
248
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) */
258 };
259
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 */
278 };
279
280 enum filetype {
281         FT_UNSUPPORTED, FT_REG, FT_DIR, FT_SYMLINK, FT_SPECIAL, FT_DEVICE
282 };
283
284 #define NDX_DONE -1
285 #define NDX_FLIST_EOF -2
286 #define NDX_DEL_STATS -3
287 #define NDX_FLIST_OFFSET -101
288
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 */
299
300 #define DEL_MAKE_ROOM (DEL_FOR_FILE|DEL_FOR_DIR|DEL_FOR_SYMLINK|DEL_FOR_DEVICE|DEL_FOR_SPECIAL)
301
302 enum delret {
303         DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
304 };
305
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 */
309
310 /* Defines for maybe_send_keepalive() */
311 #define MSK_ALLOW_FLUSH         (1<<0)
312 #define MSK_ACTIVE_RECEIVER     (1<<1)
313
314 #include "errcode.h"
315
316 #include "config.h"
317
318 /* The default RSYNC_RSH is always set in config.h. */
319
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_STRINGS_H
342 # include <strings.h>
343 #endif
344 #ifdef HAVE_INTTYPES_H
345 # include <inttypes.h>
346 #endif
347 #ifdef HAVE_STDINT_H
348 # include <stdint.h>
349 #endif
350 #ifdef HAVE_UNISTD_H
351 # include <unistd.h>
352 #endif
353
354 #ifdef HAVE_SYS_PARAM_H
355 #include <sys/param.h>
356 #endif
357
358 #if defined HAVE_MALLOC_H && (defined HAVE_MALLINFO || !defined HAVE_STDLIB_H)
359 #include <malloc.h>
360 #endif
361
362 #ifdef HAVE_SYS_SOCKET_H
363 #include <sys/socket.h>
364 #endif
365
366 #ifdef TIME_WITH_SYS_TIME
367 #include <sys/time.h>
368 #include <time.h>
369 #else
370 #ifdef HAVE_SYS_TIME_H
371 #include <sys/time.h>
372 #else
373 #include <time.h>
374 #endif
375 #endif
376
377 #ifdef HAVE_FCNTL_H
378 #include <fcntl.h>
379 #else
380 #ifdef HAVE_SYS_FCNTL_H
381 #include <sys/fcntl.h>
382 #endif
383 #endif
384
385 #ifdef HAVE_SYS_IOCTL_H
386 #include <sys/ioctl.h>
387 #endif
388
389 #ifdef HAVE_SYS_FILIO_H
390 #include <sys/filio.h>
391 #endif
392
393 #include <signal.h>
394 #ifdef HAVE_SYS_WAIT_H
395 #include <sys/wait.h>
396 #endif
397 #ifdef HAVE_CTYPE_H
398 #include <ctype.h>
399 #endif
400 #ifdef HAVE_GRP_H
401 #include <grp.h>
402 #endif
403 #include <errno.h>
404
405 #ifdef HAVE_UTIME_H
406 #include <utime.h>
407 #endif
408
409 #if defined HAVE_UTIMENSAT || defined HAVE_LUTIMES || defined HAVE_SETATTRLIST
410 #define CAN_SET_SYMLINK_TIMES 1
411 #endif
412
413 #if defined HAVE_LCHOWN || defined CHOWN_MODIFIES_SYMLINK
414 #define CAN_CHOWN_SYMLINK 1
415 #endif
416
417 #if defined HAVE_LCHMOD || defined HAVE_SETATTRLIST
418 #define CAN_CHMOD_SYMLINK 1
419 #endif
420
421 #if defined HAVE_UTIMENSAT || defined HAVE_SETATTRLIST
422 #define CAN_SET_NSEC 1
423 #endif
424
425 #ifdef CAN_SET_NSEC
426 #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
427 #define ST_MTIME_NSEC st_mtim.tv_nsec
428 #define ST_ATIME_NSEC st_atim.tv_nsec
429 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
430 #define ST_MTIME_NSEC st_mtimensec
431 #define ST_ATIME_NSEC st_atimensec
432 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
433 #define ST_MTIME_NSEC st_mtimespec.tv_nsec
434 #define ST_ATIME_NSEC st_atimespec.tv_nsec
435 #endif
436 #endif
437
438 #ifdef HAVE_SYS_SELECT_H
439 #include <sys/select.h>
440 #endif
441
442 #ifdef HAVE_SYS_MODE_H
443 /* apparently AIX needs this for S_ISLNK */
444 #ifndef S_ISLNK
445 #include <sys/mode.h>
446 #endif
447 #endif
448
449 /* these are needed for the uid/gid mapping code */
450 #include <pwd.h>
451 #include <grp.h>
452
453 #include <stdarg.h>
454 #include <netinet/in.h>
455 #include <arpa/inet.h>
456 #ifdef HAVE_NETDB_H
457 #include <netdb.h>
458 #endif
459 #include <syslog.h>
460 #ifdef HAVE_SYS_FILE_H
461 #include <sys/file.h>
462 #endif
463
464 #ifdef HAVE_DIRENT_H
465 # include <dirent.h>
466 #else
467 # define dirent direct
468 # ifdef HAVE_SYS_NDIR_H
469 #  include <sys/ndir.h>
470 # endif
471 # ifdef HAVE_SYS_DIR_H
472 #  include <sys/dir.h>
473 # endif
474 # ifdef HAVE_NDIR_H
475 #  include <ndir.h>
476 # endif
477 #endif
478
479 #ifdef MAJOR_IN_MKDEV
480 #include <sys/mkdev.h>
481 # if !defined makedev && (defined mkdev || defined _WIN32 || defined __WIN32__)
482 #  define makedev mkdev
483 # endif
484 #elif defined MAJOR_IN_SYSMACROS
485 #include <sys/sysmacros.h>
486 #endif
487
488 #ifdef MAKEDEV_TAKES_3_ARGS
489 #define MAKEDEV(devmajor,devminor) makedev(0,devmajor,devminor)
490 #else
491 #ifndef __TANDEM
492 #define MAKEDEV(devmajor,devminor) makedev(devmajor,devminor)
493 #else
494 # include <sys/stat.h>
495 # define major DEV_TO_MAJOR
496 # define minor DEV_TO_MINOR
497 # define MAKEDEV MAJORMINOR_TO_DEV
498 #endif
499 #endif
500
501 #ifdef __TANDEM
502 # include <floss.h(floss_read,floss_write,floss_fork,floss_execvp)>
503 # include <floss.h(floss_getpwuid,floss_select,floss_seteuid)>
504 # define S_IEXEC S_IXUSR
505 # define ROOT_UID 65535
506 #else
507 # define ROOT_UID 0
508 #endif
509
510 #ifdef HAVE_COMPAT_H
511 #include <compat.h>
512 #endif
513
514 #ifdef HAVE_LIMITS_H
515 # include <limits.h>
516 #endif
517
518 #if defined USE_ICONV_OPEN && defined HAVE_ICONV_H
519 #include <iconv.h>
520 #ifndef ICONV_CONST
521 #define ICONV_CONST
522 #endif
523 #else
524 #ifdef ICONV_CONST
525 #undef ICONV_CONST
526 #endif
527 #ifdef ICONV_OPTION
528 #undef ICONV_OPTION
529 #endif
530 #ifdef iconv_t
531 #undef iconv_t
532 #endif
533 #define iconv_t int
534 #endif
535
536 #include <assert.h>
537
538 #include "lib/pool_alloc.h"
539
540 #ifndef HAVE_ID_T
541 typedef unsigned int id_t;
542 #endif
543 #ifndef HAVE_PID_T
544 typedef int pid_t;
545 #endif
546 #ifndef HAVE_MODE_T
547 typedef unsigned int mode_t;
548 #endif
549 #ifndef HAVE_OFF_T
550 typedef long off_t;
551 #undef SIZEOF_OFF_T
552 #define SIZEOF_OFF_T SIZEOF_LONG
553 #endif
554 #ifndef HAVE_SIZE_T
555 typedef unsigned int size_t;
556 #endif
557
558 #define BOOL int
559
560 #ifndef uchar
561 #define uchar unsigned char
562 #endif
563
564 #ifdef SIGNED_CHAR_OK
565 #define schar signed char
566 #else
567 #define schar char
568 #endif
569
570 #ifndef int16
571 #if SIZEOF_INT16_T == 2
572 # define int16 int16_t
573 #else
574 # define int16 short
575 #endif
576 #endif
577
578 #ifndef uint16
579 #if SIZEOF_UINT16_T == 2
580 # define uint16 uint16_t
581 #else
582 # define uint16 unsigned int16
583 #endif
584 #endif
585
586 #ifndef __APPLE__ /* Do we need a configure check for this? */
587 #define SUPPORT_ATIMES 1
588 #endif
589
590 #ifdef HAVE_GETATTRLIST
591 #define SUPPORT_CRTIMES 1
592 #endif
593
594 /* Find a variable that is either exactly 32-bits or longer.
595  * If some code depends on 32-bit truncation, it will need to
596  * take special action in a "#if SIZEOF_INT32 > 4" section. */
597 #ifndef int32
598 #if SIZEOF_INT32_T == 4
599 # define int32 int32_t
600 # define SIZEOF_INT32 4
601 #elif SIZEOF_INT == 4
602 # define int32 int
603 # define SIZEOF_INT32 4
604 #elif SIZEOF_LONG == 4
605 # define int32 long
606 # define SIZEOF_INT32 4
607 #elif SIZEOF_SHORT == 4
608 # define int32 short
609 # define SIZEOF_INT32 4
610 #elif SIZEOF_INT > 4
611 # define int32 int
612 # define SIZEOF_INT32 SIZEOF_INT
613 #elif SIZEOF_LONG > 4
614 # define int32 long
615 # define SIZEOF_INT32 SIZEOF_LONG
616 #else
617 # error Could not find a 32-bit integer variable
618 #endif
619 #else
620 # define SIZEOF_INT32 4
621 #endif
622
623 #ifndef uint32
624 #if SIZEOF_UINT32_T == 4
625 # define uint32 uint32_t
626 #else
627 # define uint32 unsigned int32
628 #endif
629 #endif
630
631 #if SIZEOF_OFF_T == 8 || !SIZEOF_OFF64_T || !defined HAVE_STRUCT_STAT64
632 #define OFF_T off_t
633 #define STRUCT_STAT struct stat
634 #define SIZEOF_CAPITAL_OFF_T SIZEOF_OFF_T
635 #else
636 #define OFF_T off64_t
637 #define STRUCT_STAT struct stat64
638 #define USE_STAT64_FUNCS 1
639 #define SIZEOF_CAPITAL_OFF_T SIZEOF_OFF64_T
640 #endif
641
642 /* CAVEAT: on some systems, int64 will really be a 32-bit integer IFF
643  * that's the maximum size the file system can handle and there is no
644  * 64-bit type available.  The rsync source must therefore take steps
645  * to ensure that any code that really requires a 64-bit integer has
646  * it (e.g. the checksum code uses two 32-bit integers for its 64-bit
647  * counter). */
648 #if SIZEOF_INT64_T == 8
649 # define int64 int64_t
650 # define SIZEOF_INT64 8
651 #elif SIZEOF_LONG == 8
652 # define int64 long
653 # define SIZEOF_INT64 8
654 #elif SIZEOF_INT == 8
655 # define int64 int
656 # define SIZEOF_INT64 8
657 #elif SIZEOF_LONG_LONG == 8
658 # define int64 long long
659 # define SIZEOF_INT64 8
660 #elif SIZEOF_OFF64_T == 8
661 # define int64 off64_t
662 # define SIZEOF_INT64 8
663 #elif SIZEOF_OFF_T == 8
664 # define int64 off_t
665 # define SIZEOF_INT64 8
666 #elif SIZEOF_INT > 8
667 # define int64 int
668 # define SIZEOF_INT64 SIZEOF_INT
669 #elif SIZEOF_LONG > 8
670 # define int64 long
671 # define SIZEOF_INT64 SIZEOF_LONG
672 #elif SIZEOF_LONG_LONG > 8
673 # define int64 long long
674 # define SIZEOF_INT64 SIZEOF_LONG_LONG
675 #else
676 /* As long as it gets... */
677 # define int64 off_t
678 # define SIZEOF_INT64 SIZEOF_OFF_T
679 #endif
680
681 #define HT_KEY32 0
682 #define HT_KEY64 1
683
684 struct hashtable {
685         void *nodes;
686         int32 size, entries;
687         uint32 node_size;
688         short key64;
689 };
690
691 struct ht_int32_node {
692         void *data;
693         int32 key;
694 };
695
696 struct ht_int64_node {
697         void *data;
698         int64 key;
699 };
700
701 #define HT_NODE(tbl, bkts, i) ((void*)((char*)(bkts) + (i)*(tbl)->node_size))
702 #define HT_KEY(node, k64) ((k64)? ((struct ht_int64_node*)(node))->key \
703                          : (int64)((struct ht_int32_node*)(node))->key)
704
705 #ifndef MIN
706 #define MIN(a,b) ((a)<(b)?(a):(b))
707 #endif
708
709 #ifndef MAX
710 #define MAX(a,b) ((a)>(b)?(a):(b))
711 #endif
712
713 #ifndef MAXHOSTNAMELEN
714 #define MAXHOSTNAMELEN 256
715 #endif
716
717 #define SUM_LENGTH 16
718 #define SHORT_SUM_LENGTH 2
719 #define BLOCKSUM_BIAS 10
720
721 #ifndef MAXPATHLEN
722 #define MAXPATHLEN 1024
723 #endif
724
725 /* We want a roomy line buffer that can hold more than MAXPATHLEN,
726  * and significantly more than an overly short MAXPATHLEN. */
727 #if MAXPATHLEN < 4096
728 #define BIGPATHBUFLEN (4096+1024)
729 #else
730 #define BIGPATHBUFLEN (MAXPATHLEN+1024)
731 #endif
732
733 #ifndef NAME_MAX
734 #define NAME_MAX 255
735 #endif
736
737 #ifndef SIZE_MAX
738 #define SIZE_MAX ((size_t)-1)
739 #endif
740
741 #ifndef INADDR_NONE
742 #define INADDR_NONE 0xffffffff
743 #endif
744
745 #ifndef IN_LOOPBACKNET
746 #define IN_LOOPBACKNET 127
747 #endif
748
749 #if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
750 #define ACLS_NEED_MASK 1
751 #endif
752
753 #if defined HAVE_FALLOCATE || HAVE_SYS_FALLOCATE
754 #ifdef HAVE_LINUX_FALLOC_H
755 #include <linux/falloc.h>
756 #endif
757 #ifdef FALLOC_FL_KEEP_SIZE
758 #define SUPPORT_PREALLOCATION 1
759 #elif defined HAVE_FTRUNCATE
760 #define SUPPORT_PREALLOCATION 1
761 #define PREALLOCATE_NEEDS_TRUNCATE 1
762 #endif
763 #else /* !fallocate */
764 #if defined HAVE_EFFICIENT_POSIX_FALLOCATE && defined HAVE_FTRUNCATE
765 #define SUPPORT_PREALLOCATION 1
766 #define PREALLOCATE_NEEDS_TRUNCATE 1
767 #endif
768 #endif
769
770 #if SIZEOF_CHARP == 4
771 # define PTRS_ARE_32 1
772 # define PTR_EXTRA_CNT 1
773 #elif SIZEOF_CHARP == 8
774 # define PTRS_ARE_64 1
775 # define PTR_EXTRA_CNT EXTRA64_CNT
776 #else
777 # error Character pointers are not 4 or 8 bytes.
778 #endif
779
780 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
781 #define USE_FLEXIBLE_ARRAY 1
782 #endif
783
784 union file_extras {
785         int32 num;
786         uint32 unum;
787 #ifdef PTRS_ARE_32
788         const char* ptr;
789 #endif
790 };
791
792 union file_extras64 {
793         int64 num;
794 #ifdef PTRS_ARE_64
795         const char* ptr;
796 #endif
797 };
798
799 struct file_struct {
800         const char *dirname;    /* The dir info inside the transfer */
801         time_t modtime;         /* When the item was last modified */
802         uint32 len32;           /* Lowest 32 bits of the file's length */
803         uint16 mode;            /* The item's type and permissions */
804         uint16 flags;           /* The FLAG_* bits for this item */
805 #ifdef USE_FLEXIBLE_ARRAY
806         const char basename[];  /* The basename (AKA filename) follows */
807 #else
808         const char basename[1]; /* A kluge that should work like a flexible array */
809 #endif
810 };
811
812 extern int file_extra_cnt;
813 extern int inc_recurse;
814 extern int atimes_ndx;
815 extern int crtimes_ndx;
816 extern int pathname_ndx;
817 extern int depth_ndx;
818 extern int uid_ndx;
819 extern int gid_ndx;
820 extern int acls_ndx;
821 extern int xattrs_ndx;
822
823 #ifdef USE_FLEXIBLE_ARRAY
824 #define FILE_STRUCT_LEN (sizeof (struct file_struct))
825 #else
826 #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
827 #endif
828 #define EXTRA_LEN (sizeof (union file_extras))
829 #define DEV_EXTRA_CNT 2
830 #define DIRNODE_EXTRA_CNT 3
831 #define EXTRA64_CNT ((sizeof (union file_extras64) + EXTRA_LEN - 1) / EXTRA_LEN)
832 #define SUM_EXTRA_CNT ((MAX_DIGEST_LEN + EXTRA_LEN - 1) / EXTRA_LEN)
833
834 #define REQ_EXTRA(f,ndx) ((union file_extras*)(f) - (ndx))
835 #define OPT_EXTRA(f,bump) ((union file_extras*)(f) - file_extra_cnt - 1 - (bump))
836
837 /* These are guaranteed to be allocated first in the array so that they
838  * are aligned for direct int64-pointer access. */
839 #define REQ_EXTRA64(f,ndx) ((union file_extras64*)REQ_EXTRA(f,ndx))
840
841 #define NSEC_BUMP(f) ((f)->flags & FLAG_MOD_NSEC ? 1 : 0)
842 #define LEN64_BUMP(f) ((f)->flags & FLAG_LENGTH64 ? 1 : 0)
843 #define START_BUMP(f) (NSEC_BUMP(f) + LEN64_BUMP(f))
844 #define HLINK_BUMP(f) ((f)->flags & (FLAG_HLINKED|FLAG_HLINK_DONE) ? inc_recurse+1 : 0)
845 #define ACL_BUMP(f) (acls_ndx ? 1 : 0)
846
847 /* The length applies to all items. */
848 #if SIZEOF_INT64 < 8
849 #define F_LENGTH(f) ((int64)(f)->len32)
850 #else
851 #define F_HIGH_LEN(f) (OPT_EXTRA(f, NSEC_BUMP(f))->unum)
852 #define F_LENGTH(f) ((int64)(f)->len32 + ((f)->flags & FLAG_LENGTH64 ? (int64)F_HIGH_LEN(f) << 32 : 0))
853 #endif
854
855 #define F_MOD_NSEC(f) OPT_EXTRA(f, 0)->unum
856 #define F_MOD_NSEC_or_0(f) ((f)->flags & FLAG_MOD_NSEC ? F_MOD_NSEC(f) : 0)
857
858 /* If there is a symlink string, it is always right after the basename */
859 #define F_SYMLINK(f) ((f)->basename + strlen((f)->basename) + 1)
860
861 /* The sending side always has this available: */
862 #ifdef PTRS_ARE_32
863 #define F_PATHNAME(f) REQ_EXTRA(f, pathname_ndx)->ptr
864 #else
865 #define F_PATHNAME(f) REQ_EXTRA64(f, pathname_ndx)->ptr
866 #endif
867
868 /* The receiving side always has this available: */
869 #define F_DEPTH(f) REQ_EXTRA(f, depth_ndx)->num
870
871 /* When the associated option is on, all entries will have these present: */
872 #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum
873 #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum
874 #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num
875 #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num
876 #define F_NDX(f) REQ_EXTRA(f, unsort_ndx)->num
877 #define F_ATIME(f) REQ_EXTRA64(f, atimes_ndx)->num
878 #define F_CRTIME(f) REQ_EXTRA64(f, crtimes_ndx)->num
879
880 /* These items are per-entry optional: */
881 #define F_HL_GNUM(f) OPT_EXTRA(f, START_BUMP(f))->num /* non-dirs */
882 #define F_HL_PREV(f) OPT_EXTRA(f, START_BUMP(f)+inc_recurse)->num /* non-dirs */
883 #define F_DIR_NODE_P(f) (&OPT_EXTRA(f, START_BUMP(f) \
884                                 + DIRNODE_EXTRA_CNT - 1)->num) /* sender dirs */
885 #define F_DIR_RELNAMES_P(f) (&OPT_EXTRA(f, START_BUMP(f) + DIRNODE_EXTRA_CNT \
886                                 + PTR_EXTRA_CNT - 1)->num) /* sender dirs */
887 #define F_DIR_DEFACL(f) OPT_EXTRA(f, START_BUMP(f))->unum /* receiver dirs */
888 #define F_DIR_DEV_P(f) (&OPT_EXTRA(f, START_BUMP(f) + ACL_BUMP(f) \
889                                 + DEV_EXTRA_CNT - 1)->unum) /* receiver dirs */
890
891 /* This optional item might follow an F_HL_*() item. */
892 #define F_RDEV_P(f) (&OPT_EXTRA(f, START_BUMP(f) + HLINK_BUMP(f) + DEV_EXTRA_CNT - 1)->unum)
893
894 /* The sum is only present on regular files. */
895 #define F_SUM(f) ((char*)OPT_EXTRA(f, START_BUMP(f) + HLINK_BUMP(f) \
896                                     + SUM_EXTRA_CNT - 1))
897
898 /* Some utility defines: */
899 #define F_IS_ACTIVE(f) (f)->basename[0]
900 #define F_IS_HLINKED(f) ((f)->flags & FLAG_HLINKED)
901
902 #define F_HLINK_NOT_FIRST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_FIRST)
903 #define F_HLINK_NOT_LAST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_LAST)
904
905 /* These access the F_DIR_DEV_P() and F_RDEV_P() values: */
906 #define DEV_MAJOR(a) (a)[0]
907 #define DEV_MINOR(a) (a)[1]
908
909 /* These access the F_DIRS_NODE_P() values: */
910 #define DIR_PARENT(a) (a)[0]
911 #define DIR_FIRST_CHILD(a) (a)[1]
912 #define DIR_NEXT_SIBLING(a) (a)[2]
913
914 #define IS_MISSING_FILE(statbuf) ((statbuf).st_mode == 0)
915
916 /*
917  * Start the flist array at FLIST_START entries and grow it
918  * by doubling until FLIST_LINEAR then grow by FLIST_LINEAR
919  */
920 #define FLIST_START     (32 * 1024)
921 #define FLIST_LINEAR    (FLIST_START * 512)
922
923 /*
924  * Extent size for allocation pools: A minimum size of 128KB
925  * is needed to mmap them so that freeing will release the
926  * space to the OS.
927  *
928  * Larger sizes reduce leftover fragments and speed free calls
929  * (when they happen). Smaller sizes increase the chance of
930  * freed allocations freeing whole extents.
931  */
932 #define NORMAL_EXTENT   (256 * 1024)
933 #define SMALL_EXTENT    (128 * 1024)
934
935 #define FLIST_TEMP      (1<<1)
936
937 struct file_list {
938         struct file_list *next, *prev;
939         struct file_struct **files, **sorted;
940         alloc_pool_t file_pool;
941         void *pool_boundary;
942         int used, malloced;
943         int low, high;  /* 0-relative index values excluding empties */
944         int ndx_start;  /* the start offset for inc_recurse mode */
945         int flist_num;  /* 1-relative file_list number or 0 */
946         int parent_ndx; /* dir_flist index of parent directory */
947         int in_progress, to_redo;
948 };
949
950 #define SUMFLG_SAME_OFFSET      (1<<0)
951
952 struct sum_buf {
953         OFF_T offset;           /**< offset in file of this chunk */
954         int32 len;              /**< length of chunk of file */
955         uint32 sum1;            /**< simple checksum */
956         int32 chain;            /**< next hash-table collision */
957         short flags;            /**< flag bits */
958         char sum2[SUM_LENGTH];  /**< checksum  */
959 };
960
961 struct sum_struct {
962         OFF_T flength;          /**< total file length */
963         struct sum_buf *sums;   /**< points to info for each chunk */
964         int32 count;            /**< how many chunks */
965         int32 blength;          /**< block_length */
966         int32 remainder;        /**< flength % block_length */
967         int s2length;           /**< sum2_length */
968 };
969
970 struct map_struct {
971         OFF_T file_size;        /* File size (from stat)                */
972         OFF_T p_offset;         /* Window start                         */
973         OFF_T p_fd_offset;      /* offset of cursor in fd ala lseek     */
974         char *p;                /* Window pointer                       */
975         int32 p_size;           /* Largest window size we allocated     */
976         int32 p_len;            /* Latest (rounded) window size         */
977         int32 def_window_size;  /* Default window size                  */
978         int fd;                 /* File Descriptor                      */
979         int status;             /* first errno from read errors         */
980 };
981
982 #define NAME_IS_FILE            (0)    /* filter name as a file */
983 #define NAME_IS_DIR             (1<<0) /* filter name as a dir */
984 #define NAME_IS_XATTR           (1<<2) /* filter name as an xattr */
985
986 #define FILTRULE_WILD           (1<<0) /* pattern has '*', '[', and/or '?' */
987 #define FILTRULE_WILD2          (1<<1) /* pattern has '**' */
988 #define FILTRULE_WILD2_PREFIX   (1<<2) /* pattern starts with "**" */
989 #define FILTRULE_WILD3_SUFFIX   (1<<3) /* pattern ends with "***" */
990 #define FILTRULE_ABS_PATH       (1<<4) /* path-match on absolute path */
991 #define FILTRULE_INCLUDE        (1<<5) /* this is an include, not an exclude */
992 #define FILTRULE_DIRECTORY      (1<<6) /* this matches only directories */
993 #define FILTRULE_WORD_SPLIT     (1<<7) /* split rules on whitespace */
994 #define FILTRULE_NO_INHERIT     (1<<8) /* don't inherit these rules */
995 #define FILTRULE_NO_PREFIXES    (1<<9) /* parse no prefixes from patterns */
996 #define FILTRULE_MERGE_FILE     (1<<10)/* specifies a file to merge */
997 #define FILTRULE_PERDIR_MERGE   (1<<11)/* merge-file is searched per-dir */
998 #define FILTRULE_EXCLUDE_SELF   (1<<12)/* merge-file name should be excluded */
999 #define FILTRULE_FINISH_SETUP   (1<<13)/* per-dir merge file needs setup */
1000 #define FILTRULE_NEGATE         (1<<14)/* rule matches when pattern does not */
1001 #define FILTRULE_CVS_IGNORE     (1<<15)/* rule was -C or :C */
1002 #define FILTRULE_SENDER_SIDE    (1<<16)/* rule applies to the sending side */
1003 #define FILTRULE_RECEIVER_SIDE  (1<<17)/* rule applies to the receiving side */
1004 #define FILTRULE_CLEAR_LIST     (1<<18)/* this item is the "!" token */
1005 #define FILTRULE_PERISHABLE     (1<<19)/* perishable if parent dir goes away */
1006 #define FILTRULE_XATTR          (1<<20)/* rule only applies to xattr names */
1007
1008 #define FILTRULES_SIDES (FILTRULE_SENDER_SIDE | FILTRULE_RECEIVER_SIDE)
1009
1010 typedef struct filter_struct {
1011         struct filter_struct *next;
1012         char *pattern;
1013         uint32 rflags;
1014         union {
1015                 int slash_cnt;
1016                 struct filter_list_struct *mergelist;
1017         } u;
1018 } filter_rule;
1019
1020 typedef struct filter_list_struct {
1021         filter_rule *head;
1022         filter_rule *tail;
1023         char *debug_type;
1024 } filter_rule_list;
1025
1026 struct stats {
1027         int64 total_size;
1028         int64 total_transferred_size;
1029         int64 total_written;
1030         int64 total_read;
1031         int64 literal_data;
1032         int64 matched_data;
1033         int64 flist_buildtime;
1034         int64 flist_xfertime;
1035         int64 flist_size;
1036         int num_files, num_dirs, num_symlinks, num_devices, num_specials;
1037         int created_files, created_dirs, created_symlinks, created_devices, created_specials;
1038         int deleted_files, deleted_dirs, deleted_symlinks, deleted_devices, deleted_specials;
1039         int xferred_files;
1040 };
1041
1042 struct chmod_mode_struct;
1043
1044 struct flist_ndx_item {
1045         struct flist_ndx_item *next;
1046         int ndx;
1047 };
1048
1049 typedef struct {
1050         struct flist_ndx_item *head, *tail;
1051 } flist_ndx_list;
1052
1053 #define EMPTY_ITEM_LIST {NULL, 0, 0}
1054
1055 typedef struct {
1056         void *items;
1057         size_t count;
1058         size_t malloced;
1059 } item_list;
1060
1061 #define EXPAND_ITEM_LIST(lp, type, incr) \
1062         (type*)expand_item_list(lp, sizeof (type), #type, incr)
1063
1064 #define EMPTY_XBUF {NULL, 0, 0, 0}
1065
1066 typedef struct {
1067         char *buf;
1068         size_t pos;  /* pos = read pos in the buf */
1069         size_t len;  /* len = chars following pos */
1070         size_t size; /* size = total space in buf */
1071 } xbuf;
1072
1073 #define INIT_XBUF(xb, str, ln, sz) (xb).buf = (str), (xb).len = (ln), (xb).size = (sz), (xb).pos = 0
1074 #define INIT_XBUF_STRLEN(xb, str) (xb).buf = (str), (xb).len = strlen((xb).buf), (xb).size = (size_t)-1, (xb).pos = 0
1075 /* This one is used to make an output xbuf based on a char[] buffer: */
1076 #define INIT_CONST_XBUF(xb, bf) (xb).buf = (bf), (xb).size = sizeof (bf), (xb).len = (xb).pos = 0
1077
1078 #define ICB_EXPAND_OUT (1<<0)
1079 #define ICB_INCLUDE_BAD (1<<1)
1080 #define ICB_INCLUDE_INCOMPLETE (1<<2)
1081 #define ICB_CIRCULAR_OUT (1<<3)
1082 #define ICB_INIT (1<<4)
1083
1084 #define IOBUF_KEEP_BUFS 0
1085 #define IOBUF_FREE_BUFS 1
1086
1087 #define MPLX_SWITCHING IOBUF_KEEP_BUFS
1088 #define MPLX_ALL_DONE IOBUF_FREE_BUFS
1089 #define MPLX_TO_BUFFERED 2
1090
1091 #define RL_EOL_NULLS (1<<0)
1092 #define RL_DUMP_COMMENTS (1<<1)
1093 #define RL_CONVERT (1<<2)
1094
1095 typedef struct {
1096         char name_type;
1097 #ifdef USE_FLEXIBLE_ARRAY
1098         char fname[]; /* has variable size */
1099 #else
1100         char fname[1]; /* A kluge that should work like a flexible array */
1101 #endif
1102 } relnamecache;
1103
1104 #ifdef USE_FLEXIBLE_ARRAY
1105 #define RELNAMECACHE_LEN (sizeof (relnamecache))
1106 #else
1107 #define RELNAMECACHE_LEN (offsetof(relnamecache, fname))
1108 #endif
1109
1110 #include "byteorder.h"
1111 #include "lib/mdigest.h"
1112 #include "lib/wildmatch.h"
1113 #include "lib/permstring.h"
1114 #include "lib/addrinfo.h"
1115
1116 #ifndef __GNUC__
1117 #define __attribute__(x)
1118 #else
1119 # if __GNUC__ <= 2
1120 # define NORETURN
1121 # endif
1122 #endif
1123
1124 #define UNUSED(x) x __attribute__((__unused__))
1125 #ifndef NORETURN
1126 #define NORETURN __attribute__((__noreturn__))
1127 #endif
1128
1129 typedef struct {
1130     STRUCT_STAT st;
1131     time_t crtime;
1132 #ifdef SUPPORT_ACLS
1133     struct rsync_acl *acc_acl; /* access ACL */
1134     struct rsync_acl *def_acl; /* default ACL */
1135 #endif
1136 #ifdef SUPPORT_XATTRS
1137     item_list *xattr;
1138 #endif
1139 } stat_x;
1140
1141 #define ACL_READY(sx) ((sx).acc_acl != NULL)
1142 #define XATTR_READY(sx) ((sx).xattr != NULL)
1143
1144 #define CLVL_NOT_SPECIFIED INT_MIN
1145
1146 #define CPRES_AUTO (-1)
1147 #define CPRES_NONE 0
1148 #define CPRES_ZLIB 1
1149 #define CPRES_ZLIBX 2
1150 #define CPRES_LZ4 3
1151 #define CPRES_ZSTD 4
1152
1153 #define NSTR_CHECKSUM 0
1154 #define NSTR_COMPRESS 1
1155
1156 struct name_num_item {
1157         int num;
1158         const char *name, *main_name;
1159 };
1160
1161 struct name_num_obj {
1162         const char *type;
1163         const char *negotiated_name;
1164         uchar *saw;
1165         int saw_len;
1166         int negotiated_num;
1167         struct name_num_item list[10]; /* we'll get a compile error/warning if this is ever too small */
1168 };
1169
1170 #ifndef __cplusplus
1171 #include "proto.h"
1172 #endif
1173
1174 #ifndef SUPPORT_XATTRS
1175 #define x_stat(fn,fst,xst) do_stat(fn,fst)
1176 #define x_lstat(fn,fst,xst) do_lstat(fn,fst)
1177 #define x_fstat(fd,fst,xst) do_fstat(fd,fst)
1178 #endif
1179
1180 /* We have replacement versions of these if they're missing. */
1181 #ifndef HAVE_ASPRINTF
1182 int asprintf(char **ptr, const char *format, ...);
1183 #endif
1184
1185 #ifndef HAVE_VASPRINTF
1186 int vasprintf(char **ptr, const char *format, va_list ap);
1187 #endif
1188
1189 #if !defined HAVE_VSNPRINTF || !defined HAVE_C99_VSNPRINTF
1190 #define vsnprintf rsync_vsnprintf
1191 int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
1192 #endif
1193
1194 #if !defined HAVE_SNPRINTF || !defined HAVE_C99_VSNPRINTF
1195 #define snprintf rsync_snprintf
1196 int snprintf(char *str, size_t count, const char *fmt,...);
1197 #endif
1198
1199 #ifndef HAVE_STRERROR
1200 extern char *sys_errlist[];
1201 #define strerror(i) sys_errlist[i]
1202 #endif
1203
1204 #ifndef HAVE_STRCHR
1205 # define strchr                 index
1206 # define strrchr                rindex
1207 #endif
1208
1209 #ifndef HAVE_ERRNO_DECL
1210 extern int errno;
1211 #endif
1212
1213 #ifdef HAVE_READLINK
1214 #define SUPPORT_LINKS 1
1215 #if !defined NO_SYMLINK_XATTRS && !defined NO_SYMLINK_USER_XATTRS
1216 #define do_readlink(path, buf, bufsiz) readlink(path, buf, bufsiz)
1217 #endif
1218 #endif
1219 #ifdef HAVE_LINK
1220 #define SUPPORT_HARD_LINKS 1
1221 #endif
1222
1223 #ifdef HAVE_SIGACTION
1224 #define SIGACTION(n,h) sigact.sa_handler=(h), sigaction((n),&sigact,NULL)
1225 #define signal(n,h) we_need_to_call_SIGACTION_not_signal(n,h)
1226 #else
1227 #define SIGACTION(n,h) signal(n,h)
1228 #endif
1229
1230 #ifndef EWOULDBLOCK
1231 #define EWOULDBLOCK EAGAIN
1232 #endif
1233
1234 #ifndef STDIN_FILENO
1235 #define STDIN_FILENO 0
1236 #endif
1237
1238 #ifndef STDOUT_FILENO
1239 #define STDOUT_FILENO 1
1240 #endif
1241
1242 #ifndef STDERR_FILENO
1243 #define STDERR_FILENO 2
1244 #endif
1245
1246 #ifndef S_IRUSR
1247 #define S_IRUSR 0400
1248 #endif
1249
1250 #ifndef S_IWUSR
1251 #define S_IWUSR 0200
1252 #endif
1253
1254 #ifndef ACCESSPERMS
1255 #define ACCESSPERMS 0777
1256 #endif
1257
1258 #ifndef S_ISVTX
1259 #define S_ISVTX 0
1260 #endif
1261
1262 #define CHMOD_BITS (S_ISUID | S_ISGID | S_ISVTX | ACCESSPERMS)
1263
1264 #ifndef _S_IFMT
1265 #define _S_IFMT        0170000
1266 #endif
1267
1268 #ifndef _S_IFLNK
1269 #define _S_IFLNK  0120000
1270 #endif
1271
1272 #ifndef S_ISLNK
1273 #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
1274 #endif
1275
1276 #ifndef S_ISBLK
1277 #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
1278 #endif
1279
1280 #ifndef S_ISCHR
1281 #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
1282 #endif
1283
1284 #ifndef S_ISSOCK
1285 #ifdef _S_IFSOCK
1286 #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
1287 #else
1288 #define S_ISSOCK(mode) (0)
1289 #endif
1290 #endif
1291
1292 #ifndef S_ISFIFO
1293 #ifdef _S_IFIFO
1294 #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
1295 #else
1296 #define S_ISFIFO(mode) (0)
1297 #endif
1298 #endif
1299
1300 #ifndef S_ISDIR
1301 #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
1302 #endif
1303
1304 #ifndef S_ISREG
1305 #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
1306 #endif
1307
1308 /* work out what fcntl flag to use for non-blocking */
1309 #ifdef O_NONBLOCK
1310 # define NONBLOCK_FLAG O_NONBLOCK
1311 #elif defined SYSV
1312 # define NONBLOCK_FLAG O_NDELAY
1313 #else
1314 # define NONBLOCK_FLAG FNDELAY
1315 #endif
1316
1317 #ifndef INADDR_LOOPBACK
1318 #define INADDR_LOOPBACK 0x7f000001
1319 #endif
1320
1321 #ifndef INADDR_NONE
1322 #define INADDR_NONE 0xffffffff
1323 #endif
1324
1325 #define IS_SPECIAL(mode) (S_ISSOCK(mode) || S_ISFIFO(mode))
1326 #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode))
1327
1328 #define PRESERVE_FILE_TIMES     (1<<0)
1329 #define PRESERVE_DIR_TIMES      (1<<1)
1330 #define PRESERVE_LINK_TIMES     (1<<2)
1331
1332 /* Initial mask on permissions given to temporary files.  Mask off setuid
1333      bits and group access because of potential race-condition security
1334      holes, and mask other access because mode 707 is bizarre */
1335 #define INITACCESSPERMS 0700
1336
1337 /* handler for null strings in printf format */
1338 #define NS(s) ((s)?(s):"<NULL>")
1339
1340 extern char *do_calloc;
1341
1342 /* Convenient wrappers for malloc and realloc.  Use them. */
1343 #define new(type) ((type*)my_alloc(NULL, sizeof (type), 1, __FILE__, __LINE__))
1344 #define new0(type) ((type*)my_alloc(do_calloc, sizeof (type), 1, __FILE__, __LINE__))
1345 #define realloc_buf(ptr, num) my_alloc((ptr), (num), 1, __FILE__, __LINE__)
1346
1347 #define new_array(type, num) ((type*)my_alloc(NULL, (num), sizeof (type), __FILE__, __LINE__))
1348 #define new_array0(type, num) ((type*)my_alloc(do_calloc, (num), sizeof (type), __FILE__, __LINE__))
1349 #define realloc_array(ptr, type, num) ((type*)my_alloc((ptr), (num), sizeof (type), __FILE__, __LINE__))
1350
1351 #undef strdup
1352 #define strdup(s) my_strdup(s, __FILE__, __LINE__)
1353
1354 #define out_of_memory(msg) _out_of_memory(msg, __FILE__, __LINE__)
1355 #define overflow_exit(msg) _overflow_exit(msg, __FILE__, __LINE__)
1356
1357 /* use magic gcc attributes to catch format errors */
1358  void rprintf(enum logcode , const char *, ...)
1359      __attribute__((format (printf, 2, 3)))
1360 ;
1361
1362 /* This is just like rprintf, but it also tries to print some
1363  * representation of the error code.  Normally errcode = errno. */
1364 void rsyserr(enum logcode, int, const char *, ...)
1365      __attribute__((format (printf, 3, 4)))
1366      ;
1367
1368 /* Make sure that the O_BINARY flag is defined. */
1369 #ifndef O_BINARY
1370 #define O_BINARY 0
1371 #endif
1372
1373 #ifndef HAVE_STRLCPY
1374 size_t strlcpy(char *d, const char *s, size_t bufsize);
1375 #endif
1376
1377 #ifndef HAVE_STRLCAT
1378 size_t strlcat(char *d, const char *s, size_t bufsize);
1379 #endif
1380
1381 #ifndef WEXITSTATUS
1382 #define WEXITSTATUS(stat)       ((int)(((stat)>>8)&0xFF))
1383 #endif
1384 #ifndef WIFEXITED
1385 #define WIFEXITED(stat)         ((int)((stat)&0xFF) == 0)
1386 #endif
1387
1388 #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)
1389
1390 #ifdef HAVE_GETEUID
1391 #define MY_UID() geteuid()
1392 #else
1393 #define MY_UID() getuid()
1394 #endif
1395
1396 #ifdef HAVE_GETEGID
1397 #define MY_GID() getegid()
1398 #else
1399 #define MY_GID() getgid()
1400 #endif
1401
1402 #ifdef FORCE_FD_ZERO_MEMSET
1403 #undef FD_ZERO
1404 #define FD_ZERO(fdsetp) memset(fdsetp, 0, sizeof (fd_set))
1405 #endif
1406
1407 extern short info_levels[], debug_levels[];
1408
1409 #define INFO_GTE(flag, lvl) (info_levels[INFO_##flag] >= (lvl))
1410 #define INFO_EQ(flag, lvl) (info_levels[INFO_##flag] == (lvl))
1411 #define DEBUG_GTE(flag, lvl) (debug_levels[DEBUG_##flag] >= (lvl))
1412 #define DEBUG_EQ(flag, lvl) (debug_levels[DEBUG_##flag] == (lvl))
1413
1414 #define INFO_BACKUP 0
1415 #define INFO_COPY (INFO_BACKUP+1)
1416 #define INFO_DEL (INFO_COPY+1)
1417 #define INFO_FLIST (INFO_DEL+1)
1418 #define INFO_MISC (INFO_FLIST+1)
1419 #define INFO_MOUNT (INFO_MISC+1)
1420 #define INFO_NAME (INFO_MOUNT+1)
1421 #define INFO_PROGRESS (INFO_NAME+1)
1422 #define INFO_REMOVE (INFO_PROGRESS+1)
1423 #define INFO_SKIP (INFO_REMOVE+1)
1424 #define INFO_STATS (INFO_SKIP+1)
1425 #define INFO_SYMSAFE (INFO_STATS+1)
1426
1427 #define COUNT_INFO (INFO_SYMSAFE+1)
1428
1429 #define DEBUG_ACL 0
1430 #define DEBUG_BACKUP (DEBUG_ACL+1)
1431 #define DEBUG_BIND (DEBUG_BACKUP+1)
1432 #define DEBUG_CHDIR (DEBUG_BIND+1)
1433 #define DEBUG_CONNECT (DEBUG_CHDIR+1)
1434 #define DEBUG_CMD (DEBUG_CONNECT+1)
1435 #define DEBUG_DEL (DEBUG_CMD+1)
1436 #define DEBUG_DELTASUM (DEBUG_DEL+1)
1437 #define DEBUG_DUP (DEBUG_DELTASUM+1)
1438 #define DEBUG_EXIT (DEBUG_DUP+1)
1439 #define DEBUG_FILTER (DEBUG_EXIT+1)
1440 #define DEBUG_FLIST (DEBUG_FILTER+1)
1441 #define DEBUG_FUZZY (DEBUG_FLIST+1)
1442 #define DEBUG_GENR (DEBUG_FUZZY+1)
1443 #define DEBUG_HASH (DEBUG_GENR+1)
1444 #define DEBUG_HLINK (DEBUG_HASH+1)
1445 #define DEBUG_ICONV (DEBUG_HLINK+1)
1446 #define DEBUG_IO (DEBUG_ICONV+1)
1447 #define DEBUG_NSTR (DEBUG_IO+1)
1448 #define DEBUG_OWN (DEBUG_NSTR+1)
1449 #define DEBUG_PROTO (DEBUG_OWN+1)
1450 #define DEBUG_RECV (DEBUG_PROTO+1)
1451 #define DEBUG_SEND (DEBUG_RECV+1)
1452 #define DEBUG_TIME (DEBUG_SEND+1)
1453
1454 #define COUNT_DEBUG (DEBUG_TIME+1)
1455
1456 #ifndef HAVE_INET_NTOP
1457 const char *inet_ntop(int af, const void *src, char *dst, size_t size);
1458 #endif
1459
1460 #ifndef HAVE_INET_PTON
1461 int inet_pton(int af, const char *src, void *dst);
1462 #endif
1463
1464 #ifndef HAVE_GETPASS
1465 char *getpass(const char *prompt);
1466 #endif
1467
1468 #ifdef MAINTAINER_MODE
1469 const char *get_panic_action(void);
1470 #endif
1471
1472 #define NOISY_DEATH(msg) do { \
1473     fprintf(stderr, "%s in %s at line %d\n", msg, __FILE__, __LINE__); \
1474     exit_cleanup(RERR_UNSUPPORTED); \
1475 } while (0)