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 *flag_ptr[] = {
37 static char *flag_name[] = {
49 void write_stream_flags(int fd)
53 /* Start the batch file with a bitmap of data-stream-affecting
55 for (i = 0, flags = 0; flag_ptr[i]; i++) {
62 void read_stream_flags(int fd)
66 if (protocol_version < 29)
68 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
69 int set = flags & (1 << i) ? 1 : 0;
70 if (*flag_ptr[i] != set) {
73 "%sing the %s option to match the batchfile.\n",
74 set ? "Sett" : "Clear", flag_name[i]);
79 if (protocol_version < 29)
80 xfer_dirs = recurse ? 1 : 0;
83 static void write_arg(int fd, char *arg)
87 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
88 write(fd, arg, x - arg + 1);
92 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
94 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
95 write(fd, s, x - s + 1);
98 write(fd, s, strlen(s));
103 write(fd, arg, strlen(arg));
106 static void write_filter_rules(int fd)
108 struct filter_struct *ent;
110 write_sbuf(fd, " <<'#E#'\n");
111 for (ent = filter_list.head; ent; ent = ent->next) {
113 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
114 write_buf(fd, p, plen);
115 write_sbuf(fd, ent->pattern);
116 if (ent->match_flags & MATCHFLG_DIRECTORY)
118 write_byte(fd, eol_nulls ? 0 : '\n');
121 write_sbuf(fd, ";\n");
122 write_sbuf(fd, "#E#");
125 /* This routine tries to write out an equivalent --read-batch command
126 * given the user's --write-batch args. However, it doesn't really
127 * understand most of the options, so it uses some overly simple
128 * heuristics to munge the command line into something that will
129 * (hopefully) work. */
130 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
133 char *p, filename[MAXPATHLEN];
135 stringjoin(filename, sizeof filename,
136 batch_name, ".sh", NULL);
137 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
138 S_IRUSR | S_IWUSR | S_IEXEC);
140 rsyserr(FERROR, errno, "Batch file %s open error",
141 safe_fname(filename));
145 /* Write argvs info to BATCH.sh file */
146 write_arg(fd, argv[0]);
147 if (filter_list.head) {
148 if (protocol_version >= 29)
149 write_sbuf(fd, " --filter=._-");
151 write_sbuf(fd, " --exclude-from=-");
153 for (i = 1; i < argc - file_arg_cnt; i++) {
155 if (strncmp(p, "--files-from", 12) == 0
156 || strncmp(p, "--filter", 8) == 0
157 || strncmp(p, "--include", 9) == 0
158 || strncmp(p, "--exclude", 9) == 0) {
159 if (strchr(p, '=') == NULL)
163 if (strcmp(p, "-f") == 0) {
168 if (strncmp(p, "--write-batch", 13) == 0) {
169 write(fd, "--read-batch", 12);
172 write_arg(fd, p + 14);
177 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
179 write(fd, " ${1:-", 6);
182 if (filter_list.head)
183 write_filter_rules(fd);
184 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
185 rsyserr(FERROR, errno, "Batch file %s write error",
186 safe_fname(filename));
191 void show_flist(int index, struct file_struct **fptr)
193 /* for debugging show_flist(flist->count, flist->files * */
196 for (i = 0; i < index; i++) {
197 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
198 rprintf(FINFO, "flist->modtime=%#lx\n",
199 (long unsigned) fptr[i]->modtime);
200 rprintf(FINFO, "flist->length=%.0f\n",
201 (double) fptr[i]->length);
202 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
203 rprintf(FINFO, "flist->basename=%s\n",
204 safe_fname(fptr[i]->basename));
205 if (fptr[i]->dirname) {
206 rprintf(FINFO, "flist->dirname=%s\n",
207 safe_fname(fptr[i]->dirname));
209 if (am_sender && fptr[i]->dir.root) {
210 rprintf(FINFO, "flist->dir.root=%s\n",
211 safe_fname(fptr[i]->dir.root));
217 void show_argvs(int argc, char *argv[])
221 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
222 for (i = 0; i < argc; i++)
223 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, safe_fname(argv[i]));