1 /* -*- c-file-style: "linux" -*-
4 Batch utilities for rsync.
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;
22 extern int do_compression;
23 extern int def_compress_level;
24 extern int protocol_version;
25 extern char *batch_name;
27 extern struct filter_list_struct filter_list;
29 static int tweaked_compress_level;
31 static int *flag_ptr[] = {
33 &preserve_uid, /* 1 */
34 &preserve_gid, /* 2 */
35 &preserve_links, /* 3 */
36 &preserve_devices, /* 4 */
37 &preserve_hard_links, /* 5 */
38 &always_checksum, /* 6 */
39 &xfer_dirs, /* 7 (protocol 29) */
40 &tweaked_compress_level,/* 8 (protocol 29) */
44 static char *flag_name[] = {
57 void write_stream_flags(int fd)
61 #if Z_DEFAULT_COMPRESSION == -1
62 tweaked_compress_level = do_compression ? def_compress_level + 2 : 0;
64 #error internal logic error! Fix def_compress_level logic above and below too!
67 /* Start the batch file with a bitmap of data-stream-affecting
69 if (protocol_version < 29)
71 for (i = 0, flags = 0; flag_ptr[i]; i++) {
78 void read_stream_flags(int fd)
82 if (protocol_version < 29)
84 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
85 int set = flags & (1 << i) ? 1 : 0;
86 if (*flag_ptr[i] != set) {
89 "%sing the %s option to match the batchfile.\n",
90 set ? "Sett" : "Clear", flag_name[i]);
95 if (protocol_version < 29) {
98 else if (xfer_dirs < 2)
102 if (tweaked_compress_level == 0 || tweaked_compress_level == 2)
106 def_compress_level = tweaked_compress_level - 2;
110 static void write_arg(int fd, char *arg)
114 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
115 write(fd, arg, x - arg + 1);
119 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
121 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
122 write(fd, s, x - s + 1);
125 write(fd, s, strlen(s));
130 write(fd, arg, strlen(arg));
133 static void write_filter_rules(int fd)
135 struct filter_struct *ent;
137 write_sbuf(fd, " <<'#E#'\n");
138 for (ent = filter_list.head; ent; ent = ent->next) {
140 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
141 write_buf(fd, p, plen);
142 write_sbuf(fd, ent->pattern);
143 if (ent->match_flags & MATCHFLG_DIRECTORY)
145 write_byte(fd, eol_nulls ? 0 : '\n');
148 write_sbuf(fd, ";\n");
149 write_sbuf(fd, "#E#");
152 /* This routine tries to write out an equivalent --read-batch command
153 * given the user's --write-batch args. However, it doesn't really
154 * understand most of the options, so it uses some overly simple
155 * heuristics to munge the command line into something that will
156 * (hopefully) work. */
157 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
160 char *p, filename[MAXPATHLEN];
162 stringjoin(filename, sizeof filename,
163 batch_name, ".sh", NULL);
164 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
165 S_IRUSR | S_IWUSR | S_IEXEC);
167 rsyserr(FERROR, errno, "Batch file %s open error",
168 safe_fname(filename));
172 /* Write argvs info to BATCH.sh file */
173 write_arg(fd, argv[0]);
174 if (filter_list.head) {
175 if (protocol_version >= 29)
176 write_sbuf(fd, " --filter=._-");
178 write_sbuf(fd, " --exclude-from=-");
180 for (i = 1; i < argc - file_arg_cnt; i++) {
182 if (strncmp(p, "--files-from", 12) == 0
183 || strncmp(p, "--filter", 8) == 0
184 || strncmp(p, "--include", 9) == 0
185 || strncmp(p, "--exclude", 9) == 0) {
186 if (strchr(p, '=') == NULL)
190 if (strcmp(p, "-f") == 0) {
195 if (strncmp(p, "--write-batch", len = 13) == 0
196 || strncmp(p, "--only-write-batch", len = 18) == 0) {
197 write(fd, "--read-batch", 12);
200 write_arg(fd, p + len + 1);
205 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
207 write(fd, " ${1:-", 6);
210 if (filter_list.head)
211 write_filter_rules(fd);
212 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
213 rsyserr(FERROR, errno, "Batch file %s write error",
214 safe_fname(filename));
219 void show_flist(int index, struct file_struct **fptr)
221 /* for debugging show_flist(flist->count, flist->files * */
224 for (i = 0; i < index; i++) {
225 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
226 rprintf(FINFO, "flist->modtime=%#lx\n",
227 (long unsigned) fptr[i]->modtime);
228 rprintf(FINFO, "flist->length=%.0f\n",
229 (double) fptr[i]->length);
230 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
231 rprintf(FINFO, "flist->basename=%s\n",
232 safe_fname(fptr[i]->basename));
233 if (fptr[i]->dirname) {
234 rprintf(FINFO, "flist->dirname=%s\n",
235 safe_fname(fptr[i]->dirname));
237 if (am_sender && fptr[i]->dir.root) {
238 rprintf(FINFO, "flist->dir.root=%s\n",
239 safe_fname(fptr[i]->dir.root));
245 void show_argvs(int argc, char *argv[])
249 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
250 for (i = 0; i < argc; i++)
251 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, safe_fname(argv[i]));