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