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