report exit code when failing a test
[rsync/rsync.git] / rsync.h
CommitLineData
c627d613
AT
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
d41c7d02
DD
20#define False 0
21#define True 1
22
c627d613
AT
23#define BLOCK_SIZE 700
24#define RSYNC_RSH_ENV "RSYNC_RSH"
7b8356d0 25
c627d613 26#define RSYNC_NAME "rsync"
f0fca04e 27#define RSYNCD_CONF "/etc/rsyncd.conf"
f0fca04e 28
5e71c444 29#define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
f7632fc6
AT
30#define URL_PREFIX "rsync://"
31
c627d613
AT
32#define BACKUP_SUFFIX "~"
33
1b01b295 34/* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
6543dc0c 35 incompatible with older versions :-( */
1b01b295
AT
36#define CHAR_OFFSET 0
37
38
3333ffbd 39#define FLAG_DELETE (1<<0)
4fe159a8 40#define SAME_MODE (1<<1)
dc5ddbcc 41#define SAME_RDEV (1<<2)
4fe159a8
AT
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
c627d613 49/* update this if you make incompatible changes */
554e0a8d 50#define PROTOCOL_VERSION 22
3ec4dd97 51#define MIN_PROTOCOL_VERSION 11
6ba9279f 52#define MAX_PROTOCOL_VERSION 30
c627d613 53
9486289c
AT
54#define RSYNC_PORT 873
55
d867229b 56#define SPARSE_WRITE_SIZE (1024)
dc5ddbcc 57#define WRITE_SIZE (32*1024)
34ccb63e 58#define CHUNK_SIZE (32*1024)
4440b8aa 59#define MAX_MAP_SIZE (256*1024)
e3fe383a 60#define IO_BUFFER_SIZE (4092)
cbce490e 61#define MAX_READ_BUFFER (10*1024*1024)
c627d613 62
874895d5 63#define MAX_ARGS 1000
f0fca04e 64
8d9dc9f9 65#define MPLEX_BASE 7
9486289c
AT
66#define FERROR 1
67#define FINFO 2
11a5a3c7 68#define FLOG 3
7bec6a5c 69
65417579
AT
70#include "errcode.h"
71
c627d613
AT
72#include "config.h"
73
24d95c03
AT
74#if HAVE_REMSH
75#define RSYNC_RSH "remsh"
76#else
77#define RSYNC_RSH "rsh"
78#endif
79
c627d613 80#include <sys/types.h>
6ed67e6d
AT
81
82#ifdef HAVE_GETOPT_LONG
83#include <getopt.h>
84#else
85#include "lib/getopt.h"
86#endif
87
c627d613
AT
88#ifdef HAVE_UNISTD_H
89#include <unistd.h>
90#endif
91#include <stdio.h>
3591c066 92#include <stddef.h>
c627d613
AT
93
94#ifdef HAVE_SYS_PARAM_H
95#include <sys/param.h>
96#endif
97
d7b305fd
AT
98#ifdef HAVE_STDLIB_H
99#include <stdlib.h>
100#endif
101
fdd71e17
AT
102#ifdef HAVE_SYS_SOCKET_H
103#include <sys/socket.h>
104#endif
105
d7b305fd
AT
106#ifdef HAVE_STRING_H
107#include <string.h>
c627d613
AT
108#endif
109
c627d613
AT
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
94481d91
AT
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
c627d613
AT
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
c627d613
AT
155#ifdef HAVE_UTIME_H
156#include <utime.h>
157#endif
158
8bf73749
AT
159#ifdef HAVE_SYS_SELECT_H
160#include <sys/select.h>
161#endif
162
773f2bd4
AT
163#ifdef HAVE_SYS_MODE_H
164/* apparently AIX needs this for S_ISLNK */
05a6556d 165#ifndef S_ISLNK
773f2bd4
AT
166#include <sys/mode.h>
167#endif
05a6556d 168#endif
773f2bd4 169
c627d613
AT
170#ifdef HAVE_FNMATCH
171#include <fnmatch.h>
172#else
173#include "lib/fnmatch.h"
174#endif
175
932be9aa 176#ifdef HAVE_GLOB_H
874895d5
AT
177#include <glob.h>
178#endif
179
f6c34742
AT
180/* these are needed for the uid/gid mapping code */
181#include <pwd.h>
182#include <grp.h>
183
9486289c 184#include <stdarg.h>
f0fca04e
AT
185#include <netinet/in.h>
186#include <arpa/inet.h>
187#include <netdb.h>
ff8b29b8 188#include <syslog.h>
6c8f5373 189#include <sys/file.h>
9486289c 190
e8f5b936
AT
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
f0fca04e
AT
211#define BOOL int
212
c627d613
AT
213#ifndef uchar
214#define uchar unsigned char
215#endif
216
debb4505
AT
217#if HAVE_UNSIGNED_CHAR
218#define schar signed char
219#else
220#define schar char
221#endif
222
c627d613
AT
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
8de330a3
AT
230#else
231/* I hope this works */
232#define int32 int
f3737e06 233#define LARGE_INT32
c627d613
AT
234#endif
235#endif
236
237#ifndef uint32
238#define uint32 unsigned int32
239#endif
240
bcacc18b
AT
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)
3bee6733
AT
252#define int64 long
253#elif (SIZEOF_INT == 8)
254#define int64 int
255#elif HAVE_LONGLONG
71c46176
AT
256#define int64 long long
257#else
258#define int64 off_t
3bee6733 259#define NO_INT64
71c46176 260#endif
c627d613 261
c29ee43d
AT
262#if HAVE_SHORT_INO_T
263#define INO_T uint32
264#else
265#define INO_T ino_t
266#endif
267
c627d613
AT
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
d41c7d02
DD
276#ifndef MAXHOSTNAMELEN
277#define MAXHOSTNAMELEN 256
278#endif
279
c627d613 280/* the length of the md4 checksum */
ebb0a6f6 281#define MD4_SUM_LENGTH 16
c627d613
AT
282#define SUM_LENGTH 16
283
284#ifndef MAXPATHLEN
285#define MAXPATHLEN 1024
286#endif
287
07b7c86c
AT
288#ifndef PIPE_BUF
289#define PIPE_BUF 512
290#endif
291
0473e2a1
AT
292#ifndef INADDR_NONE
293#define INADDR_NONE 0xffffffff
294#endif
295
c627d613 296struct file_struct {
3333ffbd 297 unsigned flags;
3ec4dd97 298 time_t modtime;
bcacc18b 299 OFF_T length;
3ec4dd97 300 mode_t mode;
c29ee43d 301 INO_T inode;
3ec4dd97
AT
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;
2d0bb8eb 310 char *sum;
c627d613
AT
311};
312
4440b8aa 313
c627d613 314struct file_list {
a800434a
AT
315 int count;
316 int malloced;
317 struct file_struct **files;
c627d613
AT
318};
319
320struct sum_buf {
c29ee43d
AT
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 */
c627d613
AT
326};
327
328struct sum_struct {
bcacc18b 329 OFF_T flength; /* total file length */
c627d613
AT
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
c6e7fcb4 336struct map_struct {
4440b8aa 337 char *p;
0b910560 338 int fd,p_size,p_len;
4440b8aa 339 OFF_T file_size, p_offset, p_fd_offset;
c6e7fcb4 340};
c627d613 341
2b6b4d53
AT
342struct exclude_struct {
343 char *orig;
344 char *pattern;
345 int regular_exp;
a8b9d4ed 346 int fnmatch_flags;
2b6b4d53
AT
347 int include;
348 int directory;
349 int local;
350};
351
a800434a
AT
352struct 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
2b6b4d53 364
3b3a2fbc
AT
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 */
71c46176 368static inline int flist_up(struct file_list *flist, int i)
3b3a2fbc 369{
3ec4dd97 370 while (!flist->files[i]->basename) i++;
3b3a2fbc
AT
371 return i;
372}
373
c627d613
AT
374#include "byteorder.h"
375#include "version.h"
376#include "proto.h"
8de330a3 377#include "lib/mdfour.h"
c627d613
AT
378
379#if !HAVE_STRERROR
380extern 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
c627d613
AT
389#ifndef HAVE_ERRNO_DECL
390extern int errno;
391#endif
392
cbbe4892 393#define SUPPORT_LINKS HAVE_READLINK
dc5ddbcc 394#define SUPPORT_HARD_LINKS HAVE_LINK
c627d613 395
8bf73749
AT
396#ifndef HAVE_LCHOWN
397#define lchown chown
398#endif
399
ac1eb754 400#define SIGNAL_CAST (RETSIGTYPE (*)())
720b47f2
AT
401
402#ifndef EWOULDBLOCK
403#define EWOULDBLOCK EAGAIN
404#endif
182dca5c 405
7b8356d0
AT
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
b280a1f4
AT
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
1e9f155a
AT
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
a0b65b18
AT
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
1e9f155a
AT
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
a0b65b18
AT
466
467#define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode))
7bec6a5c 468
972a3619
DD
469#ifndef ACCESSPERMS
470#define ACCESSPERMS 0777
471#endif
5afd8aed
DD
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 */
972a3619 475#define INITACCESSPERMS 0700
e08bfe12
AT
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;
7b3d4257
AT
486
487#ifdef REPLACE_INET_NTOA
488#define inet_ntoa rep_inet_ntoa
489#endif
5a788ade
AT
490
491
492#ifndef HAVE_STRLCPY
493size_t strlcpy(char *d, const char *s, size_t bufsize);
494#endif
495
496#ifndef HAVE_STRLCAT
497size_t strlcat(char *d, const char *s, size_t bufsize);
498#endif
499
a9766ef1 500#define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)