Mention recent changes.
[rsync/rsync.git] / batch.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux" -*-
2
3 Weiss 1/1999
4 Batch utilities for rsync.
5
6*/
7
8#include "rsync.h"
9#include <time.h>
10
11extern char *batch_prefix;
12extern int csum_length;
13extern int protocol_version;
14extern struct stats stats;
15
16struct file_list *batch_flist;
17
18static char rsync_flist_file[] = ".rsync_flist";
19static char rsync_csums_file[] = ".rsync_csums";
20static char rsync_delta_file[] = ".rsync_delta";
21static char rsync_argvs_file[] = ".rsync_argvs";
22
23static int f_csums = -1;
24static int f_delta = -1;
25
26void write_batch_flist_info(int flist_count, struct file_struct **files)
27{
28 char filename[MAXPATHLEN];
29 int i, f, save_pv;
30 int64 save_written;
31
32 stringjoin(filename, sizeof filename,
33 batch_prefix, rsync_flist_file, NULL);
34
35 f = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
36 if (f < 0) {
37 rsyserr(FERROR, errno, "Batch file %s open error", filename);
38 exit_cleanup(1);
39 }
40
41 save_written = stats.total_written;
42 save_pv = protocol_version;
43 protocol_version = PROTOCOL_VERSION;
44 write_int(f, protocol_version);
45 write_int(f, flist_count);
46
47 for (i = 0; i < flist_count; i++) {
48 send_file_entry(files[i], f,
49 files[i]->flags & FLAG_TOP_DIR ? XMIT_TOP_DIR : 0);
50 }
51 send_file_entry(NULL, f, 0);
52
53 protocol_version = save_pv;
54 stats.total_written = save_written;
55
56 close(f);
57}
58
59
60void write_batch_argvs_file(int argc, char *argv[])
61{
62 int fd, i;
63 char filename[MAXPATHLEN];
64
65 stringjoin(filename, sizeof filename,
66 batch_prefix, rsync_argvs_file, NULL);
67 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
68 S_IRUSR | S_IWUSR | S_IEXEC);
69 if (fd < 0) {
70 rsyserr(FERROR, errno, "Batch file %s open error", filename);
71 exit_cleanup(1);
72 }
73
74 /* Write argvs info to BATCH.rsync_argvs file */
75 for (i = 0; i < argc; i++) {
76 if (i == argc - 2) /* Skip source directory on cmdline */
77 continue;
78 if (i != 0)
79 write(fd, " ", 1);
80 if (!strncmp(argv[i], "--write-batch=", 14)) {
81 write(fd, "--read-batch=", 13);
82 write(fd, batch_prefix, strlen(batch_prefix));
83 } else if (i == argc - 1) {
84 char *p = find_colon(argv[i]);
85 if (p) {
86 if (*++p == ':')
87 p++;
88 } else
89 p = argv[i];
90 write(fd, "${1:-", 5);
91 write(fd, p, strlen(p));
92 write(fd, "}", 1);
93 } else
94 write(fd, argv[i], strlen(argv[i]));
95 }
96 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
97 rsyserr(FERROR, errno, "Batch file %s write error", filename);
98 exit_cleanup(1);
99 }
100}
101
102struct file_list *create_flist_from_batch(void)
103{
104 char filename[MAXPATHLEN];
105 unsigned short flags;
106 int i, f, save_pv;
107 int64 save_read;
108
109 stringjoin(filename, sizeof filename,
110 batch_prefix, rsync_flist_file, NULL);
111
112 f = do_open(filename, O_RDONLY, 0);
113 if (f < 0) {
114 rsyserr(FERROR, errno, "Batch file %s open error", filename);
115 exit_cleanup(1);
116 }
117
118 batch_flist = flist_new(WITH_HLINK, "create_flist_from_batch");
119
120 save_read = stats.total_read;
121 save_pv = protocol_version;
122 protocol_version = read_int(f);
123
124 batch_flist->count = read_int(f);
125 flist_expand(batch_flist);
126
127 for (i = 0; (flags = read_byte(f)) != 0; i++) {
128 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
129 flags |= read_byte(f) << 8;
130 receive_file_entry(&batch_flist->files[i], flags, batch_flist, f);
131 }
132 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
133
134 protocol_version = save_pv;
135 stats.total_read = save_read;
136
137 return batch_flist;
138}
139
140void write_batch_csums_file(void *buff, int bytes_to_write)
141{
142 if (write(f_csums, buff, bytes_to_write) < 0) {
143 rsyserr(FERROR, errno, "Batch file write error");
144 close(f_csums);
145 exit_cleanup(1);
146 }
147}
148
149void close_batch_csums_file(void)
150{
151 close(f_csums);
152 f_csums = -1;
153}
154
155
156/**
157 * Write csum info to batch file
158 *
159 * @todo This will break if s->count is ever larger than maxint. The
160 * batch code should probably be changed to consistently use the
161 * variable-length integer routines, which is probably a compatible
162 * change.
163 **/
164void write_batch_csum_info(int *flist_entry, struct sum_struct *s)
165{
166 size_t i;
167 int int_count;
168 char filename[MAXPATHLEN];
169
170 if (f_csums < 0) {
171 stringjoin(filename, sizeof filename,
172 batch_prefix, rsync_csums_file, NULL);
173
174 f_csums = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
175 S_IRUSR | S_IWUSR);
176 if (f_csums < 0) {
177 rsyserr(FERROR, errno, "Batch file %s open error",
178 filename);
179 close(f_csums);
180 exit_cleanup(1);
181 }
182 }
183
184 write_batch_csums_file(flist_entry, sizeof (int));
185 int_count = s ? (int) s->count : 0;
186 write_batch_csums_file(&int_count, sizeof int_count);
187
188 if (s) {
189 for (i = 0; i < s->count; i++) {
190 write_batch_csums_file(&s->sums[i].sum1,
191 sizeof (uint32));
192 write_batch_csums_file(s->sums[i].sum2, csum_length);
193 }
194 }
195}
196
197int read_batch_csums_file(char *buff, int len)
198{
199 int bytes_read;
200
201 if ((bytes_read = read(f_csums, buff, len)) < 0) {
202 rsyserr(FERROR, errno, "Batch file read error");
203 close(f_csums);
204 exit_cleanup(1);
205 }
206 return bytes_read;
207}
208
209void read_batch_csum_info(int flist_entry, struct sum_struct *s,
210 int *checksums_match)
211{
212 int i;
213 int file_flist_entry;
214 int file_chunk_ct;
215 uint32 file_sum1;
216 char file_sum2[SUM_LENGTH];
217 char filename[MAXPATHLEN];
218
219 if (f_csums < 0) {
220 stringjoin(filename, sizeof filename,
221 batch_prefix, rsync_csums_file, NULL);
222
223 f_csums = do_open(filename, O_RDONLY, 0);
224 if (f_csums < 0) {
225 rsyserr(FERROR, errno, "Batch file %s open error",
226 filename);
227 close(f_csums);
228 exit_cleanup(1);
229 }
230 }
231
232 read_batch_csums_file((char *) &file_flist_entry, sizeof (int));
233 if (file_flist_entry != flist_entry) {
234 rprintf(FINFO, "file_flist_entry (%d) != flist_entry (%d)\n",
235 file_flist_entry, flist_entry);
236 close(f_csums);
237 exit_cleanup(1);
238
239 } else {
240 read_batch_csums_file((char *) &file_chunk_ct, sizeof (int));
241 *checksums_match = 1;
242 for (i = 0; i < file_chunk_ct; i++) {
243 read_batch_csums_file((char *) &file_sum1,
244 sizeof (uint32));
245 read_batch_csums_file(file_sum2, csum_length);
246
247 if ((s->sums[i].sum1 != file_sum1)
248 || memcmp(s->sums[i].sum2, file_sum2, csum_length))
249 *checksums_match = 0;
250 } /* end for */
251 }
252}
253
254void write_batch_delta_file(char *buff, int bytes_to_write)
255{
256 char filename[MAXPATHLEN];
257
258 if (f_delta < 0) {
259 stringjoin(filename, sizeof filename,
260 batch_prefix, rsync_delta_file, NULL);
261
262 f_delta = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
263 S_IRUSR | S_IWUSR);
264 if (f_delta < 0) {
265 rsyserr(FERROR, errno, "Batch file %s open error",
266 filename);
267 exit_cleanup(1);
268 }
269 }
270
271 if (write(f_delta, buff, bytes_to_write) < 0) {
272 rsyserr(FERROR, errno, "Batch file %s write error", filename);
273 close(f_delta);
274 exit_cleanup(1);
275 }
276}
277
278void close_batch_delta_file(void)
279{
280 close(f_delta);
281 f_delta = -1;
282}
283
284int read_batch_delta_file(char *buff, int len)
285{
286 int bytes_read;
287 char filename[MAXPATHLEN];
288
289 if (f_delta < 0) {
290 stringjoin(filename, sizeof filename,
291 batch_prefix, rsync_delta_file, NULL);
292
293 f_delta = do_open(filename, O_RDONLY, 0);
294 if (f_delta < 0) {
295 rsyserr(FERROR, errno, "Batch file %s open error",
296 filename);
297 close(f_delta);
298 exit_cleanup(1);
299 }
300 }
301
302 bytes_read = read(f_delta, buff, len);
303 if (bytes_read < 0) {
304 rsyserr(FERROR, errno, "Batch file %s read error", filename);
305 close(f_delta);
306 exit_cleanup(1);
307 }
308
309 return bytes_read;
310}
311
312void show_flist(int index, struct file_struct **fptr)
313{
314 /* for debugging show_flist(flist->count, flist->files * */
315
316 int i;
317 for (i = 0; i < index; i++) {
318 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
319 rprintf(FINFO, "flist->modtime=%#lx\n",
320 (long unsigned) fptr[i]->modtime);
321 rprintf(FINFO, "flist->length=%.0f\n",
322 (double) fptr[i]->length);
323 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
324 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
325 if (fptr[i]->dirname)
326 rprintf(FINFO, "flist->dirname=%s\n",
327 fptr[i]->dirname);
328 if (fptr[i]->basedir)
329 rprintf(FINFO, "flist->basedir=%s\n",
330 fptr[i]->basedir);
331 }
332}
333
334void show_argvs(int argc, char *argv[])
335{
336 /* for debugging * */
337
338 int i;
339 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
340 for (i = 0; i < argc; i++) {
341 /* if (argv[i]) */
342 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
343
344 }
345}