| 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 | |
| 11 | extern char *batch_name; |
| 12 | extern int delete_mode; |
| 13 | extern int delete_excluded; |
| 14 | extern int eol_nulls; |
| 15 | |
| 16 | extern struct exclude_list_struct exclude_list; |
| 17 | |
| 18 | static 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 | |
| 41 | static 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 | |
| 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) |
| 69 | { |
| 70 | int fd, i; |
| 71 | char *p, filename[MAXPATHLEN]; |
| 72 | |
| 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); |
| 77 | if (fd < 0) { |
| 78 | rsyserr(FERROR, errno, "Batch file %s open error", filename); |
| 79 | exit_cleanup(1); |
| 80 | } |
| 81 | |
| 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++) { |
| 87 | p = argv[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) |
| 92 | i++; |
| 93 | continue; |
| 94 | } |
| 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 | } |
| 102 | } else |
| 103 | write_arg(fd, p); |
| 104 | } |
| 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); |
| 112 | write_byte(fd, '}'); |
| 113 | if (exclude_list.head) |
| 114 | write_excludes(fd); |
| 115 | if (write(fd, "\n", 1) != 1 || close(fd) < 0) { |
| 116 | rsyserr(FERROR, errno, "Batch file %s write error", filename); |
| 117 | exit_cleanup(1); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void show_flist(int index, struct file_struct **fptr) |
| 122 | { |
| 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 | } |
| 141 | } |
| 142 | |
| 143 | void show_argvs(int argc, char *argv[]) |
| 144 | { |
| 145 | /* for debugging * */ |
| 146 | |
| 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]); |
| 152 | |
| 153 | } |
| 154 | } |