If there is no lchown(), don't try to set the user & group of a symlink.
[rsync/rsync.git] / batch.c
CommitLineData
08a740ff 1/* -*- c-file-style: "linux" -*-
71020fc3 2
6902ed17 3 Weiss 1/1999
08a740ff 4 Batch utilities for rsync.
6902ed17 5
1cd5beeb 6*/
6902ed17
MP
7
8#include "rsync.h"
9#include <time.h>
10
9b3318b0 11extern char *batch_name;
73f0ce69 12extern int eol_nulls;
d3e182af
WD
13extern 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;
73f0ce69
WD
20
21extern struct exclude_list_struct exclude_list;
e8d3168e 22
d3e182af
WD
23static 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
34static 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
45void 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
58void 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) {
6a48e792
WD
65 if (verbose) {
66 rprintf(FINFO,
67 "%sing the %s option to match the batchfile.\n",
68 set ? "Sett" : "Clear", flag_name[i]);
69 }
d3e182af
WD
70 *flag_ptr[i] = set;
71 }
72 }
73}
74
e7a69008
WD
75static 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
73f0ce69
WD
98static void write_excludes(int fd)
99{
100 struct exclude_struct *ent;
101
102 write_sbuf(fd, " <<'#E#'\n");
103 for (ent = exclude_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
e7a69008
WD
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. */
125void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
6902ed17 126{
01966df4 127 int fd, i;
e7a69008 128 char *p, filename[MAXPATHLEN];
088aac85 129
893c4cc0 130 stringjoin(filename, sizeof filename,
b462781f 131 batch_name, ".sh", NULL);
01966df4
WD
132 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
133 S_IRUSR | S_IWUSR | S_IEXEC);
134 if (fd < 0) {
d62bcc17 135 rsyserr(FERROR, errno, "Batch file %s open error", filename);
1cd5beeb
MP
136 exit_cleanup(1);
137 }
088aac85 138
e7a69008
WD
139 /* Write argvs info to BATCH.sh file */
140 write_arg(fd, argv[0]);
73f0ce69
WD
141 if (exclude_list.head)
142 write_sbuf(fd, " --exclude-from=-");
e7a69008
WD
143 for (i = 1; i < argc - file_arg_cnt; i++) {
144 p = argv[i];
145 if (strncmp(p, "--files-from", 12) == 0
73f0ce69
WD
146 || strncmp(p, "--include", 9) == 0
147 || strncmp(p, "--exclude", 9) == 0) {
e7a69008 148 if (strchr(p, '=') == NULL)
b462781f 149 i++;
b9f592fb 150 continue;
b462781f 151 }
e7a69008
WD
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 }
088aac85 159 } else
e7a69008 160 write_arg(fd, p);
1cd5beeb 161 }
e7a69008
WD
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);
73f0ce69
WD
169 write_byte(fd, '}');
170 if (exclude_list.head)
171 write_excludes(fd);
172 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
d62bcc17 173 rsyserr(FERROR, errno, "Batch file %s write error", filename);
1cd5beeb
MP
174 exit_cleanup(1);
175 }
6902ed17
MP
176}
177
6902ed17
MP
178void show_flist(int index, struct file_struct **fptr)
179{
1cd5beeb
MP
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 }
6902ed17
MP
198}
199
200void show_argvs(int argc, char *argv[])
201{
1cd5beeb 202 /* for debugging * */
6902ed17 203
1cd5beeb
MP
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]);
6902ed17 209
1cd5beeb 210 }
6902ed17 211}