When comparing -1 to a group id, cast -1 with gid_t because on some systems
[rsync/rsync.git] / rsync.h
... / ...
CommitLineData
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 imcompatible 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 19
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 INADDR_NONE
289#define INADDR_NONE 0xffffffff
290#endif
291
292struct file_struct {
293 unsigned flags;
294 time_t modtime;
295 OFF_T length;
296 mode_t mode;
297 INO_T inode;
298 dev_t dev;
299 dev_t rdev;
300 uid_t uid;
301 gid_t gid;
302 char *basename;
303 char *dirname;
304 char *basedir;
305 char *link;
306 char *sum;
307};
308
309
310struct file_list {
311 int count;
312 int malloced;
313 struct file_struct **files;
314};
315
316struct sum_buf {
317 OFF_T offset; /* offset in file of this chunk */
318 int len; /* length of chunk of file */
319 int i; /* index of this chunk */
320 uint32 sum1; /* simple checksum */
321 char sum2[SUM_LENGTH]; /* checksum */
322};
323
324struct sum_struct {
325 OFF_T flength; /* total file length */
326 int count; /* how many chunks */
327 int remainder; /* flength % block_length */
328 int n; /* block_length */
329 struct sum_buf *sums; /* points to info for each chunk */
330};
331
332struct map_struct {
333 char *p;
334 int fd,p_size,p_len;
335 OFF_T file_size, p_offset, p_fd_offset;
336};
337
338struct exclude_struct {
339 char *orig;
340 char *pattern;
341 int regular_exp;
342 int fnmatch_flags;
343 int include;
344 int directory;
345 int local;
346};
347
348struct stats {
349 int64 total_size;
350 int64 total_transferred_size;
351 int64 total_written;
352 int64 total_read;
353 int64 literal_data;
354 int64 matched_data;
355 int flist_size;
356 int num_files;
357 int num_transferred_files;
358};
359
360
361/* we need this function because of the silly way in which duplicate
362 entries are handled in the file lists - we can't change this
363 without breaking existing versions */
364static inline int flist_up(struct file_list *flist, int i)
365{
366 while (!flist->files[i]->basename) i++;
367 return i;
368}
369
370#include "byteorder.h"
371#include "version.h"
372#include "proto.h"
373#include "lib/mdfour.h"
374
375#if !HAVE_STRERROR
376extern char *sys_errlist[];
377#define strerror(i) sys_errlist[i]
378#endif
379
380#ifndef HAVE_STRCHR
381# define strchr index
382# define strrchr rindex
383#endif
384
385#ifndef HAVE_ERRNO_DECL
386extern int errno;
387#endif
388
389#define SUPPORT_LINKS HAVE_READLINK
390#define SUPPORT_HARD_LINKS HAVE_LINK
391
392#ifndef HAVE_LCHOWN
393#define lchown chown
394#endif
395
396#define SIGNAL_CAST (RETSIGTYPE (*)())
397
398#ifndef EWOULDBLOCK
399#define EWOULDBLOCK EAGAIN
400#endif
401
402#ifndef STDIN_FILENO
403#define STDIN_FILENO 0
404#endif
405
406#ifndef STDOUT_FILENO
407#define STDOUT_FILENO 1
408#endif
409
410#ifndef STDERR_FILENO
411#define STDERR_FILENO 2
412#endif
413
414#ifndef S_IWUSR
415#define S_IWUSR 0200
416#endif
417
418#ifndef _S_IFMT
419#define _S_IFMT 0170000
420#endif
421
422#ifndef _S_IFLNK
423#define _S_IFLNK 0120000
424#endif
425
426#ifndef S_ISLNK
427#define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
428#endif
429
430#ifndef S_ISBLK
431#define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
432#endif
433
434#ifndef S_ISCHR
435#define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
436#endif
437
438#ifndef S_ISSOCK
439#ifdef _S_IFSOCK
440#define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
441#else
442#define S_ISSOCK(mode) (0)
443#endif
444#endif
445
446#ifndef S_ISFIFO
447#ifdef _S_IFIFO
448#define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
449#else
450#define S_ISFIFO(mode) (0)
451#endif
452#endif
453
454#ifndef S_ISDIR
455#define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
456#endif
457
458#ifndef S_ISREG
459#define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
460#endif
461
462
463#define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode))
464
465#ifndef ACCESSPERMS
466#define ACCESSPERMS 0777
467#endif
468
469/* handler for null strings in printf format */
470#define NS(s) ((s)?(s):"<NULL>")
471
472/* use magic gcc attributes to catch format errors */
473 void rprintf(int , const char *, ...)
474#ifdef __GNUC__
475 __attribute__ ((format (printf, 2, 3)))
476#endif
477;
478
479#ifdef REPLACE_INET_NTOA
480#define inet_ntoa rep_inet_ntoa
481#endif
482
483
484#ifndef HAVE_STRLCPY
485size_t strlcpy(char *d, const char *s, size_t bufsize);
486#endif
487
488#ifndef HAVE_STRLCAT
489size_t strlcat(char *d, const char *s, size_t bufsize);
490#endif
491
492#define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)