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[])
63 char filename[MAXPATHLEN];
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);
70 rsyserr(FERROR, errno, "Batch file %s open error", filename);
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 */
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]);
90 write(fd, "${1:-", 5);
91 write(fd, p, strlen(p));
94 write(fd, argv[i], strlen(argv[i]));
96 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
97 rsyserr(FERROR, errno, "Batch file %s write error", filename);
102 struct file_list *create_flist_from_batch(void)
104 char filename[MAXPATHLEN];
105 unsigned short flags;
109 stringjoin(filename, sizeof filename,
110 batch_prefix, rsync_flist_file, NULL);
112 f = do_open(filename, O_RDONLY, 0);
114 rsyserr(FERROR, errno, "Batch file %s open error", filename);
118 batch_flist = flist_new(WITH_HLINK, "create_flist_from_batch");
120 save_read = stats.total_read;
121 save_pv = protocol_version;
122 protocol_version = read_int(f);
124 batch_flist->count = read_int(f);
125 flist_expand(batch_flist);
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);
132 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
134 protocol_version = save_pv;
135 stats.total_read = save_read;
140 void write_batch_csums_file(void *buff, int bytes_to_write)
142 if (write(f_csums, buff, bytes_to_write) < 0) {
143 rsyserr(FERROR, errno, "Batch file write error");
149 void close_batch_csums_file(void)
157 * Write csum info to batch file
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
164 void write_batch_csum_info(int *flist_entry, struct sum_struct *s)
168 char filename[MAXPATHLEN];
171 stringjoin(filename, sizeof filename,
172 batch_prefix, rsync_csums_file, NULL);
174 f_csums = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
177 rsyserr(FERROR, errno, "Batch file %s open error",
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);
189 for (i = 0; i < s->count; i++) {
190 write_batch_csums_file(&s->sums[i].sum1,
192 write_batch_csums_file(s->sums[i].sum2, csum_length);
197 int read_batch_csums_file(char *buff, int len)
201 if ((bytes_read = read(f_csums, buff, len)) < 0) {
202 rsyserr(FERROR, errno, "Batch file read error");
209 void read_batch_csum_info(int flist_entry, struct sum_struct *s,
210 int *checksums_match)
213 int file_flist_entry;
216 char file_sum2[SUM_LENGTH];
217 char filename[MAXPATHLEN];
220 stringjoin(filename, sizeof filename,
221 batch_prefix, rsync_csums_file, NULL);
223 f_csums = do_open(filename, O_RDONLY, 0);
225 rsyserr(FERROR, errno, "Batch file %s open error",
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);
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,
245 read_batch_csums_file(file_sum2, csum_length);
247 if ((s->sums[i].sum1 != file_sum1)
248 || memcmp(s->sums[i].sum2, file_sum2, csum_length))
249 *checksums_match = 0;
254 void write_batch_delta_file(char *buff, int bytes_to_write)
256 char filename[MAXPATHLEN];
259 stringjoin(filename, sizeof filename,
260 batch_prefix, rsync_delta_file, NULL);
262 f_delta = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
265 rsyserr(FERROR, errno, "Batch file %s open error",
271 if (write(f_delta, buff, bytes_to_write) < 0) {
272 rsyserr(FERROR, errno, "Batch file %s write error", filename);
278 void close_batch_delta_file(void)
284 int read_batch_delta_file(char *buff, int len)
287 char filename[MAXPATHLEN];
290 stringjoin(filename, sizeof filename,
291 batch_prefix, rsync_delta_file, NULL);
293 f_delta = do_open(filename, O_RDONLY, 0);
295 rsyserr(FERROR, errno, "Batch file %s open error",
302 bytes_read = read(f_delta, buff, len);
303 if (bytes_read < 0) {
304 rsyserr(FERROR, errno, "Batch file %s read error", filename);
312 void show_flist(int index, struct file_struct **fptr)
314 /* for debugging show_flist(flist->count, flist->files * */
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",
328 if (fptr[i]->basedir)
329 rprintf(FINFO, "flist->basedir=%s\n",
334 void show_argvs(int argc, char *argv[])
336 /* for debugging * */
339 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
340 for (i = 0; i < argc; i++) {
342 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);