We no longer force write_batch to 0 for the server.
[rsync/rsync.git] / batch.c
CommitLineData
08a740ff 1/* -*- c-file-style: "linux" -*-
71020fc3 2
6902ed17 3 Weiss 1/1999
08a740ff 4 Batch utilities for rsync.
6902ed17 5
1cd5beeb 6*/
6902ed17
MP
7
8#include "rsync.h"
9#include <time.h>
10
2b136663 11extern int am_sender;
73f0ce69 12extern int eol_nulls;
d3e182af 13extern int recurse;
6bf82264 14extern int xfer_dirs;
d3e182af
WD
15extern int preserve_links;
16extern int preserve_hard_links;
17extern int preserve_devices;
18extern int preserve_uid;
19extern int preserve_gid;
20extern int always_checksum;
e7f7064c 21extern int do_compression;
8261047b 22extern int protocol_version;
2b136663 23extern char *batch_name;
73f0ce69 24
7842418b 25extern struct filter_list_struct filter_list;
e8d3168e 26
d3e182af 27static int *flag_ptr[] = {
e7f7064c
WD
28 &recurse, /* 0 */
29 &preserve_uid, /* 1 */
30 &preserve_gid, /* 2 */
31 &preserve_links, /* 3 */
32 &preserve_devices, /* 4 */
33 &preserve_hard_links, /* 5 */
34 &always_checksum, /* 6 */
35 &xfer_dirs, /* 7 (protocol 29) */
36 &do_compression, /* 8 (protocol 29) */
d3e182af
WD
37 NULL
38};
39
40static 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)",
6bf82264 48 "--dirs (-d)",
e7f7064c 49 "--compress (-z)",
d3e182af
WD
50 NULL
51};
52
53void write_stream_flags(int fd)
54{
55 int i, flags;
56
57 /* Start the batch file with a bitmap of data-stream-affecting
58 * flags. */
e7f7064c
WD
59 if (protocol_version < 29)
60 flag_ptr[7] = NULL;
d3e182af
WD
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
68void read_stream_flags(int fd)
69{
70 int i, flags;
71
6bf82264 72 if (protocol_version < 29)
e7f7064c 73 flag_ptr[7] = NULL;
d3e182af
WD
74 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
75 int set = flags & (1 << i) ? 1 : 0;
76 if (*flag_ptr[i] != set) {
6a48e792
WD
77 if (verbose) {
78 rprintf(FINFO,
79 "%sing the %s option to match the batchfile.\n",
80 set ? "Sett" : "Clear", flag_name[i]);
81 }
d3e182af
WD
82 *flag_ptr[i] = set;
83 }
84 }
e7f7064c
WD
85 if (protocol_version < 29) {
86 if (recurse)
87 xfer_dirs |= 1;
88 else if (xfer_dirs < 2)
89 xfer_dirs = 0;
90 }
d3e182af
WD
91}
92
e7a69008
WD
93static void write_arg(int fd, char *arg)
94{
95 char *x, *s;
96
97 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
98 write(fd, arg, x - arg + 1);
99 arg += x - arg + 1;
100 }
101
102 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
103 write(fd, "'", 1);
104 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
105 write(fd, s, x - s + 1);
106 write(fd, "'", 1);
107 }
108 write(fd, s, strlen(s));
109 write(fd, "'", 1);
110 return;
111 }
112
113 write(fd, arg, strlen(arg));
114}
115
8261047b 116static void write_filter_rules(int fd)
73f0ce69 117{
7842418b 118 struct filter_struct *ent;
73f0ce69
WD
119
120 write_sbuf(fd, " <<'#E#'\n");
7842418b 121 for (ent = filter_list.head; ent; ent = ent->next) {
8261047b 122 unsigned int plen;
dd667c23 123 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
8261047b
WD
124 write_buf(fd, p, plen);
125 write_sbuf(fd, ent->pattern);
73f0ce69
WD
126 if (ent->match_flags & MATCHFLG_DIRECTORY)
127 write_byte(fd, '/');
128 write_byte(fd, eol_nulls ? 0 : '\n');
129 }
130 if (eol_nulls)
131 write_sbuf(fd, ";\n");
132 write_sbuf(fd, "#E#");
133}
134
e7a69008
WD
135/* This routine tries to write out an equivalent --read-batch command
136 * given the user's --write-batch args. However, it doesn't really
137 * understand most of the options, so it uses some overly simple
138 * heuristics to munge the command line into something that will
139 * (hopefully) work. */
140void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
6902ed17 141{
01966df4 142 int fd, i;
e7a69008 143 char *p, filename[MAXPATHLEN];
088aac85 144
893c4cc0 145 stringjoin(filename, sizeof filename,
b462781f 146 batch_name, ".sh", NULL);
01966df4
WD
147 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
148 S_IRUSR | S_IWUSR | S_IEXEC);
149 if (fd < 0) {
4875d6b6
WD
150 rsyserr(FERROR, errno, "Batch file %s open error",
151 safe_fname(filename));
1cd5beeb
MP
152 exit_cleanup(1);
153 }
088aac85 154
e7a69008
WD
155 /* Write argvs info to BATCH.sh file */
156 write_arg(fd, argv[0]);
8261047b
WD
157 if (filter_list.head) {
158 if (protocol_version >= 29)
159 write_sbuf(fd, " --filter=._-");
160 else
161 write_sbuf(fd, " --exclude-from=-");
162 }
e7a69008
WD
163 for (i = 1; i < argc - file_arg_cnt; i++) {
164 p = argv[i];
165 if (strncmp(p, "--files-from", 12) == 0
8261047b 166 || strncmp(p, "--filter", 8) == 0
73f0ce69
WD
167 || strncmp(p, "--include", 9) == 0
168 || strncmp(p, "--exclude", 9) == 0) {
e7a69008 169 if (strchr(p, '=') == NULL)
b462781f 170 i++;
b9f592fb 171 continue;
b462781f 172 }
8261047b
WD
173 if (strcmp(p, "-f") == 0) {
174 i++;
175 continue;
176 }
e7a69008
WD
177 write(fd, " ", 1);
178 if (strncmp(p, "--write-batch", 13) == 0) {
179 write(fd, "--read-batch", 12);
180 if (p[13] == '=') {
181 write(fd, "=", 1);
182 write_arg(fd, p + 14);
183 }
088aac85 184 } else
e7a69008 185 write_arg(fd, p);
1cd5beeb 186 }
4d3abf13 187 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
e7a69008
WD
188 p = argv[argc - 1];
189 write(fd, " ${1:-", 6);
190 write_arg(fd, p);
73f0ce69 191 write_byte(fd, '}');
7842418b 192 if (filter_list.head)
8261047b 193 write_filter_rules(fd);
73f0ce69 194 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
4875d6b6
WD
195 rsyserr(FERROR, errno, "Batch file %s write error",
196 safe_fname(filename));
1cd5beeb
MP
197 exit_cleanup(1);
198 }
6902ed17
MP
199}
200
6902ed17
MP
201void show_flist(int index, struct file_struct **fptr)
202{
1cd5beeb
MP
203 /* for debugging show_flist(flist->count, flist->files * */
204
205 int i;
206 for (i = 0; i < index; i++) {
207 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
208 rprintf(FINFO, "flist->modtime=%#lx\n",
209 (long unsigned) fptr[i]->modtime);
210 rprintf(FINFO, "flist->length=%.0f\n",
211 (double) fptr[i]->length);
212 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
4875d6b6
WD
213 rprintf(FINFO, "flist->basename=%s\n",
214 safe_fname(fptr[i]->basename));
215 if (fptr[i]->dirname) {
1cd5beeb 216 rprintf(FINFO, "flist->dirname=%s\n",
4875d6b6
WD
217 safe_fname(fptr[i]->dirname));
218 }
219 if (am_sender && fptr[i]->dir.root) {
2b136663 220 rprintf(FINFO, "flist->dir.root=%s\n",
4875d6b6
WD
221 safe_fname(fptr[i]->dir.root));
222 }
1cd5beeb 223 }
6902ed17
MP
224}
225
4875d6b6 226/* for debugging */
6902ed17
MP
227void show_argvs(int argc, char *argv[])
228{
1cd5beeb 229 int i;
6902ed17 230
4875d6b6
WD
231 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
232 for (i = 0; i < argc; i++)
233 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, safe_fname(argv[i]));
6902ed17 234}