batch.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Support for the batch-file options.
  3. *
  4. * Copyright (C) 1999 Weiss
  5. * Copyright (C) 2004 Chris Shoemaker
  6. * Copyright (C) 2004-2009 Wayne Davison
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, visit the http://fsf.org website.
  20. */
  21. #include "rsync.h"
  22. #include "zlib/zlib.h"
  23. #include <time.h>
  24. extern int eol_nulls;
  25. extern int recurse;
  26. extern int xfer_dirs;
  27. extern int preserve_links;
  28. extern int preserve_hard_links;
  29. extern int preserve_devices;
  30. extern int preserve_uid;
  31. extern int preserve_gid;
  32. extern int preserve_acls;
  33. extern int preserve_xattrs;
  34. extern int always_checksum;
  35. extern int do_compression;
  36. extern int inplace;
  37. extern int append_mode;
  38. extern int protocol_version;
  39. extern char *batch_name;
  40. #ifdef ICONV_OPTION
  41. extern char *iconv_opt;
  42. #endif
  43. extern struct filter_list_struct filter_list;
  44. int batch_stream_flags;
  45. static int tweaked_append;
  46. static int tweaked_append_verify;
  47. static int tweaked_iconv;
  48. static int *flag_ptr[] = {
  49. &recurse, /* 0 */
  50. &preserve_uid, /* 1 */
  51. &preserve_gid, /* 2 */
  52. &preserve_links, /* 3 */
  53. &preserve_devices, /* 4 */
  54. &preserve_hard_links, /* 5 */
  55. &always_checksum, /* 6 */
  56. &xfer_dirs, /* 7 (protocol 29) */
  57. &do_compression, /* 8 (protocol 29) */
  58. &tweaked_iconv, /* 9 (protocol 30) */
  59. &preserve_acls, /* 10 (protocol 30) */
  60. &preserve_xattrs, /* 11 (protocol 30) */
  61. &inplace, /* 12 (protocol 30) */
  62. &tweaked_append, /* 13 (protocol 30) */
  63. &tweaked_append_verify, /* 14 (protocol 30) */
  64. NULL
  65. };
  66. static char *flag_name[] = {
  67. "--recurse (-r)",
  68. "--owner (-o)",
  69. "--group (-g)",
  70. "--links (-l)",
  71. "--devices (-D)",
  72. "--hard-links (-H)",
  73. "--checksum (-c)",
  74. "--dirs (-d)",
  75. "--compress (-z)",
  76. "--iconv",
  77. "--acls (-A)",
  78. "--xattrs (-X)",
  79. "--inplace",
  80. "--append",
  81. "--append-verify",
  82. NULL
  83. };
  84. void write_stream_flags(int fd)
  85. {
  86. int i, flags;
  87. tweaked_append = append_mode == 1;
  88. tweaked_append_verify = append_mode == 2;
  89. #ifdef ICONV_OPTION
  90. tweaked_iconv = iconv_opt != NULL;
  91. #endif
  92. /* Start the batch file with a bitmap of data-stream-affecting
  93. * flags. */
  94. for (i = 0, flags = 0; flag_ptr[i]; i++) {
  95. if (*flag_ptr[i])
  96. flags |= 1 << i;
  97. }
  98. write_int(fd, flags);
  99. }
  100. void read_stream_flags(int fd)
  101. {
  102. batch_stream_flags = read_int(fd);
  103. }
  104. void check_batch_flags(void)
  105. {
  106. int i;
  107. if (protocol_version < 29)
  108. flag_ptr[7] = NULL;
  109. else if (protocol_version < 30)
  110. flag_ptr[9] = NULL;
  111. tweaked_append = append_mode == 1;
  112. tweaked_append_verify = append_mode == 2;
  113. #ifdef ICONV_OPTION
  114. tweaked_iconv = iconv_opt != NULL;
  115. #endif
  116. for (i = 0; flag_ptr[i]; i++) {
  117. int set = batch_stream_flags & (1 << i) ? 1 : 0;
  118. if (*flag_ptr[i] != set) {
  119. if (i == 9) {
  120. rprintf(FERROR,
  121. "%s specify the --iconv option to use this batch file.\n",
  122. set ? "Please" : "Do not");
  123. exit_cleanup(RERR_SYNTAX);
  124. }
  125. if (verbose) {
  126. rprintf(FINFO,
  127. "%sing the %s option to match the batchfile.\n",
  128. set ? "Sett" : "Clear", flag_name[i]);
  129. }
  130. *flag_ptr[i] = set;
  131. }
  132. }
  133. if (protocol_version < 29) {
  134. if (recurse)
  135. xfer_dirs |= 1;
  136. else if (xfer_dirs < 2)
  137. xfer_dirs = 0;
  138. }
  139. if (tweaked_append)
  140. append_mode = 1;
  141. else if (tweaked_append_verify)
  142. append_mode = 2;
  143. }
  144. static int write_arg(int fd, char *arg)
  145. {
  146. char *x, *s;
  147. int len, ret = 0;
  148. if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
  149. if (write(fd, arg, x - arg + 1) != x - arg + 1)
  150. ret = -1;
  151. arg += x - arg + 1;
  152. }
  153. if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
  154. if (write(fd, "'", 1) != 1)
  155. ret = -1;
  156. for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
  157. if (write(fd, s, x - s + 1) != x - s + 1
  158. || write(fd, "'", 1) != 1)
  159. ret = -1;
  160. }
  161. len = strlen(s);
  162. if (write(fd, s, len) != len
  163. || write(fd, "'", 1) != 1)
  164. ret = -1;
  165. return ret;
  166. }
  167. len = strlen(arg);
  168. if (write(fd, arg, len) != len)
  169. ret = -1;
  170. return ret;
  171. }
  172. static void write_filter_rules(int fd)
  173. {
  174. struct filter_struct *ent;
  175. write_sbuf(fd, " <<'#E#'\n");
  176. for (ent = filter_list.head; ent; ent = ent->next) {
  177. unsigned int plen;
  178. char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
  179. write_buf(fd, p, plen);
  180. write_sbuf(fd, ent->pattern);
  181. if (ent->match_flags & MATCHFLG_DIRECTORY)
  182. write_byte(fd, '/');
  183. write_byte(fd, eol_nulls ? 0 : '\n');
  184. }
  185. if (eol_nulls)
  186. write_sbuf(fd, ";\n");
  187. write_sbuf(fd, "#E#");
  188. }
  189. /* This routine tries to write out an equivalent --read-batch command
  190. * given the user's --write-batch args. However, it doesn't really
  191. * understand most of the options, so it uses some overly simple
  192. * heuristics to munge the command line into something that will
  193. * (hopefully) work. */
  194. void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
  195. {
  196. int fd, i, len, err = 0;
  197. char *p, filename[MAXPATHLEN];
  198. stringjoin(filename, sizeof filename,
  199. batch_name, ".sh", NULL);
  200. fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
  201. S_IRUSR | S_IWUSR | S_IEXEC);
  202. if (fd < 0) {
  203. rsyserr(FERROR, errno, "Batch file %s open error",
  204. filename);
  205. exit_cleanup(RERR_FILESELECT);
  206. }
  207. /* Write argvs info to BATCH.sh file */
  208. if (write_arg(fd, argv[0]) < 0)
  209. err = 1;
  210. if (filter_list.head) {
  211. if (protocol_version >= 29)
  212. write_sbuf(fd, " --filter=._-");
  213. else
  214. write_sbuf(fd, " --exclude-from=-");
  215. }
  216. for (i = 1; i < argc - file_arg_cnt; i++) {
  217. p = argv[i];
  218. if (strncmp(p, "--files-from", 12) == 0
  219. || strncmp(p, "--filter", 8) == 0
  220. || strncmp(p, "--include", 9) == 0
  221. || strncmp(p, "--exclude", 9) == 0) {
  222. if (strchr(p, '=') == NULL)
  223. i++;
  224. continue;
  225. }
  226. if (strcmp(p, "-f") == 0) {
  227. i++;
  228. continue;
  229. }
  230. if (write(fd, " ", 1) != 1)
  231. err = 1;
  232. if (strncmp(p, "--write-batch", len = 13) == 0
  233. || strncmp(p, "--only-write-batch", len = 18) == 0) {
  234. if (write(fd, "--read-batch", 12) != 12)
  235. err = 1;
  236. if (p[len] == '=') {
  237. if (write(fd, "=", 1) != 1
  238. || write_arg(fd, p + len + 1) < 0)
  239. err = 1;
  240. }
  241. } else {
  242. if (write_arg(fd, p) < 0)
  243. err = 1;
  244. }
  245. }
  246. if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
  247. p = argv[argc - 1];
  248. if (write(fd, " ${1:-", 6) != 6
  249. || write_arg(fd, p) < 0)
  250. err = 1;
  251. write_byte(fd, '}');
  252. if (filter_list.head)
  253. write_filter_rules(fd);
  254. if (write(fd, "\n", 1) != 1 || close(fd) < 0 || err) {
  255. rsyserr(FERROR, errno, "Batch file %s write error",
  256. filename);
  257. exit_cleanup(RERR_FILEIO);
  258. }
  259. }