Commit | Line | Data |
---|---|---|
d622d4bf | 1 | /* |
0f78b815 WD |
2 | * Copyright (C) 1996, 2000 Andrew Tridgell |
3 | * Copyright (C) 1996 Paul Mackerras | |
4 | * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> | |
ba2133d6 | 5 | * Copyright (C) 2003-2007 Wayne Davison |
0f78b815 WD |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
4fd842f9 | 8 | * it under the terms of the GNU General Public License version 3 as |
ba2133d6 | 9 | * published by the Free Software Foundation. |
0f78b815 WD |
10 | * |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
e7c67065 | 16 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 17 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 18 | */ |
0de40240 | 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" | |
8dd99390 | 25 | #define RSYNC_RSH_IO_ENV "RSYNC_RSH_IO" |
7b8356d0 | 26 | |
c627d613 | 27 | #define RSYNC_NAME "rsync" |
cd3fe9fb | 28 | /* RSYNCD_SYSCONF is now set in config.h */ |
30e8c8e1 | 29 | #define RSYNCD_USERCONF "rsyncd.conf" |
f0fca04e | 30 | |
5e71c444 | 31 | #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock" |
f7632fc6 AT |
32 | #define URL_PREFIX "rsync://" |
33 | ||
c627d613 AT |
34 | #define BACKUP_SUFFIX "~" |
35 | ||
1b01b295 | 36 | /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is |
6543dc0c | 37 | incompatible with older versions :-( */ |
1b01b295 AT |
38 | #define CHAR_OFFSET 0 |
39 | ||
a5f9cff2 WD |
40 | /* These flags are only used during the flist transfer. */ |
41 | ||
ec33e0e6 | 42 | #define XMIT_TOP_DIR (1<<0) |
a5f9cff2 | 43 | #define XMIT_SAME_MODE (1<<1) |
18438f0b WD |
44 | #define XMIT_SAME_RDEV_pre28 (1<<2) /* protocols 20 - 27 */ |
45 | #define XMIT_EXTENDED_FLAGS (1<<2) /* protocols 28 - NOW */ | |
a5f9cff2 WD |
46 | #define XMIT_SAME_UID (1<<3) |
47 | #define XMIT_SAME_GID (1<<4) | |
48 | #define XMIT_SAME_NAME (1<<5) | |
49 | #define XMIT_LONG_NAME (1<<6) | |
50 | #define XMIT_SAME_TIME (1<<7) | |
18438f0b WD |
51 | #define XMIT_SAME_RDEV_MAJOR (1<<8) /* protocols 28 - NOW */ |
52 | #define XMIT_HLINKED (1<<9) /* protocols 28 - NOW */ | |
53 | #define XMIT_SAME_DEV_pre30 (1<<10) /* protocols 28 - 29 */ | |
54 | #define XMIT_USER_NAME_FOLLOWS (1<<10) /* protocols 30 - NOW */ | |
55 | #define XMIT_RDEV_MINOR_8_pre30 (1<<11) /* protocols 28 - 29 */ | |
56 | #define XMIT_GROUP_NAME_FOLLOWS (1<<11) /* protocols 30 - NOW */ | |
57 | #define XMIT_HLINK_FIRST (1<<12) /* protocols 30 - NOW */ | |
a5f9cff2 WD |
58 | |
59 | /* These flags are used in the live flist data. */ | |
60 | ||
82ad07c4 | 61 | #define FLAG_TOP_DIR (1<<0) /* sender/receiver/generator */ |
9ec3828b WD |
62 | #define FLAG_FILE_SENT (1<<1) /* sender/receiver/generator */ |
63 | #define FLAG_DIR_CHANGED (1<<1) /* generator */ | |
82ad07c4 WD |
64 | #define FLAG_XFER_DIR (1<<2) /* sender/receiver/generator */ |
65 | #define FLAG_MOUNT_DIR (1<<3) /* sender/generator */ | |
505c0579 | 66 | #define FLAG_DUPLICATE (1<<4) /* sender */ |
96293cf9 WD |
67 | #define FLAG_MISSING_DIR (1<<4) /* generator */ |
68 | #define FLAG_HLINKED (1<<5) /* receiver/generator */ | |
82ad07c4 WD |
69 | #define FLAG_HLINK_FIRST (1<<6) /* receiver/generator */ |
70 | #define FLAG_HLINK_LAST (1<<7) /* receiver/generator */ | |
bfd31372 WD |
71 | #define FLAG_HLINK_DONE (1<<8) /* receiver/generator */ |
72 | #define FLAG_LENGTH64 (1<<9) /* sender/receiver/generator */ | |
ff2001b9 | 73 | #define FLAG_SKIP_GROUP (1<<10) /* receiver/generator */ |
bfd31372 | 74 | |
9ec3828b WD |
75 | /* These flags are passed to functions but not stored. */ |
76 | ||
77 | #define FLAG_DIVERT_DIRS (1<<16)/* sender */ | |
78 | ||
bfd31372 WD |
79 | #define BITS_SET(val,bits) (((val) & (bits)) == (bits)) |
80 | #define BITS_SETnUNSET(val,onbits,offbits) (((val) & ((onbits)|(offbits))) == (onbits)) | |
0a62f5f3 WD |
81 | #define BITS_EQUAL(b1,b2,mask) (((unsigned)(b1) & (unsigned)(mask)) \ |
82 | == ((unsigned)(b2) & (unsigned)(mask))) | |
520cf417 | 83 | |
c627d613 | 84 | /* update this if you make incompatible changes */ |
21068d8e | 85 | #define PROTOCOL_VERSION 30 |
063393d6 | 86 | |
4471d9e5 WD |
87 | /* This is used when working on a new protocol version in CVS, and should |
88 | * be a new non-zero value for each CVS change that affects the protocol. | |
89 | * It must ALWAYS be 0 when the protocol goes final! */ | |
24108eb8 | 90 | #define SUBPROTOCOL_VERSION 4 |
4471d9e5 | 91 | |
063393d6 MP |
92 | /* We refuse to interoperate with versions that are not in this range. |
93 | * Note that we assume we'll work with later versions: the onus is on | |
94 | * people writing them to make sure that they don't send us anything | |
95 | * we won't understand. | |
96 | * | |
1b2db7ae S |
97 | * Interoperation with old but supported protocol versions |
98 | * should cause a warning to be printed. At a future date | |
99 | * the old protocol will become the minimum and | |
100 | * compatibility code removed. | |
101 | * | |
91c4da3f S |
102 | * There are two possible explanations for the limit at |
103 | * MAX_PROTOCOL_VERSION: either to allow new major-rev versions that | |
104 | * do not interoperate with us, and (more likely) so that we can | |
105 | * detect an attempt to connect rsync to a non-rsync server, which is | |
106 | * unlikely to begin by sending a byte between MIN_PROTOCL_VERSION and | |
107 | * MAX_PROTOCOL_VERSION. */ | |
108 | ||
3e491682 S |
109 | #define MIN_PROTOCOL_VERSION 20 |
110 | #define OLD_PROTOCOL_VERSION 25 | |
1b2db7ae | 111 | #define MAX_PROTOCOL_VERSION 40 |
c627d613 | 112 | |
3831f063 WD |
113 | #define FILECNT_LOOKAHEAD 1000 |
114 | ||
9486289c AT |
115 | #define RSYNC_PORT 873 |
116 | ||
d867229b | 117 | #define SPARSE_WRITE_SIZE (1024) |
dc5ddbcc | 118 | #define WRITE_SIZE (32*1024) |
34ccb63e | 119 | #define CHUNK_SIZE (32*1024) |
61542c41 | 120 | #define MAX_MAP_SIZE (256*1024) |
e3fe383a | 121 | #define IO_BUFFER_SIZE (4092) |
2b2ea368 | 122 | #define MAX_BLOCK_SIZE ((int32)1 << 29) |
c627d613 | 123 | |
e2d22fee WD |
124 | #define IOERR_GENERAL (1<<0) /* For backward compatibility, this must == 1 */ |
125 | #define IOERR_VANISHED (1<<1) | |
ff3d3c32 | 126 | #define IOERR_DEL_LIMIT (1<<2) |
e2d22fee | 127 | |
874895d5 | 128 | #define MAX_ARGS 1000 |
e4977b0b | 129 | #define MAX_BASIS_DIRS 20 |
c296031d | 130 | #define MAX_SERVER_ARGS (MAX_BASIS_DIRS*2 + 100) |
e4977b0b | 131 | |
8d9dc9f9 | 132 | #define MPLEX_BASE 7 |
ff41a59f | 133 | |
7842418b WD |
134 | #define NO_FILTERS 0 |
135 | #define SERVER_FILTERS 1 | |
136 | #define ALL_FILTERS 2 | |
4762db4f | 137 | |
753b6b46 | 138 | #define XFLG_FATAL_ERRORS (1<<0) |
3b2461cf WD |
139 | #define XFLG_OLD_PREFIXES (1<<1) |
140 | #define XFLG_ANCHORED2ABS (1<<2) | |
699f7024 | 141 | #define XFLG_ABS_IF_SLASH (1<<3) |
4762db4f | 142 | |
8eda7a4b WD |
143 | #define ATTRS_REPORT (1<<0) |
144 | #define ATTRS_SKIP_MTIME (1<<1) | |
5c6fc4a6 | 145 | |
419896af WD |
146 | #define FULL_FLUSH 1 |
147 | #define NORMAL_FLUSH 0 | |
148 | ||
a7260c40 WD |
149 | #define PDIR_CREATE 1 |
150 | #define PDIR_DELETE 0 | |
151 | ||
e341588a WD |
152 | /* Note: 0x00 - 0x7F are used for basis_dir[] indexes! */ |
153 | #define FNAMECMP_BASIS_DIR_LOW 0x00 /* Must remain 0! */ | |
154 | #define FNAMECMP_BASIS_DIR_HIGH 0x7F | |
41cfde6b WD |
155 | #define FNAMECMP_FNAME 0x80 |
156 | #define FNAMECMP_PARTIAL_DIR 0x81 | |
157 | #define FNAMECMP_BACKUP 0x82 | |
0dd046d3 | 158 | #define FNAMECMP_FUZZY 0x83 |
41cfde6b | 159 | |
0cc279e0 | 160 | /* For use by the itemize_changes code */ |
b3222792 | 161 | #define ITEM_REPORT_ATIME (1<<0) |
0cc279e0 WD |
162 | #define ITEM_REPORT_CHECKSUM (1<<1) |
163 | #define ITEM_REPORT_SIZE (1<<2) | |
164 | #define ITEM_REPORT_TIME (1<<3) | |
165 | #define ITEM_REPORT_PERMS (1<<4) | |
166 | #define ITEM_REPORT_OWNER (1<<5) | |
167 | #define ITEM_REPORT_GROUP (1<<6) | |
b3222792 WD |
168 | #define ITEM_REPORT_ACL (1<<7) |
169 | #define ITEM_REPORT_XATTR (1<<8) | |
3019a9ba WD |
170 | #define ITEM_BASIS_TYPE_FOLLOWS (1<<11) |
171 | #define ITEM_XNAME_FOLLOWS (1<<12) | |
6d0e5d2e WD |
172 | #define ITEM_IS_NEW (1<<13) |
173 | #define ITEM_LOCAL_CHANGE (1<<14) | |
174 | #define ITEM_TRANSFER (1<<15) | |
d4d4890d | 175 | /* These are outside the range of the transmitted flags. */ |
d4d4890d WD |
176 | #define ITEM_MISSING_DATA (1<<16) /* used by log_formatted() */ |
177 | #define ITEM_DELETED (1<<17) /* used by log_formatted() */ | |
1e4f5f63 | 178 | #define ITEM_MATCHED (1<<18) /* used by itemize() */ |
0cc279e0 | 179 | |
3019a9ba WD |
180 | #define SIGNIFICANT_ITEM_FLAGS (~(\ |
181 | ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE)) | |
669e7671 | 182 | |
df0054ab | 183 | |
56aaa4c4 WD |
184 | /* Log-message categories. Only FERROR and FINFO get sent over the socket, |
185 | * but FLOG and FSOCKERR can be sent over the receiver -> generator pipe. | |
85909931 | 186 | * FLOG only goes to the log file, not the client; FCLIENT is the opposite. */ |
011e85a5 | 187 | enum logcode { FNONE=0, FERROR=1, FINFO=2, FLOG=3, FCLIENT=4, FSOCKERR=5 }; |
419896af WD |
188 | |
189 | /* Messages types that are sent over the message channel. The logcode | |
190 | * values must all be present here with identical numbers. */ | |
191 | enum msgcode { | |
1dcf3b43 WD |
192 | MSG_DATA=0, /* raw data on the multiplexed stream */ |
193 | MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */ | |
8388e011 | 194 | MSG_LOG=FLOG, MSG_CLIENT=FCLIENT, MSG_SOCKERR=FSOCKERR, /* sibling logging */ |
1dcf3b43 | 195 | MSG_REDO=9, /* reprocess indicated flist index */ |
9ec3828b WD |
196 | MSG_FLIST=20, /* extra file list over sibling socket */ |
197 | MSG_FLIST_EOF=21,/* we've transmitted all the file lists */ | |
198 | MSG_IO_ERROR=22,/* the sending side had an I/O error */ | |
199 | MSG_NOOP=42, /* a do-nothing message */ | |
1dcf3b43 WD |
200 | MSG_SUCCESS=100,/* successfully updated indicated flist index */ |
201 | MSG_DELETED=101,/* successfully deleted a file on receiving side */ | |
9ec3828b | 202 | MSG_NO_SEND=102,/* sender failed to open a file we wanted */ |
1dcf3b43 | 203 | MSG_DONE=86 /* current phase is done */ |
419896af | 204 | }; |
7bec6a5c | 205 | |
82ad07c4 | 206 | #define NDX_DONE -1 |
9ec3828b WD |
207 | #define NDX_FLIST_EOF -2 |
208 | #define NDX_FLIST_OFFSET -101 | |
82ad07c4 | 209 | |
65417579 AT |
210 | #include "errcode.h" |
211 | ||
c627d613 AT |
212 | #include "config.h" |
213 | ||
e3217f14 | 214 | /* The default RSYNC_RSH is always set in config.h. */ |
24d95c03 | 215 | |
c627d613 | 216 | #include <stdio.h> |
4f5b0756 | 217 | #ifdef HAVE_SYS_TYPES_H |
e95538ca WD |
218 | # include <sys/types.h> |
219 | #endif | |
4f5b0756 | 220 | #ifdef HAVE_SYS_STAT_H |
e95538ca WD |
221 | # include <sys/stat.h> |
222 | #endif | |
4f5b0756 | 223 | #ifdef STDC_HEADERS |
e95538ca WD |
224 | # include <stdlib.h> |
225 | # include <stddef.h> | |
226 | #else | |
4f5b0756 | 227 | # ifdef HAVE_STDLIB_H |
e95538ca WD |
228 | # include <stdlib.h> |
229 | # endif | |
230 | #endif | |
4f5b0756 WD |
231 | #ifdef HAVE_STRING_H |
232 | # if !defined STDC_HEADERS && defined HAVE_MEMORY_H | |
e95538ca WD |
233 | # include <memory.h> |
234 | # endif | |
235 | # include <string.h> | |
236 | #endif | |
4f5b0756 | 237 | #ifdef HAVE_STRINGS_H |
e95538ca WD |
238 | # include <strings.h> |
239 | #endif | |
2d2414f3 WD |
240 | #ifdef HAVE_INTTYPES_H |
241 | # include <inttypes.h> | |
242 | #endif | |
4f5b0756 | 243 | #ifdef HAVE_UNISTD_H |
e95538ca | 244 | # include <unistd.h> |
c627d613 AT |
245 | #endif |
246 | ||
4f5b0756 | 247 | #ifdef HAVE_SYS_PARAM_H |
e95538ca | 248 | #include <sys/param.h> |
38b02c13 | 249 | #endif |
59192f56 | 250 | |
4f5b0756 | 251 | #if defined HAVE_MALLOC_H && (defined HAVE_MALLINFO || !defined HAVE_STDLIB_H) |
59192f56 | 252 | #include <malloc.h> |
d7b305fd AT |
253 | #endif |
254 | ||
4f5b0756 | 255 | #ifdef HAVE_SYS_SOCKET_H |
fdd71e17 AT |
256 | #include <sys/socket.h> |
257 | #endif | |
258 | ||
4f5b0756 | 259 | #ifdef TIME_WITH_SYS_TIME |
c627d613 AT |
260 | #include <sys/time.h> |
261 | #include <time.h> | |
262 | #else | |
4f5b0756 | 263 | #ifdef HAVE_SYS_TIME_H |
c627d613 AT |
264 | #include <sys/time.h> |
265 | #else | |
266 | #include <time.h> | |
267 | #endif | |
268 | #endif | |
269 | ||
4f5b0756 | 270 | #ifdef HAVE_FCNTL_H |
c627d613 AT |
271 | #include <fcntl.h> |
272 | #else | |
4f5b0756 | 273 | #ifdef HAVE_SYS_FCNTL_H |
c627d613 AT |
274 | #include <sys/fcntl.h> |
275 | #endif | |
276 | #endif | |
277 | ||
4f5b0756 | 278 | #ifdef HAVE_SYS_IOCTL_H |
94481d91 AT |
279 | #include <sys/ioctl.h> |
280 | #endif | |
281 | ||
4f5b0756 | 282 | #ifdef HAVE_SYS_FILIO_H |
94481d91 AT |
283 | #include <sys/filio.h> |
284 | #endif | |
285 | ||
c627d613 | 286 | #include <signal.h> |
4f5b0756 | 287 | #ifdef HAVE_SYS_WAIT_H |
c627d613 AT |
288 | #include <sys/wait.h> |
289 | #endif | |
4f5b0756 | 290 | #ifdef HAVE_CTYPE_H |
c627d613 AT |
291 | #include <ctype.h> |
292 | #endif | |
4f5b0756 | 293 | #ifdef HAVE_GRP_H |
c627d613 AT |
294 | #include <grp.h> |
295 | #endif | |
296 | #include <errno.h> | |
297 | ||
4f5b0756 | 298 | #ifdef HAVE_UTIME_H |
c627d613 AT |
299 | #include <utime.h> |
300 | #endif | |
301 | ||
4f5b0756 | 302 | #ifdef HAVE_SYS_SELECT_H |
8bf73749 AT |
303 | #include <sys/select.h> |
304 | #endif | |
305 | ||
4f5b0756 | 306 | #ifdef HAVE_SYS_MODE_H |
773f2bd4 | 307 | /* apparently AIX needs this for S_ISLNK */ |
05a6556d | 308 | #ifndef S_ISLNK |
773f2bd4 AT |
309 | #include <sys/mode.h> |
310 | #endif | |
05a6556d | 311 | #endif |
773f2bd4 | 312 | |
4f5b0756 | 313 | #ifdef HAVE_GLOB_H |
874895d5 AT |
314 | #include <glob.h> |
315 | #endif | |
316 | ||
f6c34742 AT |
317 | /* these are needed for the uid/gid mapping code */ |
318 | #include <pwd.h> | |
319 | #include <grp.h> | |
320 | ||
9486289c | 321 | #include <stdarg.h> |
f0fca04e AT |
322 | #include <netinet/in.h> |
323 | #include <arpa/inet.h> | |
324 | #include <netdb.h> | |
ff8b29b8 | 325 | #include <syslog.h> |
6c8f5373 | 326 | #include <sys/file.h> |
9486289c | 327 | |
4f5b0756 | 328 | #ifdef HAVE_DIRENT_H |
e8f5b936 AT |
329 | # include <dirent.h> |
330 | #else | |
331 | # define dirent direct | |
4f5b0756 | 332 | # ifdef HAVE_SYS_NDIR_H |
e8f5b936 AT |
333 | # include <sys/ndir.h> |
334 | # endif | |
4f5b0756 | 335 | # ifdef HAVE_SYS_DIR_H |
e8f5b936 AT |
336 | # include <sys/dir.h> |
337 | # endif | |
4f5b0756 | 338 | # ifdef HAVE_NDIR_H |
e8f5b936 AT |
339 | # include <ndir.h> |
340 | # endif | |
341 | #endif | |
342 | ||
4f5b0756 | 343 | #ifdef MAJOR_IN_MKDEV |
c39d6514 | 344 | #include <sys/mkdev.h> |
8a33c406 | 345 | # if !defined makedev && (defined mkdev || defined _WIN32 || defined __WIN32__) |
73496a36 WD |
346 | # define makedev mkdev |
347 | # endif | |
4f5b0756 | 348 | #elif defined MAJOR_IN_SYSMACROS |
f8ed564c | 349 | #include <sys/sysmacros.h> |
c39d6514 WD |
350 | #endif |
351 | ||
5a3810b4 WD |
352 | #ifdef MAKEDEV_TAKES_3_ARGS |
353 | #define MAKEDEV(devmajor,devminor) makedev(0,devmajor,devminor) | |
354 | #else | |
355 | #define MAKEDEV(devmajor,devminor) makedev(devmajor,devminor) | |
356 | #endif | |
357 | ||
4f5b0756 | 358 | #ifdef HAVE_COMPAT_H |
e8f5b936 AT |
359 | #include <compat.h> |
360 | #endif | |
361 | ||
ea8291d8 WD |
362 | #ifdef HAVE_LIMITS_H |
363 | # include <limits.h> | |
364 | #endif | |
365 | ||
332cf6df WD |
366 | #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H |
367 | #include <iconv.h> | |
368 | #ifndef ICONV_CONST | |
369 | #define ICONV_CONST | |
370 | #endif | |
371 | #elif defined ICONV_CONST | |
372 | #undef ICONV_CONST | |
373 | #endif | |
374 | ||
1bfbf40b MP |
375 | #include <assert.h> |
376 | ||
9935066b | 377 | #include "lib/pool_alloc.h" |
e8f5b936 | 378 | |
f0fca04e AT |
379 | #define BOOL int |
380 | ||
c627d613 AT |
381 | #ifndef uchar |
382 | #define uchar unsigned char | |
383 | #endif | |
384 | ||
4f5b0756 | 385 | #ifdef SIGNED_CHAR_OK |
debb4505 AT |
386 | #define schar signed char |
387 | #else | |
388 | #define schar char | |
389 | #endif | |
390 | ||
2d2414f3 WD |
391 | #ifndef int16 |
392 | #if SIZEOF_INT16_T == 2 | |
393 | # define int16 int16_t | |
394 | #else | |
395 | # define int16 short | |
396 | #endif | |
397 | #endif | |
398 | ||
399 | #ifndef uint16 | |
400 | #if SIZEOF_UINT16_T == 2 | |
401 | # define uint16 uint16_t | |
402 | #else | |
403 | # define uint16 unsigned int16 | |
404 | #endif | |
405 | #endif | |
406 | ||
43d0f38b WD |
407 | /* Find a variable that is either exactly 32-bits or longer. |
408 | * If some code depends on 32-bit truncation, it will need to | |
409 | * take special action in a "#if SIZEOF_INT32 > 4" section. */ | |
c627d613 | 410 | #ifndef int32 |
2d2414f3 WD |
411 | #if SIZEOF_INT32_T == 4 |
412 | # define int32 int32_t | |
413 | # define SIZEOF_INT32 4 | |
414 | #elif SIZEOF_INT == 4 | |
43d0f38b WD |
415 | # define int32 int |
416 | # define SIZEOF_INT32 4 | |
417 | #elif SIZEOF_LONG == 4 | |
418 | # define int32 long | |
419 | # define SIZEOF_INT32 4 | |
420 | #elif SIZEOF_SHORT == 4 | |
421 | # define int32 short | |
422 | # define SIZEOF_INT32 4 | |
423 | #elif SIZEOF_INT > 4 | |
424 | # define int32 int | |
425 | # define SIZEOF_INT32 SIZEOF_INT | |
426 | #elif SIZEOF_LONG > 4 | |
427 | # define int32 long | |
428 | # define SIZEOF_INT32 SIZEOF_LONG | |
8de330a3 | 429 | #else |
43d0f38b | 430 | # error Could not find a 32-bit integer variable |
c627d613 | 431 | #endif |
43d0f38b WD |
432 | #else |
433 | # define SIZEOF_INT32 4 | |
c627d613 AT |
434 | #endif |
435 | ||
436 | #ifndef uint32 | |
2d2414f3 WD |
437 | #if SIZEOF_UINT32_T == 4 |
438 | # define uint32 uint32_t | |
439 | #else | |
440 | # define uint32 unsigned int32 | |
441 | #endif | |
c627d613 AT |
442 | #endif |
443 | ||
0dd046d3 | 444 | #if SIZEOF_OFF_T == 8 || !SIZEOF_OFF64_T || !defined HAVE_STRUCT_STAT64 |
bcacc18b AT |
445 | #define OFF_T off_t |
446 | #define STRUCT_STAT struct stat | |
5b5f7e3b WD |
447 | #else |
448 | #define OFF_T off64_t | |
449 | #define STRUCT_STAT struct stat64 | |
450 | #define USE_STAT64_FUNCS 1 | |
bcacc18b AT |
451 | #endif |
452 | ||
d4198823 WD |
453 | /* CAVEAT: on some systems, int64 will really be a 32-bit integer IFF |
454 | * that's the maximum size the file system can handle and there is no | |
455 | * 64-bit type available. The rsync source must therefore take steps | |
456 | * to ensure that any code that really requires a 64-bit integer has | |
457 | * it (e.g. the checksum code uses two 32-bit integers for its 64-bit | |
458 | * counter). */ | |
2d2414f3 WD |
459 | #if SIZEOF_INT64_T == 8 |
460 | # define int64 int64_t | |
461 | # define SIZEOF_INT64 8 | |
462 | #elif SIZEOF_LONG == 8 | |
d622d4bf WD |
463 | # define int64 long |
464 | # define SIZEOF_INT64 8 | |
465 | #elif SIZEOF_INT == 8 | |
466 | # define int64 int | |
467 | # define SIZEOF_INT64 8 | |
468 | #elif SIZEOF_LONG_LONG == 8 | |
469 | # define int64 long long | |
470 | # define SIZEOF_INT64 8 | |
2c177562 WD |
471 | #elif SIZEOF_OFF64_T == 8 |
472 | # define int64 off64_t | |
473 | # define SIZEOF_INT64 8 | |
d622d4bf WD |
474 | #elif SIZEOF_OFF_T == 8 |
475 | # define int64 off_t | |
476 | # define SIZEOF_INT64 8 | |
477 | #elif SIZEOF_INT > 8 | |
478 | # define int64 int | |
479 | # define SIZEOF_INT64 SIZEOF_INT | |
480 | #elif SIZEOF_LONG > 8 | |
481 | # define int64 long | |
482 | # define SIZEOF_INT64 SIZEOF_LONG | |
483 | #elif SIZEOF_LONG_LONG > 8 | |
484 | # define int64 long long | |
485 | # define SIZEOF_INT64 SIZEOF_LONG_LONG | |
71c46176 | 486 | #else |
736a6a29 | 487 | /* As long as it gets... */ |
d622d4bf WD |
488 | # define int64 off_t |
489 | # define SIZEOF_INT64 SIZEOF_OFF_T | |
71c46176 | 490 | #endif |
c627d613 | 491 | |
6abd193f MP |
492 | /* Starting from protocol version 26, we always use 64-bit |
493 | * ino_t and dev_t internally, even if this platform does not | |
494 | * allow files to have 64-bit inums. That's because the | |
495 | * receiver needs to find duplicate (dev,ino) tuples to detect | |
496 | * hardlinks, and it might have files coming from a platform | |
497 | * that has 64-bit inums. | |
498 | * | |
499 | * The only exception is if we're on a platform with no 64-bit type at | |
500 | * all. | |
501 | * | |
502 | * Because we use read_longint() to get these off the wire, if you | |
503 | * transfer devices or hardlinks with dev or inum > 2**32 to a machine | |
504 | * with no 64-bit types then you will get an overflow error. Probably | |
505 | * not many people have that combination of machines, and you can | |
506 | * avoid it by not preserving hardlinks or not transferring device | |
507 | * nodes. It's not clear that any other behaviour is better. | |
508 | * | |
509 | * Note that if you transfer devices from a 64-bit-devt machine (say, | |
510 | * Solaris) to a 32-bit-devt machine (say, Linux-2.2/x86) then the | |
511 | * device numbers will be truncated. But it's a kind of silly thing | |
512 | * to do anyhow. | |
b9df3bf2 | 513 | * |
33a2361c WD |
514 | * FIXME: I don't think the code in flist.c has ever worked on a system |
515 | * where dev_t is a struct. | |
d622d4bf | 516 | */ |
33a2361c | 517 | |
14d776ff WD |
518 | struct idev_node { |
519 | int64 key; | |
520 | void *data; | |
521 | }; | |
522 | ||
c627d613 AT |
523 | #ifndef MIN |
524 | #define MIN(a,b) ((a)<(b)?(a):(b)) | |
525 | #endif | |
526 | ||
527 | #ifndef MAX | |
528 | #define MAX(a,b) ((a)>(b)?(a):(b)) | |
529 | #endif | |
530 | ||
d41c7d02 DD |
531 | #ifndef MAXHOSTNAMELEN |
532 | #define MAXHOSTNAMELEN 256 | |
533 | #endif | |
534 | ||
c627d613 | 535 | #define SUM_LENGTH 16 |
195bd906 S |
536 | #define SHORT_SUM_LENGTH 2 |
537 | #define BLOCKSUM_BIAS 10 | |
c627d613 AT |
538 | |
539 | #ifndef MAXPATHLEN | |
540 | #define MAXPATHLEN 1024 | |
541 | #endif | |
542 | ||
a22ca885 WD |
543 | /* We want a roomy line buffer that can hold more than MAXPATHLEN, |
544 | * and significantly more than an overly short MAXPATHLEN. */ | |
545 | #if MAXPATHLEN < 4096 | |
546 | #define BIGPATHBUFLEN (4096+1024) | |
547 | #else | |
548 | #define BIGPATHBUFLEN (MAXPATHLEN+1024) | |
549 | #endif | |
550 | ||
52d3e106 S |
551 | #ifndef NAME_MAX |
552 | #define NAME_MAX 255 | |
553 | #endif | |
554 | ||
0473e2a1 AT |
555 | #ifndef INADDR_NONE |
556 | #define INADDR_NONE 0xffffffff | |
557 | #endif | |
558 | ||
bda41fa5 WD |
559 | #ifndef IN_LOOPBACKNET |
560 | #define IN_LOOPBACKNET 127 | |
561 | #endif | |
562 | ||
1c3344a1 WD |
563 | #if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS |
564 | #define ACLS_NEED_MASK 1 | |
565 | #endif | |
566 | ||
5b30412c | 567 | union file_extras { |
bd6edd3f WD |
568 | int32 num; |
569 | uint32 unum; | |
5b30412c WD |
570 | }; |
571 | ||
c627d613 | 572 | struct file_struct { |
2d2414f3 WD |
573 | const char *dirname; /* The dir info inside the transfer */ |
574 | time_t modtime; /* When the item was last modified */ | |
575 | uint32 len32; /* Lowest 32 bits of the file's length */ | |
576 | uint16 mode; /* The item's type and permissions */ | |
577 | uint16 flags; /* The FLAG_* bits for this item */ | |
578 | const char basename[1]; /* The basename (AKA filename) follows */ | |
c627d613 AT |
579 | }; |
580 | ||
99a957d3 | 581 | extern int file_extra_cnt; |
9742b386 WD |
582 | extern int uid_ndx; |
583 | extern int gid_ndx; | |
584 | extern int acls_ndx; | |
585 | extern int xattrs_ndx; | |
b96efc2f | 586 | |
c58c1dc4 | 587 | #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename)) |
5b30412c | 588 | #define EXTRA_LEN (sizeof (union file_extras)) |
bd6edd3f | 589 | #define PTR_EXTRA_LEN ((sizeof (char *) + EXTRA_LEN - 1) / EXTRA_LEN) |
a08979d5 WD |
590 | #define DEV_EXTRA_CNT 2 |
591 | #define DIRNODE_EXTRA_CNT 3 | |
a0456b9c | 592 | #define SUM_EXTRA_CNT ((MAX_DIGEST_LEN + EXTRA_LEN - 1) / EXTRA_LEN) |
96293cf9 | 593 | |
5b30412c | 594 | #define REQ_EXTRA(f,ndx) ((union file_extras*)(f) - (ndx)) |
99a957d3 | 595 | #define OPT_EXTRA(f,bump) ((union file_extras*)(f) - file_extra_cnt - 1 - (bump)) |
b37b7c99 | 596 | |
96293cf9 WD |
597 | #define LEN64_BUMP(f) ((f)->flags & FLAG_LENGTH64 ? 1 : 0) |
598 | #define HLINK_BUMP(f) (F_IS_HLINKED(f) ? 1 : 0) | |
a08979d5 | 599 | #define ACL_BUMP(f) (acls_ndx ? 1 : 0) |
96293cf9 | 600 | |
c58c1dc4 | 601 | /* The length applies to all items. */ |
bf466c0f WD |
602 | #if SIZEOF_INT64 < 8 |
603 | #define F_LENGTH(f) ((int64)(f)->len32) | |
604 | #else | |
605 | #define F_LENGTH(f) ((int64)(f)->len32 + ((f)->flags & FLAG_LENGTH64 \ | |
606 | ? (int64)OPT_EXTRA(f, 0)->unum << 32 : 0)) | |
607 | #endif | |
82ad07c4 | 608 | |
96293cf9 | 609 | /* If there is a symlink string, it is always right after the basename */ |
c58c1dc4 | 610 | #define F_SYMLINK(f) ((f)->basename + strlen((f)->basename) + 1) |
82ad07c4 | 611 | |
bd6edd3f | 612 | /* The sending side always has this available: */ |
8fc4033e | 613 | #define F_PATHNAME(f) (*(const char**)REQ_EXTRA(f, PTR_EXTRA_LEN)) |
bd6edd3f WD |
614 | |
615 | /* The receiving side always has this available: */ | |
616 | #define F_DEPTH(f) REQ_EXTRA(f, 1)->num | |
617 | ||
96293cf9 | 618 | /* When the associated option is on, all entries will have these present: */ |
9742b386 WD |
619 | #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum |
620 | #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum | |
621 | #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num | |
622 | #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num | |
332cf6df | 623 | #define F_NDX(f) REQ_EXTRA(f, ic_ndx)->num |
82ad07c4 | 624 | |
a08979d5 WD |
625 | /* These items are per-entry optional: */ |
626 | #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num /* non-dirs */ | |
627 | #define F_HL_PREV(f) OPT_EXTRA(f, LEN64_BUMP(f))->num /* non-dirs */ | |
628 | #define F_DIR_NODE_P(f) (&OPT_EXTRA(f, LEN64_BUMP(f) \ | |
629 | + DIRNODE_EXTRA_CNT - 1)->num) /* sender dirs */ | |
630 | #define F_DIR_DEFACL(f) OPT_EXTRA(f, LEN64_BUMP(f))->unum /* receiver dirs */ | |
631 | #define F_DIR_DEV_P(f) (&OPT_EXTRA(f, LEN64_BUMP(f) + ACL_BUMP(f) \ | |
632 | + DEV_EXTRA_CNT - 1)->unum) /* receiver dirs */ | |
82ad07c4 | 633 | |
b37b7c99 WD |
634 | /* This optional item might follow an F_HL_*() item. |
635 | * (Note: a device doesn't need to check LEN64_BUMP(f).) */ | |
636 | #define F_RDEV_P(f) (&OPT_EXTRA(f, HLINK_BUMP(f) + 2 - 1)->unum) | |
82ad07c4 | 637 | |
96293cf9 | 638 | /* The sum is only present on regular files. */ |
a08979d5 WD |
639 | #define F_SUM(f) ((char*)OPT_EXTRA(f, LEN64_BUMP(f) + HLINK_BUMP(f) \ |
640 | + SUM_EXTRA_CNT - 1)) | |
96293cf9 | 641 | |
b37b7c99 | 642 | /* Some utility defines: */ |
c58c1dc4 | 643 | #define F_IS_ACTIVE(f) (f)->basename[0] |
bfd31372 | 644 | #define F_IS_HLINKED(f) ((f)->flags & FLAG_HLINKED) |
5bb73485 | 645 | |
b7cfb9e2 WD |
646 | #define F_HLINK_NOT_FIRST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_FIRST) |
647 | #define F_HLINK_NOT_LAST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_LAST) | |
96293cf9 | 648 | |
b37b7c99 WD |
649 | #define DEV_MAJOR(a) (a)[0] |
650 | #define DEV_MINOR(a) (a)[1] | |
651 | ||
9ec3828b WD |
652 | #define DIR_PARENT(a) (a)[0] |
653 | #define DIR_FIRST_CHILD(a) (a)[1] | |
654 | #define DIR_NEXT_SIBLING(a) (a)[2] | |
655 | ||
96293cf9 WD |
656 | /* |
657 | * Start the flist array at FLIST_START entries and grow it | |
658 | * by doubling until FLIST_LINEAR then grow by FLIST_LINEAR | |
659 | */ | |
660 | #define FLIST_START (32 * 1024) | |
661 | #define FLIST_LINEAR (FLIST_START * 512) | |
82ad07c4 | 662 | |
9935066b | 663 | /* |
82ad07c4 | 664 | * Extent size for allocation pools: A minimum size of 128KB |
9935066b S |
665 | * is needed to mmap them so that freeing will release the |
666 | * space to the OS. | |
667 | * | |
668 | * Larger sizes reduce leftover fragments and speed free calls | |
82ad07c4 | 669 | * (when they happen). Smaller sizes increase the chance of |
9935066b S |
670 | * freed allocations freeing whole extents. |
671 | */ | |
f5ba7bfb WD |
672 | #define NORMAL_EXTENT (256 * 1024) |
673 | #define SMALL_EXTENT (128 * 1024) | |
9935066b | 674 | |
9ec3828b WD |
675 | #define FLIST_TEMP (1<<1) |
676 | ||
c627d613 | 677 | struct file_list { |
9ec3828b | 678 | struct file_list *next, *prev; |
332cf6df | 679 | struct file_struct **files, **sorted; |
9935066b | 680 | alloc_pool_t file_pool; |
f5ba7bfb | 681 | void *pool_boundary; |
9decb4d2 WD |
682 | int used, malloced; |
683 | int low, high; /* 0-relative index values excluding empties */ | |
684 | int ndx_start; /* the start offset for inc_recurse mode */ | |
24108eb8 | 685 | int ndx_end; /* the end offset for inc_recurse mode */ |
9ec3828b WD |
686 | int parent_ndx; /* dir_flist index of parent directory */ |
687 | int in_progress, to_redo; | |
c627d613 AT |
688 | }; |
689 | ||
a3221d2a WD |
690 | #define SUMFLG_SAME_OFFSET (1<<0) |
691 | ||
c627d613 | 692 | struct sum_buf { |
6ded1170 | 693 | OFF_T offset; /**< offset in file of this chunk */ |
a06b419d | 694 | int32 len; /**< length of chunk of file */ |
6ded1170 | 695 | uint32 sum1; /**< simple checksum */ |
d733de97 | 696 | int32 chain; /**< next hash-table collision */ |
a3221d2a | 697 | short flags; /**< flag bits */ |
6ded1170 | 698 | char sum2[SUM_LENGTH]; /**< checksum */ |
c627d613 AT |
699 | }; |
700 | ||
701 | struct sum_struct { | |
6ded1170 | 702 | OFF_T flength; /**< total file length */ |
2b2ea368 | 703 | struct sum_buf *sums; /**< points to info for each chunk */ |
15859584 | 704 | int32 count; /**< how many chunks */ |
a06b419d WD |
705 | int32 blength; /**< block_length */ |
706 | int32 remainder; /**< flength % block_length */ | |
da9d12f5 | 707 | int s2length; /**< sum2_length */ |
c627d613 AT |
708 | }; |
709 | ||
c6e7fcb4 | 710 | struct map_struct { |
6a7cc46c S |
711 | OFF_T file_size; /* File size (from stat) */ |
712 | OFF_T p_offset; /* Window start */ | |
713 | OFF_T p_fd_offset; /* offset of cursor in fd ala lseek */ | |
2b2ea368 WD |
714 | char *p; /* Window pointer */ |
715 | int32 p_size; /* Largest window size we allocated */ | |
716 | int32 p_len; /* Latest (rounded) window size */ | |
717 | int32 def_window_size; /* Default window size */ | |
718 | int fd; /* File Descriptor */ | |
719 | int status; /* first errno from read errors */ | |
c6e7fcb4 | 720 | }; |
c627d613 | 721 | |
e2d22fee WD |
722 | #define MATCHFLG_WILD (1<<0) /* pattern has '*', '[', and/or '?' */ |
723 | #define MATCHFLG_WILD2 (1<<1) /* pattern has '**' */ | |
685517ab WD |
724 | #define MATCHFLG_WILD2_PREFIX (1<<2) /* pattern starts with "**" */ |
725 | #define MATCHFLG_WILD3_SUFFIX (1<<3) /* pattern ends with "***" */ | |
726 | #define MATCHFLG_ABS_PATH (1<<4) /* path-match on absolute path */ | |
727 | #define MATCHFLG_INCLUDE (1<<5) /* this is an include, not an exclude */ | |
728 | #define MATCHFLG_DIRECTORY (1<<6) /* this matches only directories */ | |
46fa6025 WD |
729 | #define MATCHFLG_WORD_SPLIT (1<<7) /* split rules on whitespace */ |
730 | #define MATCHFLG_NO_INHERIT (1<<8) /* don't inherit these rules */ | |
731 | #define MATCHFLG_NO_PREFIXES (1<<9) /* parse no prefixes from patterns */ | |
732 | #define MATCHFLG_MERGE_FILE (1<<10)/* specifies a file to merge */ | |
733 | #define MATCHFLG_PERDIR_MERGE (1<<11)/* merge-file is searched per-dir */ | |
734 | #define MATCHFLG_EXCLUDE_SELF (1<<12)/* merge-file name should be excluded */ | |
735 | #define MATCHFLG_FINISH_SETUP (1<<13)/* per-dir merge file needs setup */ | |
53b417e4 WD |
736 | #define MATCHFLG_NEGATE (1<<14)/* rule matches when pattern does not */ |
737 | #define MATCHFLG_CVS_IGNORE (1<<15)/* rule was -C or :C */ | |
a427e893 WD |
738 | #define MATCHFLG_SENDER_SIDE (1<<16)/* rule applies to the sending side */ |
739 | #define MATCHFLG_RECEIVER_SIDE (1<<17)/* rule applies to the receiving side */ | |
685517ab | 740 | #define MATCHFLG_CLEAR_LIST (1<<18)/* this item is the "!" token */ |
c0f1e57b | 741 | #define MATCHFLG_PERISHABLE (1<<19)/* perishable if parent dir goes away */ |
7c8e23bd | 742 | |
3b2461cf | 743 | #define MATCHFLGS_FROM_CONTAINER (MATCHFLG_ABS_PATH | MATCHFLG_INCLUDE \ |
a427e893 | 744 | | MATCHFLG_DIRECTORY | MATCHFLG_SENDER_SIDE \ |
c0f1e57b WD |
745 | | MATCHFLG_NEGATE | MATCHFLG_RECEIVER_SIDE \ |
746 | | MATCHFLG_PERISHABLE) | |
3b2461cf | 747 | |
7842418b WD |
748 | struct filter_struct { |
749 | struct filter_struct *next; | |
2b6b4d53 | 750 | char *pattern; |
53b417e4 | 751 | uint32 match_flags; |
46fa6025 WD |
752 | union { |
753 | int slash_cnt; | |
7842418b | 754 | struct filter_list_struct *mergelist; |
46fa6025 | 755 | } u; |
2b6b4d53 AT |
756 | }; |
757 | ||
7842418b WD |
758 | struct filter_list_struct { |
759 | struct filter_struct *head; | |
760 | struct filter_struct *tail; | |
ac1d2d33 | 761 | char *debug_type; |
63d03319 WD |
762 | }; |
763 | ||
a800434a AT |
764 | struct stats { |
765 | int64 total_size; | |
766 | int64 total_transferred_size; | |
767 | int64 total_written; | |
768 | int64 total_read; | |
769 | int64 literal_data; | |
770 | int64 matched_data; | |
d4198823 WD |
771 | int64 flist_buildtime; |
772 | int64 flist_xfertime; | |
604dbf6d | 773 | int64 flist_size; |
a800434a AT |
774 | int num_files; |
775 | int num_transferred_files; | |
dc8293ff | 776 | int current_file_index; |
a800434a AT |
777 | }; |
778 | ||
f7bce90c | 779 | struct chmod_mode_struct; |
2b6b4d53 | 780 | |
1c3344a1 WD |
781 | #define EMPTY_ITEM_LIST {NULL, 0, 0} |
782 | ||
783 | typedef struct { | |
784 | void *items; | |
785 | size_t count; | |
786 | size_t malloced; | |
787 | } item_list; | |
788 | ||
789 | #define EXPAND_ITEM_LIST(lp, type, incr) \ | |
790 | (type*)expand_item_list(lp, sizeof (type), #type, incr) | |
791 | ||
c627d613 | 792 | #include "byteorder.h" |
a0456b9c | 793 | #include "lib/mdigest.h" |
87f18b62 | 794 | #include "lib/wildmatch.h" |
740819ef | 795 | #include "lib/permstring.h" |
e20a4f84 | 796 | #include "lib/addrinfo.h" |
0de40240 | 797 | |
2b916250 | 798 | #ifndef __GNUC__ |
4ea58045 | 799 | #define __attribute__(x) |
1b896f8d | 800 | #else |
7de7b49f WD |
801 | # if __GNUC__ <= 2 |
802 | # define NORETURN | |
803 | # endif | |
4ea58045 WD |
804 | #endif |
805 | ||
90b13cf6 | 806 | #define UNUSED(x) x __attribute__((__unused__)) |
7de7b49f | 807 | #ifndef NORETURN |
011e85a5 | 808 | #define NORETURN __attribute__((__noreturn__)) |
7de7b49f | 809 | #endif |
90b13cf6 | 810 | |
1c3344a1 WD |
811 | typedef struct { |
812 | STRUCT_STAT st; | |
813 | #ifdef SUPPORT_ACLS | |
814 | struct rsync_acl *acc_acl; /* access ACL */ | |
815 | struct rsync_acl *def_acl; /* default ACL */ | |
816 | #endif | |
16edf865 WD |
817 | #ifdef SUPPORT_XATTRS |
818 | item_list *xattr; | |
819 | #endif | |
1c3344a1 WD |
820 | } statx; |
821 | ||
822 | #define ACL_READY(sx) ((sx).acc_acl != NULL) | |
16edf865 | 823 | #define XATTR_READY(sx) ((sx).xattr != NULL) |
1c3344a1 | 824 | |
58c29609 MP |
825 | #include "proto.h" |
826 | ||
9439c0cb WD |
827 | #ifndef SUPPORT_XATTRS |
828 | #define x_stat(fn,fst,xst) do_stat(fn,fst) | |
829 | #define x_lstat(fn,fst,xst) do_lstat(fn,fst) | |
830 | #define x_fstat(fd,fst,xst) do_fstat(fd,fst) | |
831 | #endif | |
832 | ||
0de40240 | 833 | /* We have replacement versions of these if they're missing. */ |
4f5b0756 | 834 | #ifndef HAVE_ASPRINTF |
0de40240 MP |
835 | int asprintf(char **ptr, const char *format, ...); |
836 | #endif | |
837 | ||
4f5b0756 | 838 | #ifndef HAVE_VASPRINTF |
0de40240 MP |
839 | int vasprintf(char **ptr, const char *format, va_list ap); |
840 | #endif | |
841 | ||
4f5b0756 | 842 | #if !defined HAVE_VSNPRINTF || !defined HAVE_C99_VSNPRINTF |
6813fa7e WD |
843 | #define vsnprintf rsync_vsnprintf |
844 | int vsnprintf(char *str, size_t count, const char *fmt, va_list args); | |
0de40240 MP |
845 | #endif |
846 | ||
4f5b0756 | 847 | #if !defined HAVE_SNPRINTF || !defined HAVE_C99_VSNPRINTF |
6813fa7e | 848 | #define snprintf rsync_snprintf |
82ad07c4 | 849 | int snprintf(char *str, size_t count, const char *fmt,...); |
0de40240 MP |
850 | #endif |
851 | ||
852 | ||
4f5b0756 | 853 | #ifndef HAVE_STRERROR |
c627d613 AT |
854 | extern char *sys_errlist[]; |
855 | #define strerror(i) sys_errlist[i] | |
856 | #endif | |
857 | ||
4f5b0756 | 858 | #ifndef HAVE_STRCHR |
c627d613 AT |
859 | # define strchr index |
860 | # define strrchr rindex | |
861 | #endif | |
862 | ||
4f5b0756 | 863 | #ifndef HAVE_ERRNO_DECL |
c627d613 AT |
864 | extern int errno; |
865 | #endif | |
866 | ||
9a929c8f WD |
867 | #ifdef HAVE_READLINK |
868 | #define SUPPORT_LINKS 1 | |
869 | #endif | |
870 | #ifdef HAVE_LINK | |
871 | #define SUPPORT_HARD_LINKS 1 | |
872 | #endif | |
c627d613 | 873 | |
5173f99e | 874 | #ifdef HAVE_SIGACTION |
90b13cf6 WD |
875 | #define SIGACTION(n,h) sigact.sa_handler=(h), sigaction((n),&sigact,NULL) |
876 | #define signal(n,h) we_need_to_call_SIGACTION_not_signal(n,h) | |
877 | #else | |
878 | #define SIGACTION(n,h) signal(n,h) | |
879 | #endif | |
720b47f2 AT |
880 | |
881 | #ifndef EWOULDBLOCK | |
882 | #define EWOULDBLOCK EAGAIN | |
883 | #endif | |
182dca5c | 884 | |
7b8356d0 AT |
885 | #ifndef STDIN_FILENO |
886 | #define STDIN_FILENO 0 | |
887 | #endif | |
888 | ||
889 | #ifndef STDOUT_FILENO | |
890 | #define STDOUT_FILENO 1 | |
891 | #endif | |
892 | ||
893 | #ifndef STDERR_FILENO | |
894 | #define STDERR_FILENO 2 | |
895 | #endif | |
896 | ||
8120a98f WD |
897 | #ifndef S_IRUSR |
898 | #define S_IRUSR 0400 | |
899 | #endif | |
900 | ||
7b8356d0 AT |
901 | #ifndef S_IWUSR |
902 | #define S_IWUSR 0200 | |
903 | #endif | |
904 | ||
b0d791bb PG |
905 | #ifndef ACCESSPERMS |
906 | #define ACCESSPERMS 0777 | |
907 | #endif | |
908 | ||
909 | #ifndef S_ISVTX | |
910 | #define S_ISVTX 0 | |
911 | #endif | |
912 | ||
913 | #define CHMOD_BITS (S_ISUID | S_ISGID | S_ISVTX | ACCESSPERMS) | |
914 | ||
b280a1f4 AT |
915 | #ifndef _S_IFMT |
916 | #define _S_IFMT 0170000 | |
917 | #endif | |
918 | ||
919 | #ifndef _S_IFLNK | |
920 | #define _S_IFLNK 0120000 | |
921 | #endif | |
922 | ||
923 | #ifndef S_ISLNK | |
924 | #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK)) | |
925 | #endif | |
926 | ||
1e9f155a AT |
927 | #ifndef S_ISBLK |
928 | #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK)) | |
929 | #endif | |
930 | ||
931 | #ifndef S_ISCHR | |
932 | #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR)) | |
933 | #endif | |
934 | ||
a0b65b18 AT |
935 | #ifndef S_ISSOCK |
936 | #ifdef _S_IFSOCK | |
937 | #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK)) | |
938 | #else | |
939 | #define S_ISSOCK(mode) (0) | |
940 | #endif | |
941 | #endif | |
942 | ||
943 | #ifndef S_ISFIFO | |
944 | #ifdef _S_IFIFO | |
945 | #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO)) | |
946 | #else | |
947 | #define S_ISFIFO(mode) (0) | |
948 | #endif | |
949 | #endif | |
950 | ||
1e9f155a AT |
951 | #ifndef S_ISDIR |
952 | #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR)) | |
953 | #endif | |
954 | ||
955 | #ifndef S_ISREG | |
956 | #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG)) | |
957 | #endif | |
958 | ||
f0359dd0 AT |
959 | /* work out what fcntl flag to use for non-blocking */ |
960 | #ifdef O_NONBLOCK | |
961 | # define NONBLOCK_FLAG O_NONBLOCK | |
4f5b0756 | 962 | #elif defined SYSV |
f0359dd0 | 963 | # define NONBLOCK_FLAG O_NDELAY |
d622d4bf | 964 | #else |
f0359dd0 AT |
965 | # define NONBLOCK_FLAG FNDELAY |
966 | #endif | |
967 | ||
d79c77ca MP |
968 | #ifndef INADDR_LOOPBACK |
969 | #define INADDR_LOOPBACK 0x7f000001 | |
970 | #endif | |
971 | ||
972 | #ifndef INADDR_NONE | |
973 | #define INADDR_NONE 0xffffffff | |
974 | #endif | |
a0b65b18 | 975 | |
b5c6a6ae WD |
976 | #define IS_SPECIAL(mode) (S_ISSOCK(mode) || S_ISFIFO(mode)) |
977 | #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode)) | |
7bec6a5c | 978 | |
5afd8aed DD |
979 | /* Initial mask on permissions given to temporary files. Mask off setuid |
980 | bits and group access because of potential race-condition security | |
981 | holes, and mask other access because mode 707 is bizarre */ | |
972a3619 | 982 | #define INITACCESSPERMS 0700 |
e08bfe12 AT |
983 | |
984 | /* handler for null strings in printf format */ | |
0ee6ca98 | 985 | #define NS(s) ((s)?(s):"<NULL>") |
e08bfe12 | 986 | |
58cadc86 WD |
987 | /* Convenient wrappers for malloc and realloc. Use them. */ |
988 | #define new(type) ((type *)malloc(sizeof(type))) | |
989 | #define new_array(type, num) ((type *)_new_array(sizeof(type), (num))) | |
990 | #define realloc_array(ptr, type, num) ((type *)_realloc_array((ptr), sizeof(type), (num))) | |
fb859e56 | 991 | |
e08bfe12 | 992 | /* use magic gcc attributes to catch format errors */ |
ff41a59f | 993 | void rprintf(enum logcode , const char *, ...) |
fb859e56 | 994 | __attribute__((format (printf, 2, 3))) |
e08bfe12 | 995 | ; |
7b3d4257 | 996 | |
a039749b MP |
997 | /* This is just like rprintf, but it also tries to print some |
998 | * representation of the error code. Normally errcode = errno. */ | |
999 | void rsyserr(enum logcode, int, const char *, ...) | |
fb859e56 | 1000 | __attribute__((format (printf, 3, 4))) |
a039749b MP |
1001 | ; |
1002 | ||
afbcc8f2 WD |
1003 | /* Make sure that the O_BINARY flag is defined. */ |
1004 | #ifndef O_BINARY | |
1005 | #define O_BINARY 0 | |
1006 | #endif | |
5a788ade | 1007 | |
4f5b0756 | 1008 | #ifndef HAVE_STRLCPY |
5a788ade AT |
1009 | size_t strlcpy(char *d, const char *s, size_t bufsize); |
1010 | #endif | |
1011 | ||
4f5b0756 | 1012 | #ifndef HAVE_STRLCAT |
5a788ade AT |
1013 | size_t strlcat(char *d, const char *s, size_t bufsize); |
1014 | #endif | |
1015 | ||
82980a23 AT |
1016 | #ifndef WEXITSTATUS |
1017 | #define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF)) | |
1018 | #endif | |
47b032e9 WD |
1019 | #ifndef WIFEXITED |
1020 | #define WIFEXITED(stat) ((int)((stat)&0xFF) == 0) | |
1021 | #endif | |
82980a23 | 1022 | |
a9766ef1 | 1023 | #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__) |
eecd22ff | 1024 | |
4f5b0756 | 1025 | #ifdef HAVE_GETEUID |
b2bffbb2 WD |
1026 | #define MY_UID() geteuid() |
1027 | #else | |
1028 | #define MY_UID() getuid() | |
1029 | #endif | |
1030 | ||
4f5b0756 | 1031 | #ifdef HAVE_GETEGID |
b2bffbb2 WD |
1032 | #define MY_GID() getegid() |
1033 | #else | |
1034 | #define MY_GID() getgid() | |
1035 | #endif | |
eecd22ff | 1036 | |
6d4e718f | 1037 | extern int verbose; |
d5d4b282 | 1038 | |
4f5b0756 | 1039 | #ifndef HAVE_INET_NTOP |
d622d4bf | 1040 | const char *inet_ntop(int af, const void *src, char *dst, size_t size); |
4f5b0756 | 1041 | #endif |
8f694072 | 1042 | |
4f5b0756 | 1043 | #ifndef HAVE_INET_PTON |
bda41fa5 | 1044 | int inet_pton(int af, const char *src, void *dst); |
8f694072 | 1045 | #endif |
0f0ea7f7 | 1046 | |
f4a0483a MP |
1047 | #ifdef MAINTAINER_MODE |
1048 | const char *get_panic_action(void); | |
1049 | #endif | |
2dc7b8bd | 1050 | |
9439c0cb WD |
1051 | static inline int to_wire_mode(mode_t mode) |
1052 | { | |
1053 | #ifdef SUPPORT_LINKS | |
1054 | #if _S_IFLNK != 0120000 | |
1055 | if (S_ISLNK(mode)) | |
1056 | return (mode & ~(_S_IFMT)) | 0120000; | |
1057 | #endif | |
1058 | #endif | |
1059 | return mode; | |
1060 | } | |
1061 | ||
1062 | static inline mode_t from_wire_mode(int mode) | |
1063 | { | |
1064 | #if _S_IFLNK != 0120000 | |
1065 | if ((mode & (_S_IFMT)) == 0120000) | |
1066 | return (mode & ~(_S_IFMT)) | _S_IFLNK; | |
1067 | #endif | |
1068 | return mode; | |
1069 | } | |
1070 | ||
2dc7b8bd WD |
1071 | static inline int |
1072 | isDigit(const char *ptr) | |
1073 | { | |
1074 | return isdigit(*(unsigned char *)ptr); | |
1075 | } | |
1076 | ||
1077 | static inline int | |
1078 | isPrint(const char *ptr) | |
1079 | { | |
1080 | return isprint(*(unsigned char *)ptr); | |
1081 | } | |
1082 | ||
1083 | static inline int | |
1084 | isSpace(const char *ptr) | |
1085 | { | |
1086 | return isspace(*(unsigned char *)ptr); | |
1087 | } | |
1088 | ||
1089 | static inline int | |
1090 | isLower(const char *ptr) | |
1091 | { | |
1092 | return islower(*(unsigned char *)ptr); | |
1093 | } | |
1094 | ||
1095 | static inline int | |
1096 | isUpper(const char *ptr) | |
1097 | { | |
1098 | return isupper(*(unsigned char *)ptr); | |
1099 | } | |
1100 | ||
1101 | static inline int | |
1102 | toLower(const char *ptr) | |
1103 | { | |
1104 | return tolower(*(unsigned char *)ptr); | |
1105 | } | |
1106 | ||
1107 | static inline int | |
1108 | toUpper(const char *ptr) | |
1109 | { | |
1110 | return toupper(*(unsigned char *)ptr); | |
1111 | } |