Ensure that -i is set consistent with the batch's data.
[rsync/rsync.git] / batch.c
1 /* -*- c-file-style: "linux" -*-
2
3    Weiss 1/1999
4    Batch utilities for rsync.
5
6 */
7
8 #include "rsync.h"
9 #include <time.h>
10
11 extern int itemize_changes;
12 extern int am_sender;
13 extern int eol_nulls;
14 extern int recurse;
15 extern int xfer_dirs;
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;
24
25 extern struct filter_list_struct filter_list;
26
27 static int fudged_recurse;
28
29 static int *flag_ptr[] = {
30         &fudged_recurse,
31         &preserve_uid,
32         &preserve_gid,
33         &preserve_links,
34         &preserve_devices,
35         &preserve_hard_links,
36         &always_checksum,
37         &itemize_changes,
38         NULL
39 };
40
41 static char *flag_name[] = {
42         "--recurse (-r)",
43         "--owner (-o)",
44         "--group (-g)",
45         "--links (-l)",
46         "--devices (-D)",
47         "--hard-links (-H)",
48         "--checksum (-c)",
49         "--dirs (-d)",
50         "--itemize-changes (-i)",
51         NULL
52 };
53
54 void write_stream_flags(int fd)
55 {
56         int i, flags;
57
58         /* Start the batch file with a bitmap of data-stream-affecting
59          * flags. */
60         fudged_recurse = recurse < 0;
61         for (i = 0, flags = 0; flag_ptr[i]; i++) {
62                 if (*flag_ptr[i])
63                         flags |= 1 << i;
64         }
65         write_int(fd, flags);
66 }
67
68 void read_stream_flags(int fd)
69 {
70         int i, flags;
71
72         fudged_recurse = recurse < 0;
73         if (protocol_version < 29)
74                 xfer_dirs = 0;
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) {
78                         if (verbose) {
79                                 rprintf(FINFO,
80                                         "%sing the %s option to match the batchfile.\n",
81                                         set ? "Sett" : "Clear", flag_name[i]);
82                         }
83                         *flag_ptr[i] = set;
84                 }
85         }
86         recurse = fudged_recurse ? -1 : 0;
87         if (protocol_version < 29)
88                 xfer_dirs = recurse ? 1 : 0;
89 }
90
91 static void write_arg(int fd, char *arg)
92 {
93         char *x, *s;
94
95         if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
96                 write(fd, arg, x - arg + 1);
97                 arg += x - arg + 1;
98         }
99
100         if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
101                 write(fd, "'", 1);
102                 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
103                         write(fd, s, x - s + 1);
104                         write(fd, "'", 1);
105                 }
106                 write(fd, s, strlen(s));
107                 write(fd, "'", 1);
108                 return;
109         }
110
111         write(fd, arg, strlen(arg));
112 }
113
114 static void write_filter_rules(int fd)
115 {
116         struct filter_struct *ent;
117
118         write_sbuf(fd, " <<'#E#'\n");
119         for (ent = filter_list.head; ent; ent = ent->next) {
120                 unsigned int plen;
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)
125                         write_byte(fd, '/');
126                 write_byte(fd, eol_nulls ? 0 : '\n');
127         }
128         if (eol_nulls)
129                 write_sbuf(fd, ";\n");
130         write_sbuf(fd, "#E#");
131 }
132
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)
139 {
140         int fd, i;
141         char *p, filename[MAXPATHLEN];
142
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);
147         if (fd < 0) {
148                 rsyserr(FERROR, errno, "Batch file %s open error",
149                         safe_fname(filename));
150                 exit_cleanup(1);
151         }
152
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=._-");
158                 else
159                         write_sbuf(fd, " --exclude-from=-");
160         }
161         for (i = 1; i < argc - file_arg_cnt; i++) {
162                 p = argv[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)
168                                 i++;
169                         continue;
170                 }
171                 if (strcmp(p, "-f") == 0) {
172                         i++;
173                         continue;
174                 }
175                 write(fd, " ", 1);
176                 if (strncmp(p, "--write-batch", 13) == 0) {
177                         write(fd, "--read-batch", 12);
178                         if (p[13] == '=') {
179                                 write(fd, "=", 1);
180                                 write_arg(fd, p + 14);
181                         }
182                 } else
183                         write_arg(fd, p);
184         }
185         if ((p = find_colon(argv[argc - 1])) != NULL) {
186                 if (*++p == ':')
187                         p++;
188         } else
189                 p = argv[argc - 1];
190         write(fd, " ${1:-", 6);
191         write_arg(fd, p);
192         write_byte(fd, '}');
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));
198                 exit_cleanup(1);
199         }
200 }
201
202 void show_flist(int index, struct file_struct **fptr)
203 {
204         /*  for debugging    show_flist(flist->count, flist->files * */
205
206         int i;
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));
219                 }
220                 if (am_sender && fptr[i]->dir.root) {
221                         rprintf(FINFO, "flist->dir.root=%s\n",
222                                 safe_fname(fptr[i]->dir.root));
223                 }
224         }
225 }
226
227 /* for debugging */
228 void show_argvs(int argc, char *argv[])
229 {
230         int i;
231
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]));
235 }