save some more bytes by making the checksum smaller
[rsync/rsync.git] / compat.c
CommitLineData
4fe159a8
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/* compatability routines for older rsync protocol versions */
21
22#include "rsync.h"
23
aae43eb3
AT
24extern int am_server;
25
43a481dc
AT
26extern int csum_length;
27
4fe159a8
AT
28extern int preserve_links;
29extern int preserve_perms;
30extern int preserve_devices;
31extern int preserve_uid;
32extern int preserve_gid;
33extern int preserve_times;
34extern int always_checksum;
aae43eb3 35extern int checksum_seed;
4fe159a8
AT
36
37
38extern int remote_version;
39
40 void (*send_file_entry)(struct file_struct *file,int f) = NULL;
41 void (*receive_file_entry)(struct file_struct *file,
42 unsigned char flags,int f) = NULL;
43
44
45void send_file_entry_v10(struct file_struct *file,int f)
46{
47 unsigned char flags;
48 static mode_t last_mode=0;
dc5ddbcc 49 static dev_t last_rdev=0;
4fe159a8
AT
50 static uid_t last_uid=0;
51 static gid_t last_gid=0;
52 static char lastdir[MAXPATHLEN]="";
53 char *p=NULL;
54
55 if (f == -1) return;
56
57 if (!file) {
58 write_byte(f,0);
59 return;
60 }
61
62 flags = FILE_VALID;
63
64 if (file->mode == last_mode) flags |= SAME_MODE;
dc5ddbcc 65 if (file->rdev == last_rdev) flags |= SAME_RDEV;
4fe159a8
AT
66 if (file->uid == last_uid) flags |= SAME_UID;
67 if (file->gid == last_gid) flags |= SAME_GID;
68
69 if (strncmp(file->name,lastdir,strlen(lastdir)) == 0) {
70 flags |= SAME_DIR;
71 p = file->name + strlen(lastdir);
72 } else {
73 p = file->name;
74 }
75
76 write_byte(f,flags);
77 if (flags & SAME_DIR)
78 write_byte(f,strlen(p));
79 else
80 write_int(f,strlen(p));
81 write_buf(f,p,strlen(p));
82 write_int(f,(int)file->modtime);
83 write_int(f,(int)file->length);
84 if (!(flags & SAME_MODE))
85 write_int(f,(int)file->mode);
86 if (preserve_uid && !(flags & SAME_UID))
87 write_int(f,(int)file->uid);
88 if (preserve_gid && !(flags & SAME_GID))
89 write_int(f,(int)file->gid);
dc5ddbcc
AT
90 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
91 write_int(f,(int)file->rdev);
4fe159a8
AT
92
93#if SUPPORT_LINKS
94 if (preserve_links && S_ISLNK(file->mode)) {
95 write_int(f,strlen(file->link));
96 write_buf(f,file->link,strlen(file->link));
97 }
98#endif
99
100 if (always_checksum) {
43a481dc 101 write_buf(f,file->sum,csum_length);
4fe159a8
AT
102 }
103
104 last_mode = file->mode;
dc5ddbcc 105 last_rdev = file->rdev;
4fe159a8
AT
106 last_uid = file->uid;
107 last_gid = file->gid;
108 p = strrchr(file->name,'/');
109 if (p) {
110 int l = (int)(p - file->name) + 1;
111 strncpy(lastdir,file->name,l);
112 lastdir[l] = 0;
113 } else {
114 strcpy(lastdir,"");
115 }
116}
117
118
119
120void receive_file_entry_v10(struct file_struct *file,
121 unsigned char flags,int f)
122{
123 static mode_t last_mode=0;
dc5ddbcc 124 static dev_t last_rdev=0;
4fe159a8
AT
125 static uid_t last_uid=0;
126 static gid_t last_gid=0;
127 static char lastdir[MAXPATHLEN]="";
128 char *p=NULL;
129 int l1,l2;
130
131 if (flags & SAME_DIR) {
132 l1 = read_byte(f);
133 l2 = strlen(lastdir);
134 } else {
135 l1 = read_int(f);
136 l2 = 0;
137 }
138
139 file->name = (char *)malloc(l1+l2+1);
140 if (!file->name) out_of_memory("receive_file_entry");
141
142 strncpy(file->name,lastdir,l2);
143 read_buf(f,file->name+l2,l1);
144 file->name[l1+l2] = 0;
145
146 file->modtime = (time_t)read_int(f);
147 file->length = (off_t)read_int(f);
148 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
149 if (preserve_uid)
150 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
151 if (preserve_gid)
152 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
153 if (preserve_devices && IS_DEVICE(file->mode))
dc5ddbcc 154 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
4fe159a8
AT
155
156#if SUPPORT_LINKS
157 if (preserve_links && S_ISLNK(file->mode)) {
158 int l = read_int(f);
159 file->link = (char *)malloc(l+1);
160 if (!file->link) out_of_memory("receive_file_entry");
161 read_buf(f,file->link,l);
162 file->link[l] = 0;
163 }
164#endif
165
166 if (always_checksum)
43a481dc 167 read_buf(f,file->sum,csum_length);
4fe159a8
AT
168
169 last_mode = file->mode;
dc5ddbcc 170 last_rdev = file->rdev;
4fe159a8
AT
171 last_uid = file->uid;
172 last_gid = file->gid;
173 p = strrchr(file->name,'/');
174 if (p) {
175 int l = (int)(p - file->name) + 1;
176 strncpy(lastdir,file->name,l);
177 lastdir[l] = 0;
178 } else {
179 strcpy(lastdir,"");
180 }
181}
182
183
184
185
aae43eb3 186void setup_protocol(int f_out,int f_in)
4fe159a8 187{
aae43eb3
AT
188 if (am_server) {
189 remote_version = read_int(f_in);
190 write_int(f_out,PROTOCOL_VERSION);
191 write_flush(f_out);
192 } else {
193 write_int(f_out,PROTOCOL_VERSION);
194 write_flush(f_out);
195 remote_version = read_int(f_in);
196 }
197
198 if (remote_version < MIN_PROTOCOL_VERSION ||
199 remote_version > MAX_PROTOCOL_VERSION) {
200 fprintf(FERROR,"protocol version mismatch - is your shell clean?\n");
201 exit_cleanup(1);
202 }
203
4fe159a8
AT
204 if (remote_version == 10) {
205 send_file_entry = send_file_entry_v10;
206 receive_file_entry = receive_file_entry_v10;
207 } else {
208 send_file_entry = send_file_entry_v11;
209 receive_file_entry = receive_file_entry_v11;
210 }
aae43eb3
AT
211
212 if (remote_version >= 12) {
213 if (am_server) {
214 checksum_seed = time(NULL);
215 write_int(f_out,checksum_seed);
216 } else {
217 checksum_seed = read_int(f_in);
218 }
219 }
b98c7b81
AT
220
221 checksum_init();
4fe159a8
AT
222}
223