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