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