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