-
- static int fdb_open = 1;
-
- if (fdb_open) {
- /* Set up file extension */
- strcat(rsync_csums_file, batch_file_ext);
-
- /* Open batch csums file for writing; create it if it doesn't exist */
- fdb = do_open(rsync_csums_file, O_WRONLY|O_CREAT|O_TRUNC,
- S_IREAD|S_IWRITE);
- if (fdb == -1) {
- rprintf(FERROR, "Batch file %s open error: %s\n",
- rsync_csums_file, strerror(errno));
- close(fdb);
- exit_cleanup(1);
- }
- fdb_open = 0;
- }
-
- /* Write buffer to batch csums file */
-
- if ( write(fdb, buff, bytes_to_write) == -1 ) {
- rprintf(FERROR, "Batch file %s write error: %s\n",
- rsync_csums_file, strerror(errno));
- close(fdb);
- exit_cleanup(1);
- }
-}
-
-void close_batch_csums_file()
-{
- close(fdb);
-
-}
-
-void write_batch_csum_info(int *flist_entry, int flist_count, struct sum_struct *s)
-{
- int i;
- int int_zero = 0;
- extern int csum_length;
-
- fdb_open = 1;
-
- /* Write csum info to batch file */
-
- write_batch_csums_file ( (char *) flist_entry, sizeof(int) );
- write_batch_csums_file ( (char *) (s?&s->count:&int_zero), sizeof(int) );
- if (s) {
- for (i=0; i < s->count; i++) {
- write_batch_csums_file( (char *) &s->sums[i].sum1, sizeof(uint32));
- if ( (*flist_entry == flist_count - 1) && (i == s->count - 1) ) {
- fdb_close = 1;
- }
- write_batch_csums_file( s->sums[i].sum2, csum_length);
- }
- }
-}
-
-int read_batch_csums_file(char *buff, int len)
-{
- static int fdb_open = 1;
- int bytes_read;
-
- if (fdb_open) {
-
- /* Set up file extension */
- strcat(rsync_csums_file, batch_file_ext);
-
- /* Open batch flist file for reading */
- fdb = do_open(rsync_csums_file, O_RDONLY, 0);
- if (fdb == -1) {
- rprintf(FERROR, "Batch file %s open error: %s\n", rsync_csums_file,
- strerror(errno));
- close(fdb);
- exit_cleanup(1);
- }
- fdb_open = 0;
- }
-
- /* Read csums batch file */
-
- bytes_read = read(fdb, buff, len);
-
- if (bytes_read == -1) {
- rprintf(FERROR, "Batch file %s read error: %s\n",
- rsync_csums_file, strerror(errno));
- close(fdb);
- exit_cleanup(1);
- }
- return bytes_read;
-}
-
-
-void read_batch_csum_info(int flist_entry, struct sum_struct *s, int *checksums_match)
-{
- int i;
- int file_flist_entry;
- int file_chunk_ct;
- uint32 file_sum1;
- char file_sum2[SUM_LENGTH];
- extern int csum_length;
-
-
- read_batch_csums_file((char *)&file_flist_entry, sizeof(int));
- if (file_flist_entry != flist_entry) {
- rprintf(FINFO,"file_list_entry NE flist_entry\n");
- rprintf(FINFO,"file_flist_entry = %d flist_entry = %d\n", file_flist_entry, flist_entry);
- close(fdb);
- exit_cleanup(1);
-
- }
- else {
- read_batch_csums_file((char *)&file_chunk_ct, sizeof(int));
- *checksums_match = 1;
- for (i = 0;i < file_chunk_ct;i++) {
-
- read_batch_csums_file((char *)&file_sum1, sizeof(uint32));
- read_batch_csums_file(file_sum2, csum_length);
-
- if ( (s->sums[i].sum1 != file_sum1) ||
- ( memcmp(s->sums[i].sum2,file_sum2, csum_length)!=0) ) {
- *checksums_match = 0;
- }
- } /* end for */
- }
-
-}
-
-void write_batch_delta_file(char *buff, int bytes_to_write)
-{
- static int fdb_delta_open = 1;
-
- if (fdb_delta_open) {
- /* Set up file extension */
- strcat(rsync_delta_file, batch_file_ext);
-
- /* Open batch delta file for writing; create it if it doesn't exist */
- fdb_delta = do_open(rsync_delta_file, O_WRONLY|O_CREAT|O_TRUNC,
- S_IREAD|S_IWRITE);
- if (fdb_delta == -1) {
- rprintf(FERROR, "Batch file %s open error: %s\n",
- rsync_delta_file, strerror(errno));
- close(fdb_delta);
- exit_cleanup(1);
- }
- fdb_delta_open = 0;
- }
-
- /* Write buffer to batch delta file */
-
- if ( write(fdb_delta, buff, bytes_to_write) == -1 ) {
- rprintf(FERROR, "Batch file %s write error: %s\n",
- rsync_delta_file, strerror(errno));
- close(fdb_delta);
- exit_cleanup(1);
- }
-}
-void close_batch_delta_file()
-{
- close(fdb_delta);
-
-}
-
-int read_batch_delta_file(char *buff, int len)
-{
- static int fdb_delta_open = 1;
- int bytes_read;
-
- if (fdb_delta_open) {
-
- /* Set up file extension */
- strcat(rsync_delta_file, batch_file_ext);
-
- /* Open batch flist file for reading */
- fdb_delta = do_open(rsync_delta_file, O_RDONLY, 0);
- if (fdb_delta == -1) {
- rprintf(FERROR, "Batch file %s open error: %s\n", rsync_delta_file,
- strerror(errno));
- close(fdb_delta);
- exit_cleanup(1);
- }
- fdb_delta_open = 0;
- }
-
- /* Read delta batch file */
-
- bytes_read = read(fdb_delta, buff, len);
-
- if (bytes_read == -1) {
- rprintf(FERROR, "Batch file %s read error: %s\n",
- rsync_delta_file, strerror(errno));
- close(fdb_delta);
- exit_cleanup(1);
- }
- return bytes_read;
-}
-
-
-void show_flist(int index, struct file_struct **fptr)
-{
- /* for debugging show_flist(flist->count, flist->files **/
-
- int i;
- for (i=0;i<index;i++) {
- rprintf(FINFO, "flist->flags=%#x\n",fptr[i]->flags);
- rprintf(FINFO, "flist->modtime=%#x\n",fptr[i]->modtime);
- rprintf(FINFO, "flist->length=%.0f\n", (double) fptr[i]->length);
- rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
- rprintf(FINFO, "flist->basename=%s\n",fptr[i]->basename);
- if (fptr[i]->dirname)
- rprintf(FINFO, "flist->dirname=%s\n",fptr[i]->dirname);
- if (fptr[i]->basedir)
- rprintf(FINFO, "flist->basedir=%s\n",fptr[i]->basedir);
- }
-}
-
-void show_argvs(int argc, char *argv[])
-{
- /* for debugging **/
-
- int i;
- rprintf(FINFO,"BATCH.C:show_argvs,argc=%d\n",argc);
- for (i=0;i<argc;i++) {
- /* if (argv[i]) */
- rprintf(FINFO,"i=%d,argv[i]=%s\n",i,argv[i]);
-
- }
+ int fd, i, len, err = 0;
+ char *p, filename[MAXPATHLEN];
+
+ stringjoin(filename, sizeof filename,
+ batch_name, ".sh", NULL);
+ fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
+ S_IRUSR | S_IWUSR | S_IEXEC);
+ if (fd < 0) {
+ rsyserr(FERROR, errno, "Batch file %s open error",
+ filename);
+ exit_cleanup(RERR_FILESELECT);
+ }
+
+ /* Write argvs info to BATCH.sh file */
+ if (write_arg(fd, argv[0]) < 0)
+ err = 1;
+ if (filter_list.head) {
+ if (protocol_version >= 29)
+ write_sbuf(fd, " --filter=._-");
+ else
+ write_sbuf(fd, " --exclude-from=-");
+ }
+ for (i = 1; i < argc - file_arg_cnt; i++) {
+ p = argv[i];
+ if (strncmp(p, "--files-from", 12) == 0
+ || strncmp(p, "--filter", 8) == 0
+ || strncmp(p, "--include", 9) == 0
+ || strncmp(p, "--exclude", 9) == 0) {
+ if (strchr(p, '=') == NULL)
+ i++;
+ continue;
+ }
+ if (strcmp(p, "-f") == 0) {
+ i++;
+ continue;
+ }
+ if (write(fd, " ", 1) != 1)
+ err = 1;
+ if (strncmp(p, "--write-batch", len = 13) == 0
+ || strncmp(p, "--only-write-batch", len = 18) == 0) {
+ if (write(fd, "--read-batch", 12) != 12)
+ err = 1;
+ if (p[len] == '=') {
+ if (write(fd, "=", 1) != 1
+ || write_arg(fd, p + len + 1) < 0)
+ err = 1;
+ }
+ } else {
+ if (write_arg(fd, p) < 0)
+ err = 1;
+ }
+ }
+ if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
+ p = argv[argc - 1];
+ if (write(fd, " ${1:-", 6) != 6
+ || write_arg(fd, p) < 0)
+ err = 1;
+ write_byte(fd, '}');
+ if (filter_list.head)
+ write_filter_rules(fd);
+ if (write(fd, "\n", 1) != 1 || close(fd) < 0 || err) {
+ rsyserr(FERROR, errno, "Batch file %s write error",
+ filename);
+ exit_cleanup(RERR_FILEIO);
+ }