parent
78ac0ca45e
commit
0b09242ac0
2 changed files with 74 additions and 0 deletions
@ -0,0 +1,47 @@ |
|||||||
|
#include "file_watcher.h" |
||||||
|
|
||||||
|
FileWatcher::FileWatcher(std::string path, TimeSpan<Millisecond> ts) |
||||||
|
: target(path), ttm(ts) |
||||||
|
{ |
||||||
|
for (const std::filesystem::directory_entry& file : std::filesystem::recursive_directory_iterator(target)) |
||||||
|
paths[file.path().string()] = std::filesystem::last_write_time(file); |
||||||
|
} |
||||||
|
|
||||||
|
FileWatcher::~FileWatcher() { stop(); } |
||||||
|
|
||||||
|
void FileWatcher::start(const std::function<void (std::string, FileStatus)>& action) |
||||||
|
{ |
||||||
|
std::function<void()> task([&]() |
||||||
|
{ |
||||||
|
auto it = paths.begin(); |
||||||
|
while (it != paths.end()) |
||||||
|
{ |
||||||
|
if (!std::filesystem::exists(it->first))
|
||||||
|
{ |
||||||
|
action(it->first, FileStatus::Deleted); |
||||||
|
it = paths.erase(it); |
||||||
|
} |
||||||
|
else it++; |
||||||
|
} |
||||||
|
for (const std::filesystem::directory_entry& file : std::filesystem::recursive_directory_iterator(target)) |
||||||
|
{ |
||||||
|
auto current_file_last_write_time = std::filesystem::last_write_time(file); |
||||||
|
if (contains(file.path().string())) |
||||||
|
{ |
||||||
|
if(paths[file.path().string()] != current_file_last_write_time) { |
||||||
|
paths[file.path().string()] = current_file_last_write_time; |
||||||
|
action(file.path().string(), FileStatus::Modified); |
||||||
|
} |
||||||
|
} else
|
||||||
|
{ |
||||||
|
paths[file.path().string()] = current_file_last_write_time; |
||||||
|
action(file.path().string(), FileStatus::Created); |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
ttm.start(std::make_unique<std::function<void()>>(task)); |
||||||
|
} |
||||||
|
|
||||||
|
void FileWatcher::stop() { ttm.stop(); } |
||||||
|
|
||||||
|
bool FileWatcher::contains(std::string path) { return paths.find(path) != paths.end(); } |
@ -0,0 +1,27 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <bakatools.h> |
||||||
|
#include <bakatools/thread/task_timer.h> |
||||||
|
|
||||||
|
enum class FileStatus |
||||||
|
{ |
||||||
|
Created = 0, |
||||||
|
Modified = 1, |
||||||
|
Deleted = 2, |
||||||
|
}; |
||||||
|
|
||||||
|
class FileWatcher |
||||||
|
{ |
||||||
|
public: |
||||||
|
FileWatcher(std::string path, TimeSpan<Millisecond> ts); |
||||||
|
~FileWatcher(); |
||||||
|
|
||||||
|
void start(const std::function<void (std::string, FileStatus)>& action); |
||||||
|
void stop(); |
||||||
|
bool contains(std::string path); |
||||||
|
|
||||||
|
private: |
||||||
|
std::string target; |
||||||
|
std::unordered_map<std::string, std::filesystem::file_time_type> paths; |
||||||
|
TaskTimer<Millisecond> ttm; |
||||||
|
}; |
Loading…
Reference in New Issue