Repositioned a misplaced assignment.
[rsync/rsync.git] / rsync.h
CommitLineData
d622d4bf 1/*
0f78b815
WD
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, 2004, 2005, 2006 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 2 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 *
e7c67065
WD
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 20 */
0de40240 21
d41c7d02
DD
22#define False 0
23#define True 1
24
c627d613
AT
25#define BLOCK_SIZE 700
26#define RSYNC_RSH_ENV "RSYNC_RSH"
8dd99390 27#define RSYNC_RSH_IO_ENV "RSYNC_RSH_IO"
7b8356d0 28
c627d613 29#define RSYNC_NAME "rsync"
cd3fe9fb 30/* RSYNCD_SYSCONF is now set in config.h */
30e8c8e1 31#define RSYNCD_USERCONF "rsyncd.conf"
f0fca04e 32
5e71c444 33#define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
f7632fc6
AT
34#define URL_PREFIX "rsync://"
35
c627d613
AT
36#define BACKUP_SUFFIX "~"
37
1b01b295 38/* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
6543dc0c 39 incompatible with older versions :-( */
1b01b295
AT
40#define CHAR_OFFSET 0
41
a5f9cff2
WD
42/* These flags are only used during the flist transfer. */
43
ec33e0e6 44#define XMIT_TOP_DIR (1<<0)
a5f9cff2
WD
45#define XMIT_SAME_MODE (1<<1)
46#define XMIT_EXTENDED_FLAGS (1<<2)
14d776ff 47#define XMIT_SAME_RDEV_pre28 XMIT_EXTENDED_FLAGS /* protocols < 28 */
a5f9cff2
WD
48#define XMIT_SAME_UID (1<<3)
49#define XMIT_SAME_GID (1<<4)
50#define XMIT_SAME_NAME (1<<5)
51#define XMIT_LONG_NAME (1<<6)
52#define XMIT_SAME_TIME (1<<7)
af107f6c 53#define XMIT_SAME_RDEV_MAJOR (1<<8)
c7565dad 54#define XMIT_HLINKED (1<<9)
14d776ff
WD
55#define XMIT_SAME_DEV_pre30 (1<<10) /* protocols < 30 */
56#define XMIT_HLINK_FIRST (1<<10) /* protocols >= 30 */
af107f6c 57#define XMIT_RDEV_MINOR_IS_SMALL (1<<11)
9ec3828b
WD
58#define XMIT_USER_NAME_FOLLOWS (1<<12) /* protocols >= 30 */
59#define XMIT_GROUP_NAME_FOLLOWS (1<<13) /* protocols >= 30 */
a5f9cff2
WD
60
61/* These flags are used in the live flist data. */
62
82ad07c4 63#define FLAG_TOP_DIR (1<<0) /* sender/receiver/generator */
9ec3828b
WD
64#define FLAG_FILE_SENT (1<<1) /* sender/receiver/generator */
65#define FLAG_DIR_CHANGED (1<<1) /* generator */
82ad07c4
WD
66#define FLAG_XFER_DIR (1<<2) /* sender/receiver/generator */
67#define FLAG_MOUNT_DIR (1<<3) /* sender/generator */
96293cf9
WD
68#define FLAG_MISSING_DIR (1<<4) /* generator */
69#define FLAG_HLINKED (1<<5) /* receiver/generator */
82ad07c4
WD
70#define FLAG_HLINK_FIRST (1<<6) /* receiver/generator */
71#define FLAG_HLINK_LAST (1<<7) /* receiver/generator */
bfd31372
WD
72#define FLAG_HLINK_DONE (1<<8) /* receiver/generator */
73#define FLAG_LENGTH64 (1<<9) /* sender/receiver/generator */
74
9ec3828b
WD
75/* These flags are passed to functions but not stored. */
76
77#define FLAG_DIVERT_DIRS (1<<16)/* sender */
78
bfd31372
WD
79#define BITS_SET(val,bits) (((val) & (bits)) == (bits))
80#define BITS_SETnUNSET(val,onbits,offbits) (((val) & ((onbits)|(offbits))) == (onbits))
0a62f5f3
WD
81#define BITS_EQUAL(b1,b2,mask) (((unsigned)(b1) & (unsigned)(mask)) \
82 == ((unsigned)(b2) & (unsigned)(mask)))
520cf417 83
c627d613 84/* update this if you make incompatible changes */
21068d8e 85#define PROTOCOL_VERSION 30
063393d6
MP
86
87/* We refuse to interoperate with versions that are not in this range.
88 * Note that we assume we'll work with later versions: the onus is on
89 * people writing them to make sure that they don't send us anything
90 * we won't understand.
91 *
1b2db7ae
S
92 * Interoperation with old but supported protocol versions
93 * should cause a warning to be printed. At a future date
94 * the old protocol will become the minimum and
95 * compatibility code removed.
96 *
91c4da3f
S
97 * There are two possible explanations for the limit at
98 * MAX_PROTOCOL_VERSION: either to allow new major-rev versions that
99 * do not interoperate with us, and (more likely) so that we can
100 * detect an attempt to connect rsync to a non-rsync server, which is
101 * unlikely to begin by sending a byte between MIN_PROTOCL_VERSION and
102 * MAX_PROTOCOL_VERSION. */
103
3e491682
S
104#define MIN_PROTOCOL_VERSION 20
105#define OLD_PROTOCOL_VERSION 25
1b2db7ae 106#define MAX_PROTOCOL_VERSION 40
c627d613 107
9486289c
AT
108#define RSYNC_PORT 873
109
d867229b 110#define SPARSE_WRITE_SIZE (1024)
dc5ddbcc 111#define WRITE_SIZE (32*1024)
34ccb63e 112#define CHUNK_SIZE (32*1024)
61542c41 113#define MAX_MAP_SIZE (256*1024)
e3fe383a 114#define IO_BUFFER_SIZE (4092)
2b2ea368 115#define MAX_BLOCK_SIZE ((int32)1 << 29)
c627d613 116
e2d22fee
WD
117#define IOERR_GENERAL (1<<0) /* For backward compatibility, this must == 1 */
118#define IOERR_VANISHED (1<<1)
ff3d3c32 119#define IOERR_DEL_LIMIT (1<<2)
e2d22fee 120
874895d5 121#define MAX_ARGS 1000
e4977b0b 122#define MAX_BASIS_DIRS 20
c296031d 123#define MAX_SERVER_ARGS (MAX_BASIS_DIRS*2 + 100)
e4977b0b 124
8d9dc9f9 125#define MPLEX_BASE 7
ff41a59f 126
7842418b
WD
127#define NO_FILTERS 0
128#define SERVER_FILTERS 1
129#define ALL_FILTERS 2
4762db4f 130
753b6b46 131#define XFLG_FATAL_ERRORS (1<<0)
3b2461cf
WD
132#define XFLG_OLD_PREFIXES (1<<1)
133#define XFLG_ANCHORED2ABS (1<<2)
699f7024 134#define XFLG_ABS_IF_SLASH (1<<3)
4762db4f 135
8eda7a4b
WD
136#define ATTRS_REPORT (1<<0)
137#define ATTRS_SKIP_MTIME (1<<1)
5c6fc4a6 138
419896af
WD
139#define FULL_FLUSH 1
140#define NORMAL_FLUSH 0
141
a7260c40
WD
142#define PDIR_CREATE 1
143#define PDIR_DELETE 0
144
e341588a
WD
145/* Note: 0x00 - 0x7F are used for basis_dir[] indexes! */
146#define FNAMECMP_BASIS_DIR_LOW 0x00 /* Must remain 0! */
147#define FNAMECMP_BASIS_DIR_HIGH 0x7F
41cfde6b
WD
148#define FNAMECMP_FNAME 0x80
149#define FNAMECMP_PARTIAL_DIR 0x81
150#define FNAMECMP_BACKUP 0x82
0dd046d3 151#define FNAMECMP_FUZZY 0x83
41cfde6b 152
0cc279e0 153/* For use by the itemize_changes code */
b3222792 154#define ITEM_REPORT_ATIME (1<<0)
0cc279e0
WD
155#define ITEM_REPORT_CHECKSUM (1<<1)
156#define ITEM_REPORT_SIZE (1<<2)
157#define ITEM_REPORT_TIME (1<<3)
158#define ITEM_REPORT_PERMS (1<<4)
159#define ITEM_REPORT_OWNER (1<<5)
160#define ITEM_REPORT_GROUP (1<<6)
b3222792
WD
161#define ITEM_REPORT_ACL (1<<7)
162#define ITEM_REPORT_XATTR (1<<8)
3019a9ba
WD
163#define ITEM_BASIS_TYPE_FOLLOWS (1<<11)
164#define ITEM_XNAME_FOLLOWS (1<<12)
6d0e5d2e
WD
165#define ITEM_IS_NEW (1<<13)
166#define ITEM_LOCAL_CHANGE (1<<14)
167#define ITEM_TRANSFER (1<<15)
d4d4890d 168/* These are outside the range of the transmitted flags. */
d4d4890d
WD
169#define ITEM_MISSING_DATA (1<<16) /* used by log_formatted() */
170#define ITEM_DELETED (1<<17) /* used by log_formatted() */
1e4f5f63 171#define ITEM_MATCHED (1<<18) /* used by itemize() */
0cc279e0 172
3019a9ba
WD
173#define SIGNIFICANT_ITEM_FLAGS (~(\
174 ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
669e7671 175
df0054ab 176
56aaa4c4
WD
177/* Log-message categories. Only FERROR and FINFO get sent over the socket,
178 * but FLOG and FSOCKERR can be sent over the receiver -> generator pipe.
85909931 179 * FLOG only goes to the log file, not the client; FCLIENT is the opposite. */
011e85a5 180enum logcode { FNONE=0, FERROR=1, FINFO=2, FLOG=3, FCLIENT=4, FSOCKERR=5 };
419896af
WD
181
182/* Messages types that are sent over the message channel. The logcode
183 * values must all be present here with identical numbers. */
184enum msgcode {
1dcf3b43
WD
185 MSG_DATA=0, /* raw data on the multiplexed stream */
186 MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */
8388e011 187 MSG_LOG=FLOG, MSG_CLIENT=FCLIENT, MSG_SOCKERR=FSOCKERR, /* sibling logging */
1dcf3b43 188 MSG_REDO=9, /* reprocess indicated flist index */
9ec3828b
WD
189 MSG_FLIST=20, /* extra file list over sibling socket */
190 MSG_FLIST_EOF=21,/* we've transmitted all the file lists */
191 MSG_IO_ERROR=22,/* the sending side had an I/O error */
192 MSG_NOOP=42, /* a do-nothing message */
1dcf3b43
WD
193 MSG_SUCCESS=100,/* successfully updated indicated flist index */
194 MSG_DELETED=101,/* successfully deleted a file on receiving side */
9ec3828b 195 MSG_NO_SEND=102,/* sender failed to open a file we wanted */
1dcf3b43 196 MSG_DONE=86 /* current phase is done */
419896af 197};
7bec6a5c 198
82ad07c4 199#define NDX_DONE -1
9ec3828b
WD
200#define NDX_FLIST_EOF -2
201#define NDX_FLIST_OFFSET -101
82ad07c4 202
65417579
AT
203#include "errcode.h"
204
c627d613
AT
205#include "config.h"
206
e3217f14 207/* The default RSYNC_RSH is always set in config.h. */
24d95c03 208
c627d613 209#include <stdio.h>
4f5b0756 210#ifdef HAVE_SYS_TYPES_H
e95538ca
WD
211# include <sys/types.h>
212#endif
4f5b0756 213#ifdef HAVE_SYS_STAT_H
e95538ca
WD
214# include <sys/stat.h>
215#endif
4f5b0756 216#ifdef STDC_HEADERS
e95538ca
WD
217# include <stdlib.h>
218# include <stddef.h>
219#else
4f5b0756 220# ifdef HAVE_STDLIB_H
e95538ca
WD
221# include <stdlib.h>
222# endif
223#endif
4f5b0756
WD
224#ifdef HAVE_STRING_H
225# if !defined STDC_HEADERS && defined HAVE_MEMORY_H
e95538ca
WD
226# include <memory.h>
227# endif
228# include <string.h>
229#endif
4f5b0756 230#ifdef HAVE_STRINGS_H
e95538ca
WD
231# include <strings.h>
232#endif
2d2414f3
WD
233#ifdef HAVE_INTTYPES_H
234# include <inttypes.h>
235#endif
4f5b0756 236#ifdef HAVE_UNISTD_H
e95538ca 237# include <unistd.h>
c627d613
AT
238#endif
239
4f5b0756 240#ifdef HAVE_SYS_PARAM_H
e95538ca 241#include <sys/param.h>
38b02c13 242#endif
59192f56 243
4f5b0756 244#if defined HAVE_MALLOC_H && (defined HAVE_MALLINFO || !defined HAVE_STDLIB_H)
59192f56 245#include <malloc.h>
d7b305fd
AT
246#endif
247
4f5b0756 248#ifdef HAVE_SYS_SOCKET_H
fdd71e17
AT
249#include <sys/socket.h>
250#endif
251
4f5b0756 252#ifdef TIME_WITH_SYS_TIME
c627d613
AT
253#include <sys/time.h>
254#include <time.h>
255#else
4f5b0756 256#ifdef HAVE_SYS_TIME_H
c627d613
AT
257#include <sys/time.h>
258#else
259#include <time.h>
260#endif
261#endif
262
4f5b0756 263#ifdef HAVE_FCNTL_H
c627d613
AT
264#include <fcntl.h>
265#else
4f5b0756 266#ifdef HAVE_SYS_FCNTL_H
c627d613
AT
267#include <sys/fcntl.h>
268#endif
269#endif
270
4f5b0756 271#ifdef HAVE_SYS_IOCTL_H
94481d91
AT
272#include <sys/ioctl.h>
273#endif
274
4f5b0756 275#ifdef HAVE_SYS_FILIO_H
94481d91
AT
276#include <sys/filio.h>
277#endif
278
c627d613 279#include <signal.h>
4f5b0756 280#ifdef HAVE_SYS_WAIT_H
c627d613
AT
281#include <sys/wait.h>
282#endif
4f5b0756 283#ifdef HAVE_CTYPE_H
c627d613
AT
284#include <ctype.h>
285#endif
4f5b0756 286#ifdef HAVE_GRP_H
c627d613
AT
287#include <grp.h>
288#endif
289#include <errno.h>
290
4f5b0756 291#ifdef HAVE_UTIME_H
c627d613
AT
292#include <utime.h>
293#endif
294
4f5b0756 295#ifdef HAVE_SYS_SELECT_H
8bf73749
AT
296#include <sys/select.h>
297#endif
298
4f5b0756 299#ifdef HAVE_SYS_MODE_H
773f2bd4 300/* apparently AIX needs this for S_ISLNK */
05a6556d 301#ifndef S_ISLNK
773f2bd4
AT
302#include <sys/mode.h>
303#endif
05a6556d 304#endif
773f2bd4 305
4f5b0756 306#ifdef HAVE_GLOB_H
874895d5
AT
307#include <glob.h>
308#endif
309
f6c34742
AT
310/* these are needed for the uid/gid mapping code */
311#include <pwd.h>
312#include <grp.h>
313
9486289c 314#include <stdarg.h>
f0fca04e
AT
315#include <netinet/in.h>
316#include <arpa/inet.h>
317#include <netdb.h>
ff8b29b8 318#include <syslog.h>
6c8f5373 319#include <sys/file.h>
9486289c 320
4f5b0756 321#ifdef HAVE_DIRENT_H
e8f5b936
AT
322# include <dirent.h>
323#else
324# define dirent direct
4f5b0756 325# ifdef HAVE_SYS_NDIR_H
e8f5b936
AT
326# include <sys/ndir.h>
327# endif
4f5b0756 328# ifdef HAVE_SYS_DIR_H
e8f5b936
AT
329# include <sys/dir.h>
330# endif
4f5b0756 331# ifdef HAVE_NDIR_H
e8f5b936
AT
332# include <ndir.h>
333# endif
334#endif
335
4f5b0756 336#ifdef MAJOR_IN_MKDEV
c39d6514 337#include <sys/mkdev.h>
8a33c406 338# if !defined makedev && (defined mkdev || defined _WIN32 || defined __WIN32__)
73496a36
WD
339# define makedev mkdev
340# endif
4f5b0756 341#elif defined MAJOR_IN_SYSMACROS
f8ed564c 342#include <sys/sysmacros.h>
c39d6514
WD
343#endif
344
5a3810b4
WD
345#ifdef MAKEDEV_TAKES_3_ARGS
346#define MAKEDEV(devmajor,devminor) makedev(0,devmajor,devminor)
347#else
348#define MAKEDEV(devmajor,devminor) makedev(devmajor,devminor)
349#endif
350
4f5b0756 351#ifdef HAVE_COMPAT_H
e8f5b936
AT
352#include <compat.h>
353#endif
354
ea8291d8
WD
355#ifdef HAVE_LIMITS_H
356# include <limits.h>
357#endif
358
1bfbf40b
MP
359#include <assert.h>
360
9935066b 361#include "lib/pool_alloc.h"
e8f5b936 362
f0fca04e
AT
363#define BOOL int
364
c627d613
AT
365#ifndef uchar
366#define uchar unsigned char
367#endif
368
4f5b0756 369#ifdef SIGNED_CHAR_OK
debb4505
AT
370#define schar signed char
371#else
372#define schar char
373#endif
374
2d2414f3
WD
375#ifndef int16
376#if SIZEOF_INT16_T == 2
377# define int16 int16_t
378#else
379# define int16 short
380#endif
381#endif
382
383#ifndef uint16
384#if SIZEOF_UINT16_T == 2
385# define uint16 uint16_t
386#else
387# define uint16 unsigned int16
388#endif
389#endif
390
43d0f38b
WD
391/* Find a variable that is either exactly 32-bits or longer.
392 * If some code depends on 32-bit truncation, it will need to
393 * take special action in a "#if SIZEOF_INT32 > 4" section. */
c627d613 394#ifndef int32
2d2414f3
WD
395#if SIZEOF_INT32_T == 4
396# define int32 int32_t
397# define SIZEOF_INT32 4
398#elif SIZEOF_INT == 4
43d0f38b
WD
399# define int32 int
400# define SIZEOF_INT32 4
401#elif SIZEOF_LONG == 4
402# define int32 long
403# define SIZEOF_INT32 4
404#elif SIZEOF_SHORT == 4
405# define int32 short
406# define SIZEOF_INT32 4
407#elif SIZEOF_INT > 4
408# define int32 int
409# define SIZEOF_INT32 SIZEOF_INT
410#elif SIZEOF_LONG > 4
411# define int32 long
412# define SIZEOF_INT32 SIZEOF_LONG
8de330a3 413#else
43d0f38b 414# error Could not find a 32-bit integer variable
c627d613 415#endif
43d0f38b
WD
416#else
417# define SIZEOF_INT32 4
c627d613
AT
418#endif
419
420#ifndef uint32
2d2414f3
WD
421#if SIZEOF_UINT32_T == 4
422# define uint32 uint32_t
423#else
424# define uint32 unsigned int32
425#endif
c627d613
AT
426#endif
427
0dd046d3 428#if SIZEOF_OFF_T == 8 || !SIZEOF_OFF64_T || !defined HAVE_STRUCT_STAT64
bcacc18b
AT
429#define OFF_T off_t
430#define STRUCT_STAT struct stat
5b5f7e3b
WD
431#else
432#define OFF_T off64_t
433#define STRUCT_STAT struct stat64
434#define USE_STAT64_FUNCS 1
bcacc18b
AT
435#endif
436
d4198823
WD
437/* CAVEAT: on some systems, int64 will really be a 32-bit integer IFF
438 * that's the maximum size the file system can handle and there is no
439 * 64-bit type available. The rsync source must therefore take steps
440 * to ensure that any code that really requires a 64-bit integer has
441 * it (e.g. the checksum code uses two 32-bit integers for its 64-bit
442 * counter). */
2d2414f3
WD
443#if SIZEOF_INT64_T == 8
444# define int64 int64_t
445# define SIZEOF_INT64 8
446#elif SIZEOF_LONG == 8
d622d4bf
WD
447# define int64 long
448# define SIZEOF_INT64 8
449#elif SIZEOF_INT == 8
450# define int64 int
451# define SIZEOF_INT64 8
452#elif SIZEOF_LONG_LONG == 8
453# define int64 long long
454# define SIZEOF_INT64 8
2c177562
WD
455#elif SIZEOF_OFF64_T == 8
456# define int64 off64_t
457# define SIZEOF_INT64 8
d622d4bf
WD
458#elif SIZEOF_OFF_T == 8
459# define int64 off_t
460# define SIZEOF_INT64 8
461#elif SIZEOF_INT > 8
462# define int64 int
463# define SIZEOF_INT64 SIZEOF_INT
464#elif SIZEOF_LONG > 8
465# define int64 long
466# define SIZEOF_INT64 SIZEOF_LONG
467#elif SIZEOF_LONG_LONG > 8
468# define int64 long long
469# define SIZEOF_INT64 SIZEOF_LONG_LONG
71c46176 470#else
736a6a29 471/* As long as it gets... */
d622d4bf
WD
472# define int64 off_t
473# define SIZEOF_INT64 SIZEOF_OFF_T
71c46176 474#endif
c627d613 475
6abd193f
MP
476/* Starting from protocol version 26, we always use 64-bit
477 * ino_t and dev_t internally, even if this platform does not
478 * allow files to have 64-bit inums. That's because the
479 * receiver needs to find duplicate (dev,ino) tuples to detect
480 * hardlinks, and it might have files coming from a platform
481 * that has 64-bit inums.
482 *
483 * The only exception is if we're on a platform with no 64-bit type at
484 * all.
485 *
486 * Because we use read_longint() to get these off the wire, if you
487 * transfer devices or hardlinks with dev or inum > 2**32 to a machine
488 * with no 64-bit types then you will get an overflow error. Probably
489 * not many people have that combination of machines, and you can
490 * avoid it by not preserving hardlinks or not transferring device
491 * nodes. It's not clear that any other behaviour is better.
492 *
493 * Note that if you transfer devices from a 64-bit-devt machine (say,
494 * Solaris) to a 32-bit-devt machine (say, Linux-2.2/x86) then the
495 * device numbers will be truncated. But it's a kind of silly thing
496 * to do anyhow.
b9df3bf2 497 *
33a2361c
WD
498 * FIXME: I don't think the code in flist.c has ever worked on a system
499 * where dev_t is a struct.
d622d4bf 500 */
33a2361c 501
14d776ff
WD
502struct idev_node {
503 int64 key;
504 void *data;
505};
506
c627d613
AT
507#ifndef MIN
508#define MIN(a,b) ((a)<(b)?(a):(b))
509#endif
510
511#ifndef MAX
512#define MAX(a,b) ((a)>(b)?(a):(b))
513#endif
514
d41c7d02
DD
515#ifndef MAXHOSTNAMELEN
516#define MAXHOSTNAMELEN 256
517#endif
518
c627d613 519/* the length of the md4 checksum */
ebb0a6f6 520#define MD4_SUM_LENGTH 16
c627d613 521#define SUM_LENGTH 16
195bd906
S
522#define SHORT_SUM_LENGTH 2
523#define BLOCKSUM_BIAS 10
c627d613
AT
524
525#ifndef MAXPATHLEN
526#define MAXPATHLEN 1024
527#endif
528
a22ca885
WD
529/* We want a roomy line buffer that can hold more than MAXPATHLEN,
530 * and significantly more than an overly short MAXPATHLEN. */
531#if MAXPATHLEN < 4096
532#define BIGPATHBUFLEN (4096+1024)
533#else
534#define BIGPATHBUFLEN (MAXPATHLEN+1024)
535#endif
536
52d3e106
S
537#ifndef NAME_MAX
538#define NAME_MAX 255
539#endif
540
0473e2a1
AT
541#ifndef INADDR_NONE
542#define INADDR_NONE 0xffffffff
543#endif
544
bda41fa5
WD
545#ifndef IN_LOOPBACKNET
546#define IN_LOOPBACKNET 127
547#endif
548
25bd9945 549#define GID_NONE ((gid_t)-1)
a60e2dca 550
5b30412c 551union file_extras {
bd6edd3f
WD
552 int32 num;
553 uint32 unum;
5b30412c
WD
554};
555
c627d613 556struct file_struct {
2d2414f3
WD
557 const char *dirname; /* The dir info inside the transfer */
558 time_t modtime; /* When the item was last modified */
559 uint32 len32; /* Lowest 32 bits of the file's length */
560 uint16 mode; /* The item's type and permissions */
561 uint16 flags; /* The FLAG_* bits for this item */
562 const char basename[1]; /* The basename (AKA filename) follows */
c627d613
AT
563};
564
99a957d3 565extern int file_extra_cnt;
96293cf9
WD
566extern int preserve_uid;
567extern int preserve_gid;
b96efc2f 568
c58c1dc4 569#define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
5b30412c 570#define EXTRA_LEN (sizeof (union file_extras))
bd6edd3f 571#define PTR_EXTRA_LEN ((sizeof (char *) + EXTRA_LEN - 1) / EXTRA_LEN)
96293cf9
WD
572#define SUM_EXTRA_CNT ((MD4_SUM_LENGTH + EXTRA_LEN - 1) / EXTRA_LEN)
573
5b30412c 574#define REQ_EXTRA(f,ndx) ((union file_extras*)(f) - (ndx))
99a957d3 575#define OPT_EXTRA(f,bump) ((union file_extras*)(f) - file_extra_cnt - 1 - (bump))
b37b7c99 576
96293cf9
WD
577#define LEN64_BUMP(f) ((f)->flags & FLAG_LENGTH64 ? 1 : 0)
578#define HLINK_BUMP(f) (F_IS_HLINKED(f) ? 1 : 0)
579
c58c1dc4 580/* The length applies to all items. */
bf466c0f
WD
581#if SIZEOF_INT64 < 8
582#define F_LENGTH(f) ((int64)(f)->len32)
583#else
584#define F_LENGTH(f) ((int64)(f)->len32 + ((f)->flags & FLAG_LENGTH64 \
585 ? (int64)OPT_EXTRA(f, 0)->unum << 32 : 0))
586#endif
82ad07c4 587
96293cf9 588/* If there is a symlink string, it is always right after the basename */
c58c1dc4 589#define F_SYMLINK(f) ((f)->basename + strlen((f)->basename) + 1)
82ad07c4 590
bd6edd3f
WD
591/* The sending side always has this available: */
592#define F_ROOTDIR(f) (*(const char**)REQ_EXTRA(f, PTR_EXTRA_LEN))
593
594/* The receiving side always has this available: */
595#define F_DEPTH(f) REQ_EXTRA(f, 1)->num
596
96293cf9 597/* When the associated option is on, all entries will have these present: */
bd6edd3f
WD
598#define F_UID(f) REQ_EXTRA(f, preserve_uid)->unum
599#define F_GID(f) REQ_EXTRA(f, preserve_gid)->unum
82ad07c4 600
b37b7c99 601/* These items are per-entry optional and mutally exclusive: */
14d776ff 602#define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
bfd31372 603#define F_HL_PREV(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
9ec3828b
WD
604#define F_DIRDEV_P(f) (&OPT_EXTRA(f, LEN64_BUMP(f) + 2 - 1)->unum)
605#define F_DIRNODE_P(f) (&OPT_EXTRA(f, LEN64_BUMP(f) + 3 - 1)->num)
82ad07c4 606
b37b7c99
WD
607/* This optional item might follow an F_HL_*() item.
608 * (Note: a device doesn't need to check LEN64_BUMP(f).) */
609#define F_RDEV_P(f) (&OPT_EXTRA(f, HLINK_BUMP(f) + 2 - 1)->unum)
82ad07c4 610
96293cf9 611/* The sum is only present on regular files. */
b8c167e8
WD
612#define F_SUM(f) ((const char*)OPT_EXTRA(f, LEN64_BUMP(f) + HLINK_BUMP(f) \
613 + SUM_EXTRA_CNT - 1))
96293cf9 614
b37b7c99 615/* Some utility defines: */
c58c1dc4 616#define F_IS_ACTIVE(f) (f)->basename[0]
bfd31372 617#define F_IS_HLINKED(f) ((f)->flags & FLAG_HLINKED)
b7cfb9e2
WD
618#define F_HLINK_NOT_FIRST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_FIRST)
619#define F_HLINK_NOT_LAST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_LAST)
96293cf9 620
b37b7c99
WD
621#define DEV_MAJOR(a) (a)[0]
622#define DEV_MINOR(a) (a)[1]
623
9ec3828b
WD
624#define DIR_PARENT(a) (a)[0]
625#define DIR_FIRST_CHILD(a) (a)[1]
626#define DIR_NEXT_SIBLING(a) (a)[2]
627
96293cf9
WD
628/*
629 * Start the flist array at FLIST_START entries and grow it
630 * by doubling until FLIST_LINEAR then grow by FLIST_LINEAR
631 */
632#define FLIST_START (32 * 1024)
633#define FLIST_LINEAR (FLIST_START * 512)
82ad07c4 634
9935066b 635/*
82ad07c4 636 * Extent size for allocation pools: A minimum size of 128KB
9935066b
S
637 * is needed to mmap them so that freeing will release the
638 * space to the OS.
639 *
640 * Larger sizes reduce leftover fragments and speed free calls
82ad07c4 641 * (when they happen). Smaller sizes increase the chance of
9935066b
S
642 * freed allocations freeing whole extents.
643 */
9935066b
S
644#define FILE_EXTENT (256 * 1024)
645#define HLINK_EXTENT (128 * 1024)
646
9ec3828b
WD
647#define FLIST_TEMP (1<<1)
648
c627d613 649struct file_list {
9ec3828b 650 struct file_list *next, *prev;
2b2ea368 651 struct file_struct **files;
9935066b 652 alloc_pool_t file_pool;
82ad07c4
WD
653 int count, malloced;
654 int low, high; /* 0-relative index values excluding empties */
9ec3828b
WD
655 int ndx_start; /* the start offset when incremental */
656 int parent_ndx; /* dir_flist index of parent directory */
657 int in_progress, to_redo;
c627d613
AT
658};
659
a3221d2a
WD
660#define SUMFLG_SAME_OFFSET (1<<0)
661
c627d613 662struct sum_buf {
6ded1170 663 OFF_T offset; /**< offset in file of this chunk */
a06b419d 664 int32 len; /**< length of chunk of file */
6ded1170 665 uint32 sum1; /**< simple checksum */
d733de97 666 int32 chain; /**< next hash-table collision */
a3221d2a 667 short flags; /**< flag bits */
6ded1170 668 char sum2[SUM_LENGTH]; /**< checksum */
c627d613
AT
669};
670
671struct sum_struct {
6ded1170 672 OFF_T flength; /**< total file length */
2b2ea368 673 struct sum_buf *sums; /**< points to info for each chunk */
15859584 674 int32 count; /**< how many chunks */
a06b419d
WD
675 int32 blength; /**< block_length */
676 int32 remainder; /**< flength % block_length */
da9d12f5 677 int s2length; /**< sum2_length */
c627d613
AT
678};
679
c6e7fcb4 680struct map_struct {
6a7cc46c
S
681 OFF_T file_size; /* File size (from stat) */
682 OFF_T p_offset; /* Window start */
683 OFF_T p_fd_offset; /* offset of cursor in fd ala lseek */
2b2ea368
WD
684 char *p; /* Window pointer */
685 int32 p_size; /* Largest window size we allocated */
686 int32 p_len; /* Latest (rounded) window size */
687 int32 def_window_size; /* Default window size */
688 int fd; /* File Descriptor */
689 int status; /* first errno from read errors */
c6e7fcb4 690};
c627d613 691
e2d22fee
WD
692#define MATCHFLG_WILD (1<<0) /* pattern has '*', '[', and/or '?' */
693#define MATCHFLG_WILD2 (1<<1) /* pattern has '**' */
685517ab
WD
694#define MATCHFLG_WILD2_PREFIX (1<<2) /* pattern starts with "**" */
695#define MATCHFLG_WILD3_SUFFIX (1<<3) /* pattern ends with "***" */
696#define MATCHFLG_ABS_PATH (1<<4) /* path-match on absolute path */
697#define MATCHFLG_INCLUDE (1<<5) /* this is an include, not an exclude */
698#define MATCHFLG_DIRECTORY (1<<6) /* this matches only directories */
46fa6025
WD
699#define MATCHFLG_WORD_SPLIT (1<<7) /* split rules on whitespace */
700#define MATCHFLG_NO_INHERIT (1<<8) /* don't inherit these rules */
701#define MATCHFLG_NO_PREFIXES (1<<9) /* parse no prefixes from patterns */
702#define MATCHFLG_MERGE_FILE (1<<10)/* specifies a file to merge */
703#define MATCHFLG_PERDIR_MERGE (1<<11)/* merge-file is searched per-dir */
704#define MATCHFLG_EXCLUDE_SELF (1<<12)/* merge-file name should be excluded */
705#define MATCHFLG_FINISH_SETUP (1<<13)/* per-dir merge file needs setup */
53b417e4
WD
706#define MATCHFLG_NEGATE (1<<14)/* rule matches when pattern does not */
707#define MATCHFLG_CVS_IGNORE (1<<15)/* rule was -C or :C */
a427e893
WD
708#define MATCHFLG_SENDER_SIDE (1<<16)/* rule applies to the sending side */
709#define MATCHFLG_RECEIVER_SIDE (1<<17)/* rule applies to the receiving side */
685517ab 710#define MATCHFLG_CLEAR_LIST (1<<18)/* this item is the "!" token */
c0f1e57b 711#define MATCHFLG_PERISHABLE (1<<19)/* perishable if parent dir goes away */
7c8e23bd 712
3b2461cf 713#define MATCHFLGS_FROM_CONTAINER (MATCHFLG_ABS_PATH | MATCHFLG_INCLUDE \
a427e893 714 | MATCHFLG_DIRECTORY | MATCHFLG_SENDER_SIDE \
c0f1e57b
WD
715 | MATCHFLG_NEGATE | MATCHFLG_RECEIVER_SIDE \
716 | MATCHFLG_PERISHABLE)
3b2461cf 717
7842418b
WD
718struct filter_struct {
719 struct filter_struct *next;
2b6b4d53 720 char *pattern;
53b417e4 721 uint32 match_flags;
46fa6025
WD
722 union {
723 int slash_cnt;
7842418b 724 struct filter_list_struct *mergelist;
46fa6025 725 } u;
2b6b4d53
AT
726};
727
7842418b
WD
728struct filter_list_struct {
729 struct filter_struct *head;
730 struct filter_struct *tail;
ac1d2d33 731 char *debug_type;
63d03319
WD
732};
733
a800434a
AT
734struct stats {
735 int64 total_size;
736 int64 total_transferred_size;
737 int64 total_written;
738 int64 total_read;
739 int64 literal_data;
740 int64 matched_data;
d4198823
WD
741 int64 flist_buildtime;
742 int64 flist_xfertime;
604dbf6d 743 int64 flist_size;
a800434a
AT
744 int num_files;
745 int num_transferred_files;
dc8293ff 746 int current_file_index;
a800434a
AT
747};
748
f7bce90c 749struct chmod_mode_struct;
2b6b4d53 750
c627d613 751#include "byteorder.h"
8de330a3 752#include "lib/mdfour.h"
87f18b62 753#include "lib/wildmatch.h"
740819ef 754#include "lib/permstring.h"
e20a4f84 755#include "lib/addrinfo.h"
0de40240 756
2b916250 757#ifndef __GNUC__
4ea58045 758#define __attribute__(x)
7de7b49f
WD
759# if __GNUC__ <= 2
760# define NORETURN
761# endif
4ea58045
WD
762#endif
763
90b13cf6 764#define UNUSED(x) x __attribute__((__unused__))
7de7b49f 765#ifndef NORETURN
011e85a5 766#define NORETURN __attribute__((__noreturn__))
7de7b49f 767#endif
90b13cf6 768
58c29609
MP
769#include "proto.h"
770
0de40240 771/* We have replacement versions of these if they're missing. */
4f5b0756 772#ifndef HAVE_ASPRINTF
0de40240
MP
773int asprintf(char **ptr, const char *format, ...);
774#endif
775
4f5b0756 776#ifndef HAVE_VASPRINTF
0de40240
MP
777int vasprintf(char **ptr, const char *format, va_list ap);
778#endif
779
4f5b0756 780#if !defined HAVE_VSNPRINTF || !defined HAVE_C99_VSNPRINTF
6813fa7e
WD
781#define vsnprintf rsync_vsnprintf
782int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
0de40240
MP
783#endif
784
4f5b0756 785#if !defined HAVE_SNPRINTF || !defined HAVE_C99_VSNPRINTF
6813fa7e 786#define snprintf rsync_snprintf
82ad07c4 787int snprintf(char *str, size_t count, const char *fmt,...);
0de40240
MP
788#endif
789
790
4f5b0756 791#ifndef HAVE_STRERROR
c627d613
AT
792extern char *sys_errlist[];
793#define strerror(i) sys_errlist[i]
794#endif
795
4f5b0756 796#ifndef HAVE_STRCHR
c627d613
AT
797# define strchr index
798# define strrchr rindex
799#endif
800
4f5b0756 801#ifndef HAVE_ERRNO_DECL
c627d613
AT
802extern int errno;
803#endif
804
9a929c8f
WD
805#ifdef HAVE_READLINK
806#define SUPPORT_LINKS 1
807#endif
808#ifdef HAVE_LINK
809#define SUPPORT_HARD_LINKS 1
810#endif
c627d613 811
5173f99e 812#ifdef HAVE_SIGACTION
90b13cf6
WD
813#define SIGACTION(n,h) sigact.sa_handler=(h), sigaction((n),&sigact,NULL)
814#define signal(n,h) we_need_to_call_SIGACTION_not_signal(n,h)
815#else
816#define SIGACTION(n,h) signal(n,h)
817#endif
720b47f2
AT
818
819#ifndef EWOULDBLOCK
820#define EWOULDBLOCK EAGAIN
821#endif
182dca5c 822
7b8356d0
AT
823#ifndef STDIN_FILENO
824#define STDIN_FILENO 0
825#endif
826
827#ifndef STDOUT_FILENO
828#define STDOUT_FILENO 1
829#endif
830
831#ifndef STDERR_FILENO
832#define STDERR_FILENO 2
833#endif
834
8120a98f
WD
835#ifndef S_IRUSR
836#define S_IRUSR 0400
837#endif
838
7b8356d0
AT
839#ifndef S_IWUSR
840#define S_IWUSR 0200
841#endif
842
b0d791bb
PG
843#ifndef ACCESSPERMS
844#define ACCESSPERMS 0777
845#endif
846
847#ifndef S_ISVTX
848#define S_ISVTX 0
849#endif
850
851#define CHMOD_BITS (S_ISUID | S_ISGID | S_ISVTX | ACCESSPERMS)
852
b280a1f4
AT
853#ifndef _S_IFMT
854#define _S_IFMT 0170000
855#endif
856
857#ifndef _S_IFLNK
858#define _S_IFLNK 0120000
859#endif
860
861#ifndef S_ISLNK
862#define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
863#endif
864
1e9f155a
AT
865#ifndef S_ISBLK
866#define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
867#endif
868
869#ifndef S_ISCHR
870#define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
871#endif
872
a0b65b18
AT
873#ifndef S_ISSOCK
874#ifdef _S_IFSOCK
875#define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
876#else
877#define S_ISSOCK(mode) (0)
878#endif
879#endif
880
881#ifndef S_ISFIFO
882#ifdef _S_IFIFO
883#define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
884#else
885#define S_ISFIFO(mode) (0)
886#endif
887#endif
888
1e9f155a
AT
889#ifndef S_ISDIR
890#define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
891#endif
892
893#ifndef S_ISREG
894#define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
895#endif
896
f0359dd0
AT
897/* work out what fcntl flag to use for non-blocking */
898#ifdef O_NONBLOCK
899# define NONBLOCK_FLAG O_NONBLOCK
4f5b0756 900#elif defined SYSV
f0359dd0 901# define NONBLOCK_FLAG O_NDELAY
d622d4bf 902#else
f0359dd0
AT
903# define NONBLOCK_FLAG FNDELAY
904#endif
905
d79c77ca
MP
906#ifndef INADDR_LOOPBACK
907#define INADDR_LOOPBACK 0x7f000001
908#endif
909
910#ifndef INADDR_NONE
911#define INADDR_NONE 0xffffffff
912#endif
a0b65b18 913
b5c6a6ae
WD
914#define IS_SPECIAL(mode) (S_ISSOCK(mode) || S_ISFIFO(mode))
915#define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode))
7bec6a5c 916
5afd8aed
DD
917/* Initial mask on permissions given to temporary files. Mask off setuid
918 bits and group access because of potential race-condition security
919 holes, and mask other access because mode 707 is bizarre */
972a3619 920#define INITACCESSPERMS 0700
e08bfe12
AT
921
922/* handler for null strings in printf format */
0ee6ca98 923#define NS(s) ((s)?(s):"<NULL>")
e08bfe12 924
58cadc86
WD
925/* Convenient wrappers for malloc and realloc. Use them. */
926#define new(type) ((type *)malloc(sizeof(type)))
927#define new_array(type, num) ((type *)_new_array(sizeof(type), (num)))
928#define realloc_array(ptr, type, num) ((type *)_realloc_array((ptr), sizeof(type), (num)))
fb859e56 929
e08bfe12 930/* use magic gcc attributes to catch format errors */
ff41a59f 931 void rprintf(enum logcode , const char *, ...)
fb859e56 932 __attribute__((format (printf, 2, 3)))
e08bfe12 933;
7b3d4257 934
a039749b
MP
935/* This is just like rprintf, but it also tries to print some
936 * representation of the error code. Normally errcode = errno. */
937void rsyserr(enum logcode, int, const char *, ...)
fb859e56 938 __attribute__((format (printf, 3, 4)))
a039749b
MP
939 ;
940
afbcc8f2
WD
941/* Make sure that the O_BINARY flag is defined. */
942#ifndef O_BINARY
943#define O_BINARY 0
944#endif
5a788ade 945
4f5b0756 946#ifndef HAVE_STRLCPY
5a788ade
AT
947size_t strlcpy(char *d, const char *s, size_t bufsize);
948#endif
949
4f5b0756 950#ifndef HAVE_STRLCAT
5a788ade
AT
951size_t strlcat(char *d, const char *s, size_t bufsize);
952#endif
953
82980a23
AT
954#ifndef WEXITSTATUS
955#define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF))
956#endif
47b032e9
WD
957#ifndef WIFEXITED
958#define WIFEXITED(stat) ((int)((stat)&0xFF) == 0)
959#endif
82980a23 960
a9766ef1 961#define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)
eecd22ff 962
4f5b0756 963#ifdef HAVE_GETEUID
b2bffbb2
WD
964#define MY_UID() geteuid()
965#else
966#define MY_UID() getuid()
967#endif
968
4f5b0756 969#ifdef HAVE_GETEGID
b2bffbb2
WD
970#define MY_GID() getegid()
971#else
972#define MY_GID() getgid()
973#endif
eecd22ff
MP
974
975extern int verbose;
d5d4b282 976
4f5b0756 977#ifndef HAVE_INET_NTOP
d622d4bf 978const char *inet_ntop(int af, const void *src, char *dst, size_t size);
4f5b0756 979#endif
8f694072 980
4f5b0756 981#ifndef HAVE_INET_PTON
bda41fa5 982int inet_pton(int af, const char *src, void *dst);
8f694072 983#endif
0f0ea7f7 984
f4a0483a
MP
985#ifdef MAINTAINER_MODE
986const char *get_panic_action(void);
987#endif
2dc7b8bd
WD
988
989static inline int
990isDigit(const char *ptr)
991{
992 return isdigit(*(unsigned char *)ptr);
993}
994
995static inline int
996isPrint(const char *ptr)
997{
998 return isprint(*(unsigned char *)ptr);
999}
1000
1001static inline int
1002isSpace(const char *ptr)
1003{
1004 return isspace(*(unsigned char *)ptr);
1005}
1006
1007static inline int
1008isLower(const char *ptr)
1009{
1010 return islower(*(unsigned char *)ptr);
1011}
1012
1013static inline int
1014isUpper(const char *ptr)
1015{
1016 return isupper(*(unsigned char *)ptr);
1017}
1018
1019static inline int
1020toLower(const char *ptr)
1021{
1022 return tolower(*(unsigned char *)ptr);
1023}
1024
1025static inline int
1026toUpper(const char *ptr)
1027{
1028 return toupper(*(unsigned char *)ptr);
1029}