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