Added LIVE_FLAGS define.
[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 snprintf(filename, sizeof filename, "%s%s",
33 batch_prefix, rsync_flist_file);
34
35 f = do_open(filename, O_WRONLY|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE);
36 if (f < 0) {
37 rprintf(FERROR, "Batch file %s open error: %s\n",
38 filename, strerror(errno));
39 exit_cleanup(1);
40 }
41
42 save_written = stats.total_written;
43 save_pv = protocol_version;
44 protocol_version = PROTOCOL_VERSION;
45 write_int(f, protocol_version);
46 write_int(f, flist_count);
47
48 reset_file_entry_vars();
49 for (i = 0; i < flist_count; i++)
50 send_file_entry(files[i], f, files[i]->flags & FLAG_DELETE);
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 f;
63 int i;
64 char buff[256]; /* XXX */
65 char buff2[MAXPATHLEN + 6];
66 char filename[MAXPATHLEN];
67
68 /* Set up file extension */
69 strlcpy(filename, batch_prefix, sizeof(filename));
70 strlcat(filename, rsync_argvs_file, sizeof(filename));
71
72 /* Open batch argvs file for writing;
73 * create it if it doesn't exist. */
74 f = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
75 S_IREAD | S_IWRITE | S_IEXEC);
76 if (f < 0) {
77 rprintf(FERROR, "Batch file %s open error: %s\n",
78 filename, strerror(errno));
79 exit_cleanup(1);
80 }
81 buff[0] = '\0';
82
83 /* Write argvs info to batch file */
84
85 for (i = 0; i < argc; ++i) {
86 if (i == argc - 2) /* Skip source directory on cmdline */
87 continue;
88 /*
89 * FIXME:
90 * I think directly manipulating argv[] is probably bogus
91 */
92 if (!strncmp(argv[i], "--write-batch",
93 strlen("--write-batch"))) {
94 /* Safer to change it here than script */
95 /*
96 * Change to --read-batch=prefix
97 * to get ready for remote
98 */
99 strlcat(buff, "--read-batch=", sizeof(buff));
100 strlcat(buff, batch_prefix, sizeof(buff));
101 } else
102 if (i == argc - 1) {
103 snprintf(buff2, sizeof(buff2), "${1:-%s}", argv[i]);
104 strlcat(buff, buff2, sizeof(buff));
105 }
106 else {
107 strlcat(buff, argv[i], sizeof(buff));
108 }
109
110 if (i < (argc - 1)) {
111 strlcat(buff, " ", sizeof(buff));
112 }
113 }
114 strlcat(buff, "\n", sizeof(buff));
115 if (!write(f, buff, strlen(buff))) {
116 rprintf(FERROR, "Batch file %s write error: %s\n",
117 filename, strerror(errno));
118 close(f);
119 exit_cleanup(1);
120 }
121 close(f);
122}
123
124struct file_list *create_flist_from_batch(void)
125{
126 char filename[MAXPATHLEN];
127 unsigned short flags;
128 int i, f, save_pv;
129 int64 save_read;
130
131 snprintf(filename, sizeof filename, "%s%s",
132 batch_prefix, rsync_flist_file);
133
134 f = do_open(filename, O_RDONLY, 0);
135 if (f < 0) {
136 rprintf(FERROR, "Batch file %s open error: %s\n",
137 filename, strerror(errno));
138 exit_cleanup(1);
139 }
140
141 batch_flist = new(struct file_list);
142 if (!batch_flist)
143 out_of_memory("create_flist_from_batch");
144
145 save_read = stats.total_read;
146 save_pv = protocol_version;
147 protocol_version = read_int(f);
148
149 batch_flist->count = batch_flist->malloced = read_int(f);
150 batch_flist->files = new_array(struct file_struct *,
151 batch_flist->malloced);
152 if (!batch_flist->files)
153 out_of_memory("create_flist_from_batch");
154
155 for (i = 0; (flags = read_byte(f)) != 0; i++) {
156 if (protocol_version >= 28 && (flags & EXTENDED_FLAGS))
157 flags |= read_byte(f) << 8;
158 receive_file_entry(&batch_flist->files[i], flags, f);
159 }
160 reset_file_entry_vars();
161
162 protocol_version = save_pv;
163 stats.total_read = save_read;
164
165 return batch_flist;
166}
167
168void write_batch_csums_file(void *buff, int bytes_to_write)
169{
170 char filename[MAXPATHLEN];
171
172 if (f_csums < 0) {
173 strlcpy(filename, batch_prefix, sizeof(filename));
174 strlcat(filename, rsync_csums_file, sizeof(filename));
175
176 f_csums = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
177 S_IREAD | S_IWRITE);
178 if (f_csums < 0) {
179 rprintf(FERROR, "Batch file %s open error: %s\n",
180 filename, strerror(errno));
181 close(f_csums);
182 exit_cleanup(1);
183 }
184 }
185
186 if (write(f_csums, buff, bytes_to_write) < 0) {
187 rprintf(FERROR, "Batch file %s write error: %s\n",
188 filename, strerror(errno));
189 close(f_csums);
190 exit_cleanup(1);
191 }
192}
193
194void close_batch_csums_file(void)
195{
196 close(f_csums);
197 f_csums = -1;
198}
199
200
201/**
202 * Write csum info to batch file
203 *
204 * @todo This will break if s->count is ever larger than maxint. The
205 * batch code should probably be changed to consistently use the
206 * variable-length integer routines, which is probably a compatible
207 * change.
208 **/
209void write_batch_csum_info(int *flist_entry, struct sum_struct *s)
210{
211 size_t i;
212 int int_count;
213
214 write_batch_csums_file(flist_entry, sizeof(int));
215 int_count = s ? (int) s->count : 0;
216 write_batch_csums_file(&int_count, sizeof int_count);
217
218 if (s) {
219 for (i = 0; i < s->count; i++) {
220 write_batch_csums_file(&s->sums[i].sum1, sizeof(uint32));
221 write_batch_csums_file(s->sums[i].sum2, csum_length);
222 }
223 }
224}
225
226int read_batch_csums_file(char *buff, int len)
227{
228 int bytes_read;
229 char filename[MAXPATHLEN];
230
231 if (f_csums < 0) {
232 strlcpy(filename, batch_prefix, sizeof(filename));
233 strlcat(filename, rsync_csums_file, sizeof(filename));
234
235 f_csums = do_open(filename, O_RDONLY, 0);
236 if (f_csums < 0) {
237 rprintf(FERROR, "Batch file %s open error: %s\n",
238 filename, strerror(errno));
239 close(f_csums);
240 exit_cleanup(1);
241 }
242 }
243
244 bytes_read = read(f_csums, buff, len);
245 if (bytes_read < 0) {
246 rprintf(FERROR, "Batch file %s read error: %s\n",
247 filename, strerror(errno));
248 close(f_csums);
249 exit_cleanup(1);
250 }
251
252 return bytes_read;
253}
254
255void read_batch_csum_info(int flist_entry, struct sum_struct *s,
256 int *checksums_match)
257{
258 int i;
259 int file_flist_entry;
260 int file_chunk_ct;
261 uint32 file_sum1;
262 char file_sum2[SUM_LENGTH];
263
264 read_batch_csums_file((char *) &file_flist_entry, sizeof(int));
265 if (file_flist_entry != flist_entry) {
266 rprintf(FINFO, "file_flist_entry (%d) != flist_entry (%d)\n",
267 file_flist_entry, flist_entry);
268 close(f_csums);
269 exit_cleanup(1);
270
271 } else {
272 read_batch_csums_file((char *) &file_chunk_ct,
273 sizeof(int));
274 *checksums_match = 1;
275 for (i = 0; i < file_chunk_ct; i++) {
276
277 read_batch_csums_file((char *) &file_sum1,
278 sizeof(uint32));
279 read_batch_csums_file(file_sum2, csum_length);
280
281 if ((s->sums[i].sum1 != file_sum1) ||
282 (memcmp(s->sums[i].sum2, file_sum2, csum_length)
283 != 0)) {
284 *checksums_match = 0;
285 }
286 } /* end for */
287 }
288}
289
290void write_batch_delta_file(char *buff, int bytes_to_write)
291{
292 char filename[MAXPATHLEN];
293
294 if (f_delta < 0) {
295 strlcpy(filename, batch_prefix, sizeof(filename));
296 strlcat(filename, rsync_delta_file, sizeof(filename));
297
298 f_delta = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
299 S_IREAD | S_IWRITE);
300 if (f_delta < 0) {
301 rprintf(FERROR, "Batch file %s open error: %s\n",
302 filename, strerror(errno));
303 exit_cleanup(1);
304 }
305 }
306
307 if (write(f_delta, buff, bytes_to_write) < 0) {
308 rprintf(FERROR, "Batch file %s write error: %s\n",
309 filename, strerror(errno));
310 close(f_delta);
311 exit_cleanup(1);
312 }
313}
314
315void close_batch_delta_file(void)
316{
317 close(f_delta);
318 f_delta = -1;
319}
320
321int read_batch_delta_file(char *buff, int len)
322{
323 int bytes_read;
324 char filename[MAXPATHLEN];
325
326 if (f_delta < 0) {
327 strlcpy(filename, batch_prefix, sizeof(filename));
328 strlcat(filename, rsync_delta_file, sizeof(filename));
329
330 f_delta = do_open(filename, O_RDONLY, 0);
331 if (f_delta < 0) {
332 rprintf(FERROR, "Batch file %s open error: %s\n",
333 filename, strerror(errno));
334 close(f_delta);
335 exit_cleanup(1);
336 }
337 }
338
339 bytes_read = read(f_delta, buff, len);
340 if (bytes_read < 0) {
341 rprintf(FERROR, "Batch file %s read error: %s\n",
342 filename, strerror(errno));
343 close(f_delta);
344 exit_cleanup(1);
345 }
346
347 return bytes_read;
348}
349
350void show_flist(int index, struct file_struct **fptr)
351{
352 /* for debugging show_flist(flist->count, flist->files * */
353
354 int i;
355 for (i = 0; i < index; i++) {
356 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
357 rprintf(FINFO, "flist->modtime=%#lx\n",
358 (long unsigned) fptr[i]->modtime);
359 rprintf(FINFO, "flist->length=%.0f\n",
360 (double) fptr[i]->length);
361 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
362 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
363 if (fptr[i]->dirname)
364 rprintf(FINFO, "flist->dirname=%s\n",
365 fptr[i]->dirname);
366 if (fptr[i]->basedir)
367 rprintf(FINFO, "flist->basedir=%s\n",
368 fptr[i]->basedir);
369 }
370}
371
372void show_argvs(int argc, char *argv[])
373{
374 /* for debugging * */
375
376 int i;
377 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
378 for (i = 0; i < argc; i++) {
379 /* if (argv[i]) */
380 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
381
382 }
383}