Renamed several exclude-related functions/variables using new
[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 <time.h>
10
11 extern char *batch_name;
12 extern int eol_nulls;
13 extern int recurse;
14 extern int preserve_links;
15 extern int preserve_hard_links;
16 extern int preserve_devices;
17 extern int preserve_uid;
18 extern int preserve_gid;
19 extern int always_checksum;
20
21 extern struct filter_list_struct filter_list;
22
23 static int *flag_ptr[] = {
24         &recurse,
25         &preserve_uid,
26         &preserve_gid,
27         &preserve_links,
28         &preserve_devices,
29         &preserve_hard_links,
30         &always_checksum,
31         NULL
32 };
33
34 static char *flag_name[] = {
35         "--recurse (-r)",
36         "--owner (-o)",
37         "--group (-g)",
38         "--links (-l)",
39         "--devices (-D)",
40         "--hard-links (-H)",
41         "--checksum (-c)",
42         NULL
43 };
44
45 void write_stream_flags(int fd)
46 {
47         int i, flags;
48
49         /* Start the batch file with a bitmap of data-stream-affecting
50          * flags. */
51         for (i = 0, flags = 0; flag_ptr[i]; i++) {
52                 if (*flag_ptr[i])
53                         flags |= 1 << i;
54         }
55         write_int(fd, flags);
56 }
57
58 void read_stream_flags(int fd)
59 {
60         int i, flags;
61
62         for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
63                 int set = flags & (1 << i) ? 1 : 0;
64                 if (*flag_ptr[i] != set) {
65                         if (verbose) {
66                                 rprintf(FINFO,
67                                         "%sing the %s option to match the batchfile.\n",
68                                         set ? "Sett" : "Clear", flag_name[i]);
69                         }
70                         *flag_ptr[i] = set;
71                 }
72         }
73 }
74
75 static void write_arg(int fd, char *arg)
76 {
77         char *x, *s;
78
79         if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
80                 write(fd, arg, x - arg + 1);
81                 arg += x - arg + 1;
82         }
83
84         if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
85                 write(fd, "'", 1);
86                 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
87                         write(fd, s, x - s + 1);
88                         write(fd, "'", 1);
89                 }
90                 write(fd, s, strlen(s));
91                 write(fd, "'", 1);
92                 return;
93         }
94
95         write(fd, arg, strlen(arg));
96 }
97
98 static void write_filters(int fd)
99 {
100         struct filter_struct *ent;
101
102         write_sbuf(fd, " <<'#E#'\n");
103         for (ent = filter_list.head; ent; ent = ent->next) {
104                 char *p = ent->pattern;
105                 if (ent->match_flags & MATCHFLG_INCLUDE)
106                         write_buf(fd, "+ ", 2);
107                 else if (((*p == '-' || *p == '+') && p[1] == ' ')
108                     || *p == '#' || *p == ';')
109                         write_buf(fd, "- ", 2);
110                 write_sbuf(fd, p);
111                 if (ent->match_flags & MATCHFLG_DIRECTORY)
112                         write_byte(fd, '/');
113                 write_byte(fd, eol_nulls ? 0 : '\n');
114         }
115         if (eol_nulls)
116                 write_sbuf(fd, ";\n");
117         write_sbuf(fd, "#E#");
118 }
119
120 /* This routine tries to write out an equivalent --read-batch command
121  * given the user's --write-batch args.  However, it doesn't really
122  * understand most of the options, so it uses some overly simple
123  * heuristics to munge the command line into something that will
124  * (hopefully) work. */
125 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
126 {
127         int fd, i;
128         char *p, filename[MAXPATHLEN];
129
130         stringjoin(filename, sizeof filename,
131                    batch_name, ".sh", NULL);
132         fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
133                      S_IRUSR | S_IWUSR | S_IEXEC);
134         if (fd < 0) {
135                 rsyserr(FERROR, errno, "Batch file %s open error", filename);
136                 exit_cleanup(1);
137         }
138
139         /* Write argvs info to BATCH.sh file */
140         write_arg(fd, argv[0]);
141         if (filter_list.head)
142                 write_sbuf(fd, " --exclude-from=-");
143         for (i = 1; i < argc - file_arg_cnt; i++) {
144                 p = argv[i];
145                 if (strncmp(p, "--files-from", 12) == 0
146                     || strncmp(p, "--include", 9) == 0
147                     || strncmp(p, "--exclude", 9) == 0) {
148                         if (strchr(p, '=') == NULL)
149                                 i++;
150                         continue;
151                 }
152                 write(fd, " ", 1);
153                 if (strncmp(p, "--write-batch", 13) == 0) {
154                         write(fd, "--read-batch", 12);
155                         if (p[13] == '=') {
156                                 write(fd, "=", 1);
157                                 write_arg(fd, p + 14);
158                         }
159                 } else
160                         write_arg(fd, p);
161         }
162         if ((p = find_colon(argv[argc - 1])) != NULL) {
163                 if (*++p == ':')
164                         p++;
165         } else
166                 p = argv[argc - 1];
167         write(fd, " ${1:-", 6);
168         write_arg(fd, p);
169         write_byte(fd, '}');
170         if (filter_list.head)
171                 write_filters(fd);
172         if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
173                 rsyserr(FERROR, errno, "Batch file %s write error", filename);
174                 exit_cleanup(1);
175         }
176 }
177
178 void show_flist(int index, struct file_struct **fptr)
179 {
180         /*  for debugging    show_flist(flist->count, flist->files * */
181
182         int i;
183         for (i = 0; i < index; i++) {
184                 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
185                 rprintf(FINFO, "flist->modtime=%#lx\n",
186                         (long unsigned) fptr[i]->modtime);
187                 rprintf(FINFO, "flist->length=%.0f\n",
188                         (double) fptr[i]->length);
189                 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
190                 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
191                 if (fptr[i]->dirname)
192                         rprintf(FINFO, "flist->dirname=%s\n",
193                                 fptr[i]->dirname);
194                 if (fptr[i]->basedir)
195                         rprintf(FINFO, "flist->basedir=%s\n",
196                                 fptr[i]->basedir);
197         }
198 }
199
200 void show_argvs(int argc, char *argv[])
201 {
202         /*  for debugging  * */
203
204         int i;
205         rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
206         for (i = 0; i < argc; i++) {
207                 /*    if (argv[i])   */
208                 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
209
210         }
211 }