6bde7a467fc0ee95dc15086435527ed4dae5a6ef
[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 BLOCK_SIZE 700
21 #define RSYNC_RSH_ENV "RSYNC_RSH"
22
23 #define RSYNC_NAME "rsync"
24 #define RSYNCD_CONF "/etc/rsyncd.conf"
25
26 #define URL_PREFIX "rsync://"
27
28 #define BACKUP_SUFFIX "~"
29
30 /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
31    imcompatible with older versions :-( */
32 #define CHAR_OFFSET 0
33
34
35 #define FLAG_DELETE (1<<0)
36 #define SAME_MODE (1<<1)
37 #define SAME_RDEV (1<<2)
38 #define SAME_UID (1<<3)
39 #define SAME_GID (1<<4)
40 #define SAME_DIR (1<<5)
41 #define SAME_NAME SAME_DIR
42 #define LONG_NAME (1<<6)
43 #define SAME_TIME (1<<7)
44
45 /* update this if you make incompatible changes */
46 #define PROTOCOL_VERSION 19
47 #define MIN_PROTOCOL_VERSION 11
48 #define MAX_PROTOCOL_VERSION 30
49
50 #define RSYNC_PORT 873
51
52 #define SPARSE_WRITE_SIZE (1024)
53 #define WRITE_SIZE (32*1024)
54 #define CHUNK_SIZE (32*1024)
55 #define MAX_MAP_SIZE (1*1024*1024)
56 #define IO_BUFFER_SIZE (4092)
57 #define MAX_READ_BUFFER (1024*1024)
58
59 #define MAX_ARGS 1000
60
61 #define MPLEX_BASE 7
62 #define FERROR 1
63 #define FINFO 2
64 #define FLOG 3
65
66 #include "config.h"
67
68 #if HAVE_REMSH
69 #define RSYNC_RSH "remsh"
70 #else
71 #define RSYNC_RSH "rsh"
72 #endif
73
74 #include <sys/types.h>
75
76 #ifdef HAVE_GETOPT_LONG
77 #include <getopt.h>
78 #else
79 #include "lib/getopt.h"
80 #endif
81
82 #ifdef HAVE_UNISTD_H
83 #include <unistd.h>
84 #endif
85 #include <stdio.h>
86 #include <stddef.h>
87
88 #ifdef HAVE_SYS_PARAM_H
89 #include <sys/param.h>
90 #endif
91
92 #ifdef HAVE_STDLIB_H
93 #include <stdlib.h>
94 #endif
95
96 #ifdef HAVE_SYS_SOCKET_H
97 #include <sys/socket.h>
98 #endif
99
100 #ifdef HAVE_STRING_H
101 #include <string.h>
102 #endif
103
104 #ifdef HAVE_MALLOC_H
105 #include <malloc.h>
106 #endif
107
108 #ifdef TIME_WITH_SYS_TIME
109 #include <sys/time.h>
110 #include <time.h>
111 #else
112 #ifdef HAVE_SYS_TIME_H
113 #include <sys/time.h>
114 #else
115 #include <time.h>
116 #endif
117 #endif
118
119 #ifdef HAVE_FCNTL_H
120 #include <fcntl.h>
121 #else
122 #ifdef HAVE_SYS_FCNTL_H
123 #include <sys/fcntl.h>
124 #endif
125 #endif
126
127 #include <sys/stat.h>
128
129 #ifdef HAVE_SYS_IOCTL_H
130 #include <sys/ioctl.h>
131 #endif
132
133 #ifdef HAVE_SYS_FILIO_H
134 #include <sys/filio.h>
135 #endif
136
137 #include <signal.h>
138 #ifdef HAVE_SYS_WAIT_H
139 #include <sys/wait.h>
140 #endif
141 #ifdef HAVE_CTYPE_H
142 #include <ctype.h>
143 #endif
144 #ifdef HAVE_GRP_H
145 #include <grp.h>
146 #endif
147 #include <errno.h>
148
149 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
150 #include <sys/mman.h>
151 #define USE_MMAP 1
152 #endif
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 /* the length of the md4 checksum */
276 #define MD4_SUM_LENGTH 16
277 #define SUM_LENGTH 16
278
279 #ifndef MAXPATHLEN
280 #define MAXPATHLEN 1024
281 #endif
282
283 #ifndef INADDR_NONE
284 #define INADDR_NONE 0xffffffff
285 #endif
286
287 struct file_struct {
288         unsigned flags;
289         time_t modtime;
290         OFF_T length;
291         mode_t mode;
292         INO_T inode;
293         dev_t dev;
294         dev_t rdev;
295         uid_t uid;
296         gid_t gid;
297         char *basename;
298         char *dirname;
299         char *basedir;
300         char *link;
301         char *sum;
302 };
303
304 struct file_list {
305         int count;
306         int malloced;
307         struct file_struct **files;
308 };
309
310 struct sum_buf {
311         OFF_T offset;           /* offset in file of this chunk */
312         int len;                /* length of chunk of file */
313         int i;                  /* index of this chunk */
314         uint32 sum1;            /* simple checksum */
315         char sum2[SUM_LENGTH];  /* checksum  */
316 };
317
318 struct sum_struct {
319   OFF_T flength;                /* total file length */
320   int count;                    /* how many chunks */
321   int remainder;                /* flength % block_length */
322   int n;                        /* block_length */
323   struct sum_buf *sums;         /* points to info for each chunk */
324 };
325
326 struct map_struct {
327         char *map,*p;
328         int fd,p_size,p_len;
329         OFF_T size, p_offset;
330 };
331
332 struct exclude_struct {
333         char *orig;
334         char *pattern;
335         int regular_exp;
336         int include;
337         int directory;
338         int local;
339 };
340
341 struct stats {
342         int64 total_size;
343         int64 total_transferred_size;
344         int64 total_written;
345         int64 total_read;
346         int64 literal_data;
347         int64 matched_data;
348         int flist_size;
349         int num_files;
350         int num_transferred_files;
351 };
352
353
354 /* we need this function because of the silly way in which duplicate
355    entries are handled in the file lists - we can't change this
356    without breaking existing versions */
357 static inline int flist_up(struct file_list *flist, int i)
358 {
359         while (!flist->files[i]->basename) i++;
360         return i;
361 }
362
363 #include "byteorder.h"
364 #include "version.h"
365 #include "proto.h"
366 #include "lib/mdfour.h"
367
368 #if !HAVE_STRERROR
369 extern char *sys_errlist[];
370 #define strerror(i) sys_errlist[i]
371 #endif
372
373 #ifndef HAVE_STRCHR
374 # define strchr                 index
375 # define strrchr                rindex
376 #endif
377
378 #ifndef HAVE_ERRNO_DECL
379 extern int errno;
380 #endif
381
382 #define SUPPORT_LINKS HAVE_READLINK
383 #define SUPPORT_HARD_LINKS HAVE_LINK
384
385 #ifndef HAVE_LCHOWN
386 #define lchown chown
387 #endif
388
389 #define SIGNAL_CAST (RETSIGTYPE (*)())
390
391 #ifndef EWOULDBLOCK
392 #define EWOULDBLOCK EAGAIN
393 #endif
394
395 #ifndef STDIN_FILENO
396 #define STDIN_FILENO 0
397 #endif
398
399 #ifndef STDOUT_FILENO
400 #define STDOUT_FILENO 1
401 #endif
402
403 #ifndef STDERR_FILENO
404 #define STDERR_FILENO 2
405 #endif
406
407 #ifndef S_IWUSR
408 #define S_IWUSR 0200
409 #endif
410
411 #ifndef _S_IFMT
412 #define _S_IFMT        0170000
413 #endif
414
415 #ifndef _S_IFLNK
416 #define _S_IFLNK  0120000
417 #endif
418
419 #ifndef S_ISLNK
420 #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
421 #endif
422
423 #ifndef S_ISBLK
424 #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
425 #endif
426
427 #ifndef S_ISCHR
428 #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
429 #endif
430
431 #ifndef S_ISSOCK
432 #ifdef _S_IFSOCK
433 #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
434 #else
435 #define S_ISSOCK(mode) (0)
436 #endif
437 #endif
438
439 #ifndef S_ISFIFO
440 #ifdef _S_IFIFO
441 #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
442 #else
443 #define S_ISFIFO(mode) (0)
444 #endif
445 #endif
446
447 #ifndef S_ISDIR
448 #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
449 #endif
450
451 #ifndef S_ISREG
452 #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
453 #endif
454
455
456 #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode))
457
458 #ifndef ACCESSPERMS
459 #define ACCESSPERMS 0777
460 #endif
461
462 /* handler for null strings in printf format */
463 #define NS(s) ((s)?(s):"<NULL>")
464
465 /* use magic gcc attributes to catch format errors */
466  void rprintf(int , const char *, ...)
467 #ifdef __GNUC__
468      __attribute__ ((format (printf, 2, 3)))
469 #endif
470 ;
471
472 #ifdef REPLACE_INET_NTOA
473 #define inet_ntoa rep_inet_ntoa
474 #endif