batch.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Support for the batch-file options.
  3. *
  4. * Copyright (C) 1999 Weiss
  5. * Copyright (C) 2004 Chris Shoemaker
  6. * Copyright (C) 2004-2008 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 void write_arg(int fd, char *arg)
  145. {
  146. char *x, *s;
  147. if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
  148. write(fd, arg, x - arg + 1);
  149. arg += x - arg + 1;
  150. }
  151. if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
  152. write(fd, "'", 1);
  153. for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
  154. write(fd, s, x - s + 1);
  155. write(fd, "'", 1);
  156. }
  157. write(fd, s, strlen(s));
  158. write(fd, "'", 1);
  159. return;
  160. }
  161. write(fd, arg, strlen(arg));
  162. }
  163. static void write_filter_rules(int fd)
  164. {
  165. struct filter_struct *ent;
  166. write_sbuf(fd, " <<'#E#'\n");
  167. for (ent = filter_list.head; ent; ent = ent->next) {
  168. unsigned int plen;
  169. char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
  170. write_buf(fd, p, plen);
  171. write_sbuf(fd, ent->pattern);
  172. if (ent->match_flags & MATCHFLG_DIRECTORY)
  173. write_byte(fd, '/');
  174. write_byte(fd, eol_nulls ? 0 : '\n');
  175. }
  176. if (eol_nulls)
  177. write_sbuf(fd, ";\n");
  178. write_sbuf(fd, "#E#");
  179. }
  180. /* This routine tries to write out an equivalent --read-batch command
  181. * given the user's --write-batch args. However, it doesn't really
  182. * understand most of the options, so it uses some overly simple
  183. * heuristics to munge the command line into something that will
  184. * (hopefully) work. */
  185. void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
  186. {
  187. int fd, i, len;
  188. char *p, filename[MAXPATHLEN];
  189. stringjoin(filename, sizeof filename,
  190. batch_name, ".sh", NULL);
  191. fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
  192. S_IRUSR | S_IWUSR | S_IEXEC);
  193. if (fd < 0) {
  194. rsyserr(FERROR, errno, "Batch file %s open error",
  195. filename);
  196. exit_cleanup(RERR_FILESELECT);
  197. }
  198. /* Write argvs info to BATCH.sh file */
  199. write_arg(fd, argv[0]);
  200. if (filter_list.head) {
  201. if (protocol_version >= 29)
  202. write_sbuf(fd, " --filter=._-");
  203. else
  204. write_sbuf(fd, " --exclude-from=-");
  205. }
  206. for (i = 1; i < argc - file_arg_cnt; i++) {
  207. p = argv[i];
  208. if (strncmp(p, "--files-from", 12) == 0
  209. || strncmp(p, "--filter", 8) == 0
  210. || strncmp(p, "--include", 9) == 0
  211. || strncmp(p, "--exclude", 9) == 0) {
  212. if (strchr(p, '=') == NULL)
  213. i++;
  214. continue;
  215. }
  216. if (strcmp(p, "-f") == 0) {
  217. i++;
  218. continue;
  219. }
  220. write(fd, " ", 1);
  221. if (strncmp(p, "--write-batch", len = 13) == 0
  222. || strncmp(p, "--only-write-batch", len = 18) == 0) {
  223. write(fd, "--read-batch", 12);
  224. if (p[len] == '=') {
  225. write(fd, "=", 1);
  226. write_arg(fd, p + len + 1);
  227. }
  228. } else
  229. write_arg(fd, p);
  230. }
  231. if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
  232. p = argv[argc - 1];
  233. write(fd, " ${1:-", 6);
  234. write_arg(fd, p);
  235. write_byte(fd, '}');
  236. if (filter_list.head)
  237. write_filter_rules(fd);
  238. if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
  239. rsyserr(FERROR, errno, "Batch file %s write error",
  240. filename);
  241. exit_cleanup(RERR_FILEIO);
  242. }
  243. }