1 /* -*- c-file-style: "linux" -*-
4 Batch utilities for rsync.
11 extern int itemize_changes;
16 extern int preserve_links;
17 extern int preserve_hard_links;
18 extern int preserve_devices;
19 extern int preserve_uid;
20 extern int preserve_gid;
21 extern int always_checksum;
22 extern int protocol_version;
23 extern char *batch_name;
25 extern struct filter_list_struct filter_list;
27 static int fudged_recurse;
29 static int *flag_ptr[] = {
41 static char *flag_name[] = {
50 "--itemize-changes (-i)",
54 void write_stream_flags(int fd)
58 /* Start the batch file with a bitmap of data-stream-affecting
60 fudged_recurse = recurse < 0;
61 for (i = 0, flags = 0; flag_ptr[i]; i++) {
68 void read_stream_flags(int fd)
72 fudged_recurse = recurse < 0;
73 if (protocol_version < 29)
75 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
76 int set = flags & (1 << i) ? 1 : 0;
77 if (*flag_ptr[i] != set) {
80 "%sing the %s option to match the batchfile.\n",
81 set ? "Sett" : "Clear", flag_name[i]);
86 recurse = fudged_recurse ? -1 : 0;
87 if (protocol_version < 29)
88 xfer_dirs = recurse ? 1 : 0;
91 static void write_arg(int fd, char *arg)
95 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
96 write(fd, arg, x - arg + 1);
100 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
102 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
103 write(fd, s, x - s + 1);
106 write(fd, s, strlen(s));
111 write(fd, arg, strlen(arg));
114 static void write_filter_rules(int fd)
116 struct filter_struct *ent;
118 write_sbuf(fd, " <<'#E#'\n");
119 for (ent = filter_list.head; ent; ent = ent->next) {
121 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
122 write_buf(fd, p, plen);
123 write_sbuf(fd, ent->pattern);
124 if (ent->match_flags & MATCHFLG_DIRECTORY)
126 write_byte(fd, eol_nulls ? 0 : '\n');
129 write_sbuf(fd, ";\n");
130 write_sbuf(fd, "#E#");
133 /* This routine tries to write out an equivalent --read-batch command
134 * given the user's --write-batch args. However, it doesn't really
135 * understand most of the options, so it uses some overly simple
136 * heuristics to munge the command line into something that will
137 * (hopefully) work. */
138 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
141 char *p, filename[MAXPATHLEN];
143 stringjoin(filename, sizeof filename,
144 batch_name, ".sh", NULL);
145 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
146 S_IRUSR | S_IWUSR | S_IEXEC);
148 rsyserr(FERROR, errno, "Batch file %s open error",
149 safe_fname(filename));
153 /* Write argvs info to BATCH.sh file */
154 write_arg(fd, argv[0]);
155 if (filter_list.head) {
156 if (protocol_version >= 29)
157 write_sbuf(fd, " --filter=._-");
159 write_sbuf(fd, " --exclude-from=-");
161 for (i = 1; i < argc - file_arg_cnt; i++) {
163 if (strncmp(p, "--files-from", 12) == 0
164 || strncmp(p, "--filter", 8) == 0
165 || strncmp(p, "--include", 9) == 0
166 || strncmp(p, "--exclude", 9) == 0) {
167 if (strchr(p, '=') == NULL)
171 if (strcmp(p, "-f") == 0) {
176 if (strncmp(p, "--write-batch", 13) == 0) {
177 write(fd, "--read-batch", 12);
180 write_arg(fd, p + 14);
185 if ((p = find_colon(argv[argc - 1])) != NULL) {
190 write(fd, " ${1:-", 6);
193 if (filter_list.head)
194 write_filter_rules(fd);
195 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
196 rsyserr(FERROR, errno, "Batch file %s write error",
197 safe_fname(filename));
202 void show_flist(int index, struct file_struct **fptr)
204 /* for debugging show_flist(flist->count, flist->files * */
207 for (i = 0; i < index; i++) {
208 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
209 rprintf(FINFO, "flist->modtime=%#lx\n",
210 (long unsigned) fptr[i]->modtime);
211 rprintf(FINFO, "flist->length=%.0f\n",
212 (double) fptr[i]->length);
213 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
214 rprintf(FINFO, "flist->basename=%s\n",
215 safe_fname(fptr[i]->basename));
216 if (fptr[i]->dirname) {
217 rprintf(FINFO, "flist->dirname=%s\n",
218 safe_fname(fptr[i]->dirname));
220 if (am_sender && fptr[i]->dir.root) {
221 rprintf(FINFO, "flist->dir.root=%s\n",
222 safe_fname(fptr[i]->dir.root));
228 void show_argvs(int argc, char *argv[])
232 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
233 for (i = 0; i < argc; i++)
234 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, safe_fname(argv[i]));