Tweaked the opening comments.
[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 <time.h>
10
11 extern int am_sender;
12 extern int eol_nulls;
13 extern int recurse;
14 extern int xfer_dirs;
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;
21 extern int do_compression;
22 extern int protocol_version;
23 extern char *batch_name;
24
25 extern struct filter_list_struct filter_list;
26
27 static int *flag_ptr[] = {
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) */
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)",
48         "--dirs (-d)",
49         "--compress (-z)",
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. */
59         if (protocol_version < 29)
60                 flag_ptr[7] = NULL;
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
72         if (protocol_version < 29)
73                 flag_ptr[7] = NULL;
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) {
77                         if (verbose) {
78                                 rprintf(FINFO,
79                                         "%sing the %s option to match the batchfile.\n",
80                                         set ? "Sett" : "Clear", flag_name[i]);
81                         }
82                         *flag_ptr[i] = set;
83                 }
84         }
85         if (protocol_version < 29) {
86                 if (recurse)
87                         xfer_dirs |= 1;
88                 else if (xfer_dirs < 2)
89                         xfer_dirs = 0;
90         }
91 }
92
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
116 static void write_filter_rules(int fd)
117 {
118         struct filter_struct *ent;
119
120         write_sbuf(fd, " <<'#E#'\n");
121         for (ent = filter_list.head; ent; ent = ent->next) {
122                 unsigned int plen;
123                 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
124                 write_buf(fd, p, plen);
125                 write_sbuf(fd, ent->pattern);
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
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)
141 {
142         int fd, i, len;
143         char *p, filename[MAXPATHLEN];
144
145         stringjoin(filename, sizeof filename,
146                    batch_name, ".sh", NULL);
147         fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
148                      S_IRUSR | S_IWUSR | S_IEXEC);
149         if (fd < 0) {
150                 rsyserr(FERROR, errno, "Batch file %s open error",
151                         safe_fname(filename));
152                 exit_cleanup(1);
153         }
154
155         /* Write argvs info to BATCH.sh file */
156         write_arg(fd, argv[0]);
157         if (filter_list.head) {
158                 if (protocol_version >= 29)
159                         write_sbuf(fd, " --filter=._-");
160                 else
161                         write_sbuf(fd, " --exclude-from=-");
162         }
163         for (i = 1; i < argc - file_arg_cnt; i++) {
164                 p = argv[i];
165                 if (strncmp(p, "--files-from", 12) == 0
166                     || strncmp(p, "--filter", 8) == 0
167                     || strncmp(p, "--include", 9) == 0
168                     || strncmp(p, "--exclude", 9) == 0) {
169                         if (strchr(p, '=') == NULL)
170                                 i++;
171                         continue;
172                 }
173                 if (strcmp(p, "-f") == 0) {
174                         i++;
175                         continue;
176                 }
177                 write(fd, " ", 1);
178                 if (strncmp(p, "--write-batch", len = 13) == 0
179                  || strncmp(p, "--only-write-batch", len = 18) == 0) {
180                         write(fd, "--read-batch", 12);
181                         if (p[len] == '=') {
182                                 write(fd, "=", 1);
183                                 write_arg(fd, p + len + 1);
184                         }
185                 } else
186                         write_arg(fd, p);
187         }
188         if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
189                 p = argv[argc - 1];
190         write(fd, " ${1:-", 6);
191         write_arg(fd, p);
192         write_byte(fd, '}');
193         if (filter_list.head)
194                 write_filter_rules(fd);
195         if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
196                 rsyserr(FERROR, errno, "Batch file %s write error",
197                         safe_fname(filename));
198                 exit_cleanup(1);
199         }
200 }
201
202 void show_flist(int index, struct file_struct **fptr)
203 {
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);
214                 rprintf(FINFO, "flist->basename=%s\n",
215                         safe_fname(fptr[i]->basename));
216                 if (fptr[i]->dirname) {
217                         rprintf(FINFO, "flist->dirname=%s\n",
218                                 safe_fname(fptr[i]->dirname));
219                 }
220                 if (am_sender && fptr[i]->dir.root) {
221                         rprintf(FINFO, "flist->dir.root=%s\n",
222                                 safe_fname(fptr[i]->dir.root));
223                 }
224         }
225 }
226
227 /* for debugging */
228 void show_argvs(int argc, char *argv[])
229 {
230         int i;
231
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]));
235 }