Mention that --dry-run no longer conflicts with the batch options.
[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;
8261047b 21extern int protocol_version;
2b136663 22extern char *batch_name;
73f0ce69 23
7842418b 24extern struct filter_list_struct filter_list;
e8d3168e 25
d3e182af 26static int *flag_ptr[] = {
0a39837a 27 &recurse,
d3e182af
WD
28 &preserve_uid,
29 &preserve_gid,
30 &preserve_links,
31 &preserve_devices,
32 &preserve_hard_links,
33 &always_checksum,
34 NULL
35};
36
37static 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)",
6bf82264 45 "--dirs (-d)",
d3e182af
WD
46 NULL
47};
48
49void 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
62void read_stream_flags(int fd)
63{
64 int i, flags;
65
6bf82264
WD
66 if (protocol_version < 29)
67 xfer_dirs = 0;
d3e182af
WD
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) {
6a48e792
WD
71 if (verbose) {
72 rprintf(FINFO,
73 "%sing the %s option to match the batchfile.\n",
74 set ? "Sett" : "Clear", flag_name[i]);
75 }
d3e182af
WD
76 *flag_ptr[i] = set;
77 }
78 }
6bf82264
WD
79 if (protocol_version < 29)
80 xfer_dirs = recurse ? 1 : 0;
d3e182af
WD
81}
82
e7a69008
WD
83static 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
8261047b 106static void write_filter_rules(int fd)
73f0ce69 107{
7842418b 108 struct filter_struct *ent;
73f0ce69
WD
109
110 write_sbuf(fd, " <<'#E#'\n");
7842418b 111 for (ent = filter_list.head; ent; ent = ent->next) {
8261047b 112 unsigned int plen;
dd667c23 113 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
8261047b
WD
114 write_buf(fd, p, plen);
115 write_sbuf(fd, ent->pattern);
73f0ce69
WD
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
e7a69008
WD
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. */
130void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
6902ed17 131{
01966df4 132 int fd, i;
e7a69008 133 char *p, filename[MAXPATHLEN];
088aac85 134
893c4cc0 135 stringjoin(filename, sizeof filename,
b462781f 136 batch_name, ".sh", NULL);
01966df4
WD
137 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
138 S_IRUSR | S_IWUSR | S_IEXEC);
139 if (fd < 0) {
4875d6b6
WD
140 rsyserr(FERROR, errno, "Batch file %s open error",
141 safe_fname(filename));
1cd5beeb
MP
142 exit_cleanup(1);
143 }
088aac85 144
e7a69008
WD
145 /* Write argvs info to BATCH.sh file */
146 write_arg(fd, argv[0]);
8261047b
WD
147 if (filter_list.head) {
148 if (protocol_version >= 29)
149 write_sbuf(fd, " --filter=._-");
150 else
151 write_sbuf(fd, " --exclude-from=-");
152 }
e7a69008
WD
153 for (i = 1; i < argc - file_arg_cnt; i++) {
154 p = argv[i];
155 if (strncmp(p, "--files-from", 12) == 0
8261047b 156 || strncmp(p, "--filter", 8) == 0
73f0ce69
WD
157 || strncmp(p, "--include", 9) == 0
158 || strncmp(p, "--exclude", 9) == 0) {
e7a69008 159 if (strchr(p, '=') == NULL)
b462781f 160 i++;
b9f592fb 161 continue;
b462781f 162 }
8261047b
WD
163 if (strcmp(p, "-f") == 0) {
164 i++;
165 continue;
166 }
e7a69008
WD
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 }
088aac85 174 } else
e7a69008 175 write_arg(fd, p);
1cd5beeb 176 }
4d3abf13 177 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
e7a69008
WD
178 p = argv[argc - 1];
179 write(fd, " ${1:-", 6);
180 write_arg(fd, p);
73f0ce69 181 write_byte(fd, '}');
7842418b 182 if (filter_list.head)
8261047b 183 write_filter_rules(fd);
73f0ce69 184 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
4875d6b6
WD
185 rsyserr(FERROR, errno, "Batch file %s write error",
186 safe_fname(filename));
1cd5beeb
MP
187 exit_cleanup(1);
188 }
6902ed17
MP
189}
190
6902ed17
MP
191void show_flist(int index, struct file_struct **fptr)
192{
1cd5beeb
MP
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);
4875d6b6
WD
203 rprintf(FINFO, "flist->basename=%s\n",
204 safe_fname(fptr[i]->basename));
205 if (fptr[i]->dirname) {
1cd5beeb 206 rprintf(FINFO, "flist->dirname=%s\n",
4875d6b6
WD
207 safe_fname(fptr[i]->dirname));
208 }
209 if (am_sender && fptr[i]->dir.root) {
2b136663 210 rprintf(FINFO, "flist->dir.root=%s\n",
4875d6b6
WD
211 safe_fname(fptr[i]->dir.root));
212 }
1cd5beeb 213 }
6902ed17
MP
214}
215
4875d6b6 216/* for debugging */
6902ed17
MP
217void show_argvs(int argc, char *argv[])
218{
1cd5beeb 219 int i;
6902ed17 220
4875d6b6
WD
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]));
6902ed17 224}