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