forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileManager.cpp
177 lines (158 loc) · 5.07 KB
/
FileManager.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* This file is part of RTags (http://rtags.net).
RTags is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RTags is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#include "FileManager.h"
#include "Filter.h"
#include "Project.h"
#include "ScanThread.h"
#include "Server.h"
FileManager::FileManager(const std::shared_ptr<Project> &project)
: mProject(project), mLastReloadTime(0)
{
}
void FileManager::load(Mode mode)
{
if (!Server::instance()->options().tests.isEmpty())
mode = Synchronous;
mLastReloadTime = Rct::monoMs();
std::shared_ptr<Project> project = mProject.lock();
assert(project);
if (mode == Asynchronous) {
startScanThread();
} else {
const Set<Path> paths = ScanThread::paths(project->path());
onRecurseJobFinished(paths);
}
}
void FileManager::onRecurseJobFinished(const Set<Path> &paths)
{
std::lock_guard<std::mutex> lock(mMutex); // ### is this needed now?
std::shared_ptr<Project> project = mProject.lock();
if (!project)
return;
Files &map = project->files();
map.clear();
clearFileSystemWatcher();
for (Set<Path>::const_iterator it = paths.begin(); it != paths.end(); ++it) {
const Path parent = it->parentDir();
if (parent.isEmpty()) {
error() << "Got empty parent here" << *it;
continue;
}
assert(!parent.isEmpty());
Set<String> &dir = map[parent];
if (dir.isEmpty()) {
watch(parent);
// error() << "Watching parent" << parent;
}
dir.insert(it->fileName());
}
assert(!map.contains(Path()));
}
void FileManager::onFileAdded(const Path &path)
{
debug() << "fm file added" << path;
std::lock_guard<std::mutex> lock(mMutex);
if (path.isEmpty()) {
return;
}
const Filter::Result res = Filter::filter(path);
switch (res) {
case Filter::Directory:
watch(path);
load(Asynchronous);
return;
case Filter::Filtered:
return;
default:
break;
}
std::shared_ptr<Project> project = mProject.lock();
assert(project);
Files &map = project->files();
const Path parent = path.parentDir();
if (!parent.isEmpty()) {
Set<String> &dir = map[parent];
watch(parent);
dir.insert(path.fileName());
} else {
error() << "Got empty parent here" << path;
load(Asynchronous);
}
assert(!map.contains(Path()));
}
void FileManager::onFileRemoved(const Path &path)
{
debug() << "fm file removed" << path;
std::lock_guard<std::mutex> lock(mMutex);
std::shared_ptr<Project> project = mProject.lock();
if (!project)
return;
Files &map = project->files();
if (!map.remove(path)) {
const Path parent = path.parentDir();
if (map.contains(parent)) {
Set<String> &dir = map[parent];
dir.remove(String(path.fileName()));
if (dir.isEmpty()) {
project->unwatch(parent, Project::Watch_FileManager);
map.remove(parent);
}
}
}
}
static inline bool startsWith(const Path &left, const Path &right)
{
assert(!left.isEmpty());
return !right.isEmpty() && left.startsWith(right);
}
bool FileManager::contains(const Path &path) const
{
std::lock_guard<std::mutex> lock(mMutex);
std::shared_ptr<Project> proj = mProject.lock();
if (!proj)
return false;
if (startsWith(path, proj->path()))
return true;
const Path p = Path::resolved(path);
if (p != path && startsWith(path, proj->path()))
return true;
return false;
}
void FileManager::watch(const Path &path)
{
if (Server::instance()->options().options & Server::NoFileManagerWatch)
return;
if (path.contains("/.git/") || path.contains("/.svn/") || path.contains("/.cvs/")) {
return; // more source control systems?
}
if (auto proj = mProject.lock()) {
proj->watch(path, Project::Watch_FileManager);
}
}
void FileManager::startScanThread()
{
std::shared_ptr<Project> project = mProject.lock();
assert(project);
ScanThread *thread = new ScanThread(project->path());
thread->setAutoDelete(true);
std::weak_ptr<FileManager> that = shared_from_this();
thread->finished().connect<EventLoop::Move>([that](const Set<Path> &paths) {
if (auto strong = that.lock())
strong->onRecurseJobFinished(paths);
});
thread->start();
}
void FileManager::clearFileSystemWatcher()
{
if (auto project = mProject.lock())
project->clearWatch(Project::Watch_FileManager);
}