00001
00002
00003
00004
00005
00006
00007
00008 #include "rsync.h"
00009 #include "zlib/zlib.h"
00010 #include <time.h>
00011
00012 extern int eol_nulls;
00013 extern int recurse;
00014 extern int xfer_dirs;
00015 extern int preserve_links;
00016 extern int preserve_hard_links;
00017 extern int preserve_devices;
00018 extern int preserve_uid;
00019 extern int preserve_gid;
00020 extern int always_checksum;
00021 extern int do_compression;
00022 extern int def_compress_level;
00023 extern int protocol_version;
00024 extern char *batch_name;
00025
00026 extern struct filter_list_struct filter_list;
00027
00028 static int tweaked_compress_level;
00029
00030 static int *flag_ptr[] = {
00031 &recurse,
00032 &preserve_uid,
00033 &preserve_gid,
00034 &preserve_links,
00035 &preserve_devices,
00036 &preserve_hard_links,
00037 &always_checksum,
00038 &xfer_dirs,
00039 &tweaked_compress_level,
00040 NULL
00041 };
00042
00043 static char *flag_name[] = {
00044 "--recurse (-r)",
00045 "--owner (-o)",
00046 "--group (-g)",
00047 "--links (-l)",
00048 "--devices (-D)",
00049 "--hard-links (-H)",
00050 "--checksum (-c)",
00051 "--dirs (-d)",
00052 "--compress (-z)",
00053 NULL
00054 };
00055
00056 void write_stream_flags(int fd)
00057 {
00058 int i, flags;
00059
00060 #if Z_DEFAULT_COMPRESSION == -1
00061 tweaked_compress_level = do_compression ? def_compress_level + 2 : 0;
00062 #else
00063 #error internal logic error! Fix def_compress_level logic above and below too!
00064 #endif
00065
00066
00067
00068 if (protocol_version < 29)
00069 flag_ptr[7] = NULL;
00070 for (i = 0, flags = 0; flag_ptr[i]; i++) {
00071 if (*flag_ptr[i])
00072 flags |= 1 << i;
00073 }
00074 write_int(fd, flags);
00075 }
00076
00077 void read_stream_flags(int fd)
00078 {
00079 int i, flags;
00080
00081 if (protocol_version < 29)
00082 flag_ptr[7] = NULL;
00083 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
00084 int set = flags & (1 << i) ? 1 : 0;
00085 if (*flag_ptr[i] != set) {
00086 if (verbose) {
00087 rprintf(FINFO,
00088 "%sing the %s option to match the batchfile.\n",
00089 set ? "Sett" : "Clear", flag_name[i]);
00090 }
00091 *flag_ptr[i] = set;
00092 }
00093 }
00094 if (protocol_version < 29) {
00095 if (recurse)
00096 xfer_dirs |= 1;
00097 else if (xfer_dirs < 2)
00098 xfer_dirs = 0;
00099 }
00100
00101 if (tweaked_compress_level == 0 || tweaked_compress_level == 2)
00102 do_compression = 0;
00103 else {
00104 do_compression = 1;
00105 def_compress_level = tweaked_compress_level - 2;
00106 }
00107 }
00108
00109 static void write_arg(int fd, char *arg)
00110 {
00111 char *x, *s;
00112
00113 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
00114 write(fd, arg, x - arg + 1);
00115 arg += x - arg + 1;
00116 }
00117
00118 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
00119 write(fd, "'", 1);
00120 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
00121 write(fd, s, x - s + 1);
00122 write(fd, "'", 1);
00123 }
00124 write(fd, s, strlen(s));
00125 write(fd, "'", 1);
00126 return;
00127 }
00128
00129 write(fd, arg, strlen(arg));
00130 }
00131
00132 static void write_filter_rules(int fd)
00133 {
00134 struct filter_struct *ent;
00135
00136 write_sbuf(fd, " <<'#E#'\n");
00137 for (ent = filter_list.head; ent; ent = ent->next) {
00138 unsigned int plen;
00139 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
00140 write_buf(fd, p, plen);
00141 write_sbuf(fd, ent->pattern);
00142 if (ent->match_flags & MATCHFLG_DIRECTORY)
00143 write_byte(fd, '/');
00144 write_byte(fd, eol_nulls ? 0 : '\n');
00145 }
00146 if (eol_nulls)
00147 write_sbuf(fd, ";\n");
00148 write_sbuf(fd, "#E#");
00149 }
00150
00151
00152
00153
00154
00155
00156 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
00157 {
00158 int fd, i, len;
00159 char *p, filename[MAXPATHLEN];
00160
00161 stringjoin(filename, sizeof filename,
00162 batch_name, ".sh", NULL);
00163 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
00164 S_IRUSR | S_IWUSR | S_IEXEC);
00165 if (fd < 0) {
00166 rsyserr(FERROR, errno, "Batch file %s open error",
00167 filename);
00168 exit_cleanup(1);
00169 }
00170
00171
00172 write_arg(fd, argv[0]);
00173 if (filter_list.head) {
00174 if (protocol_version >= 29)
00175 write_sbuf(fd, " --filter=._-");
00176 else
00177 write_sbuf(fd, " --exclude-from=-");
00178 }
00179 for (i = 1; i < argc - file_arg_cnt; i++) {
00180 p = argv[i];
00181 if (strncmp(p, "--files-from", 12) == 0
00182 || strncmp(p, "--filter", 8) == 0
00183 || strncmp(p, "--include", 9) == 0
00184 || strncmp(p, "--exclude", 9) == 0) {
00185 if (strchr(p, '=') == NULL)
00186 i++;
00187 continue;
00188 }
00189 if (strcmp(p, "-f") == 0) {
00190 i++;
00191 continue;
00192 }
00193 write(fd, " ", 1);
00194 if (strncmp(p, "--write-batch", len = 13) == 0
00195 || strncmp(p, "--only-write-batch", len = 18) == 0) {
00196 write(fd, "--read-batch", 12);
00197 if (p[len] == '=') {
00198 write(fd, "=", 1);
00199 write_arg(fd, p + len + 1);
00200 }
00201 } else
00202 write_arg(fd, p);
00203 }
00204 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
00205 p = argv[argc - 1];
00206 write(fd, " ${1:-", 6);
00207 write_arg(fd, p);
00208 write_byte(fd, '}');
00209 if (filter_list.head)
00210 write_filter_rules(fd);
00211 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
00212 rsyserr(FERROR, errno, "Batch file %s write error",
00213 filename);
00214 exit_cleanup(1);
00215 }
00216 }