2 Copyright (C) by Andrew Tridgell 1996, 2000
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define BLOCK_SIZE 700
26 #define RSYNC_RSH_ENV "RSYNC_RSH"
28 #define RSYNC_NAME "rsync"
29 #define RSYNCD_CONF "/etc/rsyncd.conf"
31 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
32 #define URL_PREFIX "rsync://"
34 #define BACKUP_SUFFIX "~"
36 /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
37 incompatible with older versions :-( */
41 #define FLAG_DELETE (1<<0)
42 #define SAME_MODE (1<<1)
43 #define SAME_RDEV (1<<2)
44 #define SAME_UID (1<<3)
45 #define SAME_GID (1<<4)
46 #define SAME_DIR (1<<5)
47 #define SAME_NAME SAME_DIR
48 #define LONG_NAME (1<<6)
49 #define SAME_TIME (1<<7)
51 /* update this if you make incompatible changes */
52 #define PROTOCOL_VERSION 26
54 /* We refuse to interoperate with versions that are not in this range.
55 * Note that we assume we'll work with later versions: the onus is on
56 * people writing them to make sure that they don't send us anything
57 * we won't understand.
59 * There are two possible explanations for the limit at thirty: either
60 * to allow new major-rev versions that do not interoperate with us,
61 * and (more likely) so that we can detect an attempt to connect rsync
62 * to a non-rsync server, which is unlikely to begin by sending a byte
63 * between 15 and 30. */
64 #define MIN_PROTOCOL_VERSION 15
65 #define MAX_PROTOCOL_VERSION 30
67 #define RSYNC_PORT 873
69 #define SPARSE_WRITE_SIZE (1024)
70 #define WRITE_SIZE (32*1024)
71 #define CHUNK_SIZE (32*1024)
72 #define MAX_MAP_SIZE (256*1024)
73 #define IO_BUFFER_SIZE (4092)
79 /* Log values. I *think* what these mean is: FLOG goes to the server
80 * logfile; FERROR and FINFO try to end up on the client, with
81 * different levels of filtering. */
82 enum logcode {FNONE=0, FERROR=1, FINFO=2, FLOG=3 };
89 #define RSYNC_RSH "remsh"
91 #define RSYNC_RSH "rsh"
94 #include <sys/types.h>
102 #ifdef HAVE_SYS_PARAM_H
103 #include <sys/param.h>
110 #ifdef HAVE_SYS_SOCKET_H
111 #include <sys/socket.h>
122 #ifdef TIME_WITH_SYS_TIME
123 #include <sys/time.h>
126 #ifdef HAVE_SYS_TIME_H
127 #include <sys/time.h>
136 #ifdef HAVE_SYS_FCNTL_H
137 #include <sys/fcntl.h>
141 #include <sys/stat.h>
143 #ifdef HAVE_SYS_IOCTL_H
144 #include <sys/ioctl.h>
147 #ifdef HAVE_SYS_FILIO_H
148 #include <sys/filio.h>
152 #ifdef HAVE_SYS_WAIT_H
153 #include <sys/wait.h>
167 #ifdef HAVE_SYS_SELECT_H
168 #include <sys/select.h>
171 #ifdef HAVE_SYS_MODE_H
172 /* apparently AIX needs this for S_ISLNK */
174 #include <sys/mode.h>
181 #include "lib/fnmatch.h"
192 /* these are needed for the uid/gid mapping code */
197 #include <netinet/in.h>
198 #include <arpa/inet.h>
201 #include <sys/file.h>
206 # define dirent direct
208 # include <sys/ndir.h>
211 # include <sys/dir.h>
226 #define uchar unsigned char
229 #if HAVE_UNSIGNED_CHAR
230 #define schar signed char
236 #if (SIZEOF_INT == 4)
238 #elif (SIZEOF_LONG == 4)
240 #elif (SIZEOF_SHORT == 4)
243 /* I hope this works */
250 #define uint32 unsigned int32
254 #define OFF_T off64_t
255 #define STRUCT_STAT struct stat64
258 #define STRUCT_STAT struct stat
262 #define int64 off64_t
263 #elif (SIZEOF_LONG == 8)
265 #elif (SIZEOF_INT == 8)
268 #define int64 long long
270 /* As long as it gets... */
275 /* We want to manipulate 64-bit inums. On some systems
276 * STRUCT_STAT.st_ino can be bigger than an ino_t depending on the
277 * combination of largefile feature macros. Rather than try to guess,
278 * we just internally store them in the largest know type. Hopefully
283 #define MIN(a,b) ((a)<(b)?(a):(b))
287 #define MAX(a,b) ((a)>(b)?(a):(b))
290 #ifndef MAXHOSTNAMELEN
291 #define MAXHOSTNAMELEN 256
294 /* the length of the md4 checksum */
295 #define MD4_SUM_LENGTH 16
296 #define SUM_LENGTH 16
299 #define MAXPATHLEN 1024
303 #define INADDR_NONE 0xffffffff
324 #define ARENA_SIZE (32 * 1024)
330 struct string_area *next;
336 struct file_struct **files;
337 struct string_area *string_area;
341 OFF_T offset; /* offset in file of this chunk */
342 int len; /* length of chunk of file */
343 int i; /* index of this chunk */
344 uint32 sum1; /* simple checksum */
345 char sum2[SUM_LENGTH]; /* checksum */
349 OFF_T flength; /* total file length */
350 int count; /* how many chunks */
351 int remainder; /* flength % block_length */
352 int n; /* block_length */
353 struct sum_buf *sums; /* points to info for each chunk */
359 OFF_T file_size, p_offset, p_fd_offset;
362 struct exclude_struct {
373 int64 total_transferred_size;
380 int num_transferred_files;
384 /* we need this function because of the silly way in which duplicate
385 entries are handled in the file lists - we can't change this
386 without breaking existing versions */
387 static inline int flist_up(struct file_list *flist, int i)
389 while (!flist->files[i]->basename) i++;
393 #include "byteorder.h"
395 #include "lib/mdfour.h"
396 #include "lib/permstring.h"
397 #include "lib/addrinfo.h"
399 /* We have replacement versions of these if they're missing. */
400 #ifndef HAVE_ASPRINTF
401 int asprintf(char **ptr, const char *format, ...);
404 #ifndef HAVE_VASPRINTF
405 int vasprintf(char **ptr, const char *format, va_list ap);
408 #if !defined(HAVE_VSNPRINTF) && !defined(HAVE_C99_VSNPRINTF)
409 int vsnprintf (char *str, size_t count, const char *fmt, va_list args);
412 #if !defined(HAVE_SNPRINTF) && !defined(HAVE_C99_VSNPRINTF)
413 int snprintf(char *str,size_t count,const char *fmt,...);
418 extern char *sys_errlist[];
419 #define strerror(i) sys_errlist[i]
423 # define strchr index
424 # define strrchr rindex
427 #ifndef HAVE_ERRNO_DECL
431 #define SUPPORT_LINKS HAVE_READLINK
432 #define SUPPORT_HARD_LINKS HAVE_LINK
438 #define SIGNAL_CAST (RETSIGTYPE (*)())
441 #define EWOULDBLOCK EAGAIN
445 #define STDIN_FILENO 0
448 #ifndef STDOUT_FILENO
449 #define STDOUT_FILENO 1
452 #ifndef STDERR_FILENO
453 #define STDERR_FILENO 2
461 #define _S_IFMT 0170000
465 #define _S_IFLNK 0120000
469 #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
473 #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
477 #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
482 #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
484 #define S_ISSOCK(mode) (0)
490 #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
492 #define S_ISFIFO(mode) (0)
497 #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
501 #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
504 /* work out what fcntl flag to use for non-blocking */
506 # define NONBLOCK_FLAG O_NONBLOCK
508 # define NONBLOCK_FLAG O_NDELAY
510 # define NONBLOCK_FLAG FNDELAY
513 #ifndef INADDR_LOOPBACK
514 #define INADDR_LOOPBACK 0x7f000001
518 #define INADDR_NONE 0xffffffff
521 #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode))
524 #define ACCESSPERMS 0777
526 /* Initial mask on permissions given to temporary files. Mask off setuid
527 bits and group access because of potential race-condition security
528 holes, and mask other access because mode 707 is bizarre */
529 #define INITACCESSPERMS 0700
531 /* handler for null strings in printf format */
532 #define NS(s) ((s)?(s):"<NULL>")
534 /* use magic gcc attributes to catch format errors */
535 void rprintf(enum logcode , const char *, ...)
537 __attribute__ ((format (printf, 2, 3)))
541 /* This is just like rprintf, but it also tries to print some
542 * representation of the error code. Normally errcode = errno. */
543 void rsyserr(enum logcode, int, const char *, ...)
545 __attribute__ ((format (printf, 3, 4)))
549 #ifdef REPLACE_INET_NTOA
550 #define inet_ntoa rep_inet_ntoa
555 size_t strlcpy(char *d, const char *s, size_t bufsize);
559 size_t strlcat(char *d, const char *s, size_t bufsize);
563 #define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF))
566 #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)
571 #ifndef HAVE_INET_NTOP
573 inet_ntop(int af, const void *src, char *dst, size_t size);
574 #endif /* !HAVE_INET_NTOP */
576 #ifndef HAVE_INET_PTON
577 int isc_net_pton(int af, const char *src, void *dst);