Some new defines for incremental-recursion mode.
[rsync/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, 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  *
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.
20  */
21
22 #define False 0
23 #define True 1
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 BACKUP_SUFFIX "~"
37
38 /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
39    incompatible with older versions :-( */
40 #define CHAR_OFFSET 0
41
42 /* These flags are only used during the flist transfer. */
43
44 #define XMIT_TOP_DIR (1<<0)
45 #define XMIT_SAME_MODE (1<<1)
46 #define XMIT_EXTENDED_FLAGS (1<<2)
47 #define XMIT_SAME_RDEV_pre28 XMIT_EXTENDED_FLAGS /* protocols < 28 */
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)
53 #define XMIT_SAME_RDEV_MAJOR (1<<8)
54 #define XMIT_HLINKED (1<<9)
55 #define XMIT_SAME_DEV_pre30 (1<<10)     /* protocols < 30 */
56 #define XMIT_HLINK_FIRST (1<<10)        /* protocols >= 30 */
57 #define XMIT_RDEV_MINOR_IS_SMALL (1<<11)
58 #define XMIT_USER_NAME_FOLLOWS (1<<12)  /* protocols >= 30 */
59 #define XMIT_GROUP_NAME_FOLLOWS (1<<13) /* protocols >= 30 */
60
61 /* These flags are used in the live flist data. */
62
63 #define FLAG_TOP_DIR (1<<0)     /* sender/receiver/generator */
64 #define FLAG_FILE_SENT (1<<1)   /* sender/receiver/generator */
65 #define FLAG_DIR_CHANGED (1<<1) /* generator */
66 #define FLAG_XFER_DIR (1<<2)    /* sender/receiver/generator */
67 #define FLAG_MOUNT_DIR (1<<3)   /* sender/generator */
68 #define FLAG_MISSING_DIR (1<<4) /* generator */
69 #define FLAG_HLINKED (1<<5)     /* receiver/generator */
70 #define FLAG_HLINK_FIRST (1<<6) /* receiver/generator */
71 #define FLAG_HLINK_LAST (1<<7)  /* receiver/generator */
72 #define FLAG_HLINK_DONE (1<<8)  /* receiver/generator */
73 #define FLAG_LENGTH64 (1<<9)    /* sender/receiver/generator */
74
75 /* These flags are passed to functions but not stored. */
76
77 #define FLAG_DIVERT_DIRS (1<<16)/* sender */
78
79 #define BITS_SET(val,bits) (((val) & (bits)) == (bits))
80 #define BITS_SETnUNSET(val,onbits,offbits) (((val) & ((onbits)|(offbits))) == (onbits))
81 #define BITS_EQUAL(b1,b2,mask) (((unsigned)(b1) & (unsigned)(mask)) \
82                              == ((unsigned)(b2) & (unsigned)(mask)))
83
84 /* update this if you make incompatible changes */
85 #define PROTOCOL_VERSION 30
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  *
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  *
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
104 #define MIN_PROTOCOL_VERSION 20
105 #define OLD_PROTOCOL_VERSION 25
106 #define MAX_PROTOCOL_VERSION 40
107
108 #define RSYNC_PORT 873
109
110 #define SPARSE_WRITE_SIZE (1024)
111 #define WRITE_SIZE (32*1024)
112 #define CHUNK_SIZE (32*1024)
113 #define MAX_MAP_SIZE (256*1024)
114 #define IO_BUFFER_SIZE (4092)
115 #define MAX_BLOCK_SIZE ((int32)1 << 29)
116
117 #define IOERR_GENERAL   (1<<0) /* For backward compatibility, this must == 1 */
118 #define IOERR_VANISHED  (1<<1)
119 #define IOERR_DEL_LIMIT (1<<2)
120
121 #define MAX_ARGS 1000
122 #define MAX_BASIS_DIRS 20
123 #define MAX_SERVER_ARGS (MAX_BASIS_DIRS*2 + 100)
124
125 #define MPLEX_BASE 7
126
127 #define NO_FILTERS      0
128 #define SERVER_FILTERS  1
129 #define ALL_FILTERS     2
130
131 #define XFLG_FATAL_ERRORS       (1<<0)
132 #define XFLG_OLD_PREFIXES       (1<<1)
133 #define XFLG_ANCHORED2ABS       (1<<2)
134 #define XFLG_ABS_IF_SLASH       (1<<3)
135
136 #define ATTRS_REPORT            (1<<0)
137 #define ATTRS_SKIP_MTIME        (1<<1)
138
139 #define FULL_FLUSH      1
140 #define NORMAL_FLUSH    0
141
142 #define PDIR_CREATE     1
143 #define PDIR_DELETE     0
144
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
148 #define FNAMECMP_FNAME          0x80
149 #define FNAMECMP_PARTIAL_DIR    0x81
150 #define FNAMECMP_BACKUP         0x82
151 #define FNAMECMP_FUZZY          0x83
152
153 /* For use by the itemize_changes code */
154 #define ITEM_REPORT_ATIME (1<<0)
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)
161 #define ITEM_REPORT_ACL (1<<7)
162 #define ITEM_REPORT_XATTR (1<<8)
163 #define ITEM_BASIS_TYPE_FOLLOWS (1<<11)
164 #define ITEM_XNAME_FOLLOWS (1<<12)
165 #define ITEM_IS_NEW (1<<13)
166 #define ITEM_LOCAL_CHANGE (1<<14)
167 #define ITEM_TRANSFER (1<<15)
168 /* These are outside the range of the transmitted flags. */
169 #define ITEM_MISSING_DATA (1<<16)          /* used by log_formatted() */
170 #define ITEM_DELETED (1<<17)               /* used by log_formatted() */
171 #define ITEM_MATCHED (1<<18)               /* used by itemize() */
172
173 #define SIGNIFICANT_ITEM_FLAGS (~(\
174         ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
175
176
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.
179  * FLOG only goes to the log file, not the client; FCLIENT is the opposite. */
180 enum logcode { FNONE=0, FERROR=1, FINFO=2, FLOG=3, FCLIENT=4, FSOCKERR=5 };
181
182 /* Messages types that are sent over the message channel.  The logcode
183  * values must all be present here with identical numbers. */
184 enum msgcode {
185         MSG_DATA=0,     /* raw data on the multiplexed stream */
186         MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */
187         MSG_LOG=FLOG, MSG_CLIENT=FCLIENT, MSG_SOCKERR=FSOCKERR, /* sibling logging */
188         MSG_REDO=9,     /* reprocess indicated flist index */
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 */
193         MSG_SUCCESS=100,/* successfully updated indicated flist index */
194         MSG_DELETED=101,/* successfully deleted a file on receiving side */
195         MSG_NO_SEND=102,/* sender failed to open a file we wanted */
196         MSG_DONE=86     /* current phase is done */
197 };
198
199 #define NDX_DONE -1
200 #define NDX_FLIST_EOF -2
201 #define NDX_FLIST_OFFSET -101
202
203 #include "errcode.h"
204
205 #include "config.h"
206
207 /* The default RSYNC_RSH is always set in config.h. */
208
209 #include <stdio.h>
210 #ifdef HAVE_SYS_TYPES_H
211 # include <sys/types.h>
212 #endif
213 #ifdef HAVE_SYS_STAT_H
214 # include <sys/stat.h>
215 #endif
216 #ifdef STDC_HEADERS
217 # include <stdlib.h>
218 # include <stddef.h>
219 #else
220 # ifdef HAVE_STDLIB_H
221 #  include <stdlib.h>
222 # endif
223 #endif
224 #ifdef HAVE_STRING_H
225 # if !defined STDC_HEADERS && defined HAVE_MEMORY_H
226 #  include <memory.h>
227 # endif
228 # include <string.h>
229 #endif
230 #ifdef HAVE_STRINGS_H
231 # include <strings.h>
232 #endif
233 #ifdef HAVE_INTTYPES_H
234 # include <inttypes.h>
235 #endif
236 #ifdef HAVE_UNISTD_H
237 # include <unistd.h>
238 #endif
239
240 #ifdef HAVE_SYS_PARAM_H
241 #include <sys/param.h>
242 #endif
243
244 #if defined HAVE_MALLOC_H && (defined HAVE_MALLINFO || !defined HAVE_STDLIB_H)
245 #include <malloc.h>
246 #endif
247
248 #ifdef HAVE_SYS_SOCKET_H
249 #include <sys/socket.h>
250 #endif
251
252 #ifdef TIME_WITH_SYS_TIME
253 #include <sys/time.h>
254 #include <time.h>
255 #else
256 #ifdef HAVE_SYS_TIME_H
257 #include <sys/time.h>
258 #else
259 #include <time.h>
260 #endif
261 #endif
262
263 #ifdef HAVE_FCNTL_H
264 #include <fcntl.h>
265 #else
266 #ifdef HAVE_SYS_FCNTL_H
267 #include <sys/fcntl.h>
268 #endif
269 #endif
270
271 #ifdef HAVE_SYS_IOCTL_H
272 #include <sys/ioctl.h>
273 #endif
274
275 #ifdef HAVE_SYS_FILIO_H
276 #include <sys/filio.h>
277 #endif
278
279 #include <signal.h>
280 #ifdef HAVE_SYS_WAIT_H
281 #include <sys/wait.h>
282 #endif
283 #ifdef HAVE_CTYPE_H
284 #include <ctype.h>
285 #endif
286 #ifdef HAVE_GRP_H
287 #include <grp.h>
288 #endif
289 #include <errno.h>
290
291 #ifdef HAVE_UTIME_H
292 #include <utime.h>
293 #endif
294
295 #ifdef HAVE_SYS_SELECT_H
296 #include <sys/select.h>
297 #endif
298
299 #ifdef HAVE_SYS_MODE_H
300 /* apparently AIX needs this for S_ISLNK */
301 #ifndef S_ISLNK
302 #include <sys/mode.h>
303 #endif
304 #endif
305
306 #ifdef HAVE_GLOB_H
307 #include <glob.h>
308 #endif
309
310 /* these are needed for the uid/gid mapping code */
311 #include <pwd.h>
312 #include <grp.h>
313
314 #include <stdarg.h>
315 #include <netinet/in.h>
316 #include <arpa/inet.h>
317 #include <netdb.h>
318 #include <syslog.h>
319 #include <sys/file.h>
320
321 #ifdef HAVE_DIRENT_H
322 # include <dirent.h>
323 #else
324 # define dirent direct
325 # ifdef HAVE_SYS_NDIR_H
326 #  include <sys/ndir.h>
327 # endif
328 # ifdef HAVE_SYS_DIR_H
329 #  include <sys/dir.h>
330 # endif
331 # ifdef HAVE_NDIR_H
332 #  include <ndir.h>
333 # endif
334 #endif
335
336 #ifdef MAJOR_IN_MKDEV
337 #include <sys/mkdev.h>
338 # if !defined makedev && (defined mkdev || defined _WIN32 || defined __WIN32__)
339 #  define makedev mkdev
340 # endif
341 #elif defined MAJOR_IN_SYSMACROS
342 #include <sys/sysmacros.h>
343 #endif
344
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
351 #ifdef HAVE_COMPAT_H
352 #include <compat.h>
353 #endif
354
355 #ifdef HAVE_LIMITS_H
356 # include <limits.h>
357 #endif
358
359 #include <assert.h>
360
361 #include "lib/pool_alloc.h"
362
363 #define BOOL int
364
365 #ifndef uchar
366 #define uchar unsigned char
367 #endif
368
369 #ifdef SIGNED_CHAR_OK
370 #define schar signed char
371 #else
372 #define schar char
373 #endif
374
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
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. */
394 #ifndef int32
395 #if SIZEOF_INT32_T == 4
396 # define int32 int32_t
397 # define SIZEOF_INT32 4
398 #elif SIZEOF_INT == 4
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
413 #else
414 # error Could not find a 32-bit integer variable
415 #endif
416 #else
417 # define SIZEOF_INT32 4
418 #endif
419
420 #ifndef uint32
421 #if SIZEOF_UINT32_T == 4
422 # define uint32 uint32_t
423 #else
424 # define uint32 unsigned int32
425 #endif
426 #endif
427
428 #if SIZEOF_OFF_T == 8 || !SIZEOF_OFF64_T || !defined HAVE_STRUCT_STAT64
429 #define OFF_T off_t
430 #define STRUCT_STAT struct stat
431 #else
432 #define OFF_T off64_t
433 #define STRUCT_STAT struct stat64
434 #define USE_STAT64_FUNCS 1
435 #endif
436
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). */
443 #if SIZEOF_INT64_T == 8
444 # define int64 int64_t
445 # define SIZEOF_INT64 8
446 #elif SIZEOF_LONG == 8
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
455 #elif SIZEOF_OFF64_T == 8
456 # define int64 off64_t
457 # define SIZEOF_INT64 8
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
470 #else
471 /* As long as it gets... */
472 # define int64 off_t
473 # define SIZEOF_INT64 SIZEOF_OFF_T
474 #endif
475
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.
497  *
498  * FIXME: I don't think the code in flist.c has ever worked on a system
499  * where dev_t is a struct.
500  */
501
502 struct idev_node {
503         int64 key;
504         void *data;
505 };
506
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
515 #ifndef MAXHOSTNAMELEN
516 #define MAXHOSTNAMELEN 256
517 #endif
518
519 /* the length of the md4 checksum */
520 #define MD4_SUM_LENGTH 16
521 #define SUM_LENGTH 16
522 #define SHORT_SUM_LENGTH 2
523 #define BLOCKSUM_BIAS 10
524
525 #ifndef MAXPATHLEN
526 #define MAXPATHLEN 1024
527 #endif
528
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
537 #ifndef NAME_MAX
538 #define NAME_MAX 255
539 #endif
540
541 #ifndef INADDR_NONE
542 #define INADDR_NONE 0xffffffff
543 #endif
544
545 #ifndef IN_LOOPBACKNET
546 #define IN_LOOPBACKNET 127
547 #endif
548
549 #define GID_NONE ((gid_t)-1)
550
551 union file_extras {
552         int32 num;
553         uint32 unum;
554 };
555
556 struct file_struct {
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 */
563 };
564
565 extern int file_extra_cnt;
566 extern int preserve_uid;
567 extern int preserve_gid;
568
569 #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
570 #define EXTRA_LEN (sizeof (union file_extras))
571 #define PTR_EXTRA_LEN ((sizeof (char *) + EXTRA_LEN - 1) / EXTRA_LEN)
572 #define SUM_EXTRA_CNT ((MD4_SUM_LENGTH + EXTRA_LEN - 1) / EXTRA_LEN)
573
574 #define REQ_EXTRA(f,ndx) ((union file_extras*)(f) - (ndx))
575 #define OPT_EXTRA(f,bump) ((union file_extras*)(f) - file_extra_cnt - 1 - (bump))
576
577 #define LEN64_BUMP(f) ((f)->flags & FLAG_LENGTH64 ? 1 : 0)
578 #define HLINK_BUMP(f) (F_IS_HLINKED(f) ? 1 : 0)
579
580 /* The length applies to all items. */
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
587
588 /* If there is a symlink string, it is always right after the basename */
589 #define F_SYMLINK(f) ((f)->basename + strlen((f)->basename) + 1)
590
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
597 /* When the associated option is on, all entries will have these present: */
598 #define F_UID(f) REQ_EXTRA(f, preserve_uid)->unum
599 #define F_GID(f) REQ_EXTRA(f, preserve_gid)->unum
600
601 /* These items are per-entry optional and mutally exclusive: */
602 #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
603 #define F_HL_PREV(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
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)
606
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)
610
611 /* The sum is only present on regular files. */
612 #define F_SUM(f) ((const char*)OPT_EXTRA(f, LEN64_BUMP(f) + HLINK_BUMP(f) \
613                                           + SUM_EXTRA_CNT - 1))
614
615 /* Some utility defines: */
616 #define F_IS_ACTIVE(f) (f)->basename[0]
617 #define F_IS_HLINKED(f) ((f)->flags & FLAG_HLINKED)
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)
620
621 #define DEV_MAJOR(a) (a)[0]
622 #define DEV_MINOR(a) (a)[1]
623
624 #define DIR_PARENT(a) (a)[0]
625 #define DIR_FIRST_CHILD(a) (a)[1]
626 #define DIR_NEXT_SIBLING(a) (a)[2]
627
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)
634
635 /*
636  * Extent size for allocation pools: A minimum size of 128KB
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
641  * (when they happen). Smaller sizes increase the chance of
642  * freed allocations freeing whole extents.
643  */
644 #define FILE_EXTENT     (256 * 1024)
645 #define HLINK_EXTENT    (128 * 1024)
646
647 #define FLIST_TEMP      (1<<1)
648
649 struct file_list {
650         struct file_list *next, *prev;
651         struct file_struct **files;
652         alloc_pool_t file_pool;
653         int count, malloced;
654         int low, high; /* 0-relative index values excluding empties */
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;
658 };
659
660 #define SUMFLG_SAME_OFFSET      (1<<0)
661
662 struct sum_buf {
663         OFF_T offset;           /**< offset in file of this chunk */
664         int32 len;              /**< length of chunk of file */
665         uint32 sum1;            /**< simple checksum */
666         int32 chain;            /**< next hash-table collision */
667         short flags;            /**< flag bits */
668         char sum2[SUM_LENGTH];  /**< checksum  */
669 };
670
671 struct sum_struct {
672         OFF_T flength;          /**< total file length */
673         struct sum_buf *sums;   /**< points to info for each chunk */
674         int32 count;            /**< how many chunks */
675         int32 blength;          /**< block_length */
676         int32 remainder;        /**< flength % block_length */
677         int s2length;           /**< sum2_length */
678 };
679
680 struct map_struct {
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     */
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         */
690 };
691
692 #define MATCHFLG_WILD           (1<<0) /* pattern has '*', '[', and/or '?' */
693 #define MATCHFLG_WILD2          (1<<1) /* pattern has '**' */
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 */
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 */
706 #define MATCHFLG_NEGATE         (1<<14)/* rule matches when pattern does not */
707 #define MATCHFLG_CVS_IGNORE     (1<<15)/* rule was -C or :C */
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 */
710 #define MATCHFLG_CLEAR_LIST     (1<<18)/* this item is the "!" token */
711 #define MATCHFLG_PERISHABLE     (1<<19)/* perishable if parent dir goes away */
712
713 #define MATCHFLGS_FROM_CONTAINER (MATCHFLG_ABS_PATH | MATCHFLG_INCLUDE \
714                                 | MATCHFLG_DIRECTORY | MATCHFLG_SENDER_SIDE \
715                                 | MATCHFLG_NEGATE | MATCHFLG_RECEIVER_SIDE \
716                                 | MATCHFLG_PERISHABLE)
717
718 struct filter_struct {
719         struct filter_struct *next;
720         char *pattern;
721         uint32 match_flags;
722         union {
723                 int slash_cnt;
724                 struct filter_list_struct *mergelist;
725         } u;
726 };
727
728 struct filter_list_struct {
729         struct filter_struct *head;
730         struct filter_struct *tail;
731         char *debug_type;
732 };
733
734 struct 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;
741         int64 flist_buildtime;
742         int64 flist_xfertime;
743         int64 flist_size;
744         int num_files;
745         int num_transferred_files;
746         int current_file_index;
747 };
748
749 struct chmod_mode_struct;
750
751 #include "byteorder.h"
752 #include "lib/mdfour.h"
753 #include "lib/wildmatch.h"
754 #include "lib/permstring.h"
755 #include "lib/addrinfo.h"
756
757 #ifndef __GNUC__
758 #define __attribute__(x)
759 # if __GNUC__ <= 2
760 # define NORETURN
761 # endif
762 #endif
763
764 #define UNUSED(x) x __attribute__((__unused__))
765 #ifndef NORETURN
766 #define NORETURN __attribute__((__noreturn__))
767 #endif
768
769 #include "proto.h"
770
771 /* We have replacement versions of these if they're missing. */
772 #ifndef HAVE_ASPRINTF
773 int asprintf(char **ptr, const char *format, ...);
774 #endif
775
776 #ifndef HAVE_VASPRINTF
777 int vasprintf(char **ptr, const char *format, va_list ap);
778 #endif
779
780 #if !defined HAVE_VSNPRINTF || !defined HAVE_C99_VSNPRINTF
781 #define vsnprintf rsync_vsnprintf
782 int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
783 #endif
784
785 #if !defined HAVE_SNPRINTF || !defined HAVE_C99_VSNPRINTF
786 #define snprintf rsync_snprintf
787 int snprintf(char *str, size_t count, const char *fmt,...);
788 #endif
789
790
791 #ifndef HAVE_STRERROR
792 extern char *sys_errlist[];
793 #define strerror(i) sys_errlist[i]
794 #endif
795
796 #ifndef HAVE_STRCHR
797 # define strchr                 index
798 # define strrchr                rindex
799 #endif
800
801 #ifndef HAVE_ERRNO_DECL
802 extern int errno;
803 #endif
804
805 #ifdef HAVE_READLINK
806 #define SUPPORT_LINKS 1
807 #endif
808 #ifdef HAVE_LINK
809 #define SUPPORT_HARD_LINKS 1
810 #endif
811
812 #ifdef HAVE_SIGACTION
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
818
819 #ifndef EWOULDBLOCK
820 #define EWOULDBLOCK EAGAIN
821 #endif
822
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
835 #ifndef S_IRUSR
836 #define S_IRUSR 0400
837 #endif
838
839 #ifndef S_IWUSR
840 #define S_IWUSR 0200
841 #endif
842
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
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
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
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
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
897 /* work out what fcntl flag to use for non-blocking */
898 #ifdef O_NONBLOCK
899 # define NONBLOCK_FLAG O_NONBLOCK
900 #elif defined SYSV
901 # define NONBLOCK_FLAG O_NDELAY
902 #else
903 # define NONBLOCK_FLAG FNDELAY
904 #endif
905
906 #ifndef INADDR_LOOPBACK
907 #define INADDR_LOOPBACK 0x7f000001
908 #endif
909
910 #ifndef INADDR_NONE
911 #define INADDR_NONE 0xffffffff
912 #endif
913
914 #define IS_SPECIAL(mode) (S_ISSOCK(mode) || S_ISFIFO(mode))
915 #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode))
916
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 */
920 #define INITACCESSPERMS 0700
921
922 /* handler for null strings in printf format */
923 #define NS(s) ((s)?(s):"<NULL>")
924
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)))
929
930 /* use magic gcc attributes to catch format errors */
931  void rprintf(enum logcode , const char *, ...)
932      __attribute__((format (printf, 2, 3)))
933 ;
934
935 /* This is just like rprintf, but it also tries to print some
936  * representation of the error code.  Normally errcode = errno. */
937 void rsyserr(enum logcode, int, const char *, ...)
938      __attribute__((format (printf, 3, 4)))
939      ;
940
941 /* Make sure that the O_BINARY flag is defined. */
942 #ifndef O_BINARY
943 #define O_BINARY 0
944 #endif
945
946 #ifndef HAVE_STRLCPY
947 size_t strlcpy(char *d, const char *s, size_t bufsize);
948 #endif
949
950 #ifndef HAVE_STRLCAT
951 size_t strlcat(char *d, const char *s, size_t bufsize);
952 #endif
953
954 #ifndef WEXITSTATUS
955 #define WEXITSTATUS(stat)       ((int)(((stat)>>8)&0xFF))
956 #endif
957 #ifndef WIFEXITED
958 #define WIFEXITED(stat)         ((int)((stat)&0xFF) == 0)
959 #endif
960
961 #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)
962
963 #ifdef HAVE_GETEUID
964 #define MY_UID() geteuid()
965 #else
966 #define MY_UID() getuid()
967 #endif
968
969 #ifdef HAVE_GETEGID
970 #define MY_GID() getegid()
971 #else
972 #define MY_GID() getgid()
973 #endif
974
975 extern int verbose;
976
977 #ifndef HAVE_INET_NTOP
978 const char *inet_ntop(int af, const void *src, char *dst, size_t size);
979 #endif
980
981 #ifndef HAVE_INET_PTON
982 int inet_pton(int af, const char *src, void *dst);
983 #endif
984
985 #ifdef MAINTAINER_MODE
986 const char *get_panic_action(void);
987 #endif
988
989 static inline int
990 isDigit(const char *ptr)
991 {
992         return isdigit(*(unsigned char *)ptr);
993 }
994
995 static inline int
996 isPrint(const char *ptr)
997 {
998         return isprint(*(unsigned char *)ptr);
999 }
1000
1001 static inline int
1002 isSpace(const char *ptr)
1003 {
1004         return isspace(*(unsigned char *)ptr);
1005 }
1006
1007 static inline int
1008 isLower(const char *ptr)
1009 {
1010         return islower(*(unsigned char *)ptr);
1011 }
1012
1013 static inline int
1014 isUpper(const char *ptr)
1015 {
1016         return isupper(*(unsigned char *)ptr);
1017 }
1018
1019 static inline int
1020 toLower(const char *ptr)
1021 {
1022         return tolower(*(unsigned char *)ptr);
1023 }
1024
1025 static inline int
1026 toUpper(const char *ptr)
1027 {
1028         return toupper(*(unsigned char *)ptr);
1029 }