cleanup code a bit
[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 #define RSYNCD_LOG "/var/adm/rsyncd.log"
26
27 #define BACKUP_SUFFIX "~"
28
29 /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
30    imcompatible with older versions :-( */
31 #define CHAR_OFFSET 0
32
33
34 #define FLAG_DELETE (1<<0)
35 #define SAME_MODE (1<<1)
36 #define SAME_RDEV (1<<2)
37 #define SAME_UID (1<<3)
38 #define SAME_GID (1<<4)
39 #define SAME_DIR (1<<5)
40 #define SAME_NAME SAME_DIR
41 #define LONG_NAME (1<<6)
42 #define SAME_TIME (1<<7)
43
44 /* update this if you make incompatible changes */
45 #define PROTOCOL_VERSION 17
46 #define MIN_PROTOCOL_VERSION 11
47 #define MAX_PROTOCOL_VERSION 30
48
49 #define RSYNC_PORT 873
50
51 #define SPARSE_WRITE_SIZE (1024)
52 #define WRITE_SIZE (32*1024)
53 #define CHUNK_SIZE (32*1024)
54 #define MAX_MAP_SIZE (4*1024*1024)
55
56 #define MAX_ARGS 100
57
58 #define BLOCKING_TIMEOUT 10
59
60 #define FERROR 1
61 #define FINFO 2
62
63 #include "config.h"
64
65 #if HAVE_REMSH
66 #define RSYNC_RSH "remsh"
67 #else
68 #define RSYNC_RSH "rsh"
69 #endif
70
71 #include <sys/types.h>
72 #ifdef HAVE_UNISTD_H
73 #include <unistd.h>
74 #endif
75 #include <stdio.h>
76 #include <stddef.h>
77
78 #ifdef HAVE_SYS_PARAM_H
79 #include <sys/param.h>
80 #endif
81
82 #ifdef HAVE_STDLIB_H
83 #include <stdlib.h>
84 #endif
85
86 #ifdef HAVE_SYS_SOCKET_H
87 #include <sys/socket.h>
88 #endif
89
90 #ifdef HAVE_STRING_H
91 #include <string.h>
92 #endif
93
94 #ifdef HAVE_COMPAT_H
95 #include <compat.h>
96 #endif
97
98 #ifdef HAVE_MALLOC_H
99 #include <malloc.h>
100 #endif
101
102 #ifdef TIME_WITH_SYS_TIME
103 #include <sys/time.h>
104 #include <time.h>
105 #else
106 #ifdef HAVE_SYS_TIME_H
107 #include <sys/time.h>
108 #else
109 #include <time.h>
110 #endif
111 #endif
112
113 #ifdef HAVE_FCNTL_H
114 #include <fcntl.h>
115 #else
116 #ifdef HAVE_SYS_FCNTL_H
117 #include <sys/fcntl.h>
118 #endif
119 #endif
120
121 #include <sys/stat.h>
122
123 #ifdef HAVE_SYS_IOCTL_H
124 #include <sys/ioctl.h>
125 #endif
126
127 #ifdef HAVE_SYS_FILIO_H
128 #include <sys/filio.h>
129 #endif
130
131 #include <signal.h>
132 #ifdef HAVE_SYS_WAIT_H
133 #include <sys/wait.h>
134 #endif
135 #ifdef HAVE_CTYPE_H
136 #include <ctype.h>
137 #endif
138 #ifdef HAVE_GRP_H
139 #include <grp.h>
140 #endif
141 #include <errno.h>
142
143 #ifdef HAVE_MMAP
144 #include <sys/mman.h>
145 #endif
146
147 #ifdef HAVE_UTIME_H
148 #include <utime.h>
149 #endif
150
151 #ifdef HAVE_SYS_SELECT_H
152 #include <sys/select.h>
153 #endif
154
155 #ifdef HAVE_SYS_MODE_H
156 /* apparently AIX needs this for S_ISLNK */
157 #ifndef S_ISLNK
158 #include <sys/mode.h>
159 #endif
160 #endif
161
162 #ifdef HAVE_FNMATCH
163 #include <fnmatch.h>
164 #else
165 #include "lib/fnmatch.h"
166 #endif
167
168 #ifdef HAVE_GETOPT_LONG
169 #include <getopt.h>
170 #else
171 #include "lib/getopt.h"
172 #endif
173
174 /* these are needed for the uid/gid mapping code */
175 #include <pwd.h>
176 #include <grp.h>
177
178 #include <stdarg.h>
179 #include <netinet/in.h>
180 #include <arpa/inet.h>
181 #include <netdb.h>
182
183 #ifndef S_IFLNK
184 #define S_IFLNK  0120000
185 #endif
186
187 #ifndef S_ISLNK
188 #define S_ISLNK(mode) (((mode) & S_IFLNK) == S_IFLNK)
189 #endif
190
191 #define BOOL int
192
193 #ifndef uchar
194 #define uchar unsigned char
195 #endif
196
197 #if HAVE_UNSIGNED_CHAR
198 #define schar signed char
199 #else
200 #define schar char
201 #endif
202
203 #ifndef int32
204 #if (SIZEOF_INT == 4)
205 #define int32 int
206 #elif (SIZEOF_LONG == 4)
207 #define int32 long
208 #elif (SIZEOF_SHORT == 4)
209 #define int32 short
210 #endif
211 #endif
212
213 #ifndef uint32
214 #define uint32 unsigned int32
215 #endif
216
217 #if HAVE_OFF64_T
218 #define OFF_T off64_t
219 #define STRUCT_STAT struct stat64
220 #else
221 #define OFF_T off_t
222 #define STRUCT_STAT struct stat
223 #endif
224
225 #if HAVE_OFF64_T
226 #define int64 off64_t
227 #elif (SIZEOF_LONG == 8) 
228 #define int64 long
229 #elif (SIZEOF_INT == 8) 
230 #define int64 int
231 #elif HAVE_LONGLONG
232 #define int64 long long
233 #else
234 #define int64 off_t
235 #define NO_INT64
236 #endif
237
238 #ifndef MIN
239 #define MIN(a,b) ((a)<(b)?(a):(b))
240 #endif
241
242 #ifndef MAX
243 #define MAX(a,b) ((a)>(b)?(a):(b))
244 #endif
245
246 /* the length of the md4 checksum */
247 #define MD4_SUM_LENGTH 16
248 #define SUM_LENGTH 16
249
250 #ifndef MAXPATHLEN
251 #define MAXPATHLEN 1024
252 #endif
253
254 struct file_struct {
255         unsigned flags;
256         time_t modtime;
257         OFF_T length;
258         mode_t mode;
259         ino_t inode;
260         dev_t dev;
261         dev_t rdev;
262         uid_t uid;
263         gid_t gid;
264         char *basename;
265         char *dirname;
266         char *basedir;
267         char *link;
268         char *sum;
269 };
270
271 struct file_list {
272   int count;
273   int malloced;
274   struct file_struct **files;
275 };
276
277 struct sum_buf {
278   OFF_T offset;                 /* offset in file of this chunk */
279   int len;                      /* length of chunk of file */
280   int i;                        /* index of this chunk */
281   uint32 sum1;                  /* simple checksum */
282   char sum2[SUM_LENGTH];        /* checksum  */
283 };
284
285 struct sum_struct {
286   OFF_T flength;                /* total file length */
287   int count;                    /* how many chunks */
288   int remainder;                /* flength % block_length */
289   int n;                        /* block_length */
290   struct sum_buf *sums;         /* points to info for each chunk */
291 };
292
293 struct map_struct {
294         char *map,*p;
295         int fd,p_size,p_len;
296         OFF_T size, p_offset;
297 };
298
299 /* we need this function because of the silly way in which duplicate
300    entries are handled in the file lists - we can't change this
301    without breaking existing versions */
302 static inline int flist_up(struct file_list *flist, int i)
303 {
304         while (!flist->files[i]->basename) i++;
305         return i;
306 }
307
308 #if HAVE_DIRENT_H
309 # include <dirent.h>
310 #else
311 # define dirent direct
312 # if HAVE_SYS_NDIR_H
313 #  include <sys/ndir.h>
314 # endif
315 # if HAVE_SYS_DIR_H
316 #  include <sys/dir.h>
317 # endif
318 # if HAVE_NDIR_H
319 #  include <ndir.h>
320 # endif
321 #endif
322
323 #include "byteorder.h"
324 #include "version.h"
325 #include "proto.h"
326 #include "md4.h"
327
328 #if !HAVE_STRERROR
329 extern char *sys_errlist[];
330 #define strerror(i) sys_errlist[i]
331 #endif
332
333 #ifndef HAVE_STRCHR
334 # define strchr                 index
335 # define strrchr                rindex
336 #endif
337
338 #ifndef HAVE_ERRNO_DECL
339 extern int errno;
340 #endif
341
342 #ifndef HAVE_BCOPY
343 #define bcopy(src,dest,n) memcpy(dest,src,n)
344 #endif
345
346 #ifndef HAVE_BZERO
347 #define bzero(buf,n) memset(buf,0,n)
348 #endif
349
350 #define SUPPORT_LINKS HAVE_READLINK
351 #define SUPPORT_HARD_LINKS HAVE_LINK
352
353 #ifndef HAVE_LCHOWN
354 #define lchown chown
355 #endif
356
357 #define SIGNAL_CAST (RETSIGTYPE (*)())
358
359 #ifndef EWOULDBLOCK
360 #define EWOULDBLOCK EAGAIN
361 #endif
362
363 #ifndef STDIN_FILENO
364 #define STDIN_FILENO 0
365 #endif
366
367 #ifndef STDOUT_FILENO
368 #define STDOUT_FILENO 1
369 #endif
370
371 #ifndef STDERR_FILENO
372 #define STDERR_FILENO 2
373 #endif
374
375 #ifndef S_IWUSR
376 #define S_IWUSR 0200
377 #endif
378
379 #ifndef S_ISBLK
380 #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
381 #endif
382
383 #ifndef S_ISCHR
384 #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
385 #endif
386
387 #ifndef S_ISSOCK
388 #ifdef _S_IFSOCK
389 #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
390 #else
391 #define S_ISSOCK(mode) (0)
392 #endif
393 #endif
394
395 #ifndef S_ISFIFO
396 #ifdef _S_IFIFO
397 #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
398 #else
399 #define S_ISFIFO(mode) (0)
400 #endif
401 #endif
402
403 #ifndef S_ISDIR
404 #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
405 #endif
406
407 #ifndef S_ISREG
408 #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
409 #endif
410
411
412 #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode))
413