blob: 8a394d2329246a6a0e82738188b821e5212efe9c [file] [log] [blame]
Martin Pool08a740f2001-08-15 06:41:24 +00001/* -*- c-file-style: "linux" -*-
J.W. Schultz71020fc2004-02-06 04:51:09 +00002
Martin Pool6902ed12001-08-14 02:04:47 +00003 Weiss 1/1999
Martin Pool08a740f2001-08-15 06:41:24 +00004 Batch utilities for rsync.
Martin Pool6902ed12001-08-14 02:04:47 +00005
Martin Pool1cd5bee2001-12-02 22:28:50 +00006*/
Martin Pool6902ed12001-08-14 02:04:47 +00007
8#include "rsync.h"
9#include <time.h>
10
Wayne Davison9b3318b2004-07-15 16:27:02 +000011extern char *batch_name;
Wayne Davison73f0ce62004-07-20 17:07:55 +000012extern int eol_nulls;
Wayne Davisond3e182a2004-07-24 16:38:49 +000013extern int recurse;
14extern int preserve_links;
15extern int preserve_hard_links;
16extern int preserve_devices;
17extern int preserve_uid;
18extern int preserve_gid;
19extern int always_checksum;
Wayne Davison82610472005-01-25 12:13:53 +000020extern int protocol_version;
Wayne Davison73f0ce62004-07-20 17:07:55 +000021
Wayne Davison78424182005-01-25 10:39:14 +000022extern struct filter_list_struct filter_list;
Martin Pool6902ed12001-08-14 02:04:47 +000023
Wayne Davisond3e182a2004-07-24 16:38:49 +000024static int *flag_ptr[] = {
25 &recurse,
26 &preserve_uid,
27 &preserve_gid,
28 &preserve_links,
29 &preserve_devices,
30 &preserve_hard_links,
31 &always_checksum,
32 NULL
33};
34
35static char *flag_name[] = {
36 "--recurse (-r)",
37 "--owner (-o)",
38 "--group (-g)",
39 "--links (-l)",
40 "--devices (-D)",
41 "--hard-links (-H)",
42 "--checksum (-c)",
43 NULL
44};
45
46void write_stream_flags(int fd)
47{
48 int i, flags;
49
50 /* Start the batch file with a bitmap of data-stream-affecting
51 * flags. */
52 for (i = 0, flags = 0; flag_ptr[i]; i++) {
53 if (*flag_ptr[i])
54 flags |= 1 << i;
55 }
56 write_int(fd, flags);
57}
58
59void read_stream_flags(int fd)
60{
61 int i, flags;
62
63 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
64 int set = flags & (1 << i) ? 1 : 0;
65 if (*flag_ptr[i] != set) {
Wayne Davison6a48e792004-07-24 16:51:16 +000066 if (verbose) {
67 rprintf(FINFO,
68 "%sing the %s option to match the batchfile.\n",
69 set ? "Sett" : "Clear", flag_name[i]);
70 }
Wayne Davisond3e182a2004-07-24 16:38:49 +000071 *flag_ptr[i] = set;
72 }
73 }
74}
75
Wayne Davisone7a69002004-07-19 08:27:17 +000076static void write_arg(int fd, char *arg)
77{
78 char *x, *s;
79
80 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
81 write(fd, arg, x - arg + 1);
82 arg += x - arg + 1;
83 }
84
85 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
86 write(fd, "'", 1);
87 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
88 write(fd, s, x - s + 1);
89 write(fd, "'", 1);
90 }
91 write(fd, s, strlen(s));
92 write(fd, "'", 1);
93 return;
94 }
95
96 write(fd, arg, strlen(arg));
97}
98
Wayne Davison82610472005-01-25 12:13:53 +000099static void write_filter_rules(int fd)
Wayne Davison73f0ce62004-07-20 17:07:55 +0000100{
Wayne Davison78424182005-01-25 10:39:14 +0000101 struct filter_struct *ent;
Wayne Davison73f0ce62004-07-20 17:07:55 +0000102
103 write_sbuf(fd, " <<'#E#'\n");
Wayne Davison78424182005-01-25 10:39:14 +0000104 for (ent = filter_list.head; ent; ent = ent->next) {
Wayne Davison82610472005-01-25 12:13:53 +0000105 unsigned int plen;
106 char *p = get_rule_prefix(ent->match_flags, "- ", &plen);
107 write_buf(fd, p, plen);
108 write_sbuf(fd, ent->pattern);
Wayne Davison73f0ce62004-07-20 17:07:55 +0000109 if (ent->match_flags & MATCHFLG_DIRECTORY)
110 write_byte(fd, '/');
111 write_byte(fd, eol_nulls ? 0 : '\n');
112 }
113 if (eol_nulls)
114 write_sbuf(fd, ";\n");
115 write_sbuf(fd, "#E#");
116}
117
Wayne Davisone7a69002004-07-19 08:27:17 +0000118/* This routine tries to write out an equivalent --read-batch command
119 * given the user's --write-batch args. However, it doesn't really
120 * understand most of the options, so it uses some overly simple
121 * heuristics to munge the command line into something that will
122 * (hopefully) work. */
123void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
Martin Pool6902ed12001-08-14 02:04:47 +0000124{
Wayne Davison01966df2004-07-14 07:20:18 +0000125 int fd, i;
Wayne Davisone7a69002004-07-19 08:27:17 +0000126 char *p, filename[MAXPATHLEN];
Martin Pool6902ed12001-08-14 02:04:47 +0000127
Wayne Davison893c4cc2004-01-20 04:56:20 +0000128 stringjoin(filename, sizeof filename,
Wayne Davisonb4627812004-07-19 00:53:49 +0000129 batch_name, ".sh", NULL);
Wayne Davison01966df2004-07-14 07:20:18 +0000130 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
131 S_IRUSR | S_IWUSR | S_IEXEC);
132 if (fd < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000133 rsyserr(FERROR, errno, "Batch file %s open error", filename);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000134 exit_cleanup(1);
135 }
David Dykstra088aac82002-02-06 21:20:48 +0000136
Wayne Davisone7a69002004-07-19 08:27:17 +0000137 /* Write argvs info to BATCH.sh file */
138 write_arg(fd, argv[0]);
Wayne Davison82610472005-01-25 12:13:53 +0000139 if (filter_list.head) {
140 if (protocol_version >= 29)
141 write_sbuf(fd, " --filter=._-");
142 else
143 write_sbuf(fd, " --exclude-from=-");
144 }
Wayne Davisone7a69002004-07-19 08:27:17 +0000145 for (i = 1; i < argc - file_arg_cnt; i++) {
146 p = argv[i];
147 if (strncmp(p, "--files-from", 12) == 0
Wayne Davison82610472005-01-25 12:13:53 +0000148 || strncmp(p, "--filter", 8) == 0
Wayne Davison73f0ce62004-07-20 17:07:55 +0000149 || strncmp(p, "--include", 9) == 0
150 || strncmp(p, "--exclude", 9) == 0) {
Wayne Davisone7a69002004-07-19 08:27:17 +0000151 if (strchr(p, '=') == NULL)
Wayne Davisonb4627812004-07-19 00:53:49 +0000152 i++;
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000153 continue;
Wayne Davisonb4627812004-07-19 00:53:49 +0000154 }
Wayne Davison82610472005-01-25 12:13:53 +0000155 if (strcmp(p, "-f") == 0) {
156 i++;
157 continue;
158 }
Wayne Davisone7a69002004-07-19 08:27:17 +0000159 write(fd, " ", 1);
160 if (strncmp(p, "--write-batch", 13) == 0) {
161 write(fd, "--read-batch", 12);
162 if (p[13] == '=') {
163 write(fd, "=", 1);
164 write_arg(fd, p + 14);
165 }
David Dykstra088aac82002-02-06 21:20:48 +0000166 } else
Wayne Davisone7a69002004-07-19 08:27:17 +0000167 write_arg(fd, p);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000168 }
Wayne Davisone7a69002004-07-19 08:27:17 +0000169 if ((p = find_colon(argv[argc - 1])) != NULL) {
170 if (*++p == ':')
171 p++;
172 } else
173 p = argv[argc - 1];
174 write(fd, " ${1:-", 6);
175 write_arg(fd, p);
Wayne Davison73f0ce62004-07-20 17:07:55 +0000176 write_byte(fd, '}');
Wayne Davison78424182005-01-25 10:39:14 +0000177 if (filter_list.head)
Wayne Davison82610472005-01-25 12:13:53 +0000178 write_filter_rules(fd);
Wayne Davison73f0ce62004-07-20 17:07:55 +0000179 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000180 rsyserr(FERROR, errno, "Batch file %s write error", filename);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000181 exit_cleanup(1);
182 }
Martin Pool6902ed12001-08-14 02:04:47 +0000183}
184
Martin Pool6902ed12001-08-14 02:04:47 +0000185void show_flist(int index, struct file_struct **fptr)
186{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000187 /* for debugging show_flist(flist->count, flist->files * */
Martin Pool6902ed12001-08-14 02:04:47 +0000188
Martin Pool1cd5bee2001-12-02 22:28:50 +0000189 int i;
190 for (i = 0; i < index; i++) {
191 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
192 rprintf(FINFO, "flist->modtime=%#lx\n",
193 (long unsigned) fptr[i]->modtime);
194 rprintf(FINFO, "flist->length=%.0f\n",
195 (double) fptr[i]->length);
196 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
197 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
198 if (fptr[i]->dirname)
199 rprintf(FINFO, "flist->dirname=%s\n",
200 fptr[i]->dirname);
201 if (fptr[i]->basedir)
202 rprintf(FINFO, "flist->basedir=%s\n",
203 fptr[i]->basedir);
204 }
Martin Pool6902ed12001-08-14 02:04:47 +0000205}
206
207void show_argvs(int argc, char *argv[])
208{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000209 /* for debugging * */
Martin Pool6902ed12001-08-14 02:04:47 +0000210
Martin Pool1cd5bee2001-12-02 22:28:50 +0000211 int i;
212 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
213 for (i = 0; i < argc; i++) {
214 /* if (argv[i]) */
215 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
Martin Pool6902ed12001-08-14 02:04:47 +0000216
Martin Pool1cd5bee2001-12-02 22:28:50 +0000217 }
Martin Pool6902ed12001-08-14 02:04:47 +0000218}