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