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