Handle new def_compress_level setting.
[rsync/rsync.git] / batch.c
1 /* -*- c-file-style: "linux" -*-
2
3    Weiss 1/1999
4    Batch utilities for rsync.
5
6 */
7
8 #include "rsync.h"
9 #include "zlib/zlib.h"
10 #include <time.h>
11
12 extern int am_sender;
13 extern int eol_nulls;
14 extern int recurse;
15 extern int xfer_dirs;
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;
26
27 extern struct filter_list_struct filter_list;
28
29 static int tweaked_compress_level;
30
31 static int *flag_ptr[] = {
32         &recurse,               /* 0 */
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) */
41         NULL
42 };
43
44 static char *flag_name[] = {
45         "--recurse (-r)",
46         "--owner (-o)",
47         "--group (-g)",
48         "--links (-l)",
49         "--devices (-D)",
50         "--hard-links (-H)",
51         "--checksum (-c)",
52         "--dirs (-d)",
53         "--compress (-z)",
54         NULL
55 };
56
57 void write_stream_flags(int fd)
58 {
59         int i, flags;
60
61 #if Z_DEFAULT_COMPRESSION == -1
62         tweaked_compress_level = do_compression ? def_compress_level + 2 : 0;
63 #else
64 #error internal logic error!  Fix def_compress_level logic above and below too!
65 #endif
66
67         /* Start the batch file with a bitmap of data-stream-affecting
68          * flags. */
69         if (protocol_version < 29)
70                 flag_ptr[7] = NULL;
71         for (i = 0, flags = 0; flag_ptr[i]; i++) {
72                 if (*flag_ptr[i])
73                         flags |= 1 << i;
74         }
75         write_int(fd, flags);
76 }
77
78 void read_stream_flags(int fd)
79 {
80         int i, flags;
81
82         if (protocol_version < 29)
83                 flag_ptr[7] = NULL;
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) {
87                         if (verbose) {
88                                 rprintf(FINFO,
89                                         "%sing the %s option to match the batchfile.\n",
90                                         set ? "Sett" : "Clear", flag_name[i]);
91                         }
92                         *flag_ptr[i] = set;
93                 }
94         }
95         if (protocol_version < 29) {
96                 if (recurse)
97                         xfer_dirs |= 1;
98                 else if (xfer_dirs < 2)
99                         xfer_dirs = 0;
100         }
101
102         if (tweaked_compress_level == 0 || tweaked_compress_level == 2)
103                 do_compression = 0;
104         else {
105                 do_compression = 1;
106                 def_compress_level = tweaked_compress_level - 2;
107         }
108 }
109
110 static void write_arg(int fd, char *arg)
111 {
112         char *x, *s;
113
114         if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
115                 write(fd, arg, x - arg + 1);
116                 arg += x - arg + 1;
117         }
118
119         if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
120                 write(fd, "'", 1);
121                 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
122                         write(fd, s, x - s + 1);
123                         write(fd, "'", 1);
124                 }
125                 write(fd, s, strlen(s));
126                 write(fd, "'", 1);
127                 return;
128         }
129
130         write(fd, arg, strlen(arg));
131 }
132
133 static void write_filter_rules(int fd)
134 {
135         struct filter_struct *ent;
136
137         write_sbuf(fd, " <<'#E#'\n");
138         for (ent = filter_list.head; ent; ent = ent->next) {
139                 unsigned int plen;
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)
144                         write_byte(fd, '/');
145                 write_byte(fd, eol_nulls ? 0 : '\n');
146         }
147         if (eol_nulls)
148                 write_sbuf(fd, ";\n");
149         write_sbuf(fd, "#E#");
150 }
151
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)
158 {
159         int fd, i, len;
160         char *p, filename[MAXPATHLEN];
161
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);
166         if (fd < 0) {
167                 rsyserr(FERROR, errno, "Batch file %s open error",
168                         safe_fname(filename));
169                 exit_cleanup(1);
170         }
171
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=._-");
177                 else
178                         write_sbuf(fd, " --exclude-from=-");
179         }
180         for (i = 1; i < argc - file_arg_cnt; i++) {
181                 p = argv[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)
187                                 i++;
188                         continue;
189                 }
190                 if (strcmp(p, "-f") == 0) {
191                         i++;
192                         continue;
193                 }
194                 write(fd, " ", 1);
195                 if (strncmp(p, "--write-batch", len = 13) == 0
196                  || strncmp(p, "--only-write-batch", len = 18) == 0) {
197                         write(fd, "--read-batch", 12);
198                         if (p[len] == '=') {
199                                 write(fd, "=", 1);
200                                 write_arg(fd, p + len + 1);
201                         }
202                 } else
203                         write_arg(fd, p);
204         }
205         if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
206                 p = argv[argc - 1];
207         write(fd, " ${1:-", 6);
208         write_arg(fd, p);
209         write_byte(fd, '}');
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));
215                 exit_cleanup(1);
216         }
217 }
218
219 void show_flist(int index, struct file_struct **fptr)
220 {
221         /*  for debugging    show_flist(flist->count, flist->files * */
222
223         int i;
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));
236                 }
237                 if (am_sender && fptr[i]->dir.root) {
238                         rprintf(FINFO, "flist->dir.root=%s\n",
239                                 safe_fname(fptr[i]->dir.root));
240                 }
241         }
242 }
243
244 /* for debugging */
245 void show_argvs(int argc, char *argv[])
246 {
247         int i;
248
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]));
252 }