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