1 /* -*- c-file-style: "linux" -*-
4 Batch utilities for rsync.
15 extern int preserve_links;
16 extern int preserve_hard_links;
17 extern int preserve_devices;
18 extern int preserve_uid;
19 extern int preserve_gid;
20 extern int always_checksum;
21 extern int protocol_version;
22 extern char *batch_name;
24 extern struct filter_list_struct filter_list;
26 static int fudged_recurse;
28 static int *flag_ptr[] = {
40 static char *flag_name[] = {
52 void write_stream_flags(int fd)
56 /* Start the batch file with a bitmap of data-stream-affecting
58 fudged_recurse = recurse < 0;
59 for (i = 0, flags = 0; flag_ptr[i]; i++) {
66 void read_stream_flags(int fd)
70 fudged_recurse = recurse < 0;
71 if (protocol_version < 29)
73 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
74 int set = flags & (1 << i) ? 1 : 0;
75 if (*flag_ptr[i] != set) {
78 "%sing the %s option to match the batchfile.\n",
79 set ? "Sett" : "Clear", flag_name[i]);
84 recurse = fudged_recurse ? -1 : 0;
85 if (protocol_version < 29)
86 xfer_dirs = recurse ? 1 : 0;
89 static void write_arg(int fd, char *arg)
93 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
94 write(fd, arg, x - arg + 1);
98 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
100 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
101 write(fd, s, x - s + 1);
104 write(fd, s, strlen(s));
109 write(fd, arg, strlen(arg));
112 static void write_filter_rules(int fd)
114 struct filter_struct *ent;
116 write_sbuf(fd, " <<'#E#'\n");
117 for (ent = filter_list.head; ent; ent = ent->next) {
119 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
120 write_buf(fd, p, plen);
121 write_sbuf(fd, ent->pattern);
122 if (ent->match_flags & MATCHFLG_DIRECTORY)
124 write_byte(fd, eol_nulls ? 0 : '\n');
127 write_sbuf(fd, ";\n");
128 write_sbuf(fd, "#E#");
131 /* This routine tries to write out an equivalent --read-batch command
132 * given the user's --write-batch args. However, it doesn't really
133 * understand most of the options, so it uses some overly simple
134 * heuristics to munge the command line into something that will
135 * (hopefully) work. */
136 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
139 char *p, filename[MAXPATHLEN];
141 stringjoin(filename, sizeof filename,
142 batch_name, ".sh", NULL);
143 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
144 S_IRUSR | S_IWUSR | S_IEXEC);
146 rsyserr(FERROR, errno, "Batch file %s open error", filename);
150 /* Write argvs info to BATCH.sh file */
151 write_arg(fd, argv[0]);
152 if (filter_list.head) {
153 if (protocol_version >= 29)
154 write_sbuf(fd, " --filter=._-");
156 write_sbuf(fd, " --exclude-from=-");
158 for (i = 1; i < argc - file_arg_cnt; i++) {
160 if (strncmp(p, "--files-from", 12) == 0
161 || strncmp(p, "--filter", 8) == 0
162 || strncmp(p, "--include", 9) == 0
163 || strncmp(p, "--exclude", 9) == 0) {
164 if (strchr(p, '=') == NULL)
168 if (strcmp(p, "-f") == 0) {
173 if (strncmp(p, "--write-batch", 13) == 0) {
174 write(fd, "--read-batch", 12);
177 write_arg(fd, p + 14);
182 if ((p = find_colon(argv[argc - 1])) != NULL) {
187 write(fd, " ${1:-", 6);
190 if (filter_list.head)
191 write_filter_rules(fd);
192 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
193 rsyserr(FERROR, errno, "Batch file %s write error", filename);
198 void show_flist(int index, struct file_struct **fptr)
200 /* for debugging show_flist(flist->count, flist->files * */
203 for (i = 0; i < index; i++) {
204 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
205 rprintf(FINFO, "flist->modtime=%#lx\n",
206 (long unsigned) fptr[i]->modtime);
207 rprintf(FINFO, "flist->length=%.0f\n",
208 (double) fptr[i]->length);
209 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
210 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
211 if (fptr[i]->dirname)
212 rprintf(FINFO, "flist->dirname=%s\n",
214 if (am_sender && fptr[i]->dir.root)
215 rprintf(FINFO, "flist->dir.root=%s\n",
220 void show_argvs(int argc, char *argv[])
222 /* for debugging * */
225 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
226 for (i = 0; i < argc; i++) {
228 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);