Changed rprintf() calls that included strerror() to use rsyserr().
[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_IRUSR | S_IWUSR);
36         if (f < 0) {
37                 rsyserr(FERROR, errno, "Batch file %s open error", filename);
38                 exit_cleanup(1);
39         }
40
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);
46
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);
50         }
51         send_file_entry(NULL, f, 0);
52
53         protocol_version = save_pv;
54         stats.total_written = save_written;
55
56         close(f);
57 }
58
59
60 void write_batch_argvs_file(int argc, char *argv[])
61 {
62         int f;
63         int i;
64         char buff[256]; /* XXX */
65         char buff2[MAXPATHLEN + 6];
66         char filename[MAXPATHLEN];
67
68         stringjoin(filename, sizeof filename,
69             batch_prefix, rsync_argvs_file, NULL);
70
71         f = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
72             S_IRUSR | S_IWUSR | S_IEXEC);
73         if (f < 0) {
74                 rsyserr(FERROR, errno, "Batch file %s open error", filename);
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                 rsyserr(FERROR, errno, "Batch file %s write error", filename);
113                 close(f);
114                 exit_cleanup(1);
115         }
116         close(f);
117 }
118
119 struct file_list *create_flist_from_batch(void)
120 {
121         char filename[MAXPATHLEN];
122         unsigned short flags;
123         int i, f, save_pv;
124         int64 save_read;
125
126         stringjoin(filename, sizeof filename,
127             batch_prefix, rsync_flist_file, NULL);
128
129         f = do_open(filename, O_RDONLY, 0);
130         if (f < 0) {
131                 rsyserr(FERROR, errno, "Batch file %s open error", filename);
132                 exit_cleanup(1);
133         }
134
135         batch_flist = flist_new(WITH_HLINK, "create_flist_from_batch");
136
137         save_read = stats.total_read;
138         save_pv = protocol_version;
139         protocol_version = read_int(f);
140
141         batch_flist->count = read_int(f);
142         flist_expand(batch_flist);
143
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);
148         }
149         receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
150
151         protocol_version = save_pv;
152         stats.total_read = save_read;
153
154         return batch_flist;
155 }
156
157 void write_batch_csums_file(void *buff, int bytes_to_write)
158 {
159         if (write(f_csums, buff, bytes_to_write) < 0) {
160                 rsyserr(FERROR, errno, "Batch file write error");
161                 close(f_csums);
162                 exit_cleanup(1);
163         }
164 }
165
166 void close_batch_csums_file(void)
167 {
168         close(f_csums);
169         f_csums = -1;
170 }
171
172
173 /**
174  * Write csum info to batch file
175  *
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
179  * change.
180  **/
181 void write_batch_csum_info(int *flist_entry, struct sum_struct *s)
182 {
183         size_t i;
184         int int_count;
185         char filename[MAXPATHLEN];
186
187         if (f_csums < 0) {
188                 stringjoin(filename, sizeof filename,
189                     batch_prefix, rsync_csums_file, NULL);
190
191                 f_csums = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
192                     S_IRUSR | S_IWUSR);
193                 if (f_csums < 0) {
194                         rsyserr(FERROR, errno, "Batch file %s open error",
195                                 filename);
196                         close(f_csums);
197                         exit_cleanup(1);
198                 }
199         }
200
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);
204
205         if (s) {
206                 for (i = 0; i < s->count; i++) {
207                         write_batch_csums_file(&s->sums[i].sum1,
208                             sizeof (uint32));
209                         write_batch_csums_file(s->sums[i].sum2, csum_length);
210                 }
211         }
212 }
213
214 int read_batch_csums_file(char *buff, int len)
215 {
216         int bytes_read;
217
218         if ((bytes_read = read(f_csums, buff, len)) < 0) {
219                 rsyserr(FERROR, errno, "Batch file read error");
220                 close(f_csums);
221                 exit_cleanup(1);
222         }
223         return bytes_read;
224 }
225
226 void read_batch_csum_info(int flist_entry, struct sum_struct *s,
227                           int *checksums_match)
228 {
229         int i;
230         int file_flist_entry;
231         int file_chunk_ct;
232         uint32 file_sum1;
233         char file_sum2[SUM_LENGTH];
234         char filename[MAXPATHLEN];
235
236         if (f_csums < 0) {
237                 stringjoin(filename, sizeof filename,
238                     batch_prefix, rsync_csums_file, NULL);
239
240                 f_csums = do_open(filename, O_RDONLY, 0);
241                 if (f_csums < 0) {
242                         rsyserr(FERROR, errno, "Batch file %s open error",
243                                 filename);
244                         close(f_csums);
245                         exit_cleanup(1);
246                 }
247         }
248
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);
253                 close(f_csums);
254                 exit_cleanup(1);
255
256         } else {
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,
261                             sizeof (uint32));
262                         read_batch_csums_file(file_sum2, csum_length);
263
264                         if ((s->sums[i].sum1 != file_sum1)
265                             || memcmp(s->sums[i].sum2, file_sum2, csum_length))
266                                 *checksums_match = 0;
267                 }               /*  end for  */
268         }
269 }
270
271 void write_batch_delta_file(char *buff, int bytes_to_write)
272 {
273         char filename[MAXPATHLEN];
274
275         if (f_delta < 0) {
276                 stringjoin(filename, sizeof filename,
277                     batch_prefix, rsync_delta_file, NULL);
278
279                 f_delta = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
280                                   S_IRUSR | S_IWUSR);
281                 if (f_delta < 0) {
282                         rsyserr(FERROR, errno, "Batch file %s open error",
283                                 filename);
284                         exit_cleanup(1);
285                 }
286         }
287
288         if (write(f_delta, buff, bytes_to_write) < 0) {
289                 rsyserr(FERROR, errno, "Batch file %s write error", filename);
290                 close(f_delta);
291                 exit_cleanup(1);
292         }
293 }
294
295 void close_batch_delta_file(void)
296 {
297         close(f_delta);
298         f_delta = -1;
299 }
300
301 int read_batch_delta_file(char *buff, int len)
302 {
303         int bytes_read;
304         char filename[MAXPATHLEN];
305
306         if (f_delta < 0) {
307                 stringjoin(filename, sizeof filename,
308                     batch_prefix, rsync_delta_file, NULL);
309
310                 f_delta = do_open(filename, O_RDONLY, 0);
311                 if (f_delta < 0) {
312                         rsyserr(FERROR, errno, "Batch file %s open error",
313                                 filename);
314                         close(f_delta);
315                         exit_cleanup(1);
316                 }
317         }
318
319         bytes_read = read(f_delta, buff, len);
320         if (bytes_read < 0) {
321                 rsyserr(FERROR, errno, "Batch file %s read error", filename);
322                 close(f_delta);
323                 exit_cleanup(1);
324         }
325
326         return bytes_read;
327 }
328
329 void show_flist(int index, struct file_struct **fptr)
330 {
331         /*  for debugging    show_flist(flist->count, flist->files * */
332
333         int i;
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",
344                                 fptr[i]->dirname);
345                 if (fptr[i]->basedir)
346                         rprintf(FINFO, "flist->basedir=%s\n",
347                                 fptr[i]->basedir);
348         }
349 }
350
351 void show_argvs(int argc, char *argv[])
352 {
353         /*  for debugging  * */
354
355         int i;
356         rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
357         for (i = 0; i < argc; i++) {
358                 /*    if (argv[i])   */
359                 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
360
361         }
362 }