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