clsync-synchandler-rsyncso.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <stdlib.h>
  2. #include <errno.h>
  3. // Required header:
  4. #include <clsync/compilerflags.h>
  5. #include <clsync/clsync.h>
  6. // Optional headers:
  7. #include <clsync/configuration.h>
  8. #include <clsync/error.h>
  9. #include <clsync/ctx.h>
  10. static struct ctx *ctx_p = NULL;
  11. static struct indexes *indexes_p = NULL;
  12. static const char *argv[11] = {NULL};
  13. // Optional function, you can erase it.
  14. int clsyncapi_init(struct ctx *_ctx_p, struct indexes *_indexes_p) {
  15. debug(1, "Hello world!");
  16. ctx_p = _ctx_p;
  17. indexes_p = _indexes_p;
  18. if (ctx_p->destdir == NULL) {
  19. error("destination-dir is not set.");
  20. return EINVAL;
  21. }
  22. if (ctx_p->flags[RSYNCPREFERINCLUDE]) {
  23. error("clsync-synchandler-rsyncso.so cannot be used in conjunction with \"--rsync-prefer-include\" option.");
  24. return EINVAL;
  25. }
  26. if (ctx_p->flags[THREADING]) {
  27. error("this handler is not pthread-safe.");
  28. return EINVAL;
  29. }
  30. argv[0] = "/usr/bin/rsync";
  31. argv[1] = ctx_p->flags[DEBUG] >= 4 ? "-avvvvvvH" : "-aH";
  32. argv[2] = "--exclude-from";
  33. argv[4] = "--include-from";
  34. argv[6] = "--exclude=*";
  35. argv[7] = "--delete-before";
  36. argv[8] = ctx_p->watchdirwslash;
  37. argv[9] = ctx_p->destdirwslash;
  38. return 0;
  39. }
  40. int clsyncapi_rsync(const char *inclistfile, const char *exclistfile) {
  41. debug(1, "inclistfile == \"%s\"; exclistfile == \"%s\"", inclistfile, exclistfile);
  42. argv[3] = exclistfile;
  43. argv[5] = inclistfile;
  44. if (ctx_p->flags[DEBUG] >= 3) {
  45. int i=0;
  46. while (argv[i] != NULL) {
  47. debug(3, "argv[%i] == \"%s\"", i, argv[i]);
  48. i++;
  49. }
  50. }
  51. // Forking
  52. int pid = clsyncapi_fork(ctx_p);
  53. switch (pid) {
  54. case -1:
  55. error("Cannot fork().");
  56. return errno;
  57. case 0:
  58. execvp(argv[0], (char *const *)argv);
  59. return errno;
  60. }
  61. int status;
  62. if (waitpid(pid, &status, 0) != pid) {
  63. error("Cannot waitid().");
  64. return errno;
  65. }
  66. // Return
  67. int exitcode = WEXITSTATUS(status);
  68. debug(1, "Execution completed with exitcode %i.", exitcode);
  69. return exitcode;
  70. }
  71. // Optional function, you can erase it.
  72. int clsyncapi_deinit() {
  73. debug(1, "Goodbye cruel world!");
  74. return 0;
  75. }