syncfile.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #define EXIT_SUCCESS 0
  6. #define EXIT_FAILURE 1
  7. #define RUNNING 1
  8. // #define DEBUG
  9. void md5sum(const char *file_path, char *md5_file);
  10. const char *GetTime();
  11. int CheckFileExists(const char *file_path);
  12. int CheckWritePermission(const char *file_path);
  13. void help();
  14. void version();
  15. int main (int argc, char *argv[])
  16. {
  17. if (argc < 2)
  18. {
  19. help();
  20. return EXIT_FAILURE;
  21. }
  22. char command[50];
  23. strcpy(command, argv[1]);
  24. if (strcmp(command, "--help") == 0)
  25. {
  26. help();
  27. return EXIT_SUCCESS;
  28. }
  29. else if (strcmp(command, "--version") == 0)
  30. {
  31. version();
  32. return EXIT_SUCCESS;
  33. }
  34. char subcommand[50];
  35. if (argv[2])
  36. strcpy(subcommand, argv[2]);
  37. if (argc < 3)
  38. {
  39. help();
  40. return EXIT_FAILURE;
  41. }
  42. const char *file_sorg = command;
  43. const char *file_dest = subcommand;
  44. char md5_file_sorg[256];
  45. char md5_file_dest[256];
  46. char copy_command[256];
  47. sprintf(copy_command, "cp %s %s", file_sorg, file_dest);
  48. #ifdef DEBUG
  49. printf("copy_command: %s\n", copy_command);
  50. #endif
  51. while (RUNNING)
  52. {
  53. if (CheckFileExists(file_sorg) == -1 || CheckFileExists(file_dest) == -1)
  54. {
  55. if (CheckFileExists(file_sorg) == -1)
  56. printf("[Error]: File '%s' not found.\n", file_sorg);
  57. if (CheckFileExists(file_dest) == -1)
  58. printf("[Error]: File '%s' not found.\n", file_dest);
  59. return EXIT_FAILURE;
  60. }
  61. if (CheckWritePermission(file_dest) == -1)
  62. {
  63. printf("[Error]: Cannot write to '%s'.\n", file_dest);
  64. return EXIT_FAILURE;
  65. }
  66. md5sum(file_sorg, md5_file_sorg);
  67. md5sum(file_dest, md5_file_dest);
  68. if (md5_file_sorg == NULL || md5_file_dest == NULL)
  69. {
  70. printf("[Error]: While running md5sum.\n");
  71. return EXIT_FAILURE;
  72. }
  73. #ifdef DEBUG
  74. printf("md5sum file_sorg: %s.\n", md5_file_sorg);
  75. printf("md5sum file_dest: %s.\n", md5_file_dest);
  76. #endif
  77. if (strcmp(md5_file_sorg, md5_file_dest) != 0) // Se i due md5sum non coincidono
  78. {
  79. printf("[%s] - Syncing '%s' -> '%s'.\n", GetTime(), file_sorg, file_dest);
  80. system(copy_command);
  81. }
  82. sleep(1);
  83. }
  84. return EXIT_SUCCESS;
  85. }
  86. void md5sum(const char *file_path, char *md5_file)
  87. {
  88. FILE *cmd;
  89. char md5sum_command[256];
  90. sprintf(md5sum_command, "md5sum %s", file_path);
  91. cmd = popen(md5sum_command, "r");
  92. if (cmd != NULL)
  93. {
  94. fgets(md5_file, sizeof(md5_file), cmd);
  95. strtok(md5_file, " ");
  96. }
  97. else
  98. md5_file = NULL;
  99. pclose(cmd);
  100. }
  101. const char *GetTime()
  102. {
  103. static char time[30] = {' '};
  104. FILE *tm = popen("date +%H:%M:%S", "r");
  105. fgets(time, sizeof(time), tm);
  106. time[strlen(time)-1] = '\0';
  107. pclose(tm);
  108. return time;
  109. }
  110. int CheckFileExists(const char *file_path)
  111. {
  112. if (access(file_path, R_OK) == 0)
  113. return 0;
  114. else
  115. return -1;
  116. }
  117. int CheckWritePermission(const char *file_path)
  118. {
  119. if (access(file_path, W_OK) == 0)
  120. return 0;
  121. else
  122. return -1;
  123. }
  124. void help()
  125. {
  126. printf("Usage: syncfile <source_file> <destination_file>\n");
  127. }
  128. void version()
  129. {
  130. printf("Syncfile version 1.0\n");
  131. printf("Copyright (C) 2019 Andrea <andrea@gnu.org>\n");
  132. printf("License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>\n");
  133. printf("This is free software: you are free to change and redistribute it.\n");
  134. printf("There is NO WARRANTY, to the extent permitted by law.\n");
  135. }