The ndx arg passed to increment_active_files() is now the unique,
[rsync/rsync.git] / rsync.h
... / ...
CommitLineData
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, 2007 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. */
180enum 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. */
184enum 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
502struct 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
551union file_extras {
552 int32 num;
553 uint32 unum;
554};
555
556struct 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
565extern int file_extra_cnt;
566extern int preserve_uid;
567extern 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_OWNER(f) REQ_EXTRA(f, preserve_uid)->unum
599#define F_GROUP(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
619#define F_HLINK_NOT_FIRST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_FIRST)
620#define F_HLINK_NOT_LAST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_LAST)
621
622#define F_UID(f) ((uid_t)F_OWNER(f))
623#define F_GID(f) ((gid_t)F_GROUP(f))
624
625#define DEV_MAJOR(a) (a)[0]
626#define DEV_MINOR(a) (a)[1]
627
628#define DIR_PARENT(a) (a)[0]
629#define DIR_FIRST_CHILD(a) (a)[1]
630#define DIR_NEXT_SIBLING(a) (a)[2]
631
632/*
633 * Start the flist array at FLIST_START entries and grow it
634 * by doubling until FLIST_LINEAR then grow by FLIST_LINEAR
635 */
636#define FLIST_START (32 * 1024)
637#define FLIST_LINEAR (FLIST_START * 512)
638
639/*
640 * Extent size for allocation pools: A minimum size of 128KB
641 * is needed to mmap them so that freeing will release the
642 * space to the OS.
643 *
644 * Larger sizes reduce leftover fragments and speed free calls
645 * (when they happen). Smaller sizes increase the chance of
646 * freed allocations freeing whole extents.
647 */
648#define FILE_EXTENT (256 * 1024)
649#define HLINK_EXTENT (128 * 1024)
650
651#define FLIST_TEMP (1<<1)
652
653struct file_list {
654 struct file_list *next, *prev;
655 struct file_struct **files;
656 alloc_pool_t file_pool;
657 int count, malloced;
658 int low, high; /* 0-relative index values excluding empties */
659 int ndx_start; /* the start offset for inc_recurse mode */
660 int parent_ndx; /* dir_flist index of parent directory */
661 int in_progress, to_redo;
662};
663
664#define SUMFLG_SAME_OFFSET (1<<0)
665
666struct sum_buf {
667 OFF_T offset; /**< offset in file of this chunk */
668 int32 len; /**< length of chunk of file */
669 uint32 sum1; /**< simple checksum */
670 int32 chain; /**< next hash-table collision */
671 short flags; /**< flag bits */
672 char sum2[SUM_LENGTH]; /**< checksum */
673};
674
675struct sum_struct {
676 OFF_T flength; /**< total file length */
677 struct sum_buf *sums; /**< points to info for each chunk */
678 int32 count; /**< how many chunks */
679 int32 blength; /**< block_length */
680 int32 remainder; /**< flength % block_length */
681 int s2length; /**< sum2_length */
682};
683
684struct map_struct {
685 OFF_T file_size; /* File size (from stat) */
686 OFF_T p_offset; /* Window start */
687 OFF_T p_fd_offset; /* offset of cursor in fd ala lseek */
688 char *p; /* Window pointer */
689 int32 p_size; /* Largest window size we allocated */
690 int32 p_len; /* Latest (rounded) window size */
691 int32 def_window_size; /* Default window size */
692 int fd; /* File Descriptor */
693 int status; /* first errno from read errors */
694};
695
696#define MATCHFLG_WILD (1<<0) /* pattern has '*', '[', and/or '?' */
697#define MATCHFLG_WILD2 (1<<1) /* pattern has '**' */
698#define MATCHFLG_WILD2_PREFIX (1<<2) /* pattern starts with "**" */
699#define MATCHFLG_WILD3_SUFFIX (1<<3) /* pattern ends with "***" */
700#define MATCHFLG_ABS_PATH (1<<4) /* path-match on absolute path */
701#define MATCHFLG_INCLUDE (1<<5) /* this is an include, not an exclude */
702#define MATCHFLG_DIRECTORY (1<<6) /* this matches only directories */
703#define MATCHFLG_WORD_SPLIT (1<<7) /* split rules on whitespace */
704#define MATCHFLG_NO_INHERIT (1<<8) /* don't inherit these rules */
705#define MATCHFLG_NO_PREFIXES (1<<9) /* parse no prefixes from patterns */
706#define MATCHFLG_MERGE_FILE (1<<10)/* specifies a file to merge */
707#define MATCHFLG_PERDIR_MERGE (1<<11)/* merge-file is searched per-dir */
708#define MATCHFLG_EXCLUDE_SELF (1<<12)/* merge-file name should be excluded */
709#define MATCHFLG_FINISH_SETUP (1<<13)/* per-dir merge file needs setup */
710#define MATCHFLG_NEGATE (1<<14)/* rule matches when pattern does not */
711#define MATCHFLG_CVS_IGNORE (1<<15)/* rule was -C or :C */
712#define MATCHFLG_SENDER_SIDE (1<<16)/* rule applies to the sending side */
713#define MATCHFLG_RECEIVER_SIDE (1<<17)/* rule applies to the receiving side */
714#define MATCHFLG_CLEAR_LIST (1<<18)/* this item is the "!" token */
715#define MATCHFLG_PERISHABLE (1<<19)/* perishable if parent dir goes away */
716
717#define MATCHFLGS_FROM_CONTAINER (MATCHFLG_ABS_PATH | MATCHFLG_INCLUDE \
718 | MATCHFLG_DIRECTORY | MATCHFLG_SENDER_SIDE \
719 | MATCHFLG_NEGATE | MATCHFLG_RECEIVER_SIDE \
720 | MATCHFLG_PERISHABLE)
721
722struct filter_struct {
723 struct filter_struct *next;
724 char *pattern;
725 uint32 match_flags;
726 union {
727 int slash_cnt;
728 struct filter_list_struct *mergelist;
729 } u;
730};
731
732struct filter_list_struct {
733 struct filter_struct *head;
734 struct filter_struct *tail;
735 char *debug_type;
736};
737
738struct stats {
739 int64 total_size;
740 int64 total_transferred_size;
741 int64 total_written;
742 int64 total_read;
743 int64 literal_data;
744 int64 matched_data;
745 int64 flist_buildtime;
746 int64 flist_xfertime;
747 int64 flist_size;
748 int num_files;
749 int num_transferred_files;
750 int current_file_index;
751};
752
753struct chmod_mode_struct;
754
755#include "byteorder.h"
756#include "lib/mdfour.h"
757#include "lib/wildmatch.h"
758#include "lib/permstring.h"
759#include "lib/addrinfo.h"
760
761#ifndef __GNUC__
762#define __attribute__(x)
763# if __GNUC__ <= 2
764# define NORETURN
765# endif
766#endif
767
768#define UNUSED(x) x __attribute__((__unused__))
769#ifndef NORETURN
770#define NORETURN __attribute__((__noreturn__))
771#endif
772
773#include "proto.h"
774
775/* We have replacement versions of these if they're missing. */
776#ifndef HAVE_ASPRINTF
777int asprintf(char **ptr, const char *format, ...);
778#endif
779
780#ifndef HAVE_VASPRINTF
781int vasprintf(char **ptr, const char *format, va_list ap);
782#endif
783
784#if !defined HAVE_VSNPRINTF || !defined HAVE_C99_VSNPRINTF
785#define vsnprintf rsync_vsnprintf
786int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
787#endif
788
789#if !defined HAVE_SNPRINTF || !defined HAVE_C99_VSNPRINTF
790#define snprintf rsync_snprintf
791int snprintf(char *str, size_t count, const char *fmt,...);
792#endif
793
794
795#ifndef HAVE_STRERROR
796extern char *sys_errlist[];
797#define strerror(i) sys_errlist[i]
798#endif
799
800#ifndef HAVE_STRCHR
801# define strchr index
802# define strrchr rindex
803#endif
804
805#ifndef HAVE_ERRNO_DECL
806extern int errno;
807#endif
808
809#ifdef HAVE_READLINK
810#define SUPPORT_LINKS 1
811#endif
812#ifdef HAVE_LINK
813#define SUPPORT_HARD_LINKS 1
814#endif
815
816#ifdef HAVE_SIGACTION
817#define SIGACTION(n,h) sigact.sa_handler=(h), sigaction((n),&sigact,NULL)
818#define signal(n,h) we_need_to_call_SIGACTION_not_signal(n,h)
819#else
820#define SIGACTION(n,h) signal(n,h)
821#endif
822
823#ifndef EWOULDBLOCK
824#define EWOULDBLOCK EAGAIN
825#endif
826
827#ifndef STDIN_FILENO
828#define STDIN_FILENO 0
829#endif
830
831#ifndef STDOUT_FILENO
832#define STDOUT_FILENO 1
833#endif
834
835#ifndef STDERR_FILENO
836#define STDERR_FILENO 2
837#endif
838
839#ifndef S_IRUSR
840#define S_IRUSR 0400
841#endif
842
843#ifndef S_IWUSR
844#define S_IWUSR 0200
845#endif
846
847#ifndef ACCESSPERMS
848#define ACCESSPERMS 0777
849#endif
850
851#ifndef S_ISVTX
852#define S_ISVTX 0
853#endif
854
855#define CHMOD_BITS (S_ISUID | S_ISGID | S_ISVTX | ACCESSPERMS)
856
857#ifndef _S_IFMT
858#define _S_IFMT 0170000
859#endif
860
861#ifndef _S_IFLNK
862#define _S_IFLNK 0120000
863#endif
864
865#ifndef S_ISLNK
866#define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
867#endif
868
869#ifndef S_ISBLK
870#define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
871#endif
872
873#ifndef S_ISCHR
874#define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
875#endif
876
877#ifndef S_ISSOCK
878#ifdef _S_IFSOCK
879#define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
880#else
881#define S_ISSOCK(mode) (0)
882#endif
883#endif
884
885#ifndef S_ISFIFO
886#ifdef _S_IFIFO
887#define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
888#else
889#define S_ISFIFO(mode) (0)
890#endif
891#endif
892
893#ifndef S_ISDIR
894#define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
895#endif
896
897#ifndef S_ISREG
898#define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
899#endif
900
901/* work out what fcntl flag to use for non-blocking */
902#ifdef O_NONBLOCK
903# define NONBLOCK_FLAG O_NONBLOCK
904#elif defined SYSV
905# define NONBLOCK_FLAG O_NDELAY
906#else
907# define NONBLOCK_FLAG FNDELAY
908#endif
909
910#ifndef INADDR_LOOPBACK
911#define INADDR_LOOPBACK 0x7f000001
912#endif
913
914#ifndef INADDR_NONE
915#define INADDR_NONE 0xffffffff
916#endif
917
918#define IS_SPECIAL(mode) (S_ISSOCK(mode) || S_ISFIFO(mode))
919#define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode))
920
921/* Initial mask on permissions given to temporary files. Mask off setuid
922 bits and group access because of potential race-condition security
923 holes, and mask other access because mode 707 is bizarre */
924#define INITACCESSPERMS 0700
925
926/* handler for null strings in printf format */
927#define NS(s) ((s)?(s):"<NULL>")
928
929/* Convenient wrappers for malloc and realloc. Use them. */
930#define new(type) ((type *)malloc(sizeof(type)))
931#define new_array(type, num) ((type *)_new_array(sizeof(type), (num)))
932#define realloc_array(ptr, type, num) ((type *)_realloc_array((ptr), sizeof(type), (num)))
933
934/* use magic gcc attributes to catch format errors */
935 void rprintf(enum logcode , const char *, ...)
936 __attribute__((format (printf, 2, 3)))
937;
938
939/* This is just like rprintf, but it also tries to print some
940 * representation of the error code. Normally errcode = errno. */
941void rsyserr(enum logcode, int, const char *, ...)
942 __attribute__((format (printf, 3, 4)))
943 ;
944
945/* Make sure that the O_BINARY flag is defined. */
946#ifndef O_BINARY
947#define O_BINARY 0
948#endif
949
950#ifndef HAVE_STRLCPY
951size_t strlcpy(char *d, const char *s, size_t bufsize);
952#endif
953
954#ifndef HAVE_STRLCAT
955size_t strlcat(char *d, const char *s, size_t bufsize);
956#endif
957
958#ifndef WEXITSTATUS
959#define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF))
960#endif
961#ifndef WIFEXITED
962#define WIFEXITED(stat) ((int)((stat)&0xFF) == 0)
963#endif
964
965#define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)
966
967#ifdef HAVE_GETEUID
968#define MY_UID() geteuid()
969#else
970#define MY_UID() getuid()
971#endif
972
973#ifdef HAVE_GETEGID
974#define MY_GID() getegid()
975#else
976#define MY_GID() getgid()
977#endif
978
979extern int verbose;
980
981#ifndef HAVE_INET_NTOP
982const char *inet_ntop(int af, const void *src, char *dst, size_t size);
983#endif
984
985#ifndef HAVE_INET_PTON
986int inet_pton(int af, const char *src, void *dst);
987#endif
988
989#ifdef MAINTAINER_MODE
990const char *get_panic_action(void);
991#endif
992
993static inline int
994isDigit(const char *ptr)
995{
996 return isdigit(*(unsigned char *)ptr);
997}
998
999static inline int
1000isPrint(const char *ptr)
1001{
1002 return isprint(*(unsigned char *)ptr);
1003}
1004
1005static inline int
1006isSpace(const char *ptr)
1007{
1008 return isspace(*(unsigned char *)ptr);
1009}
1010
1011static inline int
1012isLower(const char *ptr)
1013{
1014 return islower(*(unsigned char *)ptr);
1015}
1016
1017static inline int
1018isUpper(const char *ptr)
1019{
1020 return isupper(*(unsigned char *)ptr);
1021}
1022
1023static inline int
1024toLower(const char *ptr)
1025{
1026 return tolower(*(unsigned char *)ptr);
1027}
1028
1029static inline int
1030toUpper(const char *ptr)
1031{
1032 return toupper(*(unsigned char *)ptr);
1033}