clsync-synchandler-so.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <clsync/common.h>
  2. options_t *options_p = NULL;
  3. indexes_t *indexes_p = NULL;
  4. char **argv = NULL;
  5. size_t argv_size = 0;
  6. int clsyncapi_init(options_t *_options_p, indexes_t *_indexes_p) {
  7. printf("clsyncapi_init(): Hello world!\n");
  8. options_p = _options_p;
  9. indexes_p = _indexes_p;
  10. if(options_p->destdir == NULL) {
  11. fprintf(stderr, "Error: clsyncapi_init(): dest-dir is not set.\n");
  12. return EINVAL;
  13. }
  14. argv_size = ALLOC_PORTION;
  15. argv = malloc(argv_size * sizeof(char *));
  16. argv[0] = "/bin/cp";
  17. argv[1] = "-pf";
  18. return 0;
  19. }
  20. int clsyncapi_sync(int n, api_eventinfo_t *ei) {
  21. printf("clsyncapi_sync(): n == %i\n", n, ei->path);
  22. if(n+3 > argv_size) {
  23. argv_size = n+3 + ALLOC_PORTION;
  24. argv = realloc(argv, argv_size * sizeof(char *));
  25. }
  26. int argv_i=2;
  27. int ei_i=0;
  28. while(ei_i < n) {
  29. if(ei[ei_i].path_len > 0) {
  30. printf("clsyncapi_sync(): ei[%i].path == \"%s\" (len == %i)\n", ei_i, ei[ei_i].path, ei[ei_i].path_len);
  31. argv[argv_i++] = (char *)ei[ei_i].path;
  32. }
  33. ei_i++;
  34. }
  35. if(argv_i == 2) {
  36. printf("clsyncapi_sync(): Nothing to sync.\n");
  37. return 0;
  38. }
  39. argv[argv_i++] = options_p->destdir;
  40. argv[argv_i++] = NULL;
  41. // Forking
  42. int pid = fork();
  43. switch(pid) {
  44. case -1:
  45. fprintf(stderr, "Error: Cannot fork(): %s (errno: %i).\n", strerror(errno), errno);
  46. return errno;
  47. case 0:
  48. chdir(options_p->watchdir);
  49. execvp(argv[0], (char *const *)argv);
  50. return errno;
  51. }
  52. int status;
  53. if(waitpid(pid, &status, 0) != pid) {
  54. fprintf(stderr, "Error: Cannot waitid(): %s (errno: %i).\n", strerror(errno), errno);
  55. return errno;
  56. }
  57. // Return
  58. int exitcode = WEXITSTATUS(status);
  59. printf("clsyncapi_sync(): Execution completed with exitcode %i.\n", exitcode);
  60. return exitcode;
  61. }
  62. int clsyncapi_deinit() {
  63. printf("clsyncapi_deinit(): Goodbye cruel world!\n");
  64. if(argv != NULL)
  65. free(argv);
  66. return 0;
  67. }