1 /* -*- c-file-style: "linux" -*-
4 Batch utilities for rsync.
11 extern char *batch_name;
12 extern int delete_mode;
13 extern int delete_excluded;
16 extern struct exclude_list_struct exclude_list;
18 static void write_arg(int fd, char *arg)
22 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
23 write(fd, arg, x - arg + 1);
27 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
29 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
30 write(fd, s, x - s + 1);
33 write(fd, s, strlen(s));
38 write(fd, arg, strlen(arg));
41 static void write_excludes(int fd)
43 struct exclude_struct *ent;
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);
54 if (ent->match_flags & MATCHFLG_DIRECTORY)
56 write_byte(fd, eol_nulls ? 0 : '\n');
59 write_sbuf(fd, ";\n");
60 write_sbuf(fd, "#E#");
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. */
68 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
71 char *p, filename[MAXPATHLEN];
73 stringjoin(filename, sizeof filename,
74 batch_name, ".sh", NULL);
75 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
76 S_IRUSR | S_IWUSR | S_IEXEC);
78 rsyserr(FERROR, errno, "Batch file %s open error", filename);
82 /* Write argvs info to BATCH.sh file */
83 write_arg(fd, argv[0]);
84 if (exclude_list.head)
85 write_sbuf(fd, " --exclude-from=-");
86 for (i = 1; i < argc - file_arg_cnt; i++) {
88 if (strncmp(p, "--files-from", 12) == 0
89 || strncmp(p, "--include", 9) == 0
90 || strncmp(p, "--exclude", 9) == 0) {
91 if (strchr(p, '=') == NULL)
96 if (strncmp(p, "--write-batch", 13) == 0) {
97 write(fd, "--read-batch", 12);
100 write_arg(fd, p + 14);
105 if ((p = find_colon(argv[argc - 1])) != NULL) {
110 write(fd, " ${1:-", 6);
113 if (exclude_list.head)
115 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
116 rsyserr(FERROR, errno, "Batch file %s write error", filename);
121 void show_flist(int index, struct file_struct **fptr)
123 /* for debugging show_flist(flist->count, flist->files * */
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",
137 if (fptr[i]->basedir)
138 rprintf(FINFO, "flist->basedir=%s\n",
143 void show_argvs(int argc, char *argv[])
145 /* for debugging * */
148 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
149 for (i = 0; i < argc; i++) {
151 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);