Changes needed to use check_for_hostspec() in place of find_colon().
[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 am_sender;
12 extern int eol_nulls;
13 extern int recurse;
14 extern int xfer_dirs;
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;
23
24 extern struct filter_list_struct filter_list;
25
26 static int fudged_recurse;
27
28 static int *flag_ptr[] = {
29         &fudged_recurse,
30         &preserve_uid,
31         &preserve_gid,
32         &preserve_links,
33         &preserve_devices,
34         &preserve_hard_links,
35         &always_checksum,
36         NULL
37 };
38
39 static char *flag_name[] = {
40         "--recurse (-r)",
41         "--owner (-o)",
42         "--group (-g)",
43         "--links (-l)",
44         "--devices (-D)",
45         "--hard-links (-H)",
46         "--checksum (-c)",
47         "--dirs (-d)",
48         NULL
49 };
50
51 void write_stream_flags(int fd)
52 {
53         int i, flags;
54
55         /* Start the batch file with a bitmap of data-stream-affecting
56          * flags. */
57         fudged_recurse = recurse < 0;
58         for (i = 0, flags = 0; flag_ptr[i]; i++) {
59                 if (*flag_ptr[i])
60                         flags |= 1 << i;
61         }
62         write_int(fd, flags);
63 }
64
65 void read_stream_flags(int fd)
66 {
67         int i, flags;
68
69         fudged_recurse = recurse < 0;
70         if (protocol_version < 29)
71                 xfer_dirs = 0;
72         for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
73                 int set = flags & (1 << i) ? 1 : 0;
74                 if (*flag_ptr[i] != set) {
75                         if (verbose) {
76                                 rprintf(FINFO,
77                                         "%sing the %s option to match the batchfile.\n",
78                                         set ? "Sett" : "Clear", flag_name[i]);
79                         }
80                         *flag_ptr[i] = set;
81                 }
82         }
83         recurse = fudged_recurse ? -1 : 0;
84         if (protocol_version < 29)
85                 xfer_dirs = recurse ? 1 : 0;
86 }
87
88 static void write_arg(int fd, char *arg)
89 {
90         char *x, *s;
91
92         if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
93                 write(fd, arg, x - arg + 1);
94                 arg += x - arg + 1;
95         }
96
97         if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
98                 write(fd, "'", 1);
99                 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
100                         write(fd, s, x - s + 1);
101                         write(fd, "'", 1);
102                 }
103                 write(fd, s, strlen(s));
104                 write(fd, "'", 1);
105                 return;
106         }
107
108         write(fd, arg, strlen(arg));
109 }
110
111 static void write_filter_rules(int fd)
112 {
113         struct filter_struct *ent;
114
115         write_sbuf(fd, " <<'#E#'\n");
116         for (ent = filter_list.head; ent; ent = ent->next) {
117                 unsigned int plen;
118                 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
119                 write_buf(fd, p, plen);
120                 write_sbuf(fd, ent->pattern);
121                 if (ent->match_flags & MATCHFLG_DIRECTORY)
122                         write_byte(fd, '/');
123                 write_byte(fd, eol_nulls ? 0 : '\n');
124         }
125         if (eol_nulls)
126                 write_sbuf(fd, ";\n");
127         write_sbuf(fd, "#E#");
128 }
129
130 /* This routine tries to write out an equivalent --read-batch command
131  * given the user's --write-batch args.  However, it doesn't really
132  * understand most of the options, so it uses some overly simple
133  * heuristics to munge the command line into something that will
134  * (hopefully) work. */
135 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
136 {
137         int fd, i;
138         char *p, filename[MAXPATHLEN];
139
140         stringjoin(filename, sizeof filename,
141                    batch_name, ".sh", NULL);
142         fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
143                      S_IRUSR | S_IWUSR | S_IEXEC);
144         if (fd < 0) {
145                 rsyserr(FERROR, errno, "Batch file %s open error",
146                         safe_fname(filename));
147                 exit_cleanup(1);
148         }
149
150         /* Write argvs info to BATCH.sh file */
151         write_arg(fd, argv[0]);
152         if (filter_list.head) {
153                 if (protocol_version >= 29)
154                         write_sbuf(fd, " --filter=._-");
155                 else
156                         write_sbuf(fd, " --exclude-from=-");
157         }
158         for (i = 1; i < argc - file_arg_cnt; i++) {
159                 p = argv[i];
160                 if (strncmp(p, "--files-from", 12) == 0
161                     || strncmp(p, "--filter", 8) == 0
162                     || strncmp(p, "--include", 9) == 0
163                     || strncmp(p, "--exclude", 9) == 0) {
164                         if (strchr(p, '=') == NULL)
165                                 i++;
166                         continue;
167                 }
168                 if (strcmp(p, "-f") == 0) {
169                         i++;
170                         continue;
171                 }
172                 write(fd, " ", 1);
173                 if (strncmp(p, "--write-batch", 13) == 0) {
174                         write(fd, "--read-batch", 12);
175                         if (p[13] == '=') {
176                                 write(fd, "=", 1);
177                                 write_arg(fd, p + 14);
178                         }
179                 } else
180                         write_arg(fd, p);
181         }
182         if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
183                 p = argv[argc - 1];
184         write(fd, " ${1:-", 6);
185         write_arg(fd, p);
186         write_byte(fd, '}');
187         if (filter_list.head)
188                 write_filter_rules(fd);
189         if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
190                 rsyserr(FERROR, errno, "Batch file %s write error",
191                         safe_fname(filename));
192                 exit_cleanup(1);
193         }
194 }
195
196 void show_flist(int index, struct file_struct **fptr)
197 {
198         /*  for debugging    show_flist(flist->count, flist->files * */
199
200         int i;
201         for (i = 0; i < index; i++) {
202                 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
203                 rprintf(FINFO, "flist->modtime=%#lx\n",
204                         (long unsigned) fptr[i]->modtime);
205                 rprintf(FINFO, "flist->length=%.0f\n",
206                         (double) fptr[i]->length);
207                 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
208                 rprintf(FINFO, "flist->basename=%s\n",
209                         safe_fname(fptr[i]->basename));
210                 if (fptr[i]->dirname) {
211                         rprintf(FINFO, "flist->dirname=%s\n",
212                                 safe_fname(fptr[i]->dirname));
213                 }
214                 if (am_sender && fptr[i]->dir.root) {
215                         rprintf(FINFO, "flist->dir.root=%s\n",
216                                 safe_fname(fptr[i]->dir.root));
217                 }
218         }
219 }
220
221 /* for debugging */
222 void show_argvs(int argc, char *argv[])
223 {
224         int i;
225
226         rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
227         for (i = 0; i < argc; i++)
228                 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, safe_fname(argv[i]));
229 }