1 /* -*- c-file-style: "linux" -*-
4 Batch utilities for rsync.
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;
21 extern char *batch_name;
23 extern struct filter_list_struct filter_list;
25 static int *flag_ptr[] = {
36 static char *flag_name[] = {
47 void write_stream_flags(int fd)
51 /* Start the batch file with a bitmap of data-stream-affecting
53 for (i = 0, flags = 0; flag_ptr[i]; i++) {
60 void read_stream_flags(int fd)
64 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
65 int set = flags & (1 << i) ? 1 : 0;
66 if (*flag_ptr[i] != set) {
69 "%sing the %s option to match the batchfile.\n",
70 set ? "Sett" : "Clear", flag_name[i]);
77 static void write_arg(int fd, char *arg)
81 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
82 write(fd, arg, x - arg + 1);
86 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
88 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
89 write(fd, s, x - s + 1);
92 write(fd, s, strlen(s));
97 write(fd, arg, strlen(arg));
100 static void write_filter_rules(int fd)
102 struct filter_struct *ent;
104 write_sbuf(fd, " <<'#E#'\n");
105 for (ent = filter_list.head; ent; ent = ent->next) {
107 char *p = get_rule_prefix(ent->match_flags, "- ", &plen);
108 write_buf(fd, p, plen);
109 write_sbuf(fd, ent->pattern);
110 if (ent->match_flags & MATCHFLG_DIRECTORY)
112 write_byte(fd, eol_nulls ? 0 : '\n');
115 write_sbuf(fd, ";\n");
116 write_sbuf(fd, "#E#");
119 /* This routine tries to write out an equivalent --read-batch command
120 * given the user's --write-batch args. However, it doesn't really
121 * understand most of the options, so it uses some overly simple
122 * heuristics to munge the command line into something that will
123 * (hopefully) work. */
124 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
127 char *p, filename[MAXPATHLEN];
129 stringjoin(filename, sizeof filename,
130 batch_name, ".sh", NULL);
131 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
132 S_IRUSR | S_IWUSR | S_IEXEC);
134 rsyserr(FERROR, errno, "Batch file %s open error", filename);
138 /* Write argvs info to BATCH.sh file */
139 write_arg(fd, argv[0]);
140 if (filter_list.head) {
141 if (protocol_version >= 29)
142 write_sbuf(fd, " --filter=._-");
144 write_sbuf(fd, " --exclude-from=-");
146 for (i = 1; i < argc - file_arg_cnt; i++) {
148 if (strncmp(p, "--files-from", 12) == 0
149 || strncmp(p, "--filter", 8) == 0
150 || strncmp(p, "--include", 9) == 0
151 || strncmp(p, "--exclude", 9) == 0) {
152 if (strchr(p, '=') == NULL)
156 if (strcmp(p, "-f") == 0) {
161 if (strncmp(p, "--write-batch", 13) == 0) {
162 write(fd, "--read-batch", 12);
165 write_arg(fd, p + 14);
170 if ((p = find_colon(argv[argc - 1])) != NULL) {
175 write(fd, " ${1:-", 6);
178 if (filter_list.head)
179 write_filter_rules(fd);
180 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
181 rsyserr(FERROR, errno, "Batch file %s write error", filename);
186 void show_flist(int index, struct file_struct **fptr)
188 /* for debugging show_flist(flist->count, flist->files * */
191 for (i = 0; i < index; i++) {
192 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
193 rprintf(FINFO, "flist->modtime=%#lx\n",
194 (long unsigned) fptr[i]->modtime);
195 rprintf(FINFO, "flist->length=%.0f\n",
196 (double) fptr[i]->length);
197 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
198 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
199 if (fptr[i]->dirname)
200 rprintf(FINFO, "flist->dirname=%s\n",
202 if (am_sender && fptr[i]->dir.root)
203 rprintf(FINFO, "flist->dir.root=%s\n",
208 void show_argvs(int argc, char *argv[])
210 /* for debugging * */
213 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
214 for (i = 0; i < argc; i++) {
216 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);