1 /* -*- c-file-style: "linux" -*-
4 Batch utilities for rsync.
11 extern char *batch_prefix;
12 extern int csum_length;
13 extern int protocol_version;
14 extern struct stats stats;
16 struct file_list *batch_flist;
18 static char rsync_flist_file[] = ".rsync_flist";
19 static char rsync_csums_file[] = ".rsync_csums";
20 static char rsync_delta_file[] = ".rsync_delta";
21 static char rsync_argvs_file[] = ".rsync_argvs";
23 static int f_csums = -1;
24 static int f_delta = -1;
26 void write_batch_flist_info(int flist_count, struct file_struct **files)
28 char filename[MAXPATHLEN];
32 stringjoin(filename, sizeof filename,
33 batch_prefix, rsync_flist_file, NULL);
35 f = do_open(filename, O_WRONLY|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE);
37 rprintf(FERROR, "Batch file %s open error: %s\n",
38 filename, strerror(errno));
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);
48 for (i = 0; i < flist_count; i++) {
49 send_file_entry(files[i], f,
50 files[i]->flags & FLAG_TOP_DIR ? XMIT_TOP_DIR : 0);
52 send_file_entry(NULL, f, 0);
54 protocol_version = save_pv;
55 stats.total_written = save_written;
61 void write_batch_argvs_file(int argc, char *argv[])
65 char buff[256]; /* XXX */
66 char buff2[MAXPATHLEN + 6];
67 char filename[MAXPATHLEN];
69 stringjoin(filename, sizeof filename,
70 batch_prefix, rsync_argvs_file, NULL);
72 f = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
73 S_IREAD | S_IWRITE | S_IEXEC);
75 rprintf(FERROR, "Batch file %s open error: %s\n",
76 filename, strerror(errno));
81 /* Write argvs info to batch file */
83 for (i = 0; i < argc; ++i) {
84 if (i == argc - 2) /* Skip source directory on cmdline */
88 * I think directly manipulating argv[] is probably bogus
90 if (!strncmp(argv[i], "--write-batch",
91 strlen("--write-batch"))) {
92 /* Safer to change it here than script */
94 * Change to --read-batch=prefix
95 * to get ready for remote
97 strlcat(buff, "--read-batch=", sizeof buff);
98 strlcat(buff, batch_prefix, sizeof buff);
101 snprintf(buff2, sizeof buff2, "${1:-%s}", argv[i]);
102 strlcat(buff, buff2, sizeof buff);
105 strlcat(buff, argv[i], sizeof buff);
108 if (i < (argc - 1)) {
109 strlcat(buff, " ", sizeof buff);
112 strlcat(buff, "\n", sizeof buff);
113 if (!write(f, buff, strlen(buff))) {
114 rprintf(FERROR, "Batch file %s write error: %s\n",
115 filename, strerror(errno));
122 struct file_list *create_flist_from_batch(void)
124 char filename[MAXPATHLEN];
125 unsigned short flags;
129 stringjoin(filename, sizeof filename,
130 batch_prefix, rsync_flist_file, NULL);
132 f = do_open(filename, O_RDONLY, 0);
134 rprintf(FERROR, "Batch file %s open error: %s\n",
135 filename, strerror(errno));
139 batch_flist = new(struct file_list);
141 out_of_memory("create_flist_from_batch");
143 save_read = stats.total_read;
144 save_pv = protocol_version;
145 protocol_version = read_int(f);
147 batch_flist->count = read_int(f);
148 flist_expand(batch_flist);
150 for (i = 0; (flags = read_byte(f)) != 0; i++) {
151 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
152 flags |= read_byte(f) << 8;
153 receive_file_entry(&batch_flist->files[i], flags, f);
155 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
157 protocol_version = save_pv;
158 stats.total_read = save_read;
163 void write_batch_csums_file(void *buff, int bytes_to_write)
165 if (write(f_csums, buff, bytes_to_write) < 0) {
166 rprintf(FERROR, "Batch file write error: %s\n",
173 void close_batch_csums_file(void)
181 * Write csum info to batch file
183 * @todo This will break if s->count is ever larger than maxint. The
184 * batch code should probably be changed to consistently use the
185 * variable-length integer routines, which is probably a compatible
188 void write_batch_csum_info(int *flist_entry, struct sum_struct *s)
192 char filename[MAXPATHLEN];
195 stringjoin(filename, sizeof filename,
196 batch_prefix, rsync_csums_file, NULL);
198 f_csums = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
201 rprintf(FERROR, "Batch file %s open error: %s\n",
202 filename, strerror(errno));
208 write_batch_csums_file(flist_entry, sizeof (int));
209 int_count = s ? (int) s->count : 0;
210 write_batch_csums_file(&int_count, sizeof int_count);
213 for (i = 0; i < s->count; i++) {
214 write_batch_csums_file(&s->sums[i].sum1,
216 write_batch_csums_file(s->sums[i].sum2, csum_length);
221 int read_batch_csums_file(char *buff, int len)
225 if ((bytes_read = read(f_csums, buff, len)) < 0) {
226 rprintf(FERROR, "Batch file read error: %s\n", strerror(errno));
233 void read_batch_csum_info(int flist_entry, struct sum_struct *s,
234 int *checksums_match)
237 int file_flist_entry;
240 char file_sum2[SUM_LENGTH];
241 char filename[MAXPATHLEN];
244 stringjoin(filename, sizeof filename,
245 batch_prefix, rsync_csums_file, NULL);
247 f_csums = do_open(filename, O_RDONLY, 0);
249 rprintf(FERROR, "Batch file %s open error: %s\n",
250 filename, strerror(errno));
256 read_batch_csums_file((char *) &file_flist_entry, sizeof (int));
257 if (file_flist_entry != flist_entry) {
258 rprintf(FINFO, "file_flist_entry (%d) != flist_entry (%d)\n",
259 file_flist_entry, flist_entry);
264 read_batch_csums_file((char *) &file_chunk_ct, sizeof (int));
265 *checksums_match = 1;
266 for (i = 0; i < file_chunk_ct; i++) {
267 read_batch_csums_file((char *) &file_sum1,
269 read_batch_csums_file(file_sum2, csum_length);
271 if ((s->sums[i].sum1 != file_sum1)
272 || memcmp(s->sums[i].sum2, file_sum2, csum_length))
273 *checksums_match = 0;
278 void write_batch_delta_file(char *buff, int bytes_to_write)
280 char filename[MAXPATHLEN];
283 stringjoin(filename, sizeof filename,
284 batch_prefix, rsync_delta_file, NULL);
286 f_delta = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
289 rprintf(FERROR, "Batch file %s open error: %s\n",
290 filename, strerror(errno));
295 if (write(f_delta, buff, bytes_to_write) < 0) {
296 rprintf(FERROR, "Batch file %s write error: %s\n",
297 filename, strerror(errno));
303 void close_batch_delta_file(void)
309 int read_batch_delta_file(char *buff, int len)
312 char filename[MAXPATHLEN];
315 stringjoin(filename, sizeof filename,
316 batch_prefix, rsync_delta_file, NULL);
318 f_delta = do_open(filename, O_RDONLY, 0);
320 rprintf(FERROR, "Batch file %s open error: %s\n",
321 filename, strerror(errno));
327 bytes_read = read(f_delta, buff, len);
328 if (bytes_read < 0) {
329 rprintf(FERROR, "Batch file %s read error: %s\n",
330 filename, strerror(errno));
338 void show_flist(int index, struct file_struct **fptr)
340 /* for debugging show_flist(flist->count, flist->files * */
343 for (i = 0; i < index; i++) {
344 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
345 rprintf(FINFO, "flist->modtime=%#lx\n",
346 (long unsigned) fptr[i]->modtime);
347 rprintf(FINFO, "flist->length=%.0f\n",
348 (double) fptr[i]->length);
349 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
350 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
351 if (fptr[i]->dirname)
352 rprintf(FINFO, "flist->dirname=%s\n",
354 if (fptr[i]->basedir)
355 rprintf(FINFO, "flist->basedir=%s\n",
360 void show_argvs(int argc, char *argv[])
362 /* for debugging * */
365 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
366 for (i = 0; i < argc; i++) {
368 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);