added csum-length option
[rsync/rsync.git] / compat.c
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
24 extern int csum_length;
25
26 extern int preserve_links;
27 extern int preserve_perms;
28 extern int preserve_devices;
29 extern int preserve_uid;
30 extern int preserve_gid;
31 extern int preserve_times;
32 extern int always_checksum;
33
34
35 extern int remote_version;
36
37  void (*send_file_entry)(struct file_struct *file,int f) = NULL;
38  void (*receive_file_entry)(struct file_struct *file,
39                             unsigned char flags,int f) = NULL;
40
41
42 void send_file_entry_v10(struct file_struct *file,int f)
43 {
44   unsigned char flags;
45   static mode_t last_mode=0;
46   static dev_t last_dev=0;
47   static uid_t last_uid=0;
48   static gid_t last_gid=0;
49   static char lastdir[MAXPATHLEN]="";
50   char *p=NULL;
51
52   if (f == -1) return;
53
54   if (!file) {
55     write_byte(f,0);
56     return;
57   }
58
59   flags = FILE_VALID;
60
61   if (file->mode == last_mode) flags |= SAME_MODE;
62   if (file->dev == last_dev) flags |= SAME_DEV;
63   if (file->uid == last_uid) flags |= SAME_UID;
64   if (file->gid == last_gid) flags |= SAME_GID;
65     
66   if (strncmp(file->name,lastdir,strlen(lastdir)) == 0) {
67     flags |= SAME_DIR;
68     p = file->name + strlen(lastdir);
69   } else {
70     p = file->name;
71   }
72
73   write_byte(f,flags);
74   if (flags & SAME_DIR)
75     write_byte(f,strlen(p));
76   else
77     write_int(f,strlen(p));
78   write_buf(f,p,strlen(p));
79   write_int(f,(int)file->modtime);
80   write_int(f,(int)file->length);
81   if (!(flags & SAME_MODE))
82     write_int(f,(int)file->mode);
83   if (preserve_uid && !(flags & SAME_UID))
84     write_int(f,(int)file->uid);
85   if (preserve_gid && !(flags & SAME_GID))
86     write_int(f,(int)file->gid);
87   if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_DEV))
88     write_int(f,(int)file->dev);
89
90 #if SUPPORT_LINKS
91   if (preserve_links && S_ISLNK(file->mode)) {
92     write_int(f,strlen(file->link));
93     write_buf(f,file->link,strlen(file->link));
94   }
95 #endif
96
97   if (always_checksum) {
98     write_buf(f,file->sum,csum_length);
99   }       
100
101   last_mode = file->mode;
102   last_dev = file->dev;
103   last_uid = file->uid;
104   last_gid = file->gid;
105   p = strrchr(file->name,'/');
106   if (p) {
107     int l = (int)(p - file->name) + 1;
108     strncpy(lastdir,file->name,l);
109     lastdir[l] = 0;
110   } else {
111     strcpy(lastdir,"");
112   }
113 }
114
115
116
117 void receive_file_entry_v10(struct file_struct *file,
118                             unsigned char flags,int f)
119 {
120   static mode_t last_mode=0;
121   static dev_t last_dev=0;
122   static uid_t last_uid=0;
123   static gid_t last_gid=0;
124   static char lastdir[MAXPATHLEN]="";
125   char *p=NULL;
126   int l1,l2;
127
128   if (flags & SAME_DIR) {
129     l1 = read_byte(f);
130     l2 = strlen(lastdir);
131   } else {
132     l1 = read_int(f);
133     l2 = 0;
134   }
135
136   file->name = (char *)malloc(l1+l2+1);
137   if (!file->name) out_of_memory("receive_file_entry");
138
139   strncpy(file->name,lastdir,l2);
140   read_buf(f,file->name+l2,l1);
141   file->name[l1+l2] = 0;
142
143   file->modtime = (time_t)read_int(f);
144   file->length = (off_t)read_int(f);
145   file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
146   if (preserve_uid)
147     file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
148   if (preserve_gid)
149     file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
150   if (preserve_devices && IS_DEVICE(file->mode))
151     file->dev = (flags & SAME_DEV) ? last_dev : (dev_t)read_int(f);
152
153 #if SUPPORT_LINKS
154   if (preserve_links && S_ISLNK(file->mode)) {
155     int l = read_int(f);
156     file->link = (char *)malloc(l+1);
157     if (!file->link) out_of_memory("receive_file_entry");
158     read_buf(f,file->link,l);
159     file->link[l] = 0;
160   }
161 #endif
162   
163   if (always_checksum)
164     read_buf(f,file->sum,csum_length);
165   
166   last_mode = file->mode;
167   last_dev = file->dev;
168   last_uid = file->uid;
169   last_gid = file->gid;
170   p = strrchr(file->name,'/');
171   if (p) {
172     int l = (int)(p - file->name) + 1;
173     strncpy(lastdir,file->name,l);
174     lastdir[l] = 0;
175   } else {
176     strcpy(lastdir,"");
177   }
178 }
179
180
181
182
183 void setup_protocol(void)
184 {
185   if (remote_version == 10) {
186     send_file_entry = send_file_entry_v10;
187     receive_file_entry = receive_file_entry_v10;
188   } else {
189     send_file_entry = send_file_entry_v11;
190     receive_file_entry = receive_file_entry_v11;
191   }
192 }
193