Tweaked some whitespace to match the latest version from autoconf.
[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"
3cc185a0 9#include "zlib/zlib.h"
6902ed17
MP
10#include <time.h>
11
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;
3cc185a0 22extern int def_compress_level;
8261047b 23extern int protocol_version;
2b136663 24extern char *batch_name;
73f0ce69 25
7842418b 26extern struct filter_list_struct filter_list;
e8d3168e 27
3cc185a0
WD
28static int tweaked_compress_level;
29
d3e182af 30static int *flag_ptr[] = {
e7f7064c
WD
31 &recurse, /* 0 */
32 &preserve_uid, /* 1 */
33 &preserve_gid, /* 2 */
34 &preserve_links, /* 3 */
35 &preserve_devices, /* 4 */
36 &preserve_hard_links, /* 5 */
37 &always_checksum, /* 6 */
38 &xfer_dirs, /* 7 (protocol 29) */
3cc185a0 39 &tweaked_compress_level,/* 8 (protocol 29) */
d3e182af
WD
40 NULL
41};
42
43static char *flag_name[] = {
44 "--recurse (-r)",
45 "--owner (-o)",
46 "--group (-g)",
47 "--links (-l)",
48 "--devices (-D)",
49 "--hard-links (-H)",
50 "--checksum (-c)",
6bf82264 51 "--dirs (-d)",
e7f7064c 52 "--compress (-z)",
d3e182af
WD
53 NULL
54};
55
56void write_stream_flags(int fd)
57{
58 int i, flags;
59
3cc185a0
WD
60#if Z_DEFAULT_COMPRESSION == -1
61 tweaked_compress_level = do_compression ? def_compress_level + 2 : 0;
62#else
63#error internal logic error! Fix def_compress_level logic above and below too!
64#endif
65
d3e182af
WD
66 /* Start the batch file with a bitmap of data-stream-affecting
67 * flags. */
e7f7064c
WD
68 if (protocol_version < 29)
69 flag_ptr[7] = NULL;
d3e182af
WD
70 for (i = 0, flags = 0; flag_ptr[i]; i++) {
71 if (*flag_ptr[i])
72 flags |= 1 << i;
73 }
74 write_int(fd, flags);
75}
76
77void read_stream_flags(int fd)
78{
79 int i, flags;
80
6bf82264 81 if (protocol_version < 29)
e7f7064c 82 flag_ptr[7] = NULL;
d3e182af
WD
83 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
84 int set = flags & (1 << i) ? 1 : 0;
85 if (*flag_ptr[i] != set) {
6a48e792
WD
86 if (verbose) {
87 rprintf(FINFO,
88 "%sing the %s option to match the batchfile.\n",
89 set ? "Sett" : "Clear", flag_name[i]);
90 }
d3e182af
WD
91 *flag_ptr[i] = set;
92 }
93 }
e7f7064c
WD
94 if (protocol_version < 29) {
95 if (recurse)
96 xfer_dirs |= 1;
97 else if (xfer_dirs < 2)
98 xfer_dirs = 0;
99 }
3cc185a0
WD
100
101 if (tweaked_compress_level == 0 || tweaked_compress_level == 2)
102 do_compression = 0;
103 else {
104 do_compression = 1;
105 def_compress_level = tweaked_compress_level - 2;
106 }
d3e182af
WD
107}
108
e7a69008
WD
109static void write_arg(int fd, char *arg)
110{
111 char *x, *s;
112
113 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
114 write(fd, arg, x - arg + 1);
115 arg += x - arg + 1;
116 }
117
118 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
119 write(fd, "'", 1);
120 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
121 write(fd, s, x - s + 1);
122 write(fd, "'", 1);
123 }
124 write(fd, s, strlen(s));
125 write(fd, "'", 1);
126 return;
127 }
128
129 write(fd, arg, strlen(arg));
130}
131
8261047b 132static void write_filter_rules(int fd)
73f0ce69 133{
7842418b 134 struct filter_struct *ent;
73f0ce69
WD
135
136 write_sbuf(fd, " <<'#E#'\n");
7842418b 137 for (ent = filter_list.head; ent; ent = ent->next) {
8261047b 138 unsigned int plen;
dd667c23 139 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
8261047b
WD
140 write_buf(fd, p, plen);
141 write_sbuf(fd, ent->pattern);
73f0ce69
WD
142 if (ent->match_flags & MATCHFLG_DIRECTORY)
143 write_byte(fd, '/');
144 write_byte(fd, eol_nulls ? 0 : '\n');
145 }
146 if (eol_nulls)
147 write_sbuf(fd, ";\n");
148 write_sbuf(fd, "#E#");
149}
150
e7a69008
WD
151/* This routine tries to write out an equivalent --read-batch command
152 * given the user's --write-batch args. However, it doesn't really
153 * understand most of the options, so it uses some overly simple
154 * heuristics to munge the command line into something that will
155 * (hopefully) work. */
156void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
6902ed17 157{
d630f53e 158 int fd, i, len;
e7a69008 159 char *p, filename[MAXPATHLEN];
088aac85 160
893c4cc0 161 stringjoin(filename, sizeof filename,
b462781f 162 batch_name, ".sh", NULL);
01966df4
WD
163 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
164 S_IRUSR | S_IWUSR | S_IEXEC);
165 if (fd < 0) {
4875d6b6 166 rsyserr(FERROR, errno, "Batch file %s open error",
45c49b52 167 filename);
1cd5beeb
MP
168 exit_cleanup(1);
169 }
088aac85 170
e7a69008
WD
171 /* Write argvs info to BATCH.sh file */
172 write_arg(fd, argv[0]);
8261047b
WD
173 if (filter_list.head) {
174 if (protocol_version >= 29)
175 write_sbuf(fd, " --filter=._-");
176 else
177 write_sbuf(fd, " --exclude-from=-");
178 }
e7a69008
WD
179 for (i = 1; i < argc - file_arg_cnt; i++) {
180 p = argv[i];
181 if (strncmp(p, "--files-from", 12) == 0
8261047b 182 || strncmp(p, "--filter", 8) == 0
73f0ce69
WD
183 || strncmp(p, "--include", 9) == 0
184 || strncmp(p, "--exclude", 9) == 0) {
e7a69008 185 if (strchr(p, '=') == NULL)
b462781f 186 i++;
b9f592fb 187 continue;
b462781f 188 }
8261047b
WD
189 if (strcmp(p, "-f") == 0) {
190 i++;
191 continue;
192 }
e7a69008 193 write(fd, " ", 1);
d630f53e
WD
194 if (strncmp(p, "--write-batch", len = 13) == 0
195 || strncmp(p, "--only-write-batch", len = 18) == 0) {
e7a69008 196 write(fd, "--read-batch", 12);
d630f53e 197 if (p[len] == '=') {
e7a69008 198 write(fd, "=", 1);
d630f53e 199 write_arg(fd, p + len + 1);
e7a69008 200 }
088aac85 201 } else
e7a69008 202 write_arg(fd, p);
1cd5beeb 203 }
4d3abf13 204 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
e7a69008
WD
205 p = argv[argc - 1];
206 write(fd, " ${1:-", 6);
207 write_arg(fd, p);
73f0ce69 208 write_byte(fd, '}');
7842418b 209 if (filter_list.head)
8261047b 210 write_filter_rules(fd);
73f0ce69 211 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
4875d6b6 212 rsyserr(FERROR, errno, "Batch file %s write error",
45c49b52 213 filename);
1cd5beeb
MP
214 exit_cleanup(1);
215 }
6902ed17 216}