You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include <dirent.h>
  2. #include <iterator>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <stdlib.h>
  8. #include <string>
  9. #include <sys/stat.h>
  10. #include <syslog.h>
  11. #include <unistd.h>
  12. #include <vector>
  13. #include <thread>
  14. #include <db/db.h>
  15. #include <db/create.h>
  16. #include <db/update.h>
  17. #include <db/FSWatcher.h>
  18. #include <util/notifications.h>
  19. #include <util/calendar_parsing.h>
  20. #include <ical/IcalObject.h>
  21. #include <io/debug.h>
  22. const int MINUTE = 1000000;
  23. const int MINUTE = 60;
  24. const int SLEEP_INTERVAL = 60 * MINUTE;
  25. sql::Database* init_database(std::string calendar) {
  26. return db::init(calendar + ".db");
  27. }
  28. void update_database(std::string directory, sql::Database *db) {
  29. std::vector<ical::IcalObject*> objects = util::parse_cal_dir(directory);
  30. for(auto const& obj : objects) {
  31. if(obj->empty()) continue;
  32. db::insert_object(db, obj);
  33. }
  34. }
  35. void init_fswatcher(std::string directory, sql::Database *db) {
  36. db::FSWatcher fswatcher(db, directory);
  37. fswatcher.run();
  38. }
  39. void do_heartbeat(std::string directory, sql::Database *db) {
  40. util::notify("Updating files from " + directory, "");
  41. update_database(directory, db);
  42. }
  43. // For security purposes, we don't allow any arguments to be passed into the daemon
  44. int main(void)
  45. {
  46. // Define variables
  47. //pid_t pid, sid;
  48. //// Fork the current process
  49. //pid = fork();
  50. //// The parent process continues with a process ID greater than 0
  51. //if(pid > 0)
  52. //{
  53. // exit(EXIT_SUCCESS);
  54. //}
  55. //// A process ID lower than 0 indicates a failure in either process
  56. //else if(pid < 0)
  57. //{
  58. // exit(EXIT_FAILURE);
  59. //}
  60. //// The parent process has now terminated, and the forked child process will continue
  61. //// (the pid of the child process was 0)
  62. //// Since the child process is a daemon, the umask needs to be set so files and logs can be written
  63. //umask(0);
  64. //// Open system logs for the child process
  65. //openlog("daemon-named", LOG_NOWAIT | LOG_PID, LOG_USER);
  66. //syslog(LOG_NOTICE, "Successfully started daemon-name");
  67. //// Generate a session ID for the child process
  68. //sid = setsid();
  69. //// Ensure a valid SID for the child process
  70. //if(sid < 0)
  71. //{
  72. // // Log failure and exit
  73. // syslog(LOG_ERR, "Could not generate session ID for child process");
  74. // // If a new session ID could not be generated, we must terminate the child process
  75. // // or it will be orphaned
  76. // exit(EXIT_FAILURE);
  77. //}
  78. //// Change the current working directory to a directory guaranteed to exist
  79. //if((chdir("/")) < 0)
  80. //{
  81. // // Log failure and exit
  82. // syslog(LOG_ERR, "Could not change working directory to /");
  83. // // If our guaranteed directory does not exist, terminate the child process to ensure
  84. // // the daemon has not been hijacked
  85. // exit(EXIT_FAILURE);
  86. //}
  87. //// A daemon cannot use the terminal, so close standard file descriptors for security reasons
  88. //close(STDIN_FILENO);
  89. //close(STDOUT_FILENO);
  90. //close(STDERR_FILENO);
  91. // Daemon-specific intialization should go here
  92. const int SLEEP_INTERVAL = 5 * MINUTE;
  93. std::string calendar = "dummy";
  94. std::string directory = "/home/stefan/.local/share/khal/calendars/" + calendar + "/";
  95. sql::Database* db = init_database(calendar);
  96. std::thread thread([&](){ init_fswatcher(directory, db); });
  97. // Enter daemon loop
  98. while(1)
  99. {
  100. // Execute daemon heartbeat, where your recurring activity occurs
  101. do_heartbeat(directory, db);
  102. exit(0);
  103. // Sleep for a period of time
  104. sleep(SLEEP_INTERVAL);
  105. }
  106. // Close system logs for the child process
  107. //syslog(LOG_NOTICE, "Stopping daemon-name");
  108. //closelog();
  109. // Terminate the child process when the daemon completes
  110. exit(EXIT_SUCCESS);
  111. }