Improved a sentence.
[rsync.git] / batch.c
blob8aedd8632648fbadcb6487261c51b5e50062881c
1 /* -*- c-file-style: "linux" -*-
3 Weiss 1/1999
4 Batch utilities for rsync.
6 */
8 #include "rsync.h"
9 #include <time.h>
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;
25 extern struct filter_list_struct filter_list;
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
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
53 void write_stream_flags(int fd)
55 int i, flags;
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;
65 write_int(fd, flags);
68 void read_stream_flags(int fd)
70 int i, flags;
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]);
82 *flag_ptr[i] = set;
85 if (protocol_version < 29) {
86 if (recurse)
87 xfer_dirs |= 1;
88 else if (xfer_dirs < 2)
89 xfer_dirs = 0;
93 static void write_arg(int fd, char *arg)
95 char *x, *s;
97 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
98 write(fd, arg, x - arg + 1);
99 arg += x - arg + 1;
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);
108 write(fd, s, strlen(s));
109 write(fd, "'", 1);
110 return;
113 write(fd, arg, strlen(arg));
116 static void write_filter_rules(int fd)
118 struct filter_struct *ent;
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');
130 if (eol_nulls)
131 write_sbuf(fd, ";\n");
132 write_sbuf(fd, "#E#");
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)
142 int fd, i, len;
143 char *p, filename[MAXPATHLEN];
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);
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=-");
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;
173 if (strcmp(p, "-f") == 0) {
174 i++;
175 continue;
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);
185 } else
186 write_arg(fd, p);
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);
202 void show_flist(int index, struct file_struct **fptr)
204 /* for debugging show_flist(flist->count, flist->files * */
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));
220 if (am_sender && fptr[i]->dir.root) {
221 rprintf(FINFO, "flist->dir.root=%s\n",
222 safe_fname(fptr[i]->dir.root));
227 /* for debugging */
228 void show_argvs(int argc, char *argv[])
230 int i;
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]));