Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

rsync.h

Go to the documentation of this file.
00001 /* 
00002    Copyright (C) by Andrew Tridgell 1996, 2000
00003    Copyright (C) Paul Mackerras 1996
00004    Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
00005    
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 
00022 #define False 0
00023 #define True 1
00024 
00025 #define BLOCK_SIZE 700
00026 #define RSYNC_RSH_ENV "RSYNC_RSH"
00027 
00028 #define RSYNC_NAME "rsync"
00029 #define RSYNCD_CONF "/etc/rsyncd.conf"
00030 
00031 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
00032 #define URL_PREFIX "rsync://"
00033 
00034 #define BACKUP_SUFFIX "~"
00035 
00036 /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
00037    incompatible with older versions :-( */
00038 #define CHAR_OFFSET 0
00039 
00040 
00041 #define FLAG_DELETE (1<<0)
00042 #define SAME_MODE (1<<1)
00043 #define SAME_RDEV (1<<2)
00044 #define SAME_UID (1<<3)
00045 #define SAME_GID (1<<4)
00046 #define SAME_DIR (1<<5)
00047 #define SAME_NAME SAME_DIR
00048 #define LONG_NAME (1<<6)
00049 #define SAME_TIME (1<<7)
00050 
00051 /* update this if you make incompatible changes */
00052 #define PROTOCOL_VERSION 26
00053 
00054 /* We refuse to interoperate with versions that are not in this range.
00055  * Note that we assume we'll work with later versions: the onus is on
00056  * people writing them to make sure that they don't send us anything
00057  * we won't understand.
00058  *
00059  * There are two possible explanations for the limit at thirty: either
00060  * to allow new major-rev versions that do not interoperate with us,
00061  * and (more likely) so that we can detect an attempt to connect rsync
00062  * to a non-rsync server, which is unlikely to begin by sending a byte
00063  * between 15 and 30. */
00064 #define MIN_PROTOCOL_VERSION 15
00065 #define MAX_PROTOCOL_VERSION 30
00066 
00067 #define RSYNC_PORT 873
00068 
00069 #define SPARSE_WRITE_SIZE (1024)
00070 #define WRITE_SIZE (32*1024)
00071 #define CHUNK_SIZE (32*1024)
00072 #define MAX_MAP_SIZE (256*1024)
00073 #define IO_BUFFER_SIZE (4092)
00074 
00075 #define MAX_ARGS 1000
00076 
00077 #define MPLEX_BASE 7
00078 
00079 /* Log values.  I *think* what these mean is: FLOG goes to the server
00080  * logfile; FERROR and FINFO try to end up on the client, with
00081  * different levels of filtering. */
00082 enum logcode {FNONE=0, FERROR=1, FINFO=2, FLOG=3 };
00083 
00084 #include "errcode.h"
00085 
00086 #include "config.h"
00087 
00088 /* The default RSYNC_RSH is always set in config.h, either to "remsh",
00089  * "rsh", or otherwise something specified by the user.  HAVE_REMSH
00090  * controls parameter munging for HP/UX, etc. */
00091 
00092 #include <sys/types.h>
00093 
00094 #ifdef HAVE_UNISTD_H
00095 #include <unistd.h>
00096 #endif
00097 #include <stdio.h>
00098 #include <stddef.h>
00099 
00100 #ifdef HAVE_SYS_PARAM_H
00101 #include <sys/param.h>
00102 #endif
00103 
00104 #ifdef HAVE_STDLIB_H
00105 #include <stdlib.h>
00106 #endif
00107 
00108 #ifdef HAVE_SYS_SOCKET_H
00109 #include <sys/socket.h>
00110 #endif
00111 
00112 #ifdef HAVE_STRING_H
00113 #include <string.h>
00114 #endif
00115 
00116 #ifdef HAVE_MALLOC_H
00117 #include <malloc.h>
00118 #endif
00119 
00120 #ifdef TIME_WITH_SYS_TIME
00121 #include <sys/time.h>
00122 #include <time.h>
00123 #else
00124 #ifdef HAVE_SYS_TIME_H
00125 #include <sys/time.h>
00126 #else
00127 #include <time.h>
00128 #endif
00129 #endif
00130 
00131 #ifdef HAVE_FCNTL_H
00132 #include <fcntl.h>
00133 #else
00134 #ifdef HAVE_SYS_FCNTL_H
00135 #include <sys/fcntl.h>
00136 #endif
00137 #endif
00138 
00139 #include <sys/stat.h>
00140 
00141 #ifdef HAVE_SYS_IOCTL_H
00142 #include <sys/ioctl.h>
00143 #endif
00144 
00145 #ifdef HAVE_SYS_FILIO_H
00146 #include <sys/filio.h>
00147 #endif
00148 
00149 #include <signal.h>
00150 #ifdef HAVE_SYS_WAIT_H
00151 #include <sys/wait.h>
00152 #endif
00153 #ifdef HAVE_CTYPE_H
00154 #include <ctype.h>
00155 #endif
00156 #ifdef HAVE_GRP_H
00157 #include <grp.h>
00158 #endif
00159 #include <errno.h>
00160 
00161 #ifdef HAVE_UTIME_H
00162 #include <utime.h>
00163 #endif
00164 
00165 #ifdef HAVE_SYS_SELECT_H
00166 #include <sys/select.h>
00167 #endif
00168 
00169 #ifdef HAVE_SYS_MODE_H
00170 /* apparently AIX needs this for S_ISLNK */
00171 #ifndef S_ISLNK
00172 #include <sys/mode.h>
00173 #endif
00174 #endif
00175 
00176 #ifdef HAVE_FNMATCH
00177 #include <fnmatch.h>
00178 #else
00179 #include "lib/fnmatch.h"
00180 #endif
00181 
00182 #ifdef HAVE_GLOB_H
00183 #include <glob.h>
00184 #endif
00185 
00186 #ifdef HAVE_MALLOC_H
00187 #  include <malloc.h>
00188 #endif
00189 
00190 /* these are needed for the uid/gid mapping code */
00191 #include <pwd.h>
00192 #include <grp.h>
00193 
00194 #include <stdarg.h>
00195 #include <netinet/in.h>
00196 #include <arpa/inet.h>
00197 #include <netdb.h>
00198 #include <syslog.h>
00199 #include <sys/file.h>
00200 
00201 #if HAVE_DIRENT_H
00202 # include <dirent.h>
00203 #else
00204 # define dirent direct
00205 # if HAVE_SYS_NDIR_H
00206 #  include <sys/ndir.h>
00207 # endif
00208 # if HAVE_SYS_DIR_H
00209 #  include <sys/dir.h>
00210 # endif
00211 # if HAVE_NDIR_H
00212 #  include <ndir.h>
00213 # endif
00214 #endif
00215 
00216 #ifdef HAVE_COMPAT_H
00217 #include <compat.h>
00218 #endif
00219 
00220 #include <assert.h>
00221 
00222 
00223 #define BOOL int
00224 
00225 #ifndef uchar
00226 #define uchar unsigned char
00227 #endif
00228 
00229 #if HAVE_UNSIGNED_CHAR
00230 #define schar signed char
00231 #else
00232 #define schar char
00233 #endif
00234 
00235 #ifndef int32
00236 #if (SIZEOF_INT == 4)
00237 #define int32 int
00238 #elif (SIZEOF_LONG == 4)
00239 #define int32 long
00240 #elif (SIZEOF_SHORT == 4)
00241 #define int32 short
00242 #else
00243 /* I hope this works */
00244 #define int32 int
00245 #define LARGE_INT32
00246 #endif
00247 #endif
00248 
00249 #ifndef uint32
00250 #define uint32 unsigned int32
00251 #endif
00252 
00253 #if HAVE_OFF64_T
00254 #define OFF_T off64_t
00255 #define STRUCT_STAT struct stat64
00256 #else
00257 #define OFF_T off_t
00258 #define STRUCT_STAT struct stat
00259 #endif
00260 
00261 #if HAVE_OFF64_T
00262 #define int64 off64_t
00263 #elif (SIZEOF_LONG == 8) 
00264 #define int64 long
00265 #elif (SIZEOF_INT == 8) 
00266 #define int64 int
00267 #elif HAVE_LONGLONG
00268 #define int64 long long
00269 #else
00270 /* As long as it gets... */
00271 #define int64 off_t
00272 #define NO_INT64
00273 #endif
00274 
00275 /* Starting from protocol version 26, we always use 64-bit
00276  * ino_t and dev_t internally, even if this platform does not
00277  * allow files to have 64-bit inums.  That's because the
00278  * receiver needs to find duplicate (dev,ino) tuples to detect
00279  * hardlinks, and it might have files coming from a platform
00280  * that has 64-bit inums.
00281  *
00282  * The only exception is if we're on a platform with no 64-bit type at
00283  * all.
00284  *
00285  * Because we use read_longint() to get these off the wire, if you
00286  * transfer devices or hardlinks with dev or inum > 2**32 to a machine
00287  * with no 64-bit types then you will get an overflow error.  Probably
00288  * not many people have that combination of machines, and you can
00289  * avoid it by not preserving hardlinks or not transferring device
00290  * nodes.  It's not clear that any other behaviour is better.
00291  *
00292  * Note that if you transfer devices from a 64-bit-devt machine (say,
00293  * Solaris) to a 32-bit-devt machine (say, Linux-2.2/x86) then the
00294  * device numbers will be truncated.  But it's a kind of silly thing
00295  * to do anyhow.
00296  *
00297  * FIXME: In future, we should probable split the device number into
00298  * major/minor, and transfer the two parts as 32-bit ints.  That gives
00299  * you somewhat more of a chance that they'll come from a big machine
00300  * to a little one in a useful way.
00301  *
00302  * FIXME: Really we need an unsigned type, and we perhaps ought to
00303  * cope with platforms on which this is an unsigned int or even a
00304  * struct.  Later.
00305  */ 
00306 #define INO64_T int64
00307 #define DEV64_T int64
00308 
00309 #ifndef MIN
00310 #define MIN(a,b) ((a)<(b)?(a):(b))
00311 #endif
00312 
00313 #ifndef MAX
00314 #define MAX(a,b) ((a)>(b)?(a):(b))
00315 #endif
00316 
00317 #ifndef MAXHOSTNAMELEN
00318 #define MAXHOSTNAMELEN 256
00319 #endif
00320 
00321 /* the length of the md4 checksum */
00322 #define MD4_SUM_LENGTH 16
00323 #define SUM_LENGTH 16
00324 
00325 #ifndef MAXPATHLEN
00326 #define MAXPATHLEN 1024
00327 #endif
00328 
00329 #ifndef INADDR_NONE
00330 #define INADDR_NONE 0xffffffff
00331 #endif
00332 
00333 struct file_struct {
00334         unsigned flags;
00335         time_t modtime;
00336         OFF_T length;
00337         mode_t mode;
00338 
00339         INO64_T inode;
00340         /** Device this file lives upon */
00341         DEV64_T dev;
00342 
00343         /** If this is a device node, the device number. */
00344         DEV64_T rdev;
00345         uid_t uid;
00346         gid_t gid;
00347         char *basename;
00348         char *dirname;
00349         char *basedir;
00350         char *link;
00351         char *sum;
00352 };
00353 
00354 
00355 #define ARENA_SIZE      (32 * 1024)
00356 
00357 struct string_area {
00358         char *base;
00359         char *end;
00360         char *current;
00361         struct string_area *next;
00362 };
00363 
00364 struct file_list {
00365         int count;
00366         int malloced;
00367         struct file_struct **files;
00368         struct string_area *string_area;
00369 };
00370 
00371 struct sum_buf {
00372         OFF_T offset;           /**< offset in file of this chunk */
00373         int len;                /**< length of chunk of file */
00374         int i;                  /**< index of this chunk */
00375         uint32 sum1;            /**< simple checksum */
00376         char sum2[SUM_LENGTH];  /**< checksum  */
00377 };
00378 
00379 struct sum_struct {
00380         OFF_T flength;          /**< total file length */
00381         size_t count;           /**< how many chunks */
00382         size_t remainder;       /**< flength % block_length */
00383         size_t n;               /**< block_length */
00384         struct sum_buf *sums;   /**< points to info for each chunk */
00385 };
00386 
00387 struct map_struct {
00388         char *p;
00389         int fd,p_size,p_len;
00390         OFF_T file_size, p_offset, p_fd_offset;
00391 };
00392 
00393 struct exclude_struct {
00394         char *pattern;
00395         int regular_exp;
00396         int fnmatch_flags;
00397         int include;
00398         int directory;
00399         int local;
00400 };
00401 
00402 struct stats {
00403         int64 total_size;
00404         int64 total_transferred_size;
00405         int64 total_written;
00406         int64 total_read;
00407         int64 literal_data;
00408         int64 matched_data;
00409         int flist_size;
00410         int num_files;
00411         int num_transferred_files;
00412 };
00413 
00414 
00415 /* we need this function because of the silly way in which duplicate
00416    entries are handled in the file lists - we can't change this
00417    without breaking existing versions */
00418 static inline int flist_up(struct file_list *flist, int i)
00419 {
00420         while (!flist->files[i]->basename) i++;
00421         return i;
00422 }
00423 
00424 #include "byteorder.h"
00425 #include "lib/mdfour.h"
00426 #include "lib/permstring.h"
00427 #include "lib/addrinfo.h"
00428 
00429 #include "proto.h"
00430 
00431 /* We have replacement versions of these if they're missing. */
00432 #ifndef HAVE_ASPRINTF
00433 int asprintf(char **ptr, const char *format, ...);
00434 #endif
00435 
00436 #ifndef HAVE_VASPRINTF
00437 int vasprintf(char **ptr, const char *format, va_list ap);
00438 #endif
00439 
00440 #if !defined(HAVE_VSNPRINTF) && !defined(HAVE_C99_VSNPRINTF)
00441 int vsnprintf (char *str, size_t count, const char *fmt, va_list args);
00442 #endif
00443 
00444 #if !defined(HAVE_SNPRINTF) && !defined(HAVE_C99_VSNPRINTF)
00445 int snprintf(char *str,size_t count,const char *fmt,...);
00446 #endif
00447 
00448 
00449 #if !HAVE_STRERROR
00450 extern char *sys_errlist[];
00451 #define strerror(i) sys_errlist[i]
00452 #endif
00453 
00454 #ifndef HAVE_STRCHR
00455 # define strchr                 index
00456 # define strrchr                rindex
00457 #endif
00458 
00459 #ifndef HAVE_ERRNO_DECL
00460 extern int errno;
00461 #endif
00462 
00463 #define SUPPORT_LINKS HAVE_READLINK
00464 #define SUPPORT_HARD_LINKS HAVE_LINK
00465 
00466 /* This could be bad on systems which have no lchown and where chown
00467  * follows symbollic links.  On such systems it might be better not to
00468  * try to chown symlinks at all. */
00469 #ifndef HAVE_LCHOWN
00470 #define lchown chown
00471 #endif
00472 
00473 #define SIGNAL_CAST (RETSIGTYPE (*)())
00474 
00475 #ifndef EWOULDBLOCK
00476 #define EWOULDBLOCK EAGAIN
00477 #endif
00478 
00479 #ifndef STDIN_FILENO
00480 #define STDIN_FILENO 0
00481 #endif
00482 
00483 #ifndef STDOUT_FILENO
00484 #define STDOUT_FILENO 1
00485 #endif
00486 
00487 #ifndef STDERR_FILENO
00488 #define STDERR_FILENO 2
00489 #endif
00490 
00491 #ifndef S_IWUSR
00492 #define S_IWUSR 0200
00493 #endif
00494 
00495 #ifndef _S_IFMT
00496 #define _S_IFMT        0170000
00497 #endif
00498 
00499 #ifndef _S_IFLNK
00500 #define _S_IFLNK  0120000
00501 #endif
00502 
00503 #ifndef S_ISLNK
00504 #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
00505 #endif
00506 
00507 #ifndef S_ISBLK
00508 #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
00509 #endif
00510 
00511 #ifndef S_ISCHR
00512 #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
00513 #endif
00514 
00515 #ifndef S_ISSOCK
00516 #ifdef _S_IFSOCK
00517 #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
00518 #else
00519 #define S_ISSOCK(mode) (0)
00520 #endif
00521 #endif
00522 
00523 #ifndef S_ISFIFO
00524 #ifdef _S_IFIFO
00525 #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
00526 #else
00527 #define S_ISFIFO(mode) (0)
00528 #endif
00529 #endif
00530 
00531 #ifndef S_ISDIR
00532 #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
00533 #endif
00534 
00535 #ifndef S_ISREG
00536 #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
00537 #endif
00538 
00539 /* work out what fcntl flag to use for non-blocking */
00540 #ifdef O_NONBLOCK
00541 # define NONBLOCK_FLAG O_NONBLOCK
00542 #elif defined(SYSV)
00543 # define NONBLOCK_FLAG O_NDELAY
00544 #else 
00545 # define NONBLOCK_FLAG FNDELAY
00546 #endif
00547 
00548 #ifndef INADDR_LOOPBACK
00549 #define INADDR_LOOPBACK 0x7f000001
00550 #endif
00551 
00552 #ifndef INADDR_NONE
00553 #define INADDR_NONE 0xffffffff
00554 #endif
00555 
00556 #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode))
00557 
00558 #ifndef ACCESSPERMS
00559 #define ACCESSPERMS 0777
00560 #endif
00561 /* Initial mask on permissions given to temporary files.  Mask off setuid
00562      bits and group access because of potential race-condition security
00563      holes, and mask other access because mode 707 is bizarre */
00564 #define INITACCESSPERMS 0700
00565 
00566 /* handler for null strings in printf format */
00567 #define NS(s) ((s)?(s):"<NULL>")
00568 
00569 #if !defined(__GNUC__) || defined(APPLE)
00570 /* Apparently the OS X port of gcc gags on __attribute__.
00571  *
00572  * <http://www.opensource.apple.com/bugs/X/gcc/2512150.html> */
00573 #define __attribute__(x) 
00574 
00575 #endif
00576 
00577 
00578 /* use magic gcc attributes to catch format errors */
00579  void rprintf(enum logcode , const char *, ...)
00580      __attribute__((format (printf, 2, 3)))
00581 ;
00582 
00583 /* This is just like rprintf, but it also tries to print some
00584  * representation of the error code.  Normally errcode = errno. */
00585 void rsyserr(enum logcode, int, const char *, ...)
00586      __attribute__((format (printf, 3, 4)))
00587      ;
00588 
00589 #ifdef REPLACE_INET_NTOA
00590 #define inet_ntoa rep_inet_ntoa
00591 #endif
00592 
00593 
00594 #ifndef HAVE_STRLCPY
00595 size_t strlcpy(char *d, const char *s, size_t bufsize);
00596 #endif
00597 
00598 #ifndef HAVE_STRLCAT
00599 size_t strlcat(char *d, const char *s, size_t bufsize);
00600 #endif
00601 
00602 #ifndef WEXITSTATUS
00603 #define WEXITSTATUS(stat)       ((int)(((stat)>>8)&0xFF))
00604 #endif
00605 
00606 #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)
00607 
00608 
00609 extern int verbose;
00610 
00611 #ifndef HAVE_INET_NTOP
00612 const char *                 
00613 inet_ntop(int af, const void *src, char *dst, size_t size);
00614 #endif /* !HAVE_INET_NTOP */
00615 
00616 #ifndef HAVE_INET_PTON
00617 int isc_net_pton(int af, const char *src, void *dst);
00618 #endif
00619 
00620 #ifdef MAINTAINER_MODE
00621 const char *get_panic_action(void);
00622 #endif
00623 
00624 #define UNUSED(x) x __attribute__((__unused__))
00625 
00626 const char *io_write_phase, *io_read_phase;

Generated on Tue Apr 16 12:37:37 2002 for rsync by doxygen1.2.15