Updated a questioning comment.
[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 char filename[MAXPATHLEN];
170
171 if (f_csums < 0) {
172 strlcpy(filename, batch_prefix, sizeof(filename));
173 strlcat(filename, rsync_csums_file, sizeof(filename));
174
175 f_csums = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
176 S_IREAD | S_IWRITE);
177 if (f_csums < 0) {
178 rprintf(FERROR, "Batch file %s open error: %s\n",
179 filename, strerror(errno));
180 close(f_csums);
181 exit_cleanup(1);
182 }
183 }
184
185 if (write(f_csums, buff, bytes_to_write) < 0) {
186 rprintf(FERROR, "Batch file %s write error: %s\n",
187 filename, strerror(errno));
188 close(f_csums);
189 exit_cleanup(1);
190 }
191}
192
193void close_batch_csums_file(void)
194{
195 close(f_csums);
196 f_csums = -1;
197}
198
199
200/**
201 * Write csum info to batch file
202 *
203 * @todo This will break if s->count is ever larger than maxint. The
204 * batch code should probably be changed to consistently use the
205 * variable-length integer routines, which is probably a compatible
206 * change.
207 **/
208void write_batch_csum_info(int *flist_entry, struct sum_struct *s)
209{
210 size_t i;
211 int int_count;
212
213 write_batch_csums_file(flist_entry, sizeof(int));
214 int_count = s ? (int) s->count : 0;
215 write_batch_csums_file(&int_count, sizeof int_count);
216
217 if (s) {
218 for (i = 0; i < s->count; i++) {
219 write_batch_csums_file(&s->sums[i].sum1, sizeof(uint32));
220 write_batch_csums_file(s->sums[i].sum2, csum_length);
221 }
222 }
223}
224
225int read_batch_csums_file(char *buff, int len)
226{
227 int bytes_read;
228 char filename[MAXPATHLEN];
229
230 if (f_csums < 0) {
231 strlcpy(filename, batch_prefix, sizeof(filename));
232 strlcat(filename, rsync_csums_file, sizeof(filename));
233
234 f_csums = do_open(filename, O_RDONLY, 0);
235 if (f_csums < 0) {
236 rprintf(FERROR, "Batch file %s open error: %s\n",
237 filename, strerror(errno));
238 close(f_csums);
239 exit_cleanup(1);
240 }
241 }
242
243 bytes_read = read(f_csums, buff, len);
244 if (bytes_read < 0) {
245 rprintf(FERROR, "Batch file %s read error: %s\n",
246 filename, strerror(errno));
247 close(f_csums);
248 exit_cleanup(1);
249 }
250
251 return bytes_read;
252}
253
254void read_batch_csum_info(int flist_entry, struct sum_struct *s,
255 int *checksums_match)
256{
257 int i;
258 int file_flist_entry;
259 int file_chunk_ct;
260 uint32 file_sum1;
261 char file_sum2[SUM_LENGTH];
262
263 read_batch_csums_file((char *) &file_flist_entry, sizeof(int));
264 if (file_flist_entry != flist_entry) {
265 rprintf(FINFO, "file_flist_entry (%d) != flist_entry (%d)\n",
266 file_flist_entry, flist_entry);
267 close(f_csums);
268 exit_cleanup(1);
269
270 } else {
271 read_batch_csums_file((char *) &file_chunk_ct,
272 sizeof(int));
273 *checksums_match = 1;
274 for (i = 0; i < file_chunk_ct; i++) {
275
276 read_batch_csums_file((char *) &file_sum1,
277 sizeof(uint32));
278 read_batch_csums_file(file_sum2, csum_length);
279
280 if ((s->sums[i].sum1 != file_sum1) ||
281 (memcmp(s->sums[i].sum2, file_sum2, csum_length)
282 != 0)) {
283 *checksums_match = 0;
284 }
285 } /* end for */
286 }
287}
288
289void write_batch_delta_file(char *buff, int bytes_to_write)
290{
291 char filename[MAXPATHLEN];
292
293 if (f_delta < 0) {
294 strlcpy(filename, batch_prefix, sizeof(filename));
295 strlcat(filename, rsync_delta_file, sizeof(filename));
296
297 f_delta = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
298 S_IREAD | S_IWRITE);
299 if (f_delta < 0) {
300 rprintf(FERROR, "Batch file %s open error: %s\n",
301 filename, strerror(errno));
302 exit_cleanup(1);
303 }
304 }
305
306 if (write(f_delta, buff, bytes_to_write) < 0) {
307 rprintf(FERROR, "Batch file %s write error: %s\n",
308 filename, strerror(errno));
309 close(f_delta);
310 exit_cleanup(1);
311 }
312}
313
314void close_batch_delta_file(void)
315{
316 close(f_delta);
317 f_delta = -1;
318}
319
320int read_batch_delta_file(char *buff, int len)
321{
322 int bytes_read;
323 char filename[MAXPATHLEN];
324
325 if (f_delta < 0) {
326 strlcpy(filename, batch_prefix, sizeof(filename));
327 strlcat(filename, rsync_delta_file, sizeof(filename));
328
329 f_delta = do_open(filename, O_RDONLY, 0);
330 if (f_delta < 0) {
331 rprintf(FERROR, "Batch file %s open error: %s\n",
332 filename, strerror(errno));
333 close(f_delta);
334 exit_cleanup(1);
335 }
336 }
337
338 bytes_read = read(f_delta, buff, len);
339 if (bytes_read < 0) {
340 rprintf(FERROR, "Batch file %s read error: %s\n",
341 filename, strerror(errno));
342 close(f_delta);
343 exit_cleanup(1);
344 }
345
346 return bytes_read;
347}
348
349void show_flist(int index, struct file_struct **fptr)
350{
351 /* for debugging show_flist(flist->count, flist->files * */
352
353 int i;
354 for (i = 0; i < index; i++) {
355 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
356 rprintf(FINFO, "flist->modtime=%#lx\n",
357 (long unsigned) fptr[i]->modtime);
358 rprintf(FINFO, "flist->length=%.0f\n",
359 (double) fptr[i]->length);
360 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
361 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
362 if (fptr[i]->dirname)
363 rprintf(FINFO, "flist->dirname=%s\n",
364 fptr[i]->dirname);
365 if (fptr[i]->basedir)
366 rprintf(FINFO, "flist->basedir=%s\n",
367 fptr[i]->basedir);
368 }
369}
370
371void show_argvs(int argc, char *argv[])
372{
373 /* for debugging * */
374
375 int i;
376 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
377 for (i = 0; i < argc; i++) {
378 /* if (argv[i]) */
379 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
380
381 }
382}