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