use native strlcat() and strlcpy() if available
[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
20#define BLOCK_SIZE 700
21#define RSYNC_RSH_ENV "RSYNC_RSH"
7b8356d0 22
c627d613 23#define RSYNC_NAME "rsync"
f0fca04e 24#define RSYNCD_CONF "/etc/rsyncd.conf"
f0fca04e 25
f7632fc6
AT
26#define URL_PREFIX "rsync://"
27
c627d613
AT
28#define BACKUP_SUFFIX "~"
29
1b01b295
AT
30/* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
31 imcompatible with older versions :-( */
32#define CHAR_OFFSET 0
33
34
3333ffbd 35#define FLAG_DELETE (1<<0)
4fe159a8 36#define SAME_MODE (1<<1)
dc5ddbcc 37#define SAME_RDEV (1<<2)
4fe159a8
AT
38#define SAME_UID (1<<3)
39#define SAME_GID (1<<4)
40#define SAME_DIR (1<<5)
41#define SAME_NAME SAME_DIR
42#define LONG_NAME (1<<6)
43#define SAME_TIME (1<<7)
44
c627d613 45/* update this if you make incompatible changes */
7b1ce0d7 46#define PROTOCOL_VERSION 19
3ec4dd97 47#define MIN_PROTOCOL_VERSION 11
6ba9279f 48#define MAX_PROTOCOL_VERSION 30
c627d613 49
9486289c
AT
50#define RSYNC_PORT 873
51
d867229b 52#define SPARSE_WRITE_SIZE (1024)
dc5ddbcc 53#define WRITE_SIZE (32*1024)
34ccb63e 54#define CHUNK_SIZE (32*1024)
2f326946 55#define MAX_MAP_SIZE (1*1024*1024)
e3fe383a 56#define IO_BUFFER_SIZE (4092)
5dd7e031 57#define MAX_READ_BUFFER (1024*1024)
c627d613 58
874895d5 59#define MAX_ARGS 1000
f0fca04e 60
8d9dc9f9 61#define MPLEX_BASE 7
9486289c
AT
62#define FERROR 1
63#define FINFO 2
11a5a3c7 64#define FLOG 3
7bec6a5c 65
65417579
AT
66#include "errcode.h"
67
c627d613
AT
68#include "config.h"
69
24d95c03
AT
70#if HAVE_REMSH
71#define RSYNC_RSH "remsh"
72#else
73#define RSYNC_RSH "rsh"
74#endif
75
c627d613 76#include <sys/types.h>
6ed67e6d
AT
77
78#ifdef HAVE_GETOPT_LONG
79#include <getopt.h>
80#else
81#include "lib/getopt.h"
82#endif
83
c627d613
AT
84#ifdef HAVE_UNISTD_H
85#include <unistd.h>
86#endif
87#include <stdio.h>
3591c066 88#include <stddef.h>
c627d613
AT
89
90#ifdef HAVE_SYS_PARAM_H
91#include <sys/param.h>
92#endif
93
d7b305fd
AT
94#ifdef HAVE_STDLIB_H
95#include <stdlib.h>
96#endif
97
fdd71e17
AT
98#ifdef HAVE_SYS_SOCKET_H
99#include <sys/socket.h>
100#endif
101
d7b305fd
AT
102#ifdef HAVE_STRING_H
103#include <string.h>
c627d613
AT
104#endif
105
c627d613
AT
106#ifdef HAVE_MALLOC_H
107#include <malloc.h>
108#endif
109
110#ifdef TIME_WITH_SYS_TIME
111#include <sys/time.h>
112#include <time.h>
113#else
114#ifdef HAVE_SYS_TIME_H
115#include <sys/time.h>
116#else
117#include <time.h>
118#endif
119#endif
120
121#ifdef HAVE_FCNTL_H
122#include <fcntl.h>
123#else
124#ifdef HAVE_SYS_FCNTL_H
125#include <sys/fcntl.h>
126#endif
127#endif
128
129#include <sys/stat.h>
130
94481d91
AT
131#ifdef HAVE_SYS_IOCTL_H
132#include <sys/ioctl.h>
133#endif
134
135#ifdef HAVE_SYS_FILIO_H
136#include <sys/filio.h>
137#endif
138
c627d613
AT
139#include <signal.h>
140#ifdef HAVE_SYS_WAIT_H
141#include <sys/wait.h>
142#endif
143#ifdef HAVE_CTYPE_H
144#include <ctype.h>
145#endif
146#ifdef HAVE_GRP_H
147#include <grp.h>
148#endif
149#include <errno.h>
150
bb0f7089 151#if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
c627d613 152#include <sys/mman.h>
bb0f7089 153#define USE_MMAP 1
7bec6a5c
AT
154#endif
155
c627d613
AT
156#ifdef HAVE_UTIME_H
157#include <utime.h>
158#endif
159
8bf73749
AT
160#ifdef HAVE_SYS_SELECT_H
161#include <sys/select.h>
162#endif
163
773f2bd4
AT
164#ifdef HAVE_SYS_MODE_H
165/* apparently AIX needs this for S_ISLNK */
05a6556d 166#ifndef S_ISLNK
773f2bd4
AT
167#include <sys/mode.h>
168#endif
05a6556d 169#endif
773f2bd4 170
c627d613
AT
171#ifdef HAVE_FNMATCH
172#include <fnmatch.h>
173#else
174#include "lib/fnmatch.h"
175#endif
176
932be9aa 177#ifdef HAVE_GLOB_H
874895d5
AT
178#include <glob.h>
179#endif
180
f6c34742
AT
181/* these are needed for the uid/gid mapping code */
182#include <pwd.h>
183#include <grp.h>
184
9486289c 185#include <stdarg.h>
f0fca04e
AT
186#include <netinet/in.h>
187#include <arpa/inet.h>
188#include <netdb.h>
ff8b29b8 189#include <syslog.h>
6c8f5373 190#include <sys/file.h>
9486289c 191
e8f5b936
AT
192#if HAVE_DIRENT_H
193# include <dirent.h>
194#else
195# define dirent direct
196# if HAVE_SYS_NDIR_H
197# include <sys/ndir.h>
198# endif
199# if HAVE_SYS_DIR_H
200# include <sys/dir.h>
201# endif
202# if HAVE_NDIR_H
203# include <ndir.h>
204# endif
205#endif
206
207#ifdef HAVE_COMPAT_H
208#include <compat.h>
209#endif
210
211
f0fca04e
AT
212#define BOOL int
213
c627d613
AT
214#ifndef uchar
215#define uchar unsigned char
216#endif
217
debb4505
AT
218#if HAVE_UNSIGNED_CHAR
219#define schar signed char
220#else
221#define schar char
222#endif
223
c627d613
AT
224#ifndef int32
225#if (SIZEOF_INT == 4)
226#define int32 int
227#elif (SIZEOF_LONG == 4)
228#define int32 long
229#elif (SIZEOF_SHORT == 4)
230#define int32 short
8de330a3
AT
231#else
232/* I hope this works */
233#define int32 int
f3737e06 234#define LARGE_INT32
c627d613
AT
235#endif
236#endif
237
238#ifndef uint32
239#define uint32 unsigned int32
240#endif
241
bcacc18b
AT
242#if HAVE_OFF64_T
243#define OFF_T off64_t
244#define STRUCT_STAT struct stat64
245#else
246#define OFF_T off_t
247#define STRUCT_STAT struct stat
248#endif
249
250#if HAVE_OFF64_T
251#define int64 off64_t
252#elif (SIZEOF_LONG == 8)
3bee6733
AT
253#define int64 long
254#elif (SIZEOF_INT == 8)
255#define int64 int
256#elif HAVE_LONGLONG
71c46176
AT
257#define int64 long long
258#else
259#define int64 off_t
3bee6733 260#define NO_INT64
71c46176 261#endif
c627d613 262
c29ee43d
AT
263#if HAVE_SHORT_INO_T
264#define INO_T uint32
265#else
266#define INO_T ino_t
267#endif
268
c627d613
AT
269#ifndef MIN
270#define MIN(a,b) ((a)<(b)?(a):(b))
271#endif
272
273#ifndef MAX
274#define MAX(a,b) ((a)>(b)?(a):(b))
275#endif
276
277/* the length of the md4 checksum */
ebb0a6f6 278#define MD4_SUM_LENGTH 16
c627d613
AT
279#define SUM_LENGTH 16
280
281#ifndef MAXPATHLEN
282#define MAXPATHLEN 1024
283#endif
284
0473e2a1
AT
285#ifndef INADDR_NONE
286#define INADDR_NONE 0xffffffff
287#endif
288
c627d613 289struct file_struct {
3333ffbd 290 unsigned flags;
3ec4dd97 291 time_t modtime;
bcacc18b 292 OFF_T length;
3ec4dd97 293 mode_t mode;
c29ee43d 294 INO_T inode;
3ec4dd97
AT
295 dev_t dev;
296 dev_t rdev;
297 uid_t uid;
298 gid_t gid;
299 char *basename;
300 char *dirname;
301 char *basedir;
302 char *link;
2d0bb8eb 303 char *sum;
c627d613
AT
304};
305
306struct file_list {
a800434a
AT
307 int count;
308 int malloced;
309 struct file_struct **files;
c627d613
AT
310};
311
312struct sum_buf {
c29ee43d
AT
313 OFF_T offset; /* offset in file of this chunk */
314 int len; /* length of chunk of file */
315 int i; /* index of this chunk */
316 uint32 sum1; /* simple checksum */
317 char sum2[SUM_LENGTH]; /* checksum */
c627d613
AT
318};
319
320struct sum_struct {
bcacc18b 321 OFF_T flength; /* total file length */
c627d613
AT
322 int count; /* how many chunks */
323 int remainder; /* flength % block_length */
324 int n; /* block_length */
325 struct sum_buf *sums; /* points to info for each chunk */
326};
327
c6e7fcb4 328struct map_struct {
0b910560
AT
329 char *map,*p;
330 int fd,p_size,p_len;
bcacc18b 331 OFF_T size, p_offset;
c6e7fcb4 332};
c627d613 333
2b6b4d53
AT
334struct exclude_struct {
335 char *orig;
336 char *pattern;
337 int regular_exp;
338 int include;
339 int directory;
340 int local;
341};
342
a800434a
AT
343struct stats {
344 int64 total_size;
345 int64 total_transferred_size;
346 int64 total_written;
347 int64 total_read;
348 int64 literal_data;
349 int64 matched_data;
350 int flist_size;
351 int num_files;
352 int num_transferred_files;
353};
354
2b6b4d53 355
3b3a2fbc
AT
356/* we need this function because of the silly way in which duplicate
357 entries are handled in the file lists - we can't change this
358 without breaking existing versions */
71c46176 359static inline int flist_up(struct file_list *flist, int i)
3b3a2fbc 360{
3ec4dd97 361 while (!flist->files[i]->basename) i++;
3b3a2fbc
AT
362 return i;
363}
364
c627d613
AT
365#include "byteorder.h"
366#include "version.h"
367#include "proto.h"
8de330a3 368#include "lib/mdfour.h"
c627d613
AT
369
370#if !HAVE_STRERROR
371extern char *sys_errlist[];
372#define strerror(i) sys_errlist[i]
373#endif
374
375#ifndef HAVE_STRCHR
376# define strchr index
377# define strrchr rindex
378#endif
379
c627d613
AT
380#ifndef HAVE_ERRNO_DECL
381extern int errno;
382#endif
383
cbbe4892 384#define SUPPORT_LINKS HAVE_READLINK
dc5ddbcc 385#define SUPPORT_HARD_LINKS HAVE_LINK
c627d613 386
8bf73749
AT
387#ifndef HAVE_LCHOWN
388#define lchown chown
389#endif
390
ac1eb754 391#define SIGNAL_CAST (RETSIGTYPE (*)())
720b47f2
AT
392
393#ifndef EWOULDBLOCK
394#define EWOULDBLOCK EAGAIN
395#endif
182dca5c 396
7b8356d0
AT
397#ifndef STDIN_FILENO
398#define STDIN_FILENO 0
399#endif
400
401#ifndef STDOUT_FILENO
402#define STDOUT_FILENO 1
403#endif
404
405#ifndef STDERR_FILENO
406#define STDERR_FILENO 2
407#endif
408
409#ifndef S_IWUSR
410#define S_IWUSR 0200
411#endif
412
b280a1f4
AT
413#ifndef _S_IFMT
414#define _S_IFMT 0170000
415#endif
416
417#ifndef _S_IFLNK
418#define _S_IFLNK 0120000
419#endif
420
421#ifndef S_ISLNK
422#define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
423#endif
424
1e9f155a
AT
425#ifndef S_ISBLK
426#define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
427#endif
428
429#ifndef S_ISCHR
430#define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
431#endif
432
a0b65b18
AT
433#ifndef S_ISSOCK
434#ifdef _S_IFSOCK
435#define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
436#else
437#define S_ISSOCK(mode) (0)
438#endif
439#endif
440
441#ifndef S_ISFIFO
442#ifdef _S_IFIFO
443#define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
444#else
445#define S_ISFIFO(mode) (0)
446#endif
447#endif
448
1e9f155a
AT
449#ifndef S_ISDIR
450#define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
451#endif
452
453#ifndef S_ISREG
454#define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
455#endif
456
a0b65b18
AT
457
458#define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode))
7bec6a5c 459
22b19332
AT
460#ifndef ACCESSPERMS
461#define ACCESSPERMS 0777
462#endif
e08bfe12
AT
463
464/* handler for null strings in printf format */
465#define NS(s) ((s)?(s):"<NULL>")
466
467/* use magic gcc attributes to catch format errors */
468 void rprintf(int , const char *, ...)
469#ifdef __GNUC__
470 __attribute__ ((format (printf, 2, 3)))
471#endif
472;
7b3d4257
AT
473
474#ifdef REPLACE_INET_NTOA
475#define inet_ntoa rep_inet_ntoa
476#endif
5a788ade
AT
477
478
479#ifndef HAVE_STRLCPY
480size_t strlcpy(char *d, const char *s, size_t bufsize);
481#endif
482
483#ifndef HAVE_STRLCAT
484size_t strlcat(char *d, const char *s, size_t bufsize);
485#endif
486