summaryrefslogtreecommitdiffhomepage
path: root/windows/nsis-plugins/src/cleanup/cleaningops.cpp
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2020-09-30 15:40:23 +0200
committerDavid Lönnhager <david.l@mullvad.net>2020-10-01 11:12:20 +0200
commitf07496719a79258a4a9c19453be018b5336a65e1 (patch)
tree43497dab41622321a53c1fb0b0e8d8326791ea8b /windows/nsis-plugins/src/cleanup/cleaningops.cpp
parent547e7d8ad007dc3bbb4119502872f44cd7da3161 (diff)
downloadmullvadvpn-f07496719a79258a4a9c19453be018b5336a65e1.tar.xz
mullvadvpn-f07496719a79258a4a9c19453be018b5336a65e1.zip
Remove directories created by Electron
Diffstat (limited to 'windows/nsis-plugins/src/cleanup/cleaningops.cpp')
-rw-r--r--windows/nsis-plugins/src/cleanup/cleaningops.cpp74
1 files changed, 59 insertions, 15 deletions
diff --git a/windows/nsis-plugins/src/cleanup/cleaningops.cpp b/windows/nsis-plugins/src/cleanup/cleaningops.cpp
index ed6e8dbf9d..8653bb50a9 100644
--- a/windows/nsis-plugins/src/cleanup/cleaningops.cpp
+++ b/windows/nsis-plugins/src/cleanup/cleaningops.cpp
@@ -7,6 +7,7 @@
#include <libcommon/memory.h>
#include <libcommon/security.h>
#include <libcommon/process/process.h>
+#include <optional>
#include <filesystem>
#include <utility>
#include <functional>
@@ -47,10 +48,11 @@ mirrored_range
return std::make_pair(begin, lhsBegin);
}
-std::wstring ConstructLocalAppDataPath(const std::wstring &base, const std::wstring &user,
- const std::pair<std::vector<std::wstring>::iterator, std::vector<std::wstring>::iterator> &tokens)
+template<typename IterType>
+std::wstring ConstructUserPath(const std::wstring &users, const std::wstring &user,
+ const std::pair<IterType, IterType> &tokens)
{
- auto path = std::filesystem::path(base);
+ auto path = std::filesystem::path(users);
path.append(user);
@@ -103,6 +105,21 @@ std::wstring GetSystemUserLocalAppData()
return common::fs::GetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_DEFAULT, processToken);
}
+template <class It>
+size_t EqualTokensCount(It lhsBegin, It lhsEnd, It rhsBegin, It rhsEnd)
+{
+ auto mirror = mirrored_range
+ (
+ lhsBegin, lhsEnd, rhsBegin, rhsEnd,
+ [](const std::wstring &lhs, const std::wstring &rhs)
+ {
+ return 0 == _wcsicmp(lhs.c_str(), rhs.c_str());
+ }
+ );
+
+ return static_cast<size_t>(std::distance(mirror.first, mirror.second));
+}
+
} // anonymous namespace
namespace cleaningops
@@ -114,6 +131,12 @@ void RemoveLogsCacheCurrentUser()
const auto appdir = std::filesystem::path(localAppData).append(L"Mullvad VPN");
std::filesystem::remove_all(appdir);
+
+ const auto roamingAppData = common::fs::GetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr);
+ const auto roamingAppdir = std::filesystem::path(roamingAppData).append(L"Mullvad VPN");
+
+ std::error_code dummy;
+ std::filesystem::remove_all(roamingAppdir, dummy);
}
void RemoveLogsCacheOtherUsers()
@@ -126,26 +149,23 @@ void RemoveLogsCacheOtherUsers()
//
auto localAppData = common::fs::GetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_DEFAULT, nullptr);
+ auto roamingAppData = common::fs::GetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr);
auto homeDir = common::fs::GetKnownFolderPath(FOLDERID_Profile, KF_FLAG_DEFAULT, nullptr);
//
// Tokenize to get rid of slashes pointing in different directions.
//
auto localAppDataTokens = common::string::Tokenize(localAppData, L"\\/");
+ auto roamingAppDataTokens = common::string::Tokenize(roamingAppData, L"\\/");
auto homeDirTokens = common::string::Tokenize(homeDir, L"\\/");
- auto mirror = mirrored_range
- (
- localAppDataTokens.begin(), localAppDataTokens.end(),
- homeDirTokens.begin(), homeDirTokens.end(),
- [](const std::wstring &lhs, const std::wstring &rhs)
- {
- return 0 == _wcsicmp(lhs.c_str(), rhs.c_str());
- }
+ auto equalTokensCount = EqualTokensCount(
+ localAppDataTokens.begin(),
+ localAppDataTokens.end(),
+ homeDirTokens.begin(),
+ homeDirTokens.end()
);
- auto equalTokensCount = (size_t)std::distance(mirror.first, mirror.second);
-
//
// Abort if "local app data" is not beneath home dir.
//
@@ -155,11 +175,27 @@ void RemoveLogsCacheOtherUsers()
}
auto relativeLocalAppData = std::make_pair(std::next(localAppDataTokens.begin(), equalTokensCount), localAppDataTokens.end());
+
+ using StringVectorConstIter = std::vector<std::wstring>::const_iterator;
+ std::optional<std::pair<StringVectorConstIter, StringVectorConstIter>> relativeRoamingAppData;
+
+ const auto roamingTokensCount = EqualTokensCount(
+ roamingAppDataTokens.begin(),
+ roamingAppDataTokens.end(),
+ homeDirTokens.begin(),
+ homeDirTokens.end()
+ );
+
+ if (roamingTokensCount >= homeDirTokens.size())
+ {
+ relativeRoamingAppData = std::make_optional(std::make_pair(std::next(roamingAppDataTokens.cbegin(), equalTokensCount), roamingAppDataTokens.cend()));
+ }
+
auto currentUser = *homeDirTokens.rbegin();
//
// Find all other users and construct the most plausible path for their
- // respective "local app data" dirs.
+ // respective app data dirs.
//
auto parentHomeDir = common::fs::GetKnownFolderPath(FOLDERID_UserProfiles, KF_FLAG_DEFAULT, nullptr);
@@ -181,11 +217,19 @@ void RemoveLogsCacheOtherUsers()
while (files.next(file))
{
- const auto userLocalAppData = ConstructLocalAppDataPath(files.getDirectory(), file.cFileName, relativeLocalAppData);
+ const auto userLocalAppData = ConstructUserPath(files.getDirectory(), file.cFileName, relativeLocalAppData);
const auto target = std::filesystem::path(userLocalAppData).append(L"Mullvad VPN");
std::error_code dummy;
std::filesystem::remove_all(target, dummy);
+
+ if (relativeRoamingAppData.has_value())
+ {
+ const auto userRoamingAppData = ConstructUserPath(files.getDirectory(), file.cFileName, relativeRoamingAppData.value());
+ const auto roamingTarget = std::filesystem::path(userRoamingAppData).append(L"Mullvad VPN");
+
+ std::filesystem::remove_all(roamingTarget, dummy);
+ }
}
}