diff --git a/src/main.cpp b/src/main.cpp index bd9b6d4..fd4c844 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,119 +1,72 @@ +#include #include +#include +#include #include #include -#include -#include -#include -#include -#include -#include -#include "locations.h" +#include +#include +#include #include "usermanager.h" -#include "guarder.h" -#define MAXNAMESIZE 255 -#define SCAN_DONE_SIG SIGRTMIN #define SCAN_CMD_USR "docker exec --user www-data nextcloud /var/www/html/occ files:scan --path=" -int p1[2]; -guarder guard; userManager manager; +std::condition_variable cv; +std::mutex mtx; -void flushManagerToPipe() +void readingThreadFunc() { - if (!guard.isFlagOn()) - { - std::vector users = manager.getFlaggedUsers(); + FILE *logpipe = popen(LOGFILE, "r"); + std::array buffer; + + while (fgets(buffer.data(), buffer.size(), logpipe) != nullptr) + { + std::string line(buffer.data()); + + if (line.find('|') == std::string::npos) + continue; + + std::vector x = splitLogFile(line, '|'); + std::string user(x.at(USER_LOG_LOCATION)); - for (std::vector::iterator it = users.begin(); it != users.end(); ++it) { - int size = it->size(); - write(p1[1], &size, sizeof(int)); - write(p1[1], it->data(), size * sizeof(char)); + std::lock_guard lock(mtx); + manager.addUser(user); + manager.setUserFlagged(user); } - manager.unflagAllUsers(); + cv.notify_one(); + + std::cout << "User find: " << user << std::endl; } } -void handler(int sig) +void scannerThreadFunc() { - if (sig == SCAN_DONE_SIG) + std::unique_lock lock(mtx); + while (true) { - flushManagerToPipe(); + cv.wait(lock, [] + { return manager.isAnybodyInQueue(); }); + + std::vector scanUsers = manager.getFlaggedUsers(); + manager.unflagAllUsers(); + + for (std::vector::iterator it = scanUsers.begin(); it != scanUsers.end(); it++) + { + system((std::string(SCAN_CMD_USR) + *it).c_str()); + } } } int main() { - openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER); - pipe(p1); + std::thread readingThread(readingThreadFunc); + std::thread scannerThread(scannerThreadFunc); - pid_t parent = getpid(); - pid_t child = fork(); + readingThread.join(); + scannerThread.join(); - if (child > 0) // parent - { - signal(SCAN_DONE_SIG, handler); - close(p1[0]); // read - - FILE *logpipe = popen(LOGFILE, "r"); - std::array buffer; - - while (fgets(buffer.data(), buffer.size(), logpipe) != nullptr) - { - std::string line(buffer.data()); - - if (line.find('|') == std::string::npos) - continue; - - std::vector x = splitLogFile(line, '|'); - std::string user(x.at(USER_LOG_LOCATION)); - manager.addUser(user); - manager.setUserFlagged(user); - - std::cout << "User find: " << user << std::endl; - - flushManagerToPipe(); - } - - fclose(logpipe); - close(p1[1]); // write - } - else // child - { - close(p1[1]); // write - - int size; - char *buffer = nullptr; - - while (read(p1[0], &size, sizeof(int))) - { - if (buffer == nullptr) - buffer = new char[size]; - - read(p1[0], buffer, size * sizeof(char)); - std::string name(buffer); - - std::cout << "Scan received for: " << name << std::endl; - - guard.setFlagOn(); - system((std::string(SCAN_CMD_USR) + name).data()); - guard.setFlagOff(); - - kill(parent, SCAN_DONE_SIG); - - if (buffer != nullptr) - { - delete[] buffer; - buffer = nullptr; - } - } - - close(p1[0]); // read - } - - closelog(); - - return EXIT_SUCCESS; + return 0; } \ No newline at end of file diff --git a/src/usermanager.h b/src/usermanager.h index f80be14..a313083 100644 --- a/src/usermanager.h +++ b/src/usermanager.h @@ -101,6 +101,19 @@ public: return ret; } + + bool isAnybodyInQueue() + { + std::lock_guard lock(mtx); + + for (std::map::iterator it = users.begin(); it != users.end(); ++it) + { + if (it->second) + return true; + } + + return false; + } }; #endif // _USERMAN_H \ No newline at end of file