Tweaked a compound line.
[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;
e7a69008
WD
12extern int delete_mode;
13extern int delete_excluded;
73f0ce69
WD
14extern int eol_nulls;
15
16extern struct exclude_list_struct exclude_list;
e8d3168e 17
e7a69008
WD
18static void write_arg(int fd, char *arg)
19{
20 char *x, *s;
21
22 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
23 write(fd, arg, x - arg + 1);
24 arg += x - arg + 1;
25 }
26
27 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
28 write(fd, "'", 1);
29 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
30 write(fd, s, x - s + 1);
31 write(fd, "'", 1);
32 }
33 write(fd, s, strlen(s));
34 write(fd, "'", 1);
35 return;
36 }
37
38 write(fd, arg, strlen(arg));
39}
40
73f0ce69
WD
41static void write_excludes(int fd)
42{
43 struct exclude_struct *ent;
44
45 write_sbuf(fd, " <<'#E#'\n");
46 for (ent = exclude_list.head; ent; ent = ent->next) {
47 char *p = ent->pattern;
48 if (ent->match_flags & MATCHFLG_INCLUDE)
49 write_buf(fd, "+ ", 2);
50 else if (((*p == '-' || *p == '+') && p[1] == ' ')
51 || *p == '#' || *p == ';')
52 write_buf(fd, "- ", 2);
53 write_sbuf(fd, p);
54 if (ent->match_flags & MATCHFLG_DIRECTORY)
55 write_byte(fd, '/');
56 write_byte(fd, eol_nulls ? 0 : '\n');
57 }
58 if (eol_nulls)
59 write_sbuf(fd, ";\n");
60 write_sbuf(fd, "#E#");
61}
62
e7a69008
WD
63/* This routine tries to write out an equivalent --read-batch command
64 * given the user's --write-batch args. However, it doesn't really
65 * understand most of the options, so it uses some overly simple
66 * heuristics to munge the command line into something that will
67 * (hopefully) work. */
68void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
6902ed17 69{
01966df4 70 int fd, i;
e7a69008 71 char *p, filename[MAXPATHLEN];
088aac85 72
893c4cc0 73 stringjoin(filename, sizeof filename,
b462781f 74 batch_name, ".sh", NULL);
01966df4
WD
75 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
76 S_IRUSR | S_IWUSR | S_IEXEC);
77 if (fd < 0) {
d62bcc17 78 rsyserr(FERROR, errno, "Batch file %s open error", filename);
1cd5beeb
MP
79 exit_cleanup(1);
80 }
088aac85 81
e7a69008
WD
82 /* Write argvs info to BATCH.sh file */
83 write_arg(fd, argv[0]);
73f0ce69
WD
84 if (exclude_list.head)
85 write_sbuf(fd, " --exclude-from=-");
e7a69008
WD
86 for (i = 1; i < argc - file_arg_cnt; i++) {
87 p = argv[i];
88 if (strncmp(p, "--files-from", 12) == 0
73f0ce69
WD
89 || strncmp(p, "--include", 9) == 0
90 || strncmp(p, "--exclude", 9) == 0) {
e7a69008 91 if (strchr(p, '=') == NULL)
b462781f 92 i++;
b9f592fb 93 continue;
b462781f 94 }
e7a69008
WD
95 write(fd, " ", 1);
96 if (strncmp(p, "--write-batch", 13) == 0) {
97 write(fd, "--read-batch", 12);
98 if (p[13] == '=') {
99 write(fd, "=", 1);
100 write_arg(fd, p + 14);
101 }
088aac85 102 } else
e7a69008 103 write_arg(fd, p);
1cd5beeb 104 }
e7a69008
WD
105 if ((p = find_colon(argv[argc - 1])) != NULL) {
106 if (*++p == ':')
107 p++;
108 } else
109 p = argv[argc - 1];
110 write(fd, " ${1:-", 6);
111 write_arg(fd, p);
73f0ce69
WD
112 write_byte(fd, '}');
113 if (exclude_list.head)
114 write_excludes(fd);
115 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
d62bcc17 116 rsyserr(FERROR, errno, "Batch file %s write error", filename);
1cd5beeb
MP
117 exit_cleanup(1);
118 }
6902ed17
MP
119}
120
6902ed17
MP
121void show_flist(int index, struct file_struct **fptr)
122{
1cd5beeb
MP
123 /* for debugging show_flist(flist->count, flist->files * */
124
125 int i;
126 for (i = 0; i < index; i++) {
127 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
128 rprintf(FINFO, "flist->modtime=%#lx\n",
129 (long unsigned) fptr[i]->modtime);
130 rprintf(FINFO, "flist->length=%.0f\n",
131 (double) fptr[i]->length);
132 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
133 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
134 if (fptr[i]->dirname)
135 rprintf(FINFO, "flist->dirname=%s\n",
136 fptr[i]->dirname);
137 if (fptr[i]->basedir)
138 rprintf(FINFO, "flist->basedir=%s\n",
139 fptr[i]->basedir);
140 }
6902ed17
MP
141}
142
143void show_argvs(int argc, char *argv[])
144{
1cd5beeb 145 /* for debugging * */
6902ed17 146
1cd5beeb
MP
147 int i;
148 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
149 for (i = 0; i < argc; i++) {
150 /* if (argv[i]) */
151 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
6902ed17 152
1cd5beeb 153 }
6902ed17 154}