9e7775c7ce9d45bbceba05f28fc295ef4a174547
[rsync/rsync.git] / rsync.h
1 /* 
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #define False 0
21 #define True 1
22
23 #define BLOCK_SIZE 700
24 #define RSYNC_RSH_ENV "RSYNC_RSH"
25
26 #define RSYNC_NAME "rsync"
27 #define RSYNCD_CONF "/etc/rsyncd.conf"
28
29 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
30 #define URL_PREFIX "rsync://"
31
32 #define BACKUP_SUFFIX "~"
33
34 /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
35    incompatible with older versions :-( */
36 #define CHAR_OFFSET 0
37
38
39 #define FLAG_DELETE (1<<0)
40 #define SAME_MODE (1<<1)
41 #define SAME_RDEV (1<<2)
42 #define SAME_UID (1<<3)
43 #define SAME_GID (1<<4)
44 #define SAME_DIR (1<<5)
45 #define SAME_NAME SAME_DIR
46 #define LONG_NAME (1<<6)
47 #define SAME_TIME (1<<7)
48
49 /* update this if you make incompatible changes */
50 #define PROTOCOL_VERSION 22
51 #define MIN_PROTOCOL_VERSION 11
52 #define MAX_PROTOCOL_VERSION 30
53
54 #define RSYNC_PORT 873
55
56 #define SPARSE_WRITE_SIZE (1024)
57 #define WRITE_SIZE (32*1024)
58 #define CHUNK_SIZE (32*1024)
59 #define MAX_MAP_SIZE (256*1024)
60 #define IO_BUFFER_SIZE (4092)
61
62 #define MAX_ARGS 1000
63
64 #define MPLEX_BASE 7
65 #define FERROR 1
66 #define FINFO 2
67 #define FLOG 3
68
69 #include "errcode.h"
70
71 #include "config.h"
72
73 #if HAVE_REMSH
74 #define RSYNC_RSH "remsh"
75 #else
76 #define RSYNC_RSH "rsh"
77 #endif
78
79 #include <sys/types.h>
80
81 #ifdef HAVE_GETOPT_LONG
82 #include <getopt.h>
83 #else
84 #include "lib/getopt.h"
85 #endif
86
87 #ifdef HAVE_UNISTD_H
88 #include <unistd.h>
89 #endif
90 #include <stdio.h>
91 #include <stddef.h>
92
93 #ifdef HAVE_SYS_PARAM_H
94 #include <sys/param.h>
95 #endif
96
97 #ifdef HAVE_STDLIB_H
98 #include <stdlib.h>
99 #endif
100
101 #ifdef HAVE_SYS_SOCKET_H
102 #include <sys/socket.h>
103 #endif
104
105 #ifdef HAVE_STRING_H
106 #include <string.h>
107 #endif
108
109 #ifdef HAVE_MALLOC_H
110 #include <malloc.h>
111 #endif
112
113 #ifdef TIME_WITH_SYS_TIME
114 #include <sys/time.h>
115 #include <time.h>
116 #else
117 #ifdef HAVE_SYS_TIME_H
118 #include <sys/time.h>
119 #else
120 #include <time.h>
121 #endif
122 #endif
123
124 #ifdef HAVE_FCNTL_H
125 #include <fcntl.h>
126 #else
127 #ifdef HAVE_SYS_FCNTL_H
128 #include <sys/fcntl.h>
129 #endif
130 #endif
131
132 #include <sys/stat.h>
133
134 #ifdef HAVE_SYS_IOCTL_H
135 #include <sys/ioctl.h>
136 #endif
137
138 #ifdef HAVE_SYS_FILIO_H
139 #include <sys/filio.h>
140 #endif
141
142 #include <signal.h>
143 #ifdef HAVE_SYS_WAIT_H
144 #include <sys/wait.h>
145 #endif
146 #ifdef HAVE_CTYPE_H
147 #include <ctype.h>
148 #endif
149 #ifdef HAVE_GRP_H
150 #include <grp.h>
151 #endif
152 #include <errno.h>
153
154 #ifdef HAVE_UTIME_H
155 #include <utime.h>
156 #endif
157
158 #ifdef HAVE_SYS_SELECT_H
159 #include <sys/select.h>
160 #endif
161
162 #ifdef HAVE_SYS_MODE_H
163 /* apparently AIX needs this for S_ISLNK */
164 #ifndef S_ISLNK
165 #include <sys/mode.h>
166 #endif
167 #endif
168
169 #ifdef HAVE_FNMATCH
170 #include <fnmatch.h>
171 #else
172 #include "lib/fnmatch.h"
173 #endif
174
175 #ifdef HAVE_GLOB_H
176 #include <glob.h>
177 #endif
178
179 /* these are needed for the uid/gid mapping code */
180 #include <pwd.h>
181 #include <grp.h>
182
183 #include <stdarg.h>
184 #include <netinet/in.h>
185 #include <arpa/inet.h>
186 #include <netdb.h>
187 #include <syslog.h>
188 #include <sys/file.h>
189
190 #if HAVE_DIRENT_H
191 # include <dirent.h>
192 #else
193 # define dirent direct
194 # if HAVE_SYS_NDIR_H
195 #  include <sys/ndir.h>
196 # endif
197 # if HAVE_SYS_DIR_H
198 #  include <sys/dir.h>
199 # endif
200 # if HAVE_NDIR_H
201 #  include <ndir.h>
202 # endif
203 #endif
204
205 #ifdef HAVE_COMPAT_H
206 #include <compat.h>
207 #endif
208
209
210 #define BOOL int
211
212 #ifndef uchar
213 #define uchar unsigned char
214 #endif
215
216 #if HAVE_UNSIGNED_CHAR
217 #define schar signed char
218 #else
219 #define schar char
220 #endif
221
222 #ifndef int32
223 #if (SIZEOF_INT == 4)
224 #define int32 int
225 #elif (SIZEOF_LONG == 4)
226 #define int32 long
227 #elif (SIZEOF_SHORT == 4)
228 #define int32 short
229 #else
230 /* I hope this works */
231 #define int32 int
232 #define LARGE_INT32
233 #endif
234 #endif
235
236 #ifndef uint32
237 #define uint32 unsigned int32
238 #endif
239
240 #if HAVE_OFF64_T
241 #define OFF_T off64_t
242 #define STRUCT_STAT struct stat64
243 #else
244 #define OFF_T off_t
245 #define STRUCT_STAT struct stat
246 #endif
247
248 #if HAVE_OFF64_T
249 #define int64 off64_t
250 #elif (SIZEOF_LONG == 8) 
251 #define int64 long
252 #elif (SIZEOF_INT == 8) 
253 #define int64 int
254 #elif HAVE_LONGLONG
255 #define int64 long long
256 #else
257 #define int64 off_t
258 #define NO_INT64
259 #endif
260
261 #if HAVE_SHORT_INO_T
262 #define INO_T uint32
263 #else
264 #define INO_T ino_t
265 #endif
266
267 #ifndef MIN
268 #define MIN(a,b) ((a)<(b)?(a):(b))
269 #endif
270
271 #ifndef MAX
272 #define MAX(a,b) ((a)>(b)?(a):(b))
273 #endif
274
275 #ifndef MAXHOSTNAMELEN
276 #define MAXHOSTNAMELEN 256
277 #endif
278
279 /* the length of the md4 checksum */
280 #define MD4_SUM_LENGTH 16
281 #define SUM_LENGTH 16
282
283 #ifndef MAXPATHLEN
284 #define MAXPATHLEN 1024
285 #endif
286
287 #ifndef PIPE_BUF
288 #define PIPE_BUF 512
289 #endif
290
291 #ifndef INADDR_NONE
292 #define INADDR_NONE 0xffffffff
293 #endif
294
295 struct file_struct {
296         unsigned flags;
297         time_t modtime;
298         OFF_T length;
299         mode_t mode;
300         INO_T inode;
301         dev_t dev;
302         dev_t rdev;
303         uid_t uid;
304         gid_t gid;
305         char *basename;
306         char *dirname;
307         char *basedir;
308         char *link;
309         char *sum;
310 };
311
312
313 struct file_list {
314         int count;
315         int malloced;
316         struct file_struct **files;
317 };
318
319 struct sum_buf {
320         OFF_T offset;           /* offset in file of this chunk */
321         int len;                /* length of chunk of file */
322         int i;                  /* index of this chunk */
323         uint32 sum1;            /* simple checksum */
324         char sum2[SUM_LENGTH];  /* checksum  */
325 };
326
327 struct sum_struct {
328   OFF_T flength;                /* total file length */
329   int count;                    /* how many chunks */
330   int remainder;                /* flength % block_length */
331   int n;                        /* block_length */
332   struct sum_buf *sums;         /* points to info for each chunk */
333 };
334
335 struct map_struct {
336         char *p;
337         int fd,p_size,p_len;
338         OFF_T file_size, p_offset, p_fd_offset;
339 };
340
341 struct exclude_struct {
342         char *orig;
343         char *pattern;
344         int regular_exp;
345         int fnmatch_flags;
346         int include;
347         int directory;
348         int local;
349 };
350
351 struct stats {
352         int64 total_size;
353         int64 total_transferred_size;
354         int64 total_written;
355         int64 total_read;
356         int64 literal_data;
357         int64 matched_data;
358         int flist_size;
359         int num_files;
360         int num_transferred_files;
361 };
362
363
364 /* we need this function because of the silly way in which duplicate
365    entries are handled in the file lists - we can't change this
366    without breaking existing versions */
367 static inline int flist_up(struct file_list *flist, int i)
368 {
369         while (!flist->files[i]->basename) i++;
370         return i;
371 }
372
373 #include "byteorder.h"
374 #include "version.h"
375 #include "proto.h"
376 #include "lib/mdfour.h"
377
378 #if !HAVE_STRERROR
379 extern char *sys_errlist[];
380 #define strerror(i) sys_errlist[i]
381 #endif
382
383 #ifndef HAVE_STRCHR
384 # define strchr                 index
385 # define strrchr                rindex
386 #endif
387
388 #ifndef HAVE_ERRNO_DECL
389 extern int errno;
390 #endif
391
392 #define SUPPORT_LINKS HAVE_READLINK
393 #define SUPPORT_HARD_LINKS HAVE_LINK
394
395 #ifndef HAVE_LCHOWN
396 #define lchown chown
397 #endif
398
399 #define SIGNAL_CAST (RETSIGTYPE (*)())
400
401 #ifndef EWOULDBLOCK
402 #define EWOULDBLOCK EAGAIN
403 #endif
404
405 #ifndef STDIN_FILENO
406 #define STDIN_FILENO 0
407 #endif
408
409 #ifndef STDOUT_FILENO
410 #define STDOUT_FILENO 1
411 #endif
412
413 #ifndef STDERR_FILENO
414 #define STDERR_FILENO 2
415 #endif
416
417 #ifndef S_IWUSR
418 #define S_IWUSR 0200
419 #endif
420
421 #ifndef _S_IFMT
422 #define _S_IFMT        0170000
423 #endif
424
425 #ifndef _S_IFLNK
426 #define _S_IFLNK  0120000
427 #endif
428
429 #ifndef S_ISLNK
430 #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
431 #endif
432
433 #ifndef S_ISBLK
434 #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
435 #endif
436
437 #ifndef S_ISCHR
438 #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
439 #endif
440
441 #ifndef S_ISSOCK
442 #ifdef _S_IFSOCK
443 #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
444 #else
445 #define S_ISSOCK(mode) (0)
446 #endif
447 #endif
448
449 #ifndef S_ISFIFO
450 #ifdef _S_IFIFO
451 #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
452 #else
453 #define S_ISFIFO(mode) (0)
454 #endif
455 #endif
456
457 #ifndef S_ISDIR
458 #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
459 #endif
460
461 #ifndef S_ISREG
462 #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
463 #endif
464
465
466 #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode))
467
468 #ifndef ACCESSPERMS
469 #define ACCESSPERMS 0777
470 #endif
471 /* Initial mask on permissions given to temporary files.  Mask off setuid
472      bits and group access because of potential race-condition security
473      holes, and mask other access because mode 707 is bizarre */
474 #define INITACCESSPERMS 0700
475
476 /* handler for null strings in printf format */
477 #define NS(s) ((s)?(s):"<NULL>")
478
479 /* use magic gcc attributes to catch format errors */
480  void rprintf(int , const char *, ...)
481 #ifdef __GNUC__
482      __attribute__ ((format (printf, 2, 3)))
483 #endif
484 ;
485
486 #ifdef REPLACE_INET_NTOA
487 #define inet_ntoa rep_inet_ntoa
488 #endif
489
490
491 #ifndef HAVE_STRLCPY
492 size_t strlcpy(char *d, const char *s, size_t bufsize);
493 #endif
494
495 #ifndef HAVE_STRLCAT
496 size_t strlcat(char *d, const char *s, size_t bufsize);
497 #endif
498
499 #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)