Commit | Line | Data |
---|---|---|
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 | 11 | extern int am_sender; |
73f0ce69 | 12 | extern int eol_nulls; |
d3e182af | 13 | extern int recurse; |
6bf82264 | 14 | extern int xfer_dirs; |
d3e182af WD |
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; | |
e7f7064c | 21 | extern int do_compression; |
8261047b | 22 | extern int protocol_version; |
2b136663 | 23 | extern char *batch_name; |
73f0ce69 | 24 | |
7842418b | 25 | extern struct filter_list_struct filter_list; |
e8d3168e | 26 | |
d3e182af | 27 | static 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 | ||
40 | static 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 | ||
53 | void 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 | ||
68 | void 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 |
93 | static 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 | 116 | static 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. */ | |
140 | void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt) | |
6902ed17 | 141 | { |
d630f53e | 142 | int fd, i, len; |
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 | 177 | write(fd, " ", 1); |
d630f53e WD |
178 | if (strncmp(p, "--write-batch", len = 13) == 0 |
179 | || strncmp(p, "--only-write-batch", len = 18) == 0) { | |
e7a69008 | 180 | write(fd, "--read-batch", 12); |
d630f53e | 181 | if (p[len] == '=') { |
e7a69008 | 182 | write(fd, "=", 1); |
d630f53e | 183 | write_arg(fd, p + len + 1); |
e7a69008 | 184 | } |
088aac85 | 185 | } else |
e7a69008 | 186 | write_arg(fd, p); |
1cd5beeb | 187 | } |
4d3abf13 | 188 | if (!(p = check_for_hostspec(argv[argc - 1], &p, &i))) |
e7a69008 WD |
189 | p = argv[argc - 1]; |
190 | write(fd, " ${1:-", 6); | |
191 | write_arg(fd, p); | |
73f0ce69 | 192 | write_byte(fd, '}'); |
7842418b | 193 | if (filter_list.head) |
8261047b | 194 | write_filter_rules(fd); |
73f0ce69 | 195 | if (write(fd, "\n", 1) != 1 || close(fd) < 0) { |
4875d6b6 WD |
196 | rsyserr(FERROR, errno, "Batch file %s write error", |
197 | safe_fname(filename)); | |
1cd5beeb MP |
198 | exit_cleanup(1); |
199 | } | |
6902ed17 MP |
200 | } |
201 | ||
6902ed17 MP |
202 | void show_flist(int index, struct file_struct **fptr) |
203 | { | |
1cd5beeb MP |
204 | /* for debugging show_flist(flist->count, flist->files * */ |
205 | ||
206 | int i; | |
207 | for (i = 0; i < index; i++) { | |
208 | rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags); | |
209 | rprintf(FINFO, "flist->modtime=%#lx\n", | |
210 | (long unsigned) fptr[i]->modtime); | |
211 | rprintf(FINFO, "flist->length=%.0f\n", | |
212 | (double) fptr[i]->length); | |
213 | rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode); | |
4875d6b6 WD |
214 | rprintf(FINFO, "flist->basename=%s\n", |
215 | safe_fname(fptr[i]->basename)); | |
216 | if (fptr[i]->dirname) { | |
1cd5beeb | 217 | rprintf(FINFO, "flist->dirname=%s\n", |
4875d6b6 WD |
218 | safe_fname(fptr[i]->dirname)); |
219 | } | |
220 | if (am_sender && fptr[i]->dir.root) { | |
2b136663 | 221 | rprintf(FINFO, "flist->dir.root=%s\n", |
4875d6b6 WD |
222 | safe_fname(fptr[i]->dir.root)); |
223 | } | |
1cd5beeb | 224 | } |
6902ed17 MP |
225 | } |
226 | ||
4875d6b6 | 227 | /* for debugging */ |
6902ed17 MP |
228 | void show_argvs(int argc, char *argv[]) |
229 | { | |
1cd5beeb | 230 | int i; |
6902ed17 | 231 | |
4875d6b6 WD |
232 | rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc); |
233 | for (i = 0; i < argc; i++) | |
234 | rprintf(FINFO, "i=%d,argv[i]=%s\n", i, safe_fname(argv[i])); | |
6902ed17 | 235 | } |