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_IRUSR | S_IWUSR);
37 rsyserr(FERROR, errno, "Batch file %s open error", filename);
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);
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);
51 send_file_entry(NULL, f, 0);
53 protocol_version = save_pv;
54 stats.total_written = save_written;
60 void write_batch_argvs_file(int argc, char *argv[])
64 char buff[256]; /* XXX */
65 char buff2[MAXPATHLEN + 6];
66 char filename[MAXPATHLEN];
68 stringjoin(filename, sizeof filename,
69 batch_prefix, rsync_argvs_file, NULL);
71 f = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
72 S_IRUSR | S_IWUSR | S_IEXEC);
74 rsyserr(FERROR, errno, "Batch file %s open error", filename);
79 /* Write argvs info to batch file */
81 for (i = 0; i < argc; ++i) {
82 if (i == argc - 2) /* Skip source directory on cmdline */
86 * I think directly manipulating argv[] is probably bogus
88 if (!strncmp(argv[i], "--write-batch",
89 strlen("--write-batch"))) {
90 /* Safer to change it here than script */
92 * Change to --read-batch=prefix
93 * to get ready for remote
95 strlcat(buff, "--read-batch=", sizeof buff);
96 strlcat(buff, batch_prefix, sizeof buff);
99 snprintf(buff2, sizeof buff2, "${1:-%s}", argv[i]);
100 strlcat(buff, buff2, sizeof buff);
103 strlcat(buff, argv[i], sizeof buff);
106 if (i < (argc - 1)) {
107 strlcat(buff, " ", sizeof buff);
110 strlcat(buff, "\n", sizeof buff);
111 if (!write(f, buff, strlen(buff))) {
112 rsyserr(FERROR, errno, "Batch file %s write error", filename);
119 struct file_list *create_flist_from_batch(void)
121 char filename[MAXPATHLEN];
122 unsigned short flags;
126 stringjoin(filename, sizeof filename,
127 batch_prefix, rsync_flist_file, NULL);
129 f = do_open(filename, O_RDONLY, 0);
131 rsyserr(FERROR, errno, "Batch file %s open error", filename);
135 batch_flist = flist_new(WITH_HLINK, "create_flist_from_batch");
137 save_read = stats.total_read;
138 save_pv = protocol_version;
139 protocol_version = read_int(f);
141 batch_flist->count = read_int(f);
142 flist_expand(batch_flist);
144 for (i = 0; (flags = read_byte(f)) != 0; i++) {
145 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
146 flags |= read_byte(f) << 8;
147 receive_file_entry(&batch_flist->files[i], flags, batch_flist, f);
149 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
151 protocol_version = save_pv;
152 stats.total_read = save_read;
157 void write_batch_csums_file(void *buff, int bytes_to_write)
159 if (write(f_csums, buff, bytes_to_write) < 0) {
160 rsyserr(FERROR, errno, "Batch file write error");
166 void close_batch_csums_file(void)
174 * Write csum info to batch file
176 * @todo This will break if s->count is ever larger than maxint. The
177 * batch code should probably be changed to consistently use the
178 * variable-length integer routines, which is probably a compatible
181 void write_batch_csum_info(int *flist_entry, struct sum_struct *s)
185 char filename[MAXPATHLEN];
188 stringjoin(filename, sizeof filename,
189 batch_prefix, rsync_csums_file, NULL);
191 f_csums = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
194 rsyserr(FERROR, errno, "Batch file %s open error",
201 write_batch_csums_file(flist_entry, sizeof (int));
202 int_count = s ? (int) s->count : 0;
203 write_batch_csums_file(&int_count, sizeof int_count);
206 for (i = 0; i < s->count; i++) {
207 write_batch_csums_file(&s->sums[i].sum1,
209 write_batch_csums_file(s->sums[i].sum2, csum_length);
214 int read_batch_csums_file(char *buff, int len)
218 if ((bytes_read = read(f_csums, buff, len)) < 0) {
219 rsyserr(FERROR, errno, "Batch file read error");
226 void read_batch_csum_info(int flist_entry, struct sum_struct *s,
227 int *checksums_match)
230 int file_flist_entry;
233 char file_sum2[SUM_LENGTH];
234 char filename[MAXPATHLEN];
237 stringjoin(filename, sizeof filename,
238 batch_prefix, rsync_csums_file, NULL);
240 f_csums = do_open(filename, O_RDONLY, 0);
242 rsyserr(FERROR, errno, "Batch file %s open error",
249 read_batch_csums_file((char *) &file_flist_entry, sizeof (int));
250 if (file_flist_entry != flist_entry) {
251 rprintf(FINFO, "file_flist_entry (%d) != flist_entry (%d)\n",
252 file_flist_entry, flist_entry);
257 read_batch_csums_file((char *) &file_chunk_ct, sizeof (int));
258 *checksums_match = 1;
259 for (i = 0; i < file_chunk_ct; i++) {
260 read_batch_csums_file((char *) &file_sum1,
262 read_batch_csums_file(file_sum2, csum_length);
264 if ((s->sums[i].sum1 != file_sum1)
265 || memcmp(s->sums[i].sum2, file_sum2, csum_length))
266 *checksums_match = 0;
271 void write_batch_delta_file(char *buff, int bytes_to_write)
273 char filename[MAXPATHLEN];
276 stringjoin(filename, sizeof filename,
277 batch_prefix, rsync_delta_file, NULL);
279 f_delta = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
282 rsyserr(FERROR, errno, "Batch file %s open error",
288 if (write(f_delta, buff, bytes_to_write) < 0) {
289 rsyserr(FERROR, errno, "Batch file %s write error", filename);
295 void close_batch_delta_file(void)
301 int read_batch_delta_file(char *buff, int len)
304 char filename[MAXPATHLEN];
307 stringjoin(filename, sizeof filename,
308 batch_prefix, rsync_delta_file, NULL);
310 f_delta = do_open(filename, O_RDONLY, 0);
312 rsyserr(FERROR, errno, "Batch file %s open error",
319 bytes_read = read(f_delta, buff, len);
320 if (bytes_read < 0) {
321 rsyserr(FERROR, errno, "Batch file %s read error", filename);
329 void show_flist(int index, struct file_struct **fptr)
331 /* for debugging show_flist(flist->count, flist->files * */
334 for (i = 0; i < index; i++) {
335 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
336 rprintf(FINFO, "flist->modtime=%#lx\n",
337 (long unsigned) fptr[i]->modtime);
338 rprintf(FINFO, "flist->length=%.0f\n",
339 (double) fptr[i]->length);
340 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
341 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
342 if (fptr[i]->dirname)
343 rprintf(FINFO, "flist->dirname=%s\n",
345 if (fptr[i]->basedir)
346 rprintf(FINFO, "flist->basedir=%s\n",
351 void show_argvs(int argc, char *argv[])
353 /* for debugging * */
356 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
357 for (i = 0; i < argc; i++) {
359 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);