Ignore generated files and dirs.
[rsync/rsync.git] / compat.c
CommitLineData
0f78b815 1/*
d1f83bcc 2 * Compatibility routines for older rsync protocol versions.
0f78b815
WD
3 *
4 * Copyright (C) Andrew Tridgell 1996
5 * Copyright (C) Paul Mackerras 1996
ba2133d6 6 * Copyright (C) 2004-2007 Wayne Davison
0f78b815
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
0f78b815
WD
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
e7c67065 18 * You should have received a copy of the GNU General Public License along
4fd842f9 19 * with this program; if not, visit the http://fsf.org website.
0f78b815 20 */
4fe159a8
AT
21
22#include "rsync.h"
23
8a9709de 24int remote_protocol = 0;
99a957d3 25int file_extra_cnt = 0; /* count of file-list extras that everyone gets */
3ea6e0e7 26int inc_recurse = 0;
8a9709de 27
b9f592fb 28extern int verbose;
aae43eb3 29extern int am_server;
771d0add 30extern int am_sender;
d0c59b0e 31extern int local_server;
57b12568 32extern int inplace;
e4b619b4 33extern int recurse;
c8e83946 34extern int use_qsort;
5275029d 35extern int allow_inc_recurse;
77502cba 36extern int append_mode;
e461b9be 37extern int fuzzy_basis;
b9f592fb 38extern int read_batch;
e4b619b4 39extern int delay_updates;
aae43eb3 40extern int checksum_seed;
57b12568 41extern int basis_dir_cnt;
a1cc199b 42extern int prune_empty_dirs;
8a9709de 43extern int protocol_version;
5688c74d 44extern int protect_args;
99a957d3
WD
45extern int preserve_uid;
46extern int preserve_gid;
1c3344a1 47extern int preserve_acls;
16edf865 48extern int preserve_xattrs;
e4b619b4
WD
49extern int need_messages_from_generator;
50extern int delete_mode, delete_before, delete_during, delete_after;
486f8cd1 51extern char *shell_cmd; /* contains VER.SUB string if client is a pre-release */
c1289508 52extern char *partial_dir;
57b12568 53extern char *dest_option;
5688c74d
WD
54extern char *files_from;
55extern char *filesfrom_host;
c1289508 56extern struct filter_list_struct filter_list;
f7a76b9c 57extern int need_unsorted_flist;
5688c74d 58#ifdef ICONV_OPTION
5688c74d
WD
59extern iconv_t ic_send, ic_recv;
60#endif
4fe159a8 61
9742b386 62/* These index values are for the file-list's extra-attribute array. */
f7a76b9c 63int uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
5688c74d 64
f7a76b9c 65#ifdef ICONV_OPTION
5688c74d
WD
66int filesfrom_convert = 0;
67#endif
9742b386 68
486f8cd1
WD
69/* The server makes sure that if either side only supports a pre-release
70 * version of a protocol, that both sides must speak a compatible version
71 * of that protocol for it to be advertised as available. */
72static void check_sub_protocol(void)
73{
74 char *dot;
75 int their_protocol, their_sub;
ac593860 76#if SUBPROTOCOL_VERSION != 0
486f8cd1 77 int our_sub = protocol_version < PROTOCOL_VERSION ? 0 : SUBPROTOCOL_VERSION;
ac593860
WD
78#else
79 int our_sub = 0;
80#endif
486f8cd1
WD
81
82 if (!shell_cmd || !(dot = strchr(shell_cmd, '.'))
83 || !(their_protocol = atoi(shell_cmd))
84 || !(their_sub = atoi(dot+1))) {
ac593860 85#if SUBPROTOCOL_VERSION != 0
486f8cd1
WD
86 if (our_sub)
87 protocol_version--;
ac593860 88#endif
486f8cd1
WD
89 return;
90 }
91
92 if (their_protocol < protocol_version) {
93 if (their_sub)
94 protocol_version = their_protocol - 1;
95 return;
96 }
97
98 if (their_protocol > protocol_version)
ac593860 99 their_sub = 0; /* 0 == final version of older protocol */
486f8cd1
WD
100 if (their_sub != our_sub)
101 protocol_version--;
102}
103
9970bed4
WD
104void set_allow_inc_recurse(void)
105{
aabb50d4
WD
106 if (!recurse || use_qsort)
107 allow_inc_recurse = 0;
108 else if (!am_sender
109 && (delete_before || delete_after
110 || delay_updates || prune_empty_dirs))
9970bed4
WD
111 allow_inc_recurse = 0;
112 else if (am_server && !local_server
113 && (!shell_cmd || strchr(shell_cmd, 'i') == NULL))
114 allow_inc_recurse = 0;
115}
116
aae43eb3 117void setup_protocol(int f_out,int f_in)
4fe159a8 118{
6eee13cf 119 if (am_sender)
bc5df0f4 120 file_extra_cnt += PTR_EXTRA_CNT;
6eee13cf 121 else
99a957d3 122 file_extra_cnt++;
6eee13cf 123 if (preserve_uid)
9742b386 124 uid_ndx = ++file_extra_cnt;
6eee13cf 125 if (preserve_gid)
9742b386 126 gid_ndx = ++file_extra_cnt;
1c3344a1 127 if (preserve_acls && !am_sender)
9742b386 128 acls_ndx = ++file_extra_cnt;
16edf865 129 if (preserve_xattrs)
9742b386 130 xattrs_ndx = ++file_extra_cnt;
6eee13cf 131
9970bed4
WD
132 if (am_server)
133 set_allow_inc_recurse();
134
8a9709de 135 if (remote_protocol == 0) {
486f8cd1
WD
136 if (am_server && !local_server)
137 check_sub_protocol();
aa434321 138 if (!read_batch)
8a9709de 139 write_int(f_out, protocol_version);
aa434321 140 remote_protocol = read_int(f_in);
8a9709de
WD
141 if (protocol_version > remote_protocol)
142 protocol_version = remote_protocol;
13c5fc0e 143 }
b9f592fb 144 if (read_batch && remote_protocol > protocol_version) {
486f8cd1 145 rprintf(FERROR, "The protocol version in the batch file is too new (%d > %d).\n",
b9f592fb
WD
146 remote_protocol, protocol_version);
147 exit_cleanup(RERR_PROTOCOL);
148 }
aae43eb3 149
8a9709de
WD
150 if (verbose > 3) {
151 rprintf(FINFO, "(%s) Protocol versions: remote=%d, negotiated=%d\n",
152 am_server? "Server" : "Client", remote_protocol, protocol_version);
153 }
154 if (remote_protocol < MIN_PROTOCOL_VERSION
155 || remote_protocol > MAX_PROTOCOL_VERSION) {
4ccfd96c 156 rprintf(FERROR,"protocol version mismatch -- is your shell clean?\n");
d310a212 157 rprintf(FERROR,"(see the rsync man page for an explanation)\n");
65417579 158 exit_cleanup(RERR_PROTOCOL);
8a9709de
WD
159 }
160 if (remote_protocol < OLD_PROTOCOL_VERSION) {
1b2db7ae 161 rprintf(FINFO,"%s is very old version of rsync, upgrade recommended.\n",
8a9709de
WD
162 am_server? "Client" : "Server");
163 }
164 if (protocol_version < MIN_PROTOCOL_VERSION) {
165 rprintf(FERROR, "--protocol must be at least %d on the %s.\n",
166 MIN_PROTOCOL_VERSION, am_server? "Server" : "Client");
167 exit_cleanup(RERR_PROTOCOL);
168 }
169 if (protocol_version > PROTOCOL_VERSION) {
170 rprintf(FERROR, "--protocol must be no more than %d on the %s.\n",
171 PROTOCOL_VERSION, am_server? "Server" : "Client");
172 exit_cleanup(RERR_PROTOCOL);
173 }
1b2db7ae 174
771d0add 175 if (protocol_version < 30) {
77502cba
WD
176 if (append_mode == 1)
177 append_mode = 2;
01894cf0 178 if (preserve_acls && !local_server) {
1c3344a1
WD
179 rprintf(FERROR,
180 "--acls requires protocol 30 or higher"
181 " (negotiated %d).\n",
182 protocol_version);
183 exit_cleanup(RERR_PROTOCOL);
184 }
01894cf0 185 if (preserve_xattrs && !local_server) {
16edf865
WD
186 rprintf(FERROR,
187 "--xattrs requires protocol 30 or higher"
188 " (negotiated %d).\n",
189 protocol_version);
190 exit_cleanup(RERR_PROTOCOL);
191 }
771d0add
WD
192 }
193
e4b619b4
WD
194 if (delete_mode && !(delete_before+delete_during+delete_after)) {
195 if (protocol_version < 30)
196 delete_before = 1;
197 else
198 delete_during = 1;
199 }
200
a1cc199b
WD
201 if (protocol_version < 29) {
202 if (fuzzy_basis) {
203 rprintf(FERROR,
204 "--fuzzy requires protocol 29 or higher"
205 " (negotiated %d).\n",
206 protocol_version);
207 exit_cleanup(RERR_PROTOCOL);
208 }
e461b9be 209
a1cc199b
WD
210 if (basis_dir_cnt && inplace) {
211 rprintf(FERROR,
212 "%s with --inplace requires protocol 29 or higher"
213 " (negotiated %d).\n",
214 dest_option, protocol_version);
215 exit_cleanup(RERR_PROTOCOL);
216 }
57b12568 217
a1cc199b
WD
218 if (basis_dir_cnt > 1) {
219 rprintf(FERROR,
220 "Using more than one %s option requires protocol"
221 " 29 or higher (negotiated %d).\n",
222 dest_option, protocol_version);
223 exit_cleanup(RERR_PROTOCOL);
224 }
225
226 if (prune_empty_dirs) {
227 rprintf(FERROR,
228 "--prune-empty-dirs requires protocol 29 or higher"
229 " (negotiated %d).\n",
230 protocol_version);
231 exit_cleanup(RERR_PROTOCOL);
232 }
e4b619b4 233 } else if (protocol_version >= 30) {
9970bed4
WD
234 if (am_server) {
235 inc_recurse = allow_inc_recurse;
dbd697b5 236 write_byte(f_out, inc_recurse);
9970bed4
WD
237 } else
238 inc_recurse = read_byte(f_in);
239 if (inc_recurse && !allow_inc_recurse) {
240 /* This should only be able to happen in a batch. */
241 fprintf(stderr,
242 "Incompatible options specified for inc-recursive %s.\n",
52d9a554 243 read_batch ? "batch file" : "connection");
9970bed4
WD
244 exit_cleanup(RERR_SYNTAX);
245 }
e4b619b4 246 need_messages_from_generator = 1;
57b12568
WD
247 }
248
f7a76b9c
WD
249 if (need_unsorted_flist && (!am_sender || inc_recurse))
250 unsort_ndx = ++file_extra_cnt;
7820fa94 251
d0c59b0e 252 if (partial_dir && *partial_dir != '/' && (!am_server || local_server)) {
c1289508
WD
253 int flags = MATCHFLG_NO_PREFIXES | MATCHFLG_DIRECTORY;
254 if (!am_sender || protocol_version >= 30)
255 flags |= MATCHFLG_PERISHABLE;
256 parse_rule(&filter_list, partial_dir, flags, 0);
257 }
258
5688c74d
WD
259
260#ifdef ICONV_OPTION
261 if (protect_args && files_from) {
262 if (am_sender)
263 filesfrom_convert = filesfrom_host && ic_send != (iconv_t)-1;
264 else
265 filesfrom_convert = !filesfrom_host && ic_recv != (iconv_t)-1;
266 }
267#endif
268
bc63ae3f 269 if (am_server) {
b1bf649c 270 if (!checksum_seed)
13c5fc0e 271 checksum_seed = time(NULL);
b1bf649c 272 write_int(f_out, checksum_seed);
bc63ae3f
S
273 } else {
274 checksum_seed = read_int(f_in);
13c5fc0e 275 }
4fe159a8 276}