1 /* -*- c-file-style: "linux" -*-
4 Batch utilities for rsync.
11 extern char *batch_name;
14 extern int preserve_links;
15 extern int preserve_hard_links;
16 extern int preserve_devices;
17 extern int preserve_uid;
18 extern int preserve_gid;
19 extern int always_checksum;
20 extern int protocol_version;
22 extern struct filter_list_struct filter_list;
24 static int *flag_ptr[] = {
35 static char *flag_name[] = {
46 void write_stream_flags(int fd)
50 /* Start the batch file with a bitmap of data-stream-affecting
52 for (i = 0, flags = 0; flag_ptr[i]; i++) {
59 void read_stream_flags(int fd)
63 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
64 int set = flags & (1 << i) ? 1 : 0;
65 if (*flag_ptr[i] != set) {
68 "%sing the %s option to match the batchfile.\n",
69 set ? "Sett" : "Clear", flag_name[i]);
76 static void write_arg(int fd, char *arg)
80 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
81 write(fd, arg, x - arg + 1);
85 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
87 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
88 write(fd, s, x - s + 1);
91 write(fd, s, strlen(s));
96 write(fd, arg, strlen(arg));
99 static void write_filter_rules(int fd)
101 struct filter_struct *ent;
103 write_sbuf(fd, " <<'#E#'\n");
104 for (ent = filter_list.head; ent; ent = ent->next) {
106 char *p = get_rule_prefix(ent->match_flags, "- ", &plen);
107 write_buf(fd, p, plen);
108 write_sbuf(fd, ent->pattern);
109 if (ent->match_flags & MATCHFLG_DIRECTORY)
111 write_byte(fd, eol_nulls ? 0 : '\n');
114 write_sbuf(fd, ";\n");
115 write_sbuf(fd, "#E#");
118 /* This routine tries to write out an equivalent --read-batch command
119 * given the user's --write-batch args. However, it doesn't really
120 * understand most of the options, so it uses some overly simple
121 * heuristics to munge the command line into something that will
122 * (hopefully) work. */
123 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
126 char *p, filename[MAXPATHLEN];
128 stringjoin(filename, sizeof filename,
129 batch_name, ".sh", NULL);
130 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
131 S_IRUSR | S_IWUSR | S_IEXEC);
133 rsyserr(FERROR, errno, "Batch file %s open error", filename);
137 /* Write argvs info to BATCH.sh file */
138 write_arg(fd, argv[0]);
139 if (filter_list.head) {
140 if (protocol_version >= 29)
141 write_sbuf(fd, " --filter=._-");
143 write_sbuf(fd, " --exclude-from=-");
145 for (i = 1; i < argc - file_arg_cnt; i++) {
147 if (strncmp(p, "--files-from", 12) == 0
148 || strncmp(p, "--filter", 8) == 0
149 || strncmp(p, "--include", 9) == 0
150 || strncmp(p, "--exclude", 9) == 0) {
151 if (strchr(p, '=') == NULL)
155 if (strcmp(p, "-f") == 0) {
160 if (strncmp(p, "--write-batch", 13) == 0) {
161 write(fd, "--read-batch", 12);
164 write_arg(fd, p + 14);
169 if ((p = find_colon(argv[argc - 1])) != NULL) {
174 write(fd, " ${1:-", 6);
177 if (filter_list.head)
178 write_filter_rules(fd);
179 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
180 rsyserr(FERROR, errno, "Batch file %s write error", filename);
185 void show_flist(int index, struct file_struct **fptr)
187 /* for debugging show_flist(flist->count, flist->files * */
190 for (i = 0; i < index; i++) {
191 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
192 rprintf(FINFO, "flist->modtime=%#lx\n",
193 (long unsigned) fptr[i]->modtime);
194 rprintf(FINFO, "flist->length=%.0f\n",
195 (double) fptr[i]->length);
196 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
197 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
198 if (fptr[i]->dirname)
199 rprintf(FINFO, "flist->dirname=%s\n",
201 if (fptr[i]->basedir)
202 rprintf(FINFO, "flist->basedir=%s\n",
207 void show_argvs(int argc, char *argv[])
209 /* for debugging * */
212 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
213 for (i = 0; i < argc; i++) {
215 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);