-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWatcher.cpp
More file actions
136 lines (125 loc) · 3.91 KB
/
FileWatcher.cpp
File metadata and controls
136 lines (125 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <string.h>
#include <filesystem>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <thread>
#include "FileWatcher.h"
#include "FileStatus.h"
FileWatcher::FileWatcher(Engine* _engine, std::string _dirPath){
engine = _engine;
dirToWatch = _dirPath;
}
bool FileWatcher::check() {
bool changed = false;
auto it = files.begin();
while (it != files.end()) {
if (!std::filesystem::exists(it->first)) {
std::filesystem::path path = it->first;
if (std::filesystem::exists(path.parent_path())){
action a;
a.path = it->first;
a.path = a.path.erase(1,dirToWatch.length()-1);
a.action = FileStatus::erased;
fileChanges.push(a);
it = files.erase(it);
changed = true;
} else {
it = files.erase(it);
}
}
else {
it++;
}
}
if (!directories.empty()) {
for(auto it = directories.begin(); it != directories.end(); ) {
if (!std::filesystem::exists(*it)) {
std::filesystem::path path = *it;
if (std::filesystem::exists(path.parent_path())){
dirDeletes.push(path.string().erase(1,dirToWatch.length()-1));
it = directories.erase(it);
changed = true;
} else {
it++;
}
}
else {
it++;
}
}
}
try {
for(auto &file : std::filesystem::recursive_directory_iterator(dirToWatch)) {
if (std::filesystem::is_regular_file(file.path())) {
auto curFileLastWriteTime = std::filesystem::last_write_time(file);
if(files.find(file.path().string()) == files.end()) {
files[file.path().string()] = curFileLastWriteTime;
action a;
a.path = file.path().string().erase(1,dirToWatch.length()-1);
a.action = FileStatus::created;
fileChanges.push(a);
changed = true;
} else if(files[file.path().string()] != curFileLastWriteTime) {
files[file.path().string()] = curFileLastWriteTime;
action a;
a.path = file.path().string().erase(1,dirToWatch.length()-1);
a.action = FileStatus::modified;
fileChanges.push(a);
changed = true;
}
}
else if (std::filesystem::is_directory(file.path())) {
directories.insert(file.path().string());
}
}
}
catch(std::filesystem::filesystem_error e)
{
std::cout << e.what() << std::endl;
}
return changed;
}
FileWatcher::action FileWatcher::getAction()
{
if (!dirDeletes.empty())
{
action a;
a.action = FileStatus::dirErased;
a.path = dirDeletes.front();
dirDeletes.pop();
return a;
}
if (!fileChanges.empty())
{
action a = fileChanges.front();
fileChanges.pop();
return a;
}
else
{
action a;
a.action = FileStatus::none;
return a;
}
}
std::vector<std::string> FileWatcher::getPaths()
{
std::vector<std::string> filePaths;
for (auto it : files)
{
filePaths.push_back(std::string(it.first).erase(1,dirToWatch.length()-1));
}
return filePaths;
}
void FileWatcher::updateFileTimes(std::string _path) {
std::filesystem::path path = _path;
files[path.string()] = std::filesystem::last_write_time(path);
}
void FileWatcher::deleteFile(std::string _path) {
files.erase(_path);
}
void FileWatcher::deleteDirectory(std::string _path) {
directories.erase(_path);
}