Renamed flist_extra_cnt to file_extra_cnt.
[rsync/rsync.git] / compat.c
1 /*
2  * Compatibility routines for older rsync protocol versions.
3  *
4  * Copyright (C) Andrew Tridgell 1996
5  * Copyright (C) Paul Mackerras 1996
6  * Copyright (C) 2004, 2005, 2006 Wayne Davison
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
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  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 #include "rsync.h"
24
25 int remote_protocol = 0;
26 int file_extra_cnt = 0; /* count of file-list extras that everyone gets */
27
28 extern int verbose;
29 extern int am_server;
30 extern int am_sender;
31 extern int inplace;
32 extern int fuzzy_basis;
33 extern int read_batch;
34 extern int max_delete;
35 extern int checksum_seed;
36 extern int basis_dir_cnt;
37 extern int prune_empty_dirs;
38 extern int protocol_version;
39 extern int preserve_uid;
40 extern int preserve_gid;
41 extern char *dest_option;
42
43 void setup_protocol(int f_out,int f_in)
44 {
45         if (am_sender)
46                 file_extra_cnt += PTR_EXTRA_LEN;
47         else
48                 file_extra_cnt++;
49         if (preserve_uid)
50                 preserve_uid = ++file_extra_cnt;
51         if (preserve_gid)
52                 preserve_gid = ++file_extra_cnt;
53
54         if (remote_protocol == 0) {
55                 if (!read_batch)
56                         write_int(f_out, protocol_version);
57                 remote_protocol = read_int(f_in);
58                 if (protocol_version > remote_protocol)
59                         protocol_version = remote_protocol;
60         }
61         if (read_batch && remote_protocol > protocol_version) {
62                 rprintf(FERROR, "The protocol version in the batch file is too new (%d > %d).\n",
63                         remote_protocol, protocol_version);
64                 exit_cleanup(RERR_PROTOCOL);
65         }
66
67         if (verbose > 3) {
68                 rprintf(FINFO, "(%s) Protocol versions: remote=%d, negotiated=%d\n",
69                         am_server? "Server" : "Client", remote_protocol, protocol_version);
70         }
71         if (remote_protocol < MIN_PROTOCOL_VERSION
72          || remote_protocol > MAX_PROTOCOL_VERSION) {
73                 rprintf(FERROR,"protocol version mismatch -- is your shell clean?\n");
74                 rprintf(FERROR,"(see the rsync man page for an explanation)\n");
75                 exit_cleanup(RERR_PROTOCOL);
76         }
77         if (remote_protocol < OLD_PROTOCOL_VERSION) {
78                 rprintf(FINFO,"%s is very old version of rsync, upgrade recommended.\n",
79                         am_server? "Client" : "Server");
80         }
81         if (protocol_version < MIN_PROTOCOL_VERSION) {
82                 rprintf(FERROR, "--protocol must be at least %d on the %s.\n",
83                         MIN_PROTOCOL_VERSION, am_server? "Server" : "Client");
84                 exit_cleanup(RERR_PROTOCOL);
85         }
86         if (protocol_version > PROTOCOL_VERSION) {
87                 rprintf(FERROR, "--protocol must be no more than %d on the %s.\n",
88                         PROTOCOL_VERSION, am_server? "Server" : "Client");
89                 exit_cleanup(RERR_PROTOCOL);
90         }
91
92         if (protocol_version < 30) {
93                 if (max_delete == 0 && am_sender) {
94                         rprintf(FERROR,
95                             "--max-delete=0 requires protocol 30 or higher"
96                             " (negotiated %d).\n",
97                             protocol_version);
98                         exit_cleanup(RERR_PROTOCOL);
99                 }
100         }
101
102         if (protocol_version < 29) {
103                 if (fuzzy_basis) {
104                         rprintf(FERROR,
105                             "--fuzzy requires protocol 29 or higher"
106                             " (negotiated %d).\n",
107                             protocol_version);
108                         exit_cleanup(RERR_PROTOCOL);
109                 }
110
111                 if (basis_dir_cnt && inplace) {
112                         rprintf(FERROR,
113                             "%s with --inplace requires protocol 29 or higher"
114                             " (negotiated %d).\n",
115                             dest_option, protocol_version);
116                         exit_cleanup(RERR_PROTOCOL);
117                 }
118
119                 if (basis_dir_cnt > 1) {
120                         rprintf(FERROR,
121                             "Using more than one %s option requires protocol"
122                             " 29 or higher (negotiated %d).\n",
123                             dest_option, protocol_version);
124                         exit_cleanup(RERR_PROTOCOL);
125                 }
126
127                 if (prune_empty_dirs) {
128                         rprintf(FERROR,
129                             "--prune-empty-dirs requires protocol 29 or higher"
130                             " (negotiated %d).\n",
131                             protocol_version);
132                         exit_cleanup(RERR_PROTOCOL);
133                 }
134         }
135
136         if (am_server) {
137                 if (!checksum_seed)
138                         checksum_seed = time(NULL);
139                 write_int(f_out, checksum_seed);
140         } else {
141                 checksum_seed = read_int(f_in);
142         }
143 }