2014-07-30 19:20:55 +04:00
|
|
|
/**
|
2016-07-26 17:53:11 +03:00
|
|
|
* Copyright (c) 2014 ownCloud GmbH. All rights reserved.
|
2014-07-30 19:20:55 +04:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; version 2.1 of the License
|
|
|
|
*
|
|
|
|
* This library 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 Lesser General Public License for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CommunicationSocket.h"
|
|
|
|
|
|
|
|
#include "RemotePathChecker.h"
|
2014-08-05 21:23:40 +04:00
|
|
|
#include "StringUtil.h"
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2014-11-03 22:45:38 +03:00
|
|
|
#include <shlobj.h>
|
|
|
|
|
2014-07-30 19:20:55 +04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iterator>
|
2014-10-14 18:05:48 +04:00
|
|
|
#include <unordered_set>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <shlobj.h>
|
2014-07-30 19:20:55 +04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2014-10-14 18:05:48 +04:00
|
|
|
// This code is run in a thread
|
|
|
|
void RemotePathChecker::workerThreadLoop()
|
2014-07-30 19:20:55 +04:00
|
|
|
{
|
2016-05-09 16:49:52 +03:00
|
|
|
auto pipename = CommunicationSocket::DefaultPipePath();
|
2014-10-15 17:13:04 +04:00
|
|
|
bool connected = false;
|
2014-10-14 18:05:48 +04:00
|
|
|
CommunicationSocket socket;
|
|
|
|
std::unordered_set<std::wstring> asked;
|
|
|
|
|
|
|
|
while(!_stop) {
|
2017-01-05 19:28:28 +03:00
|
|
|
Sleep(50);
|
2014-10-15 17:13:04 +04:00
|
|
|
|
|
|
|
if (!connected) {
|
|
|
|
asked.clear();
|
2015-01-22 18:30:02 +03:00
|
|
|
if (!WaitNamedPipe(pipename.data(), 100)) {
|
2014-10-15 17:13:04 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!socket.Connect(pipename)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
connected = true;
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
_connected = true;
|
|
|
|
}
|
|
|
|
|
2014-10-14 18:05:48 +04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
while (!_pending.empty() && !_stop) {
|
|
|
|
auto filePath = _pending.front();
|
|
|
|
_pending.pop();
|
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
if (!asked.count(filePath)) {
|
|
|
|
asked.insert(filePath);
|
|
|
|
socket.SendMsg(wstring(L"RETRIEVE_FILE_STATUS:" + filePath + L'\n').data());
|
|
|
|
}
|
|
|
|
lock.lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring response;
|
|
|
|
while (!_stop && socket.ReadLine(&response)) {
|
2017-01-05 19:28:28 +03:00
|
|
|
if (StringUtil::begins_with(response, wstring(L"REGISTER_PATH:"))) {
|
|
|
|
wstring responsePath = response.substr(14); // length of REGISTER_PATH:
|
|
|
|
|
2017-01-09 18:28:26 +03:00
|
|
|
auto sharedPtrCopy = atomic_load(&_watchedDirectories);
|
|
|
|
auto vectorCopy = make_shared<vector<wstring>>(*sharedPtrCopy);
|
|
|
|
vectorCopy->push_back(responsePath);
|
|
|
|
atomic_store(&_watchedDirectories, shared_ptr<const vector<wstring>>(vectorCopy));
|
|
|
|
|
|
|
|
// We don't keep track of all files and can't know which file is currently visible
|
|
|
|
// to the user, but at least reload the root dir so that any shortcut to the root
|
|
|
|
// is updated without the user needing to refresh.
|
2020-06-05 01:51:32 +03:00
|
|
|
SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATH | SHCNF_FLUSHNOWAIT, responsePath.data(), nullptr);
|
2017-01-05 19:28:28 +03:00
|
|
|
} else if (StringUtil::begins_with(response, wstring(L"UNREGISTER_PATH:"))) {
|
2014-10-15 17:13:04 +04:00
|
|
|
wstring responsePath = response.substr(16); // length of UNREGISTER_PATH:
|
|
|
|
|
2017-01-09 18:28:26 +03:00
|
|
|
auto sharedPtrCopy = atomic_load(&_watchedDirectories);
|
|
|
|
auto vectorCopy = make_shared<vector<wstring>>(*sharedPtrCopy);
|
|
|
|
vectorCopy->erase(
|
|
|
|
std::remove(vectorCopy->begin(), vectorCopy->end(), responsePath),
|
|
|
|
vectorCopy->end());
|
|
|
|
atomic_store(&_watchedDirectories, shared_ptr<const vector<wstring>>(vectorCopy));
|
|
|
|
|
2017-01-05 19:45:39 +03:00
|
|
|
vector<wstring> removedPaths;
|
2014-10-15 17:13:04 +04:00
|
|
|
{ std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
// Remove any item from the cache
|
|
|
|
for (auto it = _cache.begin(); it != _cache.end() ; ) {
|
2017-01-09 18:28:26 +03:00
|
|
|
if (StringUtil::isDescendantOf(it->first, responsePath)) {
|
2017-01-05 19:45:39 +03:00
|
|
|
removedPaths.emplace_back(move(it->first));
|
2014-10-15 17:13:04 +04:00
|
|
|
it = _cache.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-05 19:45:39 +03:00
|
|
|
for (auto& path : removedPaths)
|
2020-06-05 01:51:32 +03:00
|
|
|
SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH | SHCNF_FLUSHNOWAIT, path.data(), nullptr);
|
2014-10-14 18:05:48 +04:00
|
|
|
} else if (StringUtil::begins_with(response, wstring(L"STATUS:")) ||
|
|
|
|
StringUtil::begins_with(response, wstring(L"BROADCAST:"))) {
|
|
|
|
|
2017-07-04 17:58:55 +03:00
|
|
|
wstring responseStatus, responsePath;
|
|
|
|
if (!StringUtil::extractChunks(response, responseStatus, responsePath))
|
2014-10-14 18:05:48 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
auto state = _StrToFileState(responseStatus);
|
2015-05-06 16:51:41 +03:00
|
|
|
bool wasAsked = asked.erase(responsePath) > 0;
|
2014-10-14 18:05:48 +04:00
|
|
|
|
2017-01-09 16:25:00 +03:00
|
|
|
bool updateView = false;
|
2014-10-14 18:05:48 +04:00
|
|
|
{ std::unique_lock<std::mutex> lock(_mutex);
|
2017-01-09 16:25:00 +03:00
|
|
|
auto it = _cache.find(responsePath);
|
|
|
|
if (it == _cache.end()) {
|
|
|
|
// The client only approximates requested files, if the bloom
|
|
|
|
// filter becomes saturated after navigating multiple directories we'll start getting
|
|
|
|
// status pushes that we never requested and fill our cache. Ignore those.
|
|
|
|
if (!wasAsked) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
it = _cache.insert(make_pair(responsePath, StateNone)).first;
|
2015-05-06 16:51:41 +03:00
|
|
|
}
|
2017-01-09 16:25:00 +03:00
|
|
|
|
|
|
|
updateView = it->second != state;
|
|
|
|
it->second = state;
|
2014-10-21 16:51:18 +04:00
|
|
|
}
|
2017-01-09 16:25:00 +03:00
|
|
|
if (updateView) {
|
2020-06-05 01:51:32 +03:00
|
|
|
SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH | SHCNF_FLUSHNOWAIT, responsePath.data(), nullptr);
|
2014-10-14 18:05:48 +04:00
|
|
|
}
|
2017-01-05 19:28:28 +03:00
|
|
|
}
|
|
|
|
}
|
2014-10-14 18:05:48 +04:00
|
|
|
|
2017-01-05 19:28:28 +03:00
|
|
|
if (socket.Event() == INVALID_HANDLE_VALUE) {
|
2017-01-09 18:28:26 +03:00
|
|
|
atomic_store(&_watchedDirectories, make_shared<const vector<wstring>>());
|
2017-01-05 19:28:28 +03:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
_connected = connected = false;
|
2017-01-05 19:45:39 +03:00
|
|
|
|
|
|
|
// Swap to make a copy of the cache under the mutex and clear the one stored.
|
|
|
|
std::unordered_map<std::wstring, FileState> cache;
|
|
|
|
swap(cache, _cache);
|
|
|
|
lock.unlock();
|
|
|
|
// Let explorer know about each invalidated cache entry that needs to get its icon removed.
|
|
|
|
for (auto it = cache.begin(); it != cache.end(); ++it) {
|
2020-06-05 01:51:32 +03:00
|
|
|
SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH | SHCNF_FLUSHNOWAIT, it->first.data(), nullptr);
|
2017-01-05 19:45:39 +03:00
|
|
|
}
|
2017-01-05 19:28:28 +03:00
|
|
|
}
|
2014-10-15 18:09:35 +04:00
|
|
|
|
2017-01-05 19:28:28 +03:00
|
|
|
if (_stop) return;
|
2014-10-15 18:09:35 +04:00
|
|
|
|
2017-01-05 19:28:28 +03:00
|
|
|
HANDLE handles[2] = { _newQueries, socket.Event() };
|
|
|
|
WaitForMultipleObjects(2, handles, false, 0);
|
2014-10-14 18:05:48 +04:00
|
|
|
}
|
2014-07-30 19:20:55 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 18:05:48 +04:00
|
|
|
|
|
|
|
|
|
|
|
RemotePathChecker::RemotePathChecker()
|
2017-06-13 11:28:12 +03:00
|
|
|
: _stop(false)
|
|
|
|
, _watchedDirectories(make_shared<const vector<wstring>>())
|
2017-01-09 18:28:26 +03:00
|
|
|
, _connected(false)
|
2020-06-05 01:51:32 +03:00
|
|
|
, _newQueries(CreateEvent(nullptr, FALSE, FALSE, nullptr))
|
2017-01-05 19:28:28 +03:00
|
|
|
, _thread([this]{ this->workerThreadLoop(); })
|
2014-08-05 21:23:40 +04:00
|
|
|
{
|
2014-10-14 18:05:48 +04:00
|
|
|
}
|
2014-08-05 21:23:40 +04:00
|
|
|
|
2014-10-14 18:05:48 +04:00
|
|
|
RemotePathChecker::~RemotePathChecker()
|
|
|
|
{
|
|
|
|
_stop = true;
|
|
|
|
//_newQueries.notify_all();
|
|
|
|
SetEvent(_newQueries);
|
|
|
|
_thread.join();
|
|
|
|
CloseHandle(_newQueries);
|
|
|
|
}
|
|
|
|
|
2017-01-09 18:28:26 +03:00
|
|
|
std::shared_ptr<const std::vector<std::wstring>> RemotePathChecker::WatchedDirectories() const
|
2014-10-14 18:05:48 +04:00
|
|
|
{
|
2017-01-09 18:28:26 +03:00
|
|
|
return atomic_load(&_watchedDirectories);
|
2014-08-05 21:23:40 +04:00
|
|
|
}
|
|
|
|
|
2014-08-04 17:40:08 +04:00
|
|
|
bool RemotePathChecker::IsMonitoredPath(const wchar_t* filePath, int* state)
|
2014-07-30 19:20:55 +04:00
|
|
|
{
|
2014-10-14 18:05:48 +04:00
|
|
|
assert(state); assert(filePath);
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2014-10-14 18:05:48 +04:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2014-10-15 17:13:04 +04:00
|
|
|
if (!_connected) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2014-10-14 18:05:48 +04:00
|
|
|
auto path = std::wstring(filePath);
|
|
|
|
|
|
|
|
auto it = _cache.find(path);
|
|
|
|
if (it != _cache.end()) {
|
2017-01-05 19:45:39 +03:00
|
|
|
// The path is in our cache, and we'll get updates pushed if the status changes.
|
2014-10-14 18:05:48 +04:00
|
|
|
*state = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_pending.push(filePath);
|
2015-05-05 12:24:01 +03:00
|
|
|
|
2014-12-03 11:10:09 +03:00
|
|
|
lock.unlock();
|
2014-10-14 18:05:48 +04:00
|
|
|
SetEvent(_newQueries);
|
2017-01-05 19:45:39 +03:00
|
|
|
return false;
|
2014-07-30 19:20:55 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 18:05:48 +04:00
|
|
|
RemotePathChecker::FileState RemotePathChecker::_StrToFileState(const std::wstring &str)
|
2014-07-30 19:20:55 +04:00
|
|
|
{
|
2017-01-05 19:28:28 +03:00
|
|
|
if (str == L"NOP" || str == L"NONE") {
|
|
|
|
return StateNone;
|
|
|
|
} else if (str == L"SYNC" || str == L"NEW") {
|
|
|
|
return StateSync;
|
|
|
|
} else if (str == L"SYNC+SWM" || str == L"NEW+SWM") {
|
|
|
|
return StateSync;
|
|
|
|
} else if (str == L"OK") {
|
|
|
|
return StateOk;
|
|
|
|
} else if (str == L"OK+SWM") {
|
|
|
|
return StateOkSWM;
|
|
|
|
} else if (str == L"IGNORE") {
|
|
|
|
return StateWarning;
|
|
|
|
} else if (str == L"IGNORE+SWM") {
|
|
|
|
return StateWarning;
|
|
|
|
} else if (str == L"ERROR") {
|
|
|
|
return StateError;
|
|
|
|
} else if (str == L"ERROR+SWM") {
|
|
|
|
return StateError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return StateNone;
|
2016-04-20 17:51:17 +03:00
|
|
|
}
|