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;
21 extern struct filter_list_struct filter_list;
23 static int *flag_ptr[] = {
34 static char *flag_name[] = {
45 void write_stream_flags(int fd)
49 /* Start the batch file with a bitmap of data-stream-affecting
51 for (i = 0, flags = 0; flag_ptr[i]; i++) {
58 void read_stream_flags(int fd)
62 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
63 int set = flags & (1 << i) ? 1 : 0;
64 if (*flag_ptr[i] != set) {
67 "%sing the %s option to match the batchfile.\n",
68 set ? "Sett" : "Clear", flag_name[i]);
75 static void write_arg(int fd, char *arg)
79 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
80 write(fd, arg, x - arg + 1);
84 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
86 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
87 write(fd, s, x - s + 1);
90 write(fd, s, strlen(s));
95 write(fd, arg, strlen(arg));
98 static void write_filters(int fd)
100 struct filter_struct *ent;
102 write_sbuf(fd, " <<'#E#'\n");
103 for (ent = filter_list.head; ent; ent = ent->next) {
104 char *p = ent->pattern;
105 if (ent->match_flags & MATCHFLG_INCLUDE)
106 write_buf(fd, "+ ", 2);
107 else if (((*p == '-' || *p == '+') && p[1] == ' ')
108 || *p == '#' || *p == ';')
109 write_buf(fd, "- ", 2);
111 if (ent->match_flags & MATCHFLG_DIRECTORY)
113 write_byte(fd, eol_nulls ? 0 : '\n');
116 write_sbuf(fd, ";\n");
117 write_sbuf(fd, "#E#");
120 /* This routine tries to write out an equivalent --read-batch command
121 * given the user's --write-batch args. However, it doesn't really
122 * understand most of the options, so it uses some overly simple
123 * heuristics to munge the command line into something that will
124 * (hopefully) work. */
125 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
128 char *p, filename[MAXPATHLEN];
130 stringjoin(filename, sizeof filename,
131 batch_name, ".sh", NULL);
132 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
133 S_IRUSR | S_IWUSR | S_IEXEC);
135 rsyserr(FERROR, errno, "Batch file %s open error", filename);
139 /* Write argvs info to BATCH.sh file */
140 write_arg(fd, argv[0]);
141 if (filter_list.head)
142 write_sbuf(fd, " --exclude-from=-");
143 for (i = 1; i < argc - file_arg_cnt; i++) {
145 if (strncmp(p, "--files-from", 12) == 0
146 || strncmp(p, "--include", 9) == 0
147 || strncmp(p, "--exclude", 9) == 0) {
148 if (strchr(p, '=') == NULL)
153 if (strncmp(p, "--write-batch", 13) == 0) {
154 write(fd, "--read-batch", 12);
157 write_arg(fd, p + 14);
162 if ((p = find_colon(argv[argc - 1])) != NULL) {
167 write(fd, " ${1:-", 6);
170 if (filter_list.head)
172 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
173 rsyserr(FERROR, errno, "Batch file %s write error", filename);
178 void show_flist(int index, struct file_struct **fptr)
180 /* for debugging show_flist(flist->count, flist->files * */
183 for (i = 0; i < index; i++) {
184 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
185 rprintf(FINFO, "flist->modtime=%#lx\n",
186 (long unsigned) fptr[i]->modtime);
187 rprintf(FINFO, "flist->length=%.0f\n",
188 (double) fptr[i]->length);
189 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
190 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
191 if (fptr[i]->dirname)
192 rprintf(FINFO, "flist->dirname=%s\n",
194 if (fptr[i]->basedir)
195 rprintf(FINFO, "flist->basedir=%s\n",
200 void show_argvs(int argc, char *argv[])
202 /* for debugging * */
205 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
206 for (i = 0; i < argc; i++) {
208 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);