From fe818a49551a5c9d58638c48eced6b25df8c6e8c Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 29 Mar 2018 12:02:35 -0400 Subject: [PATCH 01/18] add console for ctrl+c to daemon app when debugging in foreground --- src/lib/barrier/win32/DaemonApp.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/barrier/win32/DaemonApp.cpp b/src/lib/barrier/win32/DaemonApp.cpp index 62fecf8f..48e7cc8e 100644 --- a/src/lib/barrier/win32/DaemonApp.cpp +++ b/src/lib/barrier/win32/DaemonApp.cpp @@ -130,8 +130,10 @@ DaemonApp::run(int argc, char** argv) } if (foreground) { - // run process in foreground instead of daemonizing. - // useful for debugging. + // add a console to catch Ctrl+C and run process in foreground + // instead of daemonizing. useful for debugging. + if (IsDebuggerPresent()) + AllocConsole(); mainLoop(false); } else { From 72cc7e3d89acb3b642e33479b0abc05d07d15bd8 Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 29 Mar 2018 14:01:07 -0400 Subject: [PATCH 02/18] link gui with common; reimplement finding personal and profile directories on windows (not yet used) --- src/gui/CMakeLists.txt | 2 + src/lib/common/CMakeLists.txt | 11 +++++ src/lib/common/DataDirectories.h | 7 +++ src/lib/common/win32/DataDirectories.cpp | 54 ++++++++++++++++++++++++ src/lib/common/win32/DataDirectories.h | 20 +++++++++ 5 files changed, 94 insertions(+) create mode 100644 src/lib/common/DataDirectories.h create mode 100644 src/lib/common/win32/DataDirectories.cpp create mode 100644 src/lib/common/win32/DataDirectories.h diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 5d248912..9c902867 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -45,6 +45,8 @@ if (HAVE_X11) target_link_libraries (barrier X11) endif() +target_link_libraries (barrier common) + if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") install (TARGETS barrier DESTINATION ${BARRIER_BUNDLE_BINARY_DIR}) elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux") diff --git a/src/lib/common/CMakeLists.txt b/src/lib/common/CMakeLists.txt index 56d9fc96..eda7fa1a 100644 --- a/src/lib/common/CMakeLists.txt +++ b/src/lib/common/CMakeLists.txt @@ -17,6 +17,17 @@ file(GLOB headers "*.h") file(GLOB sources "*.cpp") +if (WIN32) + file(GLOB arch_headers "win32/*.h") + file(GLOB arch_sources "win32/*.cpp") +elseif (UNIX) + file(GLOB arch_headers "unix/*.h") + file(GLOB arch_sources "unix/*.cpp") +endif() + +list(APPEND headers ${arch_headers}) +list(APPEND sources ${arch_sources}) + if (BARRIER_ADD_HEADERS) list(APPEND sources ${headers}) endif() diff --git a/src/lib/common/DataDirectories.h b/src/lib/common/DataDirectories.h new file mode 100644 index 00000000..fad4b559 --- /dev/null +++ b/src/lib/common/DataDirectories.h @@ -0,0 +1,7 @@ +#pragma once + +#ifdef _WIN32 +#include "win32/DataDirectories.h" +#else +#include "unix/DataDirectories.h" +#endif diff --git a/src/lib/common/win32/DataDirectories.cpp b/src/lib/common/win32/DataDirectories.cpp new file mode 100644 index 00000000..832fd821 --- /dev/null +++ b/src/lib/common/win32/DataDirectories.cpp @@ -0,0 +1,54 @@ +#include "DataDirectories.h" + +#include + +// static member +std::string DataDirectories::_personal; +std::string DataDirectories::_profile; + +std::string unicode_to_mb(const WCHAR* utfStr) +{ + int utfLength = lstrlenW(utfStr); + int mbLength = WideCharToMultiByte(CP_UTF8, 0, utfStr, utfLength, NULL, 0, NULL, NULL); + std::string mbStr(mbLength, 0); + WideCharToMultiByte(CP_UTF8, 0, utfStr, utfLength, &mbStr[0], mbLength, NULL, NULL); + return mbStr; +} + +std::string known_folder_path(const KNOWNFOLDERID& id) +{ + std::string path; + WCHAR* buffer; + HRESULT result = SHGetKnownFolderPath(id, 0, NULL, &buffer); + if (result == S_OK) { + path = unicode_to_mb(buffer); + CoTaskMemFree(buffer); + } + return path; +} + +const std::string& DataDirectories::personal() +{ + if (_personal.empty()) + _personal = known_folder_path(FOLDERID_Documents); + return _personal; +} + +const std::string& DataDirectories::personal(const std::string& path) +{ + _personal = path; + return _personal; +} + +const std::string& DataDirectories::profile() +{ + if (_profile.empty()) + _profile = known_folder_path(FOLDERID_LocalAppData) + "\\Barrier"; + return _profile; +} + +const std::string& DataDirectories::profile(const std::string& path) +{ + _profile = path; + return _profile; +} \ No newline at end of file diff --git a/src/lib/common/win32/DataDirectories.h b/src/lib/common/win32/DataDirectories.h new file mode 100644 index 00000000..3b276162 --- /dev/null +++ b/src/lib/common/win32/DataDirectories.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +class DataDirectories +{ +public: + static const std::string& personal(); + static const std::string& personal(const std::string& path); + + static const std::string& profile(); + static const std::string& profile(const std::string& path); + +private: + // static class + DataDirectories() {} + + static std::string _personal; + static std::string _profile; +}; \ No newline at end of file From c5e70af09a4cab3212056bbf98020705bbe4c544 Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 29 Mar 2018 15:50:23 -0400 Subject: [PATCH 03/18] DataDirectories header should be shared between platform-specific implementations --- src/lib/common/DataDirectories.h | 24 +++++++++++++++++++----- src/lib/common/win32/DataDirectories.h | 20 -------------------- 2 files changed, 19 insertions(+), 25 deletions(-) delete mode 100644 src/lib/common/win32/DataDirectories.h diff --git a/src/lib/common/DataDirectories.h b/src/lib/common/DataDirectories.h index fad4b559..f615a653 100644 --- a/src/lib/common/DataDirectories.h +++ b/src/lib/common/DataDirectories.h @@ -1,7 +1,21 @@ #pragma once -#ifdef _WIN32 -#include "win32/DataDirectories.h" -#else -#include "unix/DataDirectories.h" -#endif +#include + +class DataDirectories +{ +public: + static const std::string& personal(); + static const std::string& personal(const std::string& path); + + static const std::string& profile(); + static const std::string& profile(const std::string& path); + +private: + // static class + DataDirectories() {} + + static std::string _personal; + static std::string _profile; +}; + diff --git a/src/lib/common/win32/DataDirectories.h b/src/lib/common/win32/DataDirectories.h deleted file mode 100644 index 3b276162..00000000 --- a/src/lib/common/win32/DataDirectories.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include - -class DataDirectories -{ -public: - static const std::string& personal(); - static const std::string& personal(const std::string& path); - - static const std::string& profile(); - static const std::string& profile(const std::string& path); - -private: - // static class - DataDirectories() {} - - static std::string _personal; - static std::string _profile; -}; \ No newline at end of file From 96627f4f07292e65d72dd848310230447286c348 Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 29 Mar 2018 15:54:41 -0400 Subject: [PATCH 04/18] reimplement finding personal & profile directories on unix (not yet used) --- src/lib/common/CMakeLists.txt | 5 ++ src/lib/common/unix/DataDirectories.cpp | 76 ++++++++++++++++++++++++ src/lib/common/win32/DataDirectories.cpp | 5 +- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/lib/common/unix/DataDirectories.cpp diff --git a/src/lib/common/CMakeLists.txt b/src/lib/common/CMakeLists.txt index eda7fa1a..b3791c1c 100644 --- a/src/lib/common/CMakeLists.txt +++ b/src/lib/common/CMakeLists.txt @@ -33,3 +33,8 @@ if (BARRIER_ADD_HEADERS) endif() add_library(common STATIC ${sources}) + +if (HAVE_GETPWUID_R) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_GETPWUID_R=1") +endif() + diff --git a/src/lib/common/unix/DataDirectories.cpp b/src/lib/common/unix/DataDirectories.cpp new file mode 100644 index 00000000..95aa08e1 --- /dev/null +++ b/src/lib/common/unix/DataDirectories.cpp @@ -0,0 +1,76 @@ +#include "../DataDirectories.h" + +#include // sysconf +#include // getpwuid(_r) +#include // getpwuid(_r) + +#ifdef WINAPI_XWINDOWS +const std::string ProfileSubdir = "/.barrier"; +#else // macos +const std::string ProfileSubdir = "/Library/Application Support/Barrier"; +#endif + +// static members +std::string DataDirectories::_personal; +std::string DataDirectories::_profile; + +static std::string pw_dir(struct passwd* pwentp) +{ + if (pwentp != NULL && pwentp->pw_dir != NULL) + return pwentp->pw_dir; + return ""; +} + +#ifdef HAVE_GETPWUID_R + +static std::string unix_home() +{ + long size = -1; +#if defined(_SC_GETPW_R_SIZE_MAX) + size = sysconf(_SC_GETPW_R_SIZE_MAX); +#endif + if (size == -1) + size = BUFSIZ; + + struct passwd pwent; + struct passwd* pwentp; + std::string buffer(size, 0); + getpwuid_r(getuid(), &pwent, &buffer[0], size, &pwentp); + return pw_dir(pwentp); +} + +#else // not HAVE_GETPWUID_R + +static std::string unix_home() +{ + return pw_dir(getpwuid(getuid())); +} + +#endif // HAVE_GETPWUID_R + +const std::string& DataDirectories::personal() +{ + if (_personal.empty()) + _personal = unix_home(); + return _personal; +} + +const std::string& DataDirectories::personal(const std::string& path) +{ + _personal = path; + return _personal; +} + +const std::string& DataDirectories::profile() +{ + if (_profile.empty()) + _profile = personal() + ProfileSubdir; + return _profile; +} + +const std::string& DataDirectories::profile(const std::string& path) +{ + _profile = path; + return _profile; +} + diff --git a/src/lib/common/win32/DataDirectories.cpp b/src/lib/common/win32/DataDirectories.cpp index 832fd821..02e216f3 100644 --- a/src/lib/common/win32/DataDirectories.cpp +++ b/src/lib/common/win32/DataDirectories.cpp @@ -1,4 +1,4 @@ -#include "DataDirectories.h" +#include "../DataDirectories.h" #include @@ -51,4 +51,5 @@ const std::string& DataDirectories::profile(const std::string& path) { _profile = path; return _profile; -} \ No newline at end of file +} + From c16fd089f6c620f142b09d20b8edc833afbb776c Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 29 Mar 2018 16:12:45 -0400 Subject: [PATCH 05/18] old personal and profile directory functions now wrap the new implementations --- src/lib/arch/unix/ArchFileUnix.cpp | 41 ++--------------- src/lib/arch/unix/ArchFileUnix.h | 1 - src/lib/arch/win32/ArchFileWindows.cpp | 62 ++------------------------ src/lib/arch/win32/ArchFileWindows.h | 1 - src/lib/platform/MSWindowsHook.cpp | 3 +- 5 files changed, 10 insertions(+), 98 deletions(-) diff --git a/src/lib/arch/unix/ArchFileUnix.cpp b/src/lib/arch/unix/ArchFileUnix.cpp index d9ae8b21..a687f5a5 100644 --- a/src/lib/arch/unix/ArchFileUnix.cpp +++ b/src/lib/arch/unix/ArchFileUnix.cpp @@ -17,6 +17,7 @@ */ #include "arch/unix/ArchFileUnix.h" +#include "common/DataDirectories.h" #include #include @@ -57,29 +58,7 @@ ArchFileUnix::getBasename(const char* pathname) std::string ArchFileUnix::getUserDirectory() { - char* buffer = NULL; - std::string dir; -#if HAVE_GETPWUID_R - struct passwd pwent; - struct passwd* pwentp; -#if defined(_SC_GETPW_R_SIZE_MAX) - long size = sysconf(_SC_GETPW_R_SIZE_MAX); - if (size == -1) { - size = BUFSIZ; - } -#else - long size = BUFSIZ; -#endif - buffer = new char[size]; - getpwuid_r(getuid(), &pwent, buffer, size, &pwentp); -#else - struct passwd* pwentp = getpwuid(getuid()); -#endif - if (pwentp != NULL && pwentp->pw_dir != NULL) { - dir = pwentp->pw_dir; - } - delete[] buffer; - return dir; + return DataDirectories::personal(); } std::string @@ -121,19 +100,7 @@ ArchFileUnix::getPluginDirectory() std::string ArchFileUnix::getProfileDirectory() { - String dir; - if (!m_profileDirectory.empty()) { - dir = m_profileDirectory; - } - else { -#if WINAPI_XWINDOWS - dir = getUserDirectory().append("/.barrier"); -#else - dir = getUserDirectory().append("/Library/Application Support/Barrier"); -#endif - } - return dir; - + return DataDirectories::profile(); } std::string @@ -153,7 +120,7 @@ ArchFileUnix::concatPath(const std::string& prefix, void ArchFileUnix::setProfileDirectory(const String& s) { - m_profileDirectory = s; + DataDirectories::profile(s); } void diff --git a/src/lib/arch/unix/ArchFileUnix.h b/src/lib/arch/unix/ArchFileUnix.h index 86dea0c9..a59e6015 100644 --- a/src/lib/arch/unix/ArchFileUnix.h +++ b/src/lib/arch/unix/ArchFileUnix.h @@ -42,6 +42,5 @@ public: virtual void setPluginDirectory(const String& s); private: - String m_profileDirectory; String m_pluginDirectory; }; diff --git a/src/lib/arch/win32/ArchFileWindows.cpp b/src/lib/arch/win32/ArchFileWindows.cpp index 53b4b594..190fa9ce 100644 --- a/src/lib/arch/win32/ArchFileWindows.cpp +++ b/src/lib/arch/win32/ArchFileWindows.cpp @@ -17,6 +17,7 @@ */ #include "arch/win32/ArchFileWindows.h" +#include "common/DataDirectories.h" #define WIN32_LEAN_AND_MEAN #include @@ -66,45 +67,7 @@ ArchFileWindows::getBasename(const char* pathname) std::string ArchFileWindows::getUserDirectory() { - // try %HOMEPATH% - TCHAR dir[MAX_PATH]; - DWORD size = sizeof(dir) / sizeof(TCHAR); - DWORD result = GetEnvironmentVariable(_T("HOMEPATH"), dir, size); - if (result != 0 && result <= size) { - // sanity check -- if dir doesn't appear to start with a - // drive letter and isn't a UNC name then don't use it - // FIXME -- allow UNC names - if (dir[0] != '\0' && (dir[1] == ':' || - ((dir[0] == '\\' || dir[0] == '/') && - (dir[1] == '\\' || dir[1] == '/')))) { - return dir; - } - } - - // get the location of the personal files. that's as close to - // a home directory as we're likely to find. - ITEMIDLIST* idl; - if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &idl))) { - TCHAR* path = NULL; - if (SHGetPathFromIDList(idl, dir)) { - DWORD attr = GetFileAttributes(dir); - if (attr != 0xffffffff && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0) - path = dir; - } - - IMalloc* shalloc; - if (SUCCEEDED(SHGetMalloc(&shalloc))) { - shalloc->Free(idl); - shalloc->Release(); - } - - if (path != NULL) { - return path; - } - } - - // use root of C drive as a default - return "C:"; + return DataDirectories::personal(); } std::string @@ -154,24 +117,7 @@ ArchFileWindows::getPluginDirectory() std::string ArchFileWindows::getProfileDirectory() { - String dir; - if (!m_profileDirectory.empty()) { - dir = m_profileDirectory; - } - else { - TCHAR result[MAX_PATH]; - if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, result))) { - dir = result; - } - else { - dir = getUserDirectory(); - } - } - - // HACK: append program name, this seems wrong. - dir.append("\\Barrier"); - - return dir; + return DataDirectories::profile(); } std::string @@ -193,7 +139,7 @@ ArchFileWindows::concatPath(const std::string& prefix, void ArchFileWindows::setProfileDirectory(const String& s) { - m_profileDirectory = s; + DataDirectories::profile(s); } void diff --git a/src/lib/arch/win32/ArchFileWindows.h b/src/lib/arch/win32/ArchFileWindows.h index 4747b9c8..453aa61a 100644 --- a/src/lib/arch/win32/ArchFileWindows.h +++ b/src/lib/arch/win32/ArchFileWindows.h @@ -42,6 +42,5 @@ public: virtual void setPluginDirectory(const String& s); private: - String m_profileDirectory; String m_pluginDirectory; }; diff --git a/src/lib/platform/MSWindowsHook.cpp b/src/lib/platform/MSWindowsHook.cpp index 929888e4..2d7845a0 100644 --- a/src/lib/platform/MSWindowsHook.cpp +++ b/src/lib/platform/MSWindowsHook.cpp @@ -22,6 +22,7 @@ #include "platform/ImmuneKeysReader.h" #include "barrier/protocol_types.h" #include "barrier/XScreen.h" +#include "common/DataDirectories.h" #include "base/Log.h" // @@ -52,7 +53,7 @@ static BYTE g_keyState[256] = { 0 }; static bool g_fakeServerInput = false; static std::vector g_immuneKeys; -static const std::string ImmuneKeysPath = ArchFileWindows().getProfileDirectory() + "\\ImmuneKeys.txt"; +static const std::string ImmuneKeysPath = DataDirectories::profile() + "\\ImmuneKeys.txt"; static std::vector immune_keys_list() { From 451bd72b3040ed9d247e758c1dc57b7c7fceeb58 Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 29 Mar 2018 16:32:51 -0400 Subject: [PATCH 06/18] MSWindowsWatchdog checks active desktop without external call to syntool --- src/lib/platform/MSWindowsWatchdog.cpp | 99 ++++---------------------- src/lib/platform/MSWindowsWatchdog.h | 7 -- 2 files changed, 15 insertions(+), 91 deletions(-) diff --git a/src/lib/platform/MSWindowsWatchdog.cpp b/src/lib/platform/MSWindowsWatchdog.cpp index ba1890e5..1e34bcbf 100644 --- a/src/lib/platform/MSWindowsWatchdog.cpp +++ b/src/lib/platform/MSWindowsWatchdog.cpp @@ -44,7 +44,19 @@ enum { typedef VOID (WINAPI *SendSas)(BOOL asUser); -const char g_activeDesktop[] = {"activeDesktop:"}; +std::string activeDesktopName() +{ + const std::size_t BufferLength = 1024; + std::string name; + HDESK desk = OpenInputDesktop(0, FALSE, GENERIC_READ); + if (desk != NULL) { + TCHAR buffer[BufferLength]; + if (GetUserObjectInformation(desk, UOI_NAME, buffer, BufferLength - 1, NULL) == TRUE) + name = buffer; + CloseDesktop(desk); + } + return name; +} MSWindowsWatchdog::MSWindowsWatchdog( bool daemonized, @@ -64,22 +76,8 @@ MSWindowsWatchdog::MSWindowsWatchdog( m_processRunning(false), m_fileLogOutputter(NULL), m_autoElevated(false), - m_ready(false), m_daemonized(daemonized) { - m_mutex = ARCH->newMutex(); - m_condVar = ARCH->newCondVar(); -} - -MSWindowsWatchdog::~MSWindowsWatchdog() -{ - if (m_condVar != NULL) { - ARCH->closeCondVar(m_condVar); - } - - if (m_mutex != NULL) { - ARCH->closeMutex(m_mutex); - } } void @@ -288,12 +286,9 @@ MSWindowsWatchdog::startProcess() if (!m_daemonized) { createRet = doStartProcessAsSelf(m_command); } else { - SECURITY_ATTRIBUTES sa; - ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); + m_autoElevated = activeDesktopName() != "Default"; - getActiveDesktop(&sa); - - ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); + SECURITY_ATTRIBUTES sa{ 0 }; HANDLE userToken = getUserToken(&sa); m_elevateProcess = m_autoElevated ? m_autoElevated : m_elevateProcess; m_autoElevated = false; @@ -444,11 +439,7 @@ MSWindowsWatchdog::outputLoop(void*) } else { buffer[bytesRead] = '\0'; - - testOutput(buffer); - m_ipcLogOutputter.write(kINFO, buffer); - if (m_fileLogOutputter != NULL) { m_fileLogOutputter->write(kINFO, buffer); } @@ -549,63 +540,3 @@ MSWindowsWatchdog::shutdownExistingProcesses() CloseHandle(snapshot); m_processRunning = false; } - -void -MSWindowsWatchdog::getActiveDesktop(LPSECURITY_ATTRIBUTES security) -{ - String installedDir = ARCH->getInstalledDirectory(); - if (!installedDir.empty()) { - String syntoolCommand; - syntoolCommand.append("\"").append(installedDir).append("\\").append("syntool").append("\""); - syntoolCommand.append(" --get-active-desktop"); - - m_session.updateActiveSession(); - bool elevateProcess = m_elevateProcess; - m_elevateProcess = true; - HANDLE userToken = getUserToken(security); - m_elevateProcess = elevateProcess; - - BOOL createRet = doStartProcessAsUser(syntoolCommand, userToken, security); - - if (!createRet) { - DWORD rc = GetLastError(); - RevertToSelf(); - } - else { - LOG((CLOG_DEBUG "launched syntool to check active desktop")); - } - - ARCH->lockMutex(m_mutex); - int waitTime = 0; - while (!m_ready) { - if (waitTime >= MAXIMUM_WAIT_TIME) { - break; - } - - ARCH->waitCondVar(m_condVar, m_mutex, 1.0); - waitTime++; - } - m_ready = false; - ARCH->unlockMutex(m_mutex); - } -} - -void -MSWindowsWatchdog::testOutput(String buffer) -{ - // HACK: check standard output seems hacky. - size_t i = buffer.find(g_activeDesktop); - if (i != String::npos) { - size_t s = sizeof(g_activeDesktop); - String defaultDesktop("Default"); - String sub = buffer.substr(i + s - 1, defaultDesktop.size()); - if (sub != defaultDesktop) { - m_autoElevated = true; - } - - ARCH->lockMutex(m_mutex); - m_ready = true; - ARCH->broadcastCondVar(m_condVar); - ARCH->unlockMutex(m_mutex); - } -} diff --git a/src/lib/platform/MSWindowsWatchdog.h b/src/lib/platform/MSWindowsWatchdog.h index 64ffab30..7a1f6617 100644 --- a/src/lib/platform/MSWindowsWatchdog.h +++ b/src/lib/platform/MSWindowsWatchdog.h @@ -39,7 +39,6 @@ public: bool autoDetectCommand, IpcServer& ipcServer, IpcLogOutputter& ipcLogOutputter); - virtual ~MSWindowsWatchdog(); void startAsync(); std::string getCommand() const; @@ -58,9 +57,6 @@ private: void startProcess(); BOOL doStartProcessAsUser(String& command, HANDLE userToken, LPSECURITY_ATTRIBUTES sa); BOOL doStartProcessAsSelf(String& command); - void sendSas(); - void getActiveDesktop(LPSECURITY_ATTRIBUTES security); - void testOutput(String buffer); private: Thread* m_thread; @@ -80,9 +76,6 @@ private: bool m_processRunning; FileLogOutputter* m_fileLogOutputter; bool m_autoElevated; - ArchMutex m_mutex; - ArchCond m_condVar; - bool m_ready; bool m_daemonized; }; From 6e5b340bccb376f0932f0b08aa433b1d67653a2f Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 29 Mar 2018 16:38:50 -0400 Subject: [PATCH 07/18] replace CoreInterface syntool calls with DataDirectory calls --- src/gui/src/Fingerprint.cpp | 5 ++--- src/gui/src/MainWindow.cpp | 4 ++-- src/gui/src/SettingsDialog.cpp | 1 - src/gui/src/SettingsDialog.h | 2 -- src/gui/src/SetupWizard.cpp | 1 - src/gui/src/SslCertificate.cpp | 4 ++-- src/gui/src/SslCertificate.h | 3 --- 7 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/gui/src/Fingerprint.cpp b/src/gui/src/Fingerprint.cpp index be8b4017..24c8a1a3 100644 --- a/src/gui/src/Fingerprint.cpp +++ b/src/gui/src/Fingerprint.cpp @@ -17,7 +17,7 @@ #include "Fingerprint.h" -#include "CoreInterface.h" +#include "common/DataDirectories.h" #include #include @@ -125,8 +125,7 @@ void Fingerprint::persistDirectory() QString Fingerprint::directoryPath() { - CoreInterface coreInterface; - QString profileDir = coreInterface.getProfileDir(); + auto profileDir = QString::fromStdString(DataDirectories::profile()); return QString("%1/%2") .arg(profileDir) diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index b92e5f7c..bb3b9a55 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -31,6 +31,7 @@ #include "ProcessorArch.h" #include "SslCertificate.h" #include "ShutdownCh.h" +#include "common/DataDirectories.h" #include #include @@ -1254,8 +1255,7 @@ void MainWindow::bonjourInstallFinished() QString MainWindow::getProfileRootForArg() { - CoreInterface coreInterface; - QString dir = coreInterface.getProfileDir(); + auto dir = QString::fromStdString(DataDirectories::profile()); // HACK: strip our app name since we're returning the root dir. #if defined(Q_OS_WIN) diff --git a/src/gui/src/SettingsDialog.cpp b/src/gui/src/SettingsDialog.cpp index dc073134..15f7067f 100644 --- a/src/gui/src/SettingsDialog.cpp +++ b/src/gui/src/SettingsDialog.cpp @@ -18,7 +18,6 @@ #include "SettingsDialog.h" -#include "CoreInterface.h" #include "BarrierLocale.h" #include "QBarrierApplication.h" #include "QUtility.h" diff --git a/src/gui/src/SettingsDialog.h b/src/gui/src/SettingsDialog.h index c16b8218..b733bcc9 100644 --- a/src/gui/src/SettingsDialog.h +++ b/src/gui/src/SettingsDialog.h @@ -23,7 +23,6 @@ #include #include "ui_SettingsDialogBase.h" #include "BarrierLocale.h" -#include "CoreInterface.h" class AppConfig; @@ -43,7 +42,6 @@ class SettingsDialog : public QDialog, public Ui::SettingsDialogBase private: AppConfig& m_appConfig; BarrierLocale m_Locale; - CoreInterface m_CoreInterface; private slots: void on_m_pComboLanguage_currentIndexChanged(int index); diff --git a/src/gui/src/SetupWizard.cpp b/src/gui/src/SetupWizard.cpp index 313e48b6..0cfdb81e 100644 --- a/src/gui/src/SetupWizard.cpp +++ b/src/gui/src/SetupWizard.cpp @@ -17,7 +17,6 @@ #include "SetupWizard.h" #include "MainWindow.h" -#include "WebClient.h" #include "QBarrierApplication.h" #include "QUtility.h" diff --git a/src/gui/src/SslCertificate.cpp b/src/gui/src/SslCertificate.cpp index 7de7eaac..9b31c5da 100644 --- a/src/gui/src/SslCertificate.cpp +++ b/src/gui/src/SslCertificate.cpp @@ -16,8 +16,8 @@ */ #include "SslCertificate.h" - #include "Fingerprint.h" +#include "common/DataDirectories.h" #include #include @@ -37,7 +37,7 @@ static const char kConfigFile[] = "barrier.conf"; SslCertificate::SslCertificate(QObject *parent) : QObject(parent) { - m_ProfileDir = m_CoreInterface.getProfileDir(); + m_ProfileDir = QString::fromStdString(DataDirectories::profile()); if (m_ProfileDir.isEmpty()) { emit error(tr("Failed to get profile directory.")); } diff --git a/src/gui/src/SslCertificate.h b/src/gui/src/SslCertificate.h index 8acda4b2..8b209138 100644 --- a/src/gui/src/SslCertificate.h +++ b/src/gui/src/SslCertificate.h @@ -17,8 +17,6 @@ #pragma once -#include "CoreInterface.h" - #include class SslCertificate : public QObject @@ -43,5 +41,4 @@ private: private: QString m_ProfileDir; QString m_ToolOutput; - CoreInterface m_CoreInterface; }; From ea025f5958e4ca53578eab720ed95c821d7833bc Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 29 Mar 2018 17:13:45 -0400 Subject: [PATCH 08/18] fix --profile-dir argument --- src/gui/src/MainWindow.cpp | 16 +--------------- src/gui/src/MainWindow.h | 1 - 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index bb3b9a55..2b4d10fc 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -504,7 +504,7 @@ void MainWindow::startBarrier() // launched the process (e.g. when launched with elevation). setting the // profile dir on launch ensures it uses the same profile dir is used // no matter how its relaunched. - args << "--profile-dir" << getProfileRootForArg(); + args << "--profile-dir" << QString::fromStdString("\"" + DataDirectories::profile() + "\""); #endif if ((barrierType() == barrierClient && !clientArgs(args, app)) @@ -1253,20 +1253,6 @@ void MainWindow::bonjourInstallFinished() m_pCheckBoxAutoConfig->setChecked(true); } -QString MainWindow::getProfileRootForArg() -{ - auto dir = QString::fromStdString(DataDirectories::profile()); - - // HACK: strip our app name since we're returning the root dir. -#if defined(Q_OS_WIN) - dir.replace("\\Barrier", ""); -#else - dir.replace("/.barrier", ""); -#endif - - return QString("\"%1\"").arg(dir); -} - void MainWindow::windowStateChanged() { if (windowState() == Qt::WindowMinimized && appConfig().getMinimizeToTray()) diff --git a/src/gui/src/MainWindow.h b/src/gui/src/MainWindow.h index 27b30d11..5bb28950 100644 --- a/src/gui/src/MainWindow.h +++ b/src/gui/src/MainWindow.h @@ -166,7 +166,6 @@ public slots: bool isBonjourRunning(); void downloadBonjour(); void promptAutoConfig(); - QString getProfileRootForArg(); void checkConnected(const QString& line); void checkFingerprint(const QString& line); void restartBarrier(); From 1be86a924897a0e6df11799dffe87f3b15cf2cdf Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 29 Mar 2018 17:14:57 -0400 Subject: [PATCH 09/18] remove syntool, CoreInterface, and WebClient --- src/cmd/CMakeLists.txt | 1 - src/cmd/syntool/CMakeLists.txt | 27 ----- src/cmd/syntool/syntool.cpp | 31 ----- src/gui/src/CoreInterface.cpp | 96 --------------- src/gui/src/CoreInterface.h | 36 ------ src/gui/src/WebClient.cpp | 83 ------------- src/gui/src/WebClient.h | 49 -------- src/lib/barrier/ArgParser.cpp | 41 ------- src/lib/barrier/ArgParser.h | 2 - src/lib/barrier/ToolApp.cpp | 205 --------------------------------- src/lib/barrier/ToolApp.h | 37 ------ src/lib/barrier/ToolArgs.cpp | 29 ----- src/lib/barrier/ToolArgs.h | 34 ------ 13 files changed, 671 deletions(-) delete mode 100644 src/cmd/syntool/CMakeLists.txt delete mode 100644 src/cmd/syntool/syntool.cpp delete mode 100644 src/gui/src/CoreInterface.cpp delete mode 100644 src/gui/src/CoreInterface.h delete mode 100644 src/gui/src/WebClient.cpp delete mode 100644 src/gui/src/WebClient.h delete mode 100644 src/lib/barrier/ToolApp.cpp delete mode 100644 src/lib/barrier/ToolApp.h delete mode 100644 src/lib/barrier/ToolArgs.cpp delete mode 100644 src/lib/barrier/ToolArgs.h diff --git a/src/cmd/CMakeLists.txt b/src/cmd/CMakeLists.txt index 0fdfe339..ebf2a0d2 100644 --- a/src/cmd/CMakeLists.txt +++ b/src/cmd/CMakeLists.txt @@ -16,7 +16,6 @@ add_subdirectory(barrierc) add_subdirectory(barriers) -add_subdirectory(syntool) if (WIN32) add_subdirectory(barrierd) diff --git a/src/cmd/syntool/CMakeLists.txt b/src/cmd/syntool/CMakeLists.txt deleted file mode 100644 index 863d4f48..00000000 --- a/src/cmd/syntool/CMakeLists.txt +++ /dev/null @@ -1,27 +0,0 @@ -# barrier -- mouse and keyboard sharing utility -# Copyright (C) 2014-2016 Symless Ltd. -# -# This package is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# found in the file LICENSE that should have accompanied this file. -# -# This package 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 this program. If not, see . - -file(GLOB headers "*.h") -file(GLOB sources "*.cpp") - -add_executable(syntool ${sources}) -target_link_libraries(syntool - synlib arch base client common io ipc mt net platform server ${libs} ${OPENSSL_LIBS}) - -if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - install (TARGETS syntool DESTINATION ${BARRIER_BUNDLE_BINARY_DIR}) -elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux") - install (TARGETS syntool DESTINATION bin) -endif() diff --git a/src/cmd/syntool/syntool.cpp b/src/cmd/syntool/syntool.cpp deleted file mode 100644 index 72d35b9d..00000000 --- a/src/cmd/syntool/syntool.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2014-2016 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#include "barrier/ToolApp.h" -#include "arch/Arch.h" - -int -main(int argc, char** argv) -{ -#if SYSAPI_WIN32 - // record window instance for tray icon, etc - ArchMiscWindows::setInstanceWin32(GetModuleHandle(NULL)); -#endif - - ToolApp app; - return app.run(argc, argv); -} diff --git a/src/gui/src/CoreInterface.cpp b/src/gui/src/CoreInterface.cpp deleted file mode 100644 index d5ed40d2..00000000 --- a/src/gui/src/CoreInterface.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2015-2016 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#include "CoreInterface.h" - -#include "CommandProcess.h" -#include "QUtility.h" - -#include -#include -#include -#include -#include - -static const char kCoreBinary[] = "syntool"; - -#ifdef Q_WS_WIN -static const char kSerialKeyFilename[] = "Barrier.subkey"; -#else -static const char kSerialKeyFilename[] = ".barrier.subkey"; -#endif - -CoreInterface::CoreInterface() -{ -} - -QString CoreInterface::getProfileDir() -{ - QStringList args("--get-profile-dir"); - return run(args); -} - -QString CoreInterface::getInstalledDir() -{ - QStringList args("--get-installed-dir"); - return run(args); -} - -QString CoreInterface::getArch() -{ - QStringList args("--get-arch"); - return run(args); -} - -QString CoreInterface::getSerialKeyFilePath() -{ - QString filename = getProfileDir() + QDir::separator() + kSerialKeyFilename; - return filename; -} - -QString CoreInterface::notifyUpdate (QString const& fromVersion, - QString const& toVersion, - QString const& serialKey) { - QStringList args("--notify-update"); - QString input(fromVersion + ":" + toVersion + ":" + serialKey); - input.append("\n"); - return run(args, input); -} - -QString CoreInterface::notifyActivation(const QString& identity) -{ - QStringList args("--notify-activation"); - - QString input(identity + ":" + hash(getFirstMacAddress())); - QString os= getOSInformation(); - if (!os.isEmpty()) { - input.append(":").append(os); - } - input.append("\n"); - - return run(args, input); -} - -QString CoreInterface::run(const QStringList& args, const QString& input) -{ - QString program( - QCoreApplication::applicationDirPath() - + "/" + kCoreBinary); - - CommandProcess commandProcess(program, args, input); - return commandProcess.run(); -} diff --git a/src/gui/src/CoreInterface.h b/src/gui/src/CoreInterface.h deleted file mode 100644 index 26b9c0ab..00000000 --- a/src/gui/src/CoreInterface.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2015-2016 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#pragma once - -#include - -class CoreInterface -{ -public: - CoreInterface(); - - QString getProfileDir(); - QString getInstalledDir(); - QString getArch(); - QString getSerialKeyFilePath(); - QString notifyActivation(const QString& identity); - QString notifyUpdate (QString const& fromVersion, - QString const& toVersion, - QString const& serialKey); - QString run(const QStringList& args, const QString& input = ""); -}; diff --git a/src/gui/src/WebClient.cpp b/src/gui/src/WebClient.cpp deleted file mode 100644 index 8cded2cb..00000000 --- a/src/gui/src/WebClient.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2015-2016 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#include "WebClient.h" - -#include "QUtility.h" - -#include -#include -#include -#include - -bool -WebClient::getEdition (int& edition, QString& errorOut) { - QString responseJson = request(); - - /* TODO: This is horrible and should be ripped out as soon as we move - * to Qt 5. See issue #5630 - */ - - QRegExp resultRegex(".*\"result\".*:.*(true|false).*"); - if (resultRegex.exactMatch (responseJson)) { - QString boolString = resultRegex.cap(1); - if (boolString == "true") { - QRegExp editionRegex(".*\"edition\".*:.*\"([^\"]+)\".*"); - if (editionRegex.exactMatch(responseJson)) { - QString e = editionRegex.cap(1); - edition = e.toInt(); - return true; - } else { - throw std::runtime_error ("Unrecognised server response."); - } - } else { - errorOut = tr("Login failed. Invalid email address or password."); - return false; - } - } else { - QRegExp errorRegex(".*\"error\".*:.*\"([^\"]+)\".*"); - if (errorRegex.exactMatch (responseJson)) { - errorOut = errorRegex.cap(1).replace("\\n", "\n"); - return false; - } else { - throw std::runtime_error ("Unrecognised server response."); - } - } -} - -bool -WebClient::setEmail (QString email, QString& errorOut) { - if (email.isEmpty()) { - errorOut = tr("Your email address cannot be left blank."); - return false; - } - m_Email = email; - return true; -} - -bool -WebClient::setPassword (QString password, QString&) { - m_Password = password; - return true; -} - -QString -WebClient::request() { - QStringList args("--login-auth"); - QString credentials (m_Email + ":" + hash(m_Password) + "\n"); - return m_CoreInterface.run (args, credentials); -} diff --git a/src/gui/src/WebClient.h b/src/gui/src/WebClient.h deleted file mode 100644 index 9874bd58..00000000 --- a/src/gui/src/WebClient.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2015-2016 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#ifndef WEBCLIENT_H -#define WEBCLIENT_H - -#include -#include - -#include "CoreInterface.h" - -class QMessageBox; -class QWidget; -class QStringList; - -class WebClient : public QObject -{ - Q_OBJECT - -public: - bool getEdition (int& edition, QString& errorOut); - bool setEmail (QString email, QString& errorOut); - bool setPassword (QString password, QString& errorOut); -signals: - void error(QString e); - -private: - QString request(); - - QString m_Email; - QString m_Password; - CoreInterface m_CoreInterface; -}; - -#endif // WEBCLIENT_H diff --git a/src/lib/barrier/ArgParser.cpp b/src/lib/barrier/ArgParser.cpp index 20e849cf..1ce918c7 100644 --- a/src/lib/barrier/ArgParser.cpp +++ b/src/lib/barrier/ArgParser.cpp @@ -21,7 +21,6 @@ #include "barrier/App.h" #include "barrier/ServerArgs.h" #include "barrier/ClientArgs.h" -#include "barrier/ToolArgs.h" #include "barrier/ArgsBase.h" #include "base/Log.h" #include "base/String.h" @@ -171,46 +170,6 @@ ArgParser::parsePlatformArg(ArgsBase& argsBase, const int& argc, const char* con #endif } -bool -ArgParser::parseToolArgs(ToolArgs& args, int argc, const char* const* argv) -{ - for (int i = 1; i < argc; ++i) { - if (isArg(i, argc, argv, NULL, "--get-active-desktop", 0)) { - args.m_printActiveDesktopName = true; - return true; - } - else if (isArg(i, argc, argv, NULL, "--login-auth", 0)) { - args.m_loginAuthenticate = true; - return true; - } - else if (isArg(i, argc, argv, NULL, "--get-installed-dir", 0)) { - args.m_getInstalledDir = true; - return true; - } - else if (isArg(i, argc, argv, NULL, "--get-profile-dir", 0)) { - args.m_getProfileDir = true; - return true; - } - else if (isArg(i, argc, argv, NULL, "--get-arch", 0)) { - args.m_getArch = true; - return true; - } - else if (isArg(i, argc, argv, NULL, "--notify-activation", 0)) { - args.m_notifyActivation = true; - return true; - } - else if (isArg(i, argc, argv, NULL, "--notify-update", 0)) { - args.m_notifyUpdate = true; - return true; - } - else { - return false; - } - } - - return false; -} - bool ArgParser::parseGenericArgs(int argc, const char* const* argv, int& i) { diff --git a/src/lib/barrier/ArgParser.h b/src/lib/barrier/ArgParser.h index 5fc2649c..32300c61 100644 --- a/src/lib/barrier/ArgParser.h +++ b/src/lib/barrier/ArgParser.h @@ -22,7 +22,6 @@ class ServerArgs; class ClientArgs; -class ToolArgs; class ArgsBase; class App; @@ -34,7 +33,6 @@ public: bool parseServerArgs(ServerArgs& args, int argc, const char* const* argv); bool parseClientArgs(ClientArgs& args, int argc, const char* const* argv); bool parsePlatformArg(ArgsBase& argsBase, const int& argc, const char* const* argv, int& i); - bool parseToolArgs(ToolArgs& args, int argc, const char* const* argv); bool parseGenericArgs(int argc, const char* const* argv, int& i); bool parseDeprecatedArgs(int argc, const char* const* argv, int& i); void setArgsBase(ArgsBase& argsBase) { m_argsBase = &argsBase; } diff --git a/src/lib/barrier/ToolApp.cpp b/src/lib/barrier/ToolApp.cpp deleted file mode 100644 index ae85e6dc..00000000 --- a/src/lib/barrier/ToolApp.cpp +++ /dev/null @@ -1,205 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2014-2016 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#include "barrier/ToolApp.h" - -#include "barrier/ArgParser.h" -#include "arch/Arch.h" -#include "base/Log.h" -#include "base/String.h" - -#include -#include - -#if SYSAPI_WIN32 -#include "platform/MSWindowsSession.h" -#endif - -#define JSON_URL "https://symless.com/account/json/" - -enum { - kErrorOk, - kErrorArgs, - kErrorException, - kErrorUnknown -}; - -UInt32 -ToolApp::run(int argc, char** argv) -{ - if (argc <= 1) { - std::cerr << "no args" << std::endl; - return kErrorArgs; - } - - try { - ArgParser argParser(this); - bool result = argParser.parseToolArgs(m_args, argc, argv); - - if (!result) { - m_bye(kExitArgs); - } - - if (m_args.m_printActiveDesktopName) { -#if SYSAPI_WIN32 - MSWindowsSession session; - String name = session.getActiveDesktopName(); - if (name.empty()) { - LOG((CLOG_CRIT "failed to get active desktop name")); - return kExitFailed; - } - else { - String output = barrier::string::sprintf("activeDesktop:%s", name.c_str()); - LOG((CLOG_INFO "%s", output.c_str())); - } -#endif - } - else if (m_args.m_loginAuthenticate) { - loginAuth(); - } - else if (m_args.m_getInstalledDir) { - std::cout << ARCH->getInstalledDirectory() << std::endl; - } - else if (m_args.m_getProfileDir) { - std::cout << ARCH->getProfileDirectory() << std::endl; - } - else if (m_args.m_getArch) { - std::cout << ARCH->getPlatformName() << std::endl; - } - else if (m_args.m_notifyUpdate) { - notifyUpdate(); - } - else if (m_args.m_notifyActivation) { - notifyActivation(); - } - else { - throw XBarrier("Nothing to do"); - } - } - catch (std::exception& e) { - LOG((CLOG_CRIT "An error occurred: %s\n", e.what())); - return kExitFailed; - } - catch (...) { - LOG((CLOG_CRIT "An unknown error occurred.\n")); - return kExitFailed; - } - -#if WINAPI_XWINDOWS - // HACK: avoid sigsegv on linux - m_bye(kErrorOk); -#endif - - return kErrorOk; -} - -void -ToolApp::help() -{ -} - -void -ToolApp::loginAuth() -{ - String credentials; - std::cin >> credentials; - - std::vector parts = barrier::string::splitString(credentials, ':'); - size_t count = parts.size(); - - if (count == 2 ) { - String email = parts[0]; - String password = parts[1]; - - std::stringstream ss; - ss << JSON_URL << "auth/"; - ss << "?email=" << ARCH->internet().urlEncode(email); - ss << "&password=" << password; - - std::cout << ARCH->internet().get(ss.str()) << std::endl; - } - else { - throw XBarrier("Invalid credentials."); - } -} - -void -ToolApp::notifyUpdate() -{ - String data; - std::cin >> data; - - std::vector parts = barrier::string::splitString(data, ':'); - size_t count = parts.size(); - - if (count == 3) { - std::stringstream ss; - ss << JSON_URL << "notify/update"; - ss << "?from=" << parts[0]; - ss << "&to=" << parts[1]; - - std::cout << ARCH->internet().get(ss.str()) << std::endl; - } - else { - throw XBarrier("Invalid update data."); - } -} - -void -ToolApp::notifyActivation() -{ - String info; - std::cin >> info; - - std::vector parts = barrier::string::splitString(info, ':'); - size_t count = parts.size(); - - if (count == 3 || count == 4) { - String action = parts[0]; - String identity = parts[1]; - String macHash = parts[2]; - String os; - - if (count == 4) { - os = parts[3]; - } - else { - os = ARCH->getOSName(); - } - - std::stringstream ss; - ss << JSON_URL << "notify/"; - ss << "?action=" << action; - ss << "&identity=" << ARCH->internet().urlEncode(identity); - ss << "&mac=" << ARCH->internet().urlEncode(macHash); - ss << "&os=" << ARCH->internet().urlEncode(ARCH->getOSName()); - ss << "&arch=" << ARCH->internet().urlEncode(ARCH->getPlatformName()); - - try { - std::cout << ARCH->internet().get(ss.str()) << std::endl; - } - catch (std::exception& e) { - LOG((CLOG_NOTE "An error occurred during notification: %s\n", e.what())); - } - catch (...) { - LOG((CLOG_NOTE "An unknown error occurred during notification.\n")); - } - } - else { - LOG((CLOG_NOTE "notification failed")); - } -} diff --git a/src/lib/barrier/ToolApp.h b/src/lib/barrier/ToolApp.h deleted file mode 100644 index 5cb9a7c4..00000000 --- a/src/lib/barrier/ToolApp.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2014-2016 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#pragma once - -#include "barrier/App.h" -#include "barrier/ToolArgs.h" -#include "common/basic_types.h" - -class ToolApp : public MinimalApp -{ -public: - UInt32 run(int argc, char** argv); - void help(); - -private: - void loginAuth(); - void notifyActivation(); - void notifyUpdate(); - -private: - ToolArgs m_args; -}; diff --git a/src/lib/barrier/ToolArgs.cpp b/src/lib/barrier/ToolArgs.cpp deleted file mode 100644 index 634a7843..00000000 --- a/src/lib/barrier/ToolArgs.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2014-2016 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#include "barrier/ToolArgs.h" - -ToolArgs::ToolArgs() : - m_printActiveDesktopName(false), - m_loginAuthenticate(false), - m_getInstalledDir(false), - m_getProfileDir(false), - m_getArch(false), - m_notifyActivation(false), - m_notifyUpdate(false) -{ -} diff --git a/src/lib/barrier/ToolArgs.h b/src/lib/barrier/ToolArgs.h deleted file mode 100644 index 36b4be3e..00000000 --- a/src/lib/barrier/ToolArgs.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2014-2016 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#pragma once - -#include "base/String.h" - -class ToolArgs { -public: - ToolArgs(); - -public: - bool m_printActiveDesktopName; - bool m_loginAuthenticate; - bool m_getInstalledDir; - bool m_getProfileDir; - bool m_getArch; - bool m_notifyActivation; - bool m_notifyUpdate; -}; From d81054ab6ea602ee6a6d2be4f2586879a13e6b1d Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 29 Mar 2018 21:41:30 -0400 Subject: [PATCH 10/18] remove some stale code and put windows service logfile in a better spot --- src/lib/arch/IArchFile.h | 26 -------------- src/lib/arch/unix/ArchFileUnix.cpp | 30 ---------------- src/lib/arch/unix/ArchFileUnix.h | 3 -- src/lib/arch/win32/ArchFileWindows.cpp | 46 ------------------------ src/lib/arch/win32/ArchFileWindows.h | 10 ------ src/lib/barrier/App.cpp | 1 - src/lib/barrier/win32/DaemonApp.cpp | 14 ++++---- src/lib/common/DataDirectories.h | 4 +++ src/lib/common/unix/DataDirectories.cpp | 17 +++++++-- src/lib/common/win32/DataDirectories.cpp | 14 ++++++-- src/lib/platform/MSWindowsUtil.cpp | 11 ++++++ src/lib/platform/MSWindowsUtil.h | 7 ++++ 12 files changed, 55 insertions(+), 128 deletions(-) diff --git a/src/lib/arch/IArchFile.h b/src/lib/arch/IArchFile.h index 5fdd2882..0c34243a 100644 --- a/src/lib/arch/IArchFile.h +++ b/src/lib/arch/IArchFile.h @@ -51,25 +51,6 @@ public: */ virtual std::string getSystemDirectory() = 0; - //! Get installed directory - /*! - Returns the directory in which Barrier is installed. - */ - virtual std::string getInstalledDirectory() = 0; - - //! Get log directory - /*! - Returns the log file directory. - */ - virtual std::string getLogDirectory() = 0; - - //! Get plugins directory - /*! - Returns the plugin files directory. If no plugin directory is set, - this will return the plugin folder within the user's profile. - */ - virtual std::string getPluginDirectory() = 0; - //! Get user's profile directory /*! Returns the user's profile directory. If no profile directory is set, @@ -95,11 +76,4 @@ public: Returns the user's profile directory. */ virtual void setProfileDirectory(const String& s) = 0; - - //@} - //! Set the user's plugin directory - /* - Returns the user's plugin directory. - */ - virtual void setPluginDirectory(const String& s) = 0; }; diff --git a/src/lib/arch/unix/ArchFileUnix.cpp b/src/lib/arch/unix/ArchFileUnix.cpp index a687f5a5..04d1be68 100644 --- a/src/lib/arch/unix/ArchFileUnix.cpp +++ b/src/lib/arch/unix/ArchFileUnix.cpp @@ -67,36 +67,6 @@ ArchFileUnix::getSystemDirectory() return "/etc"; } -std::string -ArchFileUnix::getInstalledDirectory() -{ -#if WINAPI_XWINDOWS - return "/usr/bin"; -#else - return "/Applications/Barrier.app/Contents/MacOS"; -#endif -} - -std::string -ArchFileUnix::getLogDirectory() -{ - return "/var/log"; -} - -std::string -ArchFileUnix::getPluginDirectory() -{ - if (!m_pluginDirectory.empty()) { - return m_pluginDirectory; - } - -#if WINAPI_XWINDOWS - return getProfileDirectory().append("/plugins"); -#else - return getProfileDirectory().append("/Plugins"); -#endif -} - std::string ArchFileUnix::getProfileDirectory() { diff --git a/src/lib/arch/unix/ArchFileUnix.h b/src/lib/arch/unix/ArchFileUnix.h index a59e6015..65154dfd 100644 --- a/src/lib/arch/unix/ArchFileUnix.h +++ b/src/lib/arch/unix/ArchFileUnix.h @@ -32,9 +32,6 @@ public: virtual const char* getBasename(const char* pathname); virtual std::string getUserDirectory(); virtual std::string getSystemDirectory(); - virtual std::string getInstalledDirectory(); - virtual std::string getLogDirectory(); - virtual std::string getPluginDirectory(); virtual std::string getProfileDirectory(); virtual std::string concatPath(const std::string& prefix, const std::string& suffix); diff --git a/src/lib/arch/win32/ArchFileWindows.cpp b/src/lib/arch/win32/ArchFileWindows.cpp index 190fa9ce..ce1ddf27 100644 --- a/src/lib/arch/win32/ArchFileWindows.cpp +++ b/src/lib/arch/win32/ArchFileWindows.cpp @@ -29,16 +29,6 @@ // ArchFileWindows // -ArchFileWindows::ArchFileWindows() -{ - // do nothing -} - -ArchFileWindows::~ArchFileWindows() -{ - // do nothing -} - const char* ArchFileWindows::getBasename(const char* pathname) { @@ -84,36 +74,6 @@ ArchFileWindows::getSystemDirectory() } } -std::string -ArchFileWindows::getInstalledDirectory() -{ - char fileNameBuffer[MAX_PATH]; - GetModuleFileName(NULL, fileNameBuffer, MAX_PATH); - std::string fileName(fileNameBuffer); - size_t lastSlash = fileName.find_last_of("\\"); - fileName = fileName.substr(0, lastSlash); - - return fileName; -} - -std::string -ArchFileWindows::getLogDirectory() -{ - return getInstalledDirectory(); -} - -std::string -ArchFileWindows::getPluginDirectory() -{ - if (!m_pluginDirectory.empty()) { - return m_pluginDirectory; - } - - std::string dir = getProfileDirectory(); - dir.append("\\Plugins"); - return dir; -} - std::string ArchFileWindows::getProfileDirectory() { @@ -141,9 +101,3 @@ ArchFileWindows::setProfileDirectory(const String& s) { DataDirectories::profile(s); } - -void -ArchFileWindows::setPluginDirectory(const String& s) -{ - m_pluginDirectory = s; -} diff --git a/src/lib/arch/win32/ArchFileWindows.h b/src/lib/arch/win32/ArchFileWindows.h index 453aa61a..8c95536e 100644 --- a/src/lib/arch/win32/ArchFileWindows.h +++ b/src/lib/arch/win32/ArchFileWindows.h @@ -25,22 +25,12 @@ //! Win32 implementation of IArchFile class ArchFileWindows : public IArchFile { public: - ArchFileWindows(); - virtual ~ArchFileWindows(); - // IArchFile overrides virtual const char* getBasename(const char* pathname); virtual std::string getUserDirectory(); virtual std::string getSystemDirectory(); - virtual std::string getInstalledDirectory(); - virtual std::string getLogDirectory(); - virtual std::string getPluginDirectory(); virtual std::string getProfileDirectory(); virtual std::string concatPath(const std::string& prefix, const std::string& suffix); virtual void setProfileDirectory(const String& s); - virtual void setPluginDirectory(const String& s); - -private: - String m_pluginDirectory; }; diff --git a/src/lib/barrier/App.cpp b/src/lib/barrier/App.cpp index 1f4eda32..db28f7cf 100644 --- a/src/lib/barrier/App.cpp +++ b/src/lib/barrier/App.cpp @@ -176,7 +176,6 @@ App::initApp(int argc, const char** argv) parseArgs(argc, argv); ARCH->setProfileDirectory(argsBase().m_profileDirectory); - ARCH->setPluginDirectory(argsBase().m_pluginDirectory); // set log filter if (!CLOG->setFilter(argsBase().m_logFilter)) { diff --git a/src/lib/barrier/win32/DaemonApp.cpp b/src/lib/barrier/win32/DaemonApp.cpp index 48e7cc8e..eafd8936 100644 --- a/src/lib/barrier/win32/DaemonApp.cpp +++ b/src/lib/barrier/win32/DaemonApp.cpp @@ -33,6 +33,7 @@ #include "base/EventQueue.h" #include "base/log_outputters.h" #include "base/Log.h" +#include "common/DataDirectories.h" #include "arch/win32/ArchMiscWindows.h" #include "arch/win32/XArchWindows.h" @@ -41,6 +42,7 @@ #include "platform/MSWindowsDebugOutputter.h" #include "platform/MSWindowsWatchdog.h" #include "platform/MSWindowsEventQueueBuffer.h" +#include "platform/MSWindowsUtil.h" #define WIN32_LEAN_AND_MEAN #include @@ -241,14 +243,10 @@ DaemonApp::foregroundError(const char* message) std::string DaemonApp::logFilename() { - string logFilename; - logFilename = ARCH->setting("LogFilename"); - if (logFilename.empty()) { - logFilename = ARCH->getLogDirectory(); - logFilename.append("/"); - logFilename.append(LOG_FILENAME); - } - + string logFilename = ARCH->setting("LogFilename"); + if (logFilename.empty()) + logFilename = DataDirectories::global() + "\\" + LOG_FILENAME; + MSWindowsUtil::createDirectory(logFilename, true); return logFilename; } diff --git a/src/lib/common/DataDirectories.h b/src/lib/common/DataDirectories.h index f615a653..fd024710 100644 --- a/src/lib/common/DataDirectories.h +++ b/src/lib/common/DataDirectories.h @@ -11,11 +11,15 @@ public: static const std::string& profile(); static const std::string& profile(const std::string& path); + static const std::string& global(); + static const std::string& global(const std::string& path); + private: // static class DataDirectories() {} static std::string _personal; static std::string _profile; + static std::string _global; }; diff --git a/src/lib/common/unix/DataDirectories.cpp b/src/lib/common/unix/DataDirectories.cpp index 95aa08e1..9d0e66b7 100644 --- a/src/lib/common/unix/DataDirectories.cpp +++ b/src/lib/common/unix/DataDirectories.cpp @@ -13,6 +13,7 @@ const std::string ProfileSubdir = "/Library/Application Support/Barrier"; // static members std::string DataDirectories::_personal; std::string DataDirectories::_profile; +std::string DataDirectories::_global; static std::string pw_dir(struct passwd* pwentp) { @@ -54,7 +55,6 @@ const std::string& DataDirectories::personal() _personal = unix_home(); return _personal; } - const std::string& DataDirectories::personal(const std::string& path) { _personal = path; @@ -67,10 +67,23 @@ const std::string& DataDirectories::profile() _profile = personal() + ProfileSubdir; return _profile; } - const std::string& DataDirectories::profile(const std::string& path) { _profile = path; return _profile; } +const std::string& DataDirectories::global() +{ + if (_global.empty()) + // TODO: where on a unix system should public/global shared data go? + // as of march 2018 global() is not used for unix + _global = "/tmp"; + return _global; +} +const std::string& DataDirectories::global(const std::string& path) +{ + _global = path; + return _global; +} + diff --git a/src/lib/common/win32/DataDirectories.cpp b/src/lib/common/win32/DataDirectories.cpp index 02e216f3..4c171296 100644 --- a/src/lib/common/win32/DataDirectories.cpp +++ b/src/lib/common/win32/DataDirectories.cpp @@ -5,6 +5,7 @@ // static member std::string DataDirectories::_personal; std::string DataDirectories::_profile; +std::string DataDirectories::_global; std::string unicode_to_mb(const WCHAR* utfStr) { @@ -33,7 +34,6 @@ const std::string& DataDirectories::personal() _personal = known_folder_path(FOLDERID_Documents); return _personal; } - const std::string& DataDirectories::personal(const std::string& path) { _personal = path; @@ -46,10 +46,20 @@ const std::string& DataDirectories::profile() _profile = known_folder_path(FOLDERID_LocalAppData) + "\\Barrier"; return _profile; } - const std::string& DataDirectories::profile(const std::string& path) { _profile = path; return _profile; } +const std::string& DataDirectories::global() +{ + if (_global.empty()) + _global = known_folder_path(FOLDERID_ProgramData) + "\\Barrier"; + return _global; +} +const std::string& DataDirectories::global(const std::string& path) +{ + _global = path; + return _global; +} diff --git a/src/lib/platform/MSWindowsUtil.cpp b/src/lib/platform/MSWindowsUtil.cpp index 4b51781c..87b1ddc6 100644 --- a/src/lib/platform/MSWindowsUtil.cpp +++ b/src/lib/platform/MSWindowsUtil.cpp @@ -62,3 +62,14 @@ MSWindowsUtil::getErrorString(HINSTANCE hinstance, DWORD error, DWORD id) return result; } } + +void +MSWindowsUtil::createDirectory(const std::string& path, bool stripLast) +{ + // create parent directories + for (auto pos = path.find_first_of('\\'); pos != std::string::npos; pos = path.find_first_of('\\', pos + 1)) + CreateDirectory(path.substr(0, pos).c_str(), NULL); + if (!stripLast) + // create innermost directory + CreateDirectory(path.c_str(), NULL); +} diff --git a/src/lib/platform/MSWindowsUtil.h b/src/lib/platform/MSWindowsUtil.h index 95ec2cfc..29eef2ea 100644 --- a/src/lib/platform/MSWindowsUtil.h +++ b/src/lib/platform/MSWindowsUtil.h @@ -37,4 +37,11 @@ public: found return the string for \p id, replacing ${1} with \p error. */ static String getErrorString(HINSTANCE, DWORD error, DWORD id); + + //! Create directory + /*! + Create directory \p recursively optionally stripping the last component + (presumably the filename) if \p is set. + */ + static void createDirectory(const std::string& path, bool stripLast = false); }; From 6c5acdd5525db51dc0189c6c80ccb80b2e6fda85 Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Fri, 30 Mar 2018 14:01:18 -0400 Subject: [PATCH 11/18] remove DataDirectory wrappers in ArchFile* --- src/lib/arch/IArchFile.h | 22 ----------------- src/lib/arch/unix/ArchFileUnix.cpp | 34 -------------------------- src/lib/arch/unix/ArchFileUnix.h | 10 -------- src/lib/arch/win32/ArchFileWindows.cpp | 18 -------------- src/lib/arch/win32/ArchFileWindows.h | 3 --- src/lib/barrier/App.cpp | 6 ++--- src/lib/barrier/ServerApp.cpp | 3 ++- src/lib/net/SecureListenSocket.cpp | 3 ++- src/lib/net/SecureSocket.cpp | 3 ++- 9 files changed, 8 insertions(+), 94 deletions(-) diff --git a/src/lib/arch/IArchFile.h b/src/lib/arch/IArchFile.h index 0c34243a..a974a1b1 100644 --- a/src/lib/arch/IArchFile.h +++ b/src/lib/arch/IArchFile.h @@ -38,27 +38,12 @@ public: */ virtual const char* getBasename(const char* pathname) = 0; - //! Get user's home directory - /*! - Returns the user's home directory. Returns the empty string if - this cannot be determined. - */ - virtual std::string getUserDirectory() = 0; - //! Get system directory /*! Returns the ussystem configuration file directory. */ virtual std::string getSystemDirectory() = 0; - //! Get user's profile directory - /*! - Returns the user's profile directory. If no profile directory is set, - this will return the user's profile according to the operating system, - which will depend on which user launched the program. - */ - virtual std::string getProfileDirectory() = 0; - //! Concatenate path components /*! Concatenate pathname components with a directory separator @@ -69,11 +54,4 @@ public: virtual std::string concatPath( const std::string& prefix, const std::string& suffix) = 0; - - //@} - //! Set the user's profile directory - /* - Returns the user's profile directory. - */ - virtual void setProfileDirectory(const String& s) = 0; }; diff --git a/src/lib/arch/unix/ArchFileUnix.cpp b/src/lib/arch/unix/ArchFileUnix.cpp index 04d1be68..1b13a65e 100644 --- a/src/lib/arch/unix/ArchFileUnix.cpp +++ b/src/lib/arch/unix/ArchFileUnix.cpp @@ -29,16 +29,6 @@ // ArchFileUnix // -ArchFileUnix::ArchFileUnix() -{ - // do nothing -} - -ArchFileUnix::~ArchFileUnix() -{ - // do nothing -} - const char* ArchFileUnix::getBasename(const char* pathname) { @@ -55,24 +45,12 @@ ArchFileUnix::getBasename(const char* pathname) } } -std::string -ArchFileUnix::getUserDirectory() -{ - return DataDirectories::personal(); -} - std::string ArchFileUnix::getSystemDirectory() { return "/etc"; } -std::string -ArchFileUnix::getProfileDirectory() -{ - return DataDirectories::profile(); -} - std::string ArchFileUnix::concatPath(const std::string& prefix, const std::string& suffix) @@ -86,15 +64,3 @@ ArchFileUnix::concatPath(const std::string& prefix, path += suffix; return path; } - -void -ArchFileUnix::setProfileDirectory(const String& s) -{ - DataDirectories::profile(s); -} - -void -ArchFileUnix::setPluginDirectory(const String& s) -{ - m_pluginDirectory = s; -} diff --git a/src/lib/arch/unix/ArchFileUnix.h b/src/lib/arch/unix/ArchFileUnix.h index 65154dfd..67b27b2a 100644 --- a/src/lib/arch/unix/ArchFileUnix.h +++ b/src/lib/arch/unix/ArchFileUnix.h @@ -25,19 +25,9 @@ //! Unix implementation of IArchFile class ArchFileUnix : public IArchFile { public: - ArchFileUnix(); - virtual ~ArchFileUnix(); - // IArchFile overrides virtual const char* getBasename(const char* pathname); - virtual std::string getUserDirectory(); virtual std::string getSystemDirectory(); - virtual std::string getProfileDirectory(); virtual std::string concatPath(const std::string& prefix, const std::string& suffix); - virtual void setProfileDirectory(const String& s); - virtual void setPluginDirectory(const String& s); - -private: - String m_pluginDirectory; }; diff --git a/src/lib/arch/win32/ArchFileWindows.cpp b/src/lib/arch/win32/ArchFileWindows.cpp index ce1ddf27..022cfe9a 100644 --- a/src/lib/arch/win32/ArchFileWindows.cpp +++ b/src/lib/arch/win32/ArchFileWindows.cpp @@ -54,12 +54,6 @@ ArchFileWindows::getBasename(const char* pathname) return basename; } -std::string -ArchFileWindows::getUserDirectory() -{ - return DataDirectories::personal(); -} - std::string ArchFileWindows::getSystemDirectory() { @@ -74,12 +68,6 @@ ArchFileWindows::getSystemDirectory() } } -std::string -ArchFileWindows::getProfileDirectory() -{ - return DataDirectories::profile(); -} - std::string ArchFileWindows::concatPath(const std::string& prefix, const std::string& suffix) @@ -95,9 +83,3 @@ ArchFileWindows::concatPath(const std::string& prefix, path += suffix; return path; } - -void -ArchFileWindows::setProfileDirectory(const String& s) -{ - DataDirectories::profile(s); -} diff --git a/src/lib/arch/win32/ArchFileWindows.h b/src/lib/arch/win32/ArchFileWindows.h index 8c95536e..d00b4aa1 100644 --- a/src/lib/arch/win32/ArchFileWindows.h +++ b/src/lib/arch/win32/ArchFileWindows.h @@ -27,10 +27,7 @@ class ArchFileWindows : public IArchFile { public: // IArchFile overrides virtual const char* getBasename(const char* pathname); - virtual std::string getUserDirectory(); virtual std::string getSystemDirectory(); - virtual std::string getProfileDirectory(); virtual std::string concatPath(const std::string& prefix, const std::string& suffix); - virtual void setProfileDirectory(const String& s); }; diff --git a/src/lib/barrier/App.cpp b/src/lib/barrier/App.cpp index db28f7cf..42df945e 100644 --- a/src/lib/barrier/App.cpp +++ b/src/lib/barrier/App.cpp @@ -21,9 +21,7 @@ #include "base/Log.h" #include "common/Version.h" #include "barrier/protocol_types.h" -#include "arch/Arch.h" #include "base/XBase.h" -#include "arch/XArch.h" #include "base/log_outputters.h" #include "barrier/XBarrier.h" #include "barrier/ArgsBase.h" @@ -32,9 +30,9 @@ #include "ipc/IpcMessage.h" #include "ipc/Ipc.h" #include "base/EventQueue.h" +#include "common/DataDirectories.h" #if SYSAPI_WIN32 -#include "arch/win32/ArchMiscWindows.h" #include "base/IEventQueue.h" #include "base/TMethodJob.h" #endif @@ -175,7 +173,7 @@ App::initApp(int argc, const char** argv) // parse command line parseArgs(argc, argv); - ARCH->setProfileDirectory(argsBase().m_profileDirectory); + DataDirectories::profile(argsBase().m_profileDirectory); // set log filter if (!CLOG->setFilter(argsBase().m_logFilter)) { diff --git a/src/lib/barrier/ServerApp.cpp b/src/lib/barrier/ServerApp.cpp index 112f2902..92f71fbb 100644 --- a/src/lib/barrier/ServerApp.cpp +++ b/src/lib/barrier/ServerApp.cpp @@ -39,6 +39,7 @@ #include "base/Log.h" #include "base/TMethodEventJob.h" #include "common/Version.h" +#include "common/DataDirectories.h" #if SYSAPI_WIN32 #include "arch/win32/ArchMiscWindows.h" @@ -181,7 +182,7 @@ ServerApp::loadConfig() // load the default configuration if no explicit file given else { // get the user's home directory - String path = ARCH->getUserDirectory(); + String path = DataDirectories::personal(); if (!path.empty()) { // complete path path = ARCH->concatPath(path, USR_CONFIG_NAME); diff --git a/src/lib/net/SecureListenSocket.cpp b/src/lib/net/SecureListenSocket.cpp index 58ffe096..7af905e6 100644 --- a/src/lib/net/SecureListenSocket.cpp +++ b/src/lib/net/SecureListenSocket.cpp @@ -22,6 +22,7 @@ #include "net/SocketMultiplexer.h" #include "net/TSocketMultiplexerMethodJob.h" #include "arch/XArch.h" +#include "common/DataDirectories.h" static const char s_certificateDir[] = { "SSL" }; static const char s_certificateFilename[] = { "Barrier.pem" }; @@ -54,7 +55,7 @@ SecureListenSocket::accept() } String certificateFilename = barrier::string::sprintf("%s/%s/%s", - ARCH->getProfileDirectory().c_str(), + DataDirectories::profile().c_str(), s_certificateDir, s_certificateFilename); diff --git a/src/lib/net/SecureSocket.cpp b/src/lib/net/SecureSocket.cpp index 1fefae0d..6670f5f2 100644 --- a/src/lib/net/SecureSocket.cpp +++ b/src/lib/net/SecureSocket.cpp @@ -23,6 +23,7 @@ #include "mt/Lock.h" #include "arch/XArch.h" #include "base/Log.h" +#include "common/DataDirectories.h" #include #include @@ -699,7 +700,7 @@ SecureSocket::verifyCertFingerprint() String trustedServersFilename; trustedServersFilename = barrier::string::sprintf( "%s/%s/%s", - ARCH->getProfileDirectory().c_str(), + DataDirectories::profile().c_str(), kFingerprintDirName, kFingerprintTrustedServersFilename); From 131a19d478a98f822ebd1d4b2f1b7b1efc0c69dd Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Fri, 30 Mar 2018 14:39:12 -0400 Subject: [PATCH 12/18] reimplement ArchFile*::getSystemDirectory() as DataDirectories::systemconfig(). windows will now use ProgramData by default rather than C:\Windows --- src/lib/arch/IArchFile.h | 6 ------ src/lib/arch/unix/ArchFileUnix.cpp | 6 ------ src/lib/arch/unix/ArchFileUnix.h | 1 - src/lib/arch/win32/ArchFileWindows.cpp | 14 -------------- src/lib/arch/win32/ArchFileWindows.h | 1 - src/lib/barrier/ServerApp.cpp | 4 ++-- src/lib/common/DataDirectories.h | 4 ++++ src/lib/common/DataDirectories_static.cpp | 7 +++++++ src/lib/common/unix/DataDirectories.cpp | 17 ++++++++++++----- src/lib/common/win32/DataDirectories.cpp | 21 ++++++++++++++++----- 10 files changed, 41 insertions(+), 40 deletions(-) create mode 100644 src/lib/common/DataDirectories_static.cpp diff --git a/src/lib/arch/IArchFile.h b/src/lib/arch/IArchFile.h index a974a1b1..88cb7792 100644 --- a/src/lib/arch/IArchFile.h +++ b/src/lib/arch/IArchFile.h @@ -38,12 +38,6 @@ public: */ virtual const char* getBasename(const char* pathname) = 0; - //! Get system directory - /*! - Returns the ussystem configuration file directory. - */ - virtual std::string getSystemDirectory() = 0; - //! Concatenate path components /*! Concatenate pathname components with a directory separator diff --git a/src/lib/arch/unix/ArchFileUnix.cpp b/src/lib/arch/unix/ArchFileUnix.cpp index 1b13a65e..206f1f18 100644 --- a/src/lib/arch/unix/ArchFileUnix.cpp +++ b/src/lib/arch/unix/ArchFileUnix.cpp @@ -45,12 +45,6 @@ ArchFileUnix::getBasename(const char* pathname) } } -std::string -ArchFileUnix::getSystemDirectory() -{ - return "/etc"; -} - std::string ArchFileUnix::concatPath(const std::string& prefix, const std::string& suffix) diff --git a/src/lib/arch/unix/ArchFileUnix.h b/src/lib/arch/unix/ArchFileUnix.h index 67b27b2a..b39f9827 100644 --- a/src/lib/arch/unix/ArchFileUnix.h +++ b/src/lib/arch/unix/ArchFileUnix.h @@ -27,7 +27,6 @@ class ArchFileUnix : public IArchFile { public: // IArchFile overrides virtual const char* getBasename(const char* pathname); - virtual std::string getSystemDirectory(); virtual std::string concatPath(const std::string& prefix, const std::string& suffix); }; diff --git a/src/lib/arch/win32/ArchFileWindows.cpp b/src/lib/arch/win32/ArchFileWindows.cpp index 022cfe9a..8f362551 100644 --- a/src/lib/arch/win32/ArchFileWindows.cpp +++ b/src/lib/arch/win32/ArchFileWindows.cpp @@ -54,20 +54,6 @@ ArchFileWindows::getBasename(const char* pathname) return basename; } -std::string -ArchFileWindows::getSystemDirectory() -{ - // get windows directory - char dir[MAX_PATH]; - if (GetWindowsDirectory(dir, sizeof(dir)) != 0) { - return dir; - } - else { - // can't get it. use C:\ as a default. - return "C:"; - } -} - std::string ArchFileWindows::concatPath(const std::string& prefix, const std::string& suffix) diff --git a/src/lib/arch/win32/ArchFileWindows.h b/src/lib/arch/win32/ArchFileWindows.h index d00b4aa1..2bfa316d 100644 --- a/src/lib/arch/win32/ArchFileWindows.h +++ b/src/lib/arch/win32/ArchFileWindows.h @@ -27,7 +27,6 @@ class ArchFileWindows : public IArchFile { public: // IArchFile overrides virtual const char* getBasename(const char* pathname); - virtual std::string getSystemDirectory(); virtual std::string concatPath(const std::string& prefix, const std::string& suffix); }; diff --git a/src/lib/barrier/ServerApp.cpp b/src/lib/barrier/ServerApp.cpp index 92f71fbb..240db855 100644 --- a/src/lib/barrier/ServerApp.cpp +++ b/src/lib/barrier/ServerApp.cpp @@ -144,7 +144,7 @@ ServerApp::help() << "If no configuration file pathname is provided then the first of the" << std::endl << "following to load successfully sets the configuration:" << std::endl << " $HOME/" << USR_CONFIG_NAME << std::endl - << " " << ARCH->concatPath(ARCH->getSystemDirectory(), SYS_CONFIG_NAME) << std::endl; + << " " << ARCH->concatPath(DataDirectories::systemconfig(), SYS_CONFIG_NAME) << std::endl; LOG((CLOG_PRINT "%s", buffer.str().c_str())); } @@ -195,7 +195,7 @@ ServerApp::loadConfig() } if (!loaded) { // try the system-wide config file - path = ARCH->getSystemDirectory(); + path = DataDirectories::systemconfig(); if (!path.empty()) { path = ARCH->concatPath(path, SYS_CONFIG_NAME); if (loadConfig(path)) { diff --git a/src/lib/common/DataDirectories.h b/src/lib/common/DataDirectories.h index fd024710..ddb903e4 100644 --- a/src/lib/common/DataDirectories.h +++ b/src/lib/common/DataDirectories.h @@ -14,6 +14,9 @@ public: static const std::string& global(); static const std::string& global(const std::string& path); + static const std::string& systemconfig(); + static const std::string& systemconfig(const std::string& path); + private: // static class DataDirectories() {} @@ -21,5 +24,6 @@ private: static std::string _personal; static std::string _profile; static std::string _global; + static std::string _systemconfig; }; diff --git a/src/lib/common/DataDirectories_static.cpp b/src/lib/common/DataDirectories_static.cpp new file mode 100644 index 00000000..51ed9c7b --- /dev/null +++ b/src/lib/common/DataDirectories_static.cpp @@ -0,0 +1,7 @@ +#include "DataDirectories.h" + +// static member +std::string DataDirectories::_personal; +std::string DataDirectories::_profile; +std::string DataDirectories::_global; +std::string DataDirectories::_systemconfig; diff --git a/src/lib/common/unix/DataDirectories.cpp b/src/lib/common/unix/DataDirectories.cpp index 9d0e66b7..0dc8234e 100644 --- a/src/lib/common/unix/DataDirectories.cpp +++ b/src/lib/common/unix/DataDirectories.cpp @@ -10,11 +10,6 @@ const std::string ProfileSubdir = "/.barrier"; const std::string ProfileSubdir = "/Library/Application Support/Barrier"; #endif -// static members -std::string DataDirectories::_personal; -std::string DataDirectories::_profile; -std::string DataDirectories::_global; - static std::string pw_dir(struct passwd* pwentp) { if (pwentp != NULL && pwentp->pw_dir != NULL) @@ -87,3 +82,15 @@ const std::string& DataDirectories::global(const std::string& path) return _global; } +const std::string& DataDirectories::systemconfig() +{ + if (_systemconfig.empty()) + _systemconfig = "/etc"; + return _systemconfig; +} + +const std::string& DataDirectories::systemconfig(const std::string& path) +{ + _systemconfig = path; + return _systemconfig; +} diff --git a/src/lib/common/win32/DataDirectories.cpp b/src/lib/common/win32/DataDirectories.cpp index 4c171296..5fb36a41 100644 --- a/src/lib/common/win32/DataDirectories.cpp +++ b/src/lib/common/win32/DataDirectories.cpp @@ -2,11 +2,6 @@ #include -// static member -std::string DataDirectories::_personal; -std::string DataDirectories::_profile; -std::string DataDirectories::_global; - std::string unicode_to_mb(const WCHAR* utfStr) { int utfLength = lstrlenW(utfStr); @@ -63,3 +58,19 @@ const std::string& DataDirectories::global(const std::string& path) _global = path; return _global; } + +const std::string& DataDirectories::systemconfig() +{ + // systemconfig() is a special case in that it will track the current value + // of global() unless and until it is explictly set otherwise + // previously it would default to the windows folder which was horrible! + if (_systemconfig.empty()) + return global(); + return _systemconfig; +} + +const std::string& DataDirectories::systemconfig(const std::string& path) +{ + _systemconfig = path; + return _systemconfig; +} From 4c04f39685be93b87dc4159e6430d07421bbec46 Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Sat, 31 Mar 2018 22:41:00 -0400 Subject: [PATCH 13/18] reimplement path operations basename() and concat() in Common. these were the last bits remaining in ArchFile* so it was removed --- src/lib/arch/Arch.h | 3 -- src/lib/arch/IArchFile.h | 51 ------------------ src/lib/arch/unix/ArchFileUnix.cpp | 60 ---------------------- src/lib/arch/unix/ArchFileUnix.h | 32 ------------ src/lib/arch/win32/ArchFileWindows.cpp | 71 -------------------------- src/lib/arch/win32/ArchFileWindows.h | 32 ------------ src/lib/barrier/ArgParser.cpp | 3 +- src/lib/barrier/ClientApp.cpp | 7 +-- src/lib/barrier/ServerApp.cpp | 11 ++-- src/lib/common/DataDirectories.h | 1 - src/lib/common/PathUtilities.cpp | 45 ++++++++++++++++ src/lib/common/PathUtilities.h | 14 +++++ 12 files changed, 69 insertions(+), 261 deletions(-) delete mode 100644 src/lib/arch/IArchFile.h delete mode 100644 src/lib/arch/unix/ArchFileUnix.cpp delete mode 100644 src/lib/arch/unix/ArchFileUnix.h delete mode 100644 src/lib/arch/win32/ArchFileWindows.cpp delete mode 100644 src/lib/arch/win32/ArchFileWindows.h create mode 100644 src/lib/common/PathUtilities.cpp create mode 100644 src/lib/common/PathUtilities.h diff --git a/src/lib/arch/Arch.h b/src/lib/arch/Arch.h index 42a73c2e..c062d6f3 100644 --- a/src/lib/arch/Arch.h +++ b/src/lib/arch/Arch.h @@ -40,7 +40,6 @@ #if SYSAPI_WIN32 # include "arch/win32/ArchConsoleWindows.h" # include "arch/win32/ArchDaemonWindows.h" -# include "arch/win32/ArchFileWindows.h" # include "arch/win32/ArchLogWindows.h" # include "arch/win32/ArchMiscWindows.h" # include "arch/win32/ArchMultithreadWindows.h" @@ -54,7 +53,6 @@ #elif SYSAPI_UNIX # include "arch/unix/ArchConsoleUnix.h" # include "arch/unix/ArchDaemonUnix.h" -# include "arch/unix/ArchFileUnix.h" # include "arch/unix/ArchLogUnix.h" # if HAVE_PTHREAD # include "arch/unix/ArchMultithreadPosix.h" @@ -86,7 +84,6 @@ typically at the beginning of \c main(). */ class Arch : public ARCH_CONSOLE, public ARCH_DAEMON, - public ARCH_FILE, public ARCH_LOG, public ARCH_MULTITHREAD, public ARCH_NETWORK, diff --git a/src/lib/arch/IArchFile.h b/src/lib/arch/IArchFile.h deleted file mode 100644 index 88cb7792..00000000 --- a/src/lib/arch/IArchFile.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2012-2016 Symless Ltd. - * Copyright (C) 2002 Chris Schoeneman - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#pragma once - -#include "common/IInterface.h" -#include "common/stdstring.h" -#include "base/String.h" - -//! Interface for architecture dependent file system operations -/*! -This interface defines the file system operations required by -barrier. Each architecture must implement this interface. -*/ -class IArchFile : public IInterface { -public: - //! @name manipulators - //@{ - - //! Extract base name - /*! - Find the base name in the given \c pathname. - */ - virtual const char* getBasename(const char* pathname) = 0; - - //! Concatenate path components - /*! - Concatenate pathname components with a directory separator - between them. This should not check if the resulting path - is longer than allowed by the system; we'll rely on the - system calls to tell us that. - */ - virtual std::string concatPath( - const std::string& prefix, - const std::string& suffix) = 0; -}; diff --git a/src/lib/arch/unix/ArchFileUnix.cpp b/src/lib/arch/unix/ArchFileUnix.cpp deleted file mode 100644 index 206f1f18..00000000 --- a/src/lib/arch/unix/ArchFileUnix.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2012-2016 Symless Ltd. - * Copyright (C) 2002 Chris Schoeneman - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#include "arch/unix/ArchFileUnix.h" -#include "common/DataDirectories.h" - -#include -#include -#include -#include -#include - -// -// ArchFileUnix -// - -const char* -ArchFileUnix::getBasename(const char* pathname) -{ - if (pathname == NULL) { - return NULL; - } - - const char* basename = strrchr(pathname, '/'); - if (basename != NULL) { - return basename + 1; - } - else { - return pathname; - } -} - -std::string -ArchFileUnix::concatPath(const std::string& prefix, - const std::string& suffix) -{ - std::string path; - path.reserve(prefix.size() + 1 + suffix.size()); - path += prefix; - if (path.size() == 0 || path[path.size() - 1] != '/') { - path += '/'; - } - path += suffix; - return path; -} diff --git a/src/lib/arch/unix/ArchFileUnix.h b/src/lib/arch/unix/ArchFileUnix.h deleted file mode 100644 index b39f9827..00000000 --- a/src/lib/arch/unix/ArchFileUnix.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2012-2016 Symless Ltd. - * Copyright (C) 2002 Chris Schoeneman - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#pragma once - -#include "arch/IArchFile.h" - -#define ARCH_FILE ArchFileUnix - -//! Unix implementation of IArchFile -class ArchFileUnix : public IArchFile { -public: - // IArchFile overrides - virtual const char* getBasename(const char* pathname); - virtual std::string concatPath(const std::string& prefix, - const std::string& suffix); -}; diff --git a/src/lib/arch/win32/ArchFileWindows.cpp b/src/lib/arch/win32/ArchFileWindows.cpp deleted file mode 100644 index 8f362551..00000000 --- a/src/lib/arch/win32/ArchFileWindows.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2012-2016 Symless Ltd. - * Copyright (C) 2002 Chris Schoeneman - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#include "arch/win32/ArchFileWindows.h" -#include "common/DataDirectories.h" - -#define WIN32_LEAN_AND_MEAN -#include -#include -#include -#include - -// -// ArchFileWindows -// - -const char* -ArchFileWindows::getBasename(const char* pathname) -{ - if (pathname == NULL) { - return NULL; - } - - // check for last slash - const char* basename = strrchr(pathname, '/'); - if (basename != NULL) { - ++basename; - } - else { - basename = pathname; - } - - // check for last backslash - const char* basename2 = strrchr(pathname, '\\'); - if (basename2 != NULL && basename2 > basename) { - basename = basename2 + 1; - } - - return basename; -} - -std::string -ArchFileWindows::concatPath(const std::string& prefix, - const std::string& suffix) -{ - std::string path; - path.reserve(prefix.size() + 1 + suffix.size()); - path += prefix; - if (path.size() == 0 || - (path[path.size() - 1] != '\\' && - path[path.size() - 1] != '/')) { - path += '\\'; - } - path += suffix; - return path; -} diff --git a/src/lib/arch/win32/ArchFileWindows.h b/src/lib/arch/win32/ArchFileWindows.h deleted file mode 100644 index 2bfa316d..00000000 --- a/src/lib/arch/win32/ArchFileWindows.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2012-2016 Symless Ltd. - * Copyright (C) 2002 Chris Schoeneman - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package 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 this program. If not, see . - */ - -#pragma once - -#include "arch/IArchFile.h" - -#define ARCH_FILE ArchFileWindows - -//! Win32 implementation of IArchFile -class ArchFileWindows : public IArchFile { -public: - // IArchFile overrides - virtual const char* getBasename(const char* pathname); - virtual std::string concatPath(const std::string& prefix, - const std::string& suffix); -}; diff --git a/src/lib/barrier/ArgParser.cpp b/src/lib/barrier/ArgParser.cpp index 1ce918c7..df478ab9 100644 --- a/src/lib/barrier/ArgParser.cpp +++ b/src/lib/barrier/ArgParser.cpp @@ -24,6 +24,7 @@ #include "barrier/ArgsBase.h" #include "base/Log.h" #include "base/String.h" +#include "common/PathUtilities.h" #ifdef WINAPI_MSWINDOWS #include @@ -455,7 +456,7 @@ void ArgParser::updateCommonArgs(const char* const* argv) { argsBase().m_name = ARCH->getHostName(); - argsBase().m_pname = ARCH->getBasename(argv[0]); + argsBase().m_pname = PathUtilities::basename(argv[0]).c_str(); } bool diff --git a/src/lib/barrier/ClientApp.cpp b/src/lib/barrier/ClientApp.cpp index 87686f29..23c0ea36 100644 --- a/src/lib/barrier/ClientApp.cpp +++ b/src/lib/barrier/ClientApp.cpp @@ -40,10 +40,7 @@ #include "base/TMethodJob.h" #include "base/Log.h" #include "common/Version.h" - -#if SYSAPI_WIN32 -#include "arch/win32/ArchMiscWindows.h" -#endif +#include "common/PathUtilities.h" #if WINAPI_MSWINDOWS #include "platform/MSWindowsScreen.h" @@ -519,7 +516,7 @@ ClientApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc { // general initialization m_serverAddress = new NetworkAddress; - args().m_pname = ARCH->getBasename(argv[0]); + args().m_pname = PathUtilities::basename(argv[0]).c_str(); // install caller's output filter if (outputter != NULL) { diff --git a/src/lib/barrier/ServerApp.cpp b/src/lib/barrier/ServerApp.cpp index 240db855..c6e31a5d 100644 --- a/src/lib/barrier/ServerApp.cpp +++ b/src/lib/barrier/ServerApp.cpp @@ -40,6 +40,7 @@ #include "base/TMethodEventJob.h" #include "common/Version.h" #include "common/DataDirectories.h" +#include "common/PathUtilities.h" #if SYSAPI_WIN32 #include "arch/win32/ArchMiscWindows.h" @@ -143,8 +144,8 @@ ServerApp::help() << std::endl << "If no configuration file pathname is provided then the first of the" << std::endl << "following to load successfully sets the configuration:" << std::endl - << " $HOME/" << USR_CONFIG_NAME << std::endl - << " " << ARCH->concatPath(DataDirectories::systemconfig(), SYS_CONFIG_NAME) << std::endl; + << " " << PathUtilities::concat(DataDirectories::personal(), SYS_CONFIG_NAME) << std::endl + << " " << PathUtilities::concat(DataDirectories::systemconfig(), SYS_CONFIG_NAME) << std::endl; LOG((CLOG_PRINT "%s", buffer.str().c_str())); } @@ -185,7 +186,7 @@ ServerApp::loadConfig() String path = DataDirectories::personal(); if (!path.empty()) { // complete path - path = ARCH->concatPath(path, USR_CONFIG_NAME); + path = PathUtilities::concat(path, USR_CONFIG_NAME); // now try loading the user's configuration if (loadConfig(path)) { @@ -197,7 +198,7 @@ ServerApp::loadConfig() // try the system-wide config file path = DataDirectories::systemconfig(); if (!path.empty()) { - path = ARCH->concatPath(path, SYS_CONFIG_NAME); + path = PathUtilities::concat(path, SYS_CONFIG_NAME); if (loadConfig(path)) { loaded = true; args().m_configFile = path; @@ -780,7 +781,7 @@ ServerApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc // general initialization m_barrierAddress = new NetworkAddress; args().m_config = new Config(m_events); - args().m_pname = ARCH->getBasename(argv[0]); + args().m_pname = PathUtilities::basename(argv[0]).c_str(); // install caller's output filter if (outputter != NULL) { diff --git a/src/lib/common/DataDirectories.h b/src/lib/common/DataDirectories.h index ddb903e4..edc604a6 100644 --- a/src/lib/common/DataDirectories.h +++ b/src/lib/common/DataDirectories.h @@ -26,4 +26,3 @@ private: static std::string _global; static std::string _systemconfig; }; - diff --git a/src/lib/common/PathUtilities.cpp b/src/lib/common/PathUtilities.cpp new file mode 100644 index 00000000..9859e048 --- /dev/null +++ b/src/lib/common/PathUtilities.cpp @@ -0,0 +1,45 @@ +#include "PathUtilities.h" + +// keep the default platform delimiter as the first in the list +#ifdef _WIN32 +static const char *Delimiters = "\\/"; +#else +static const char *Delimiters = "/"; +#endif + +static const char DefaultDelimiter = Delimiters[0]; + +std::string PathUtilities::basename(const std::string& path) +{ + return path.substr(path.find_last_of(Delimiters) + 1); +} + +std::string PathUtilities::concat(const std::string& left, const std::string& right) +{ + // although npos is usually (-1) we can't count on that so handle it explicitly + auto leftEnd = left.find_last_not_of(Delimiters); + if (leftEnd == std::string::npos) + leftEnd = 0; + else + ++leftEnd; + auto rightStart = right.find_first_not_of(Delimiters, 0); + if (rightStart == std::string::npos) { + // l/r empty + if (left.size() == 0 && right.size() == 0) + return ""; + // r useless, l okay + if (leftEnd > 0) + return left.substr(0, leftEnd); + // both useless but one has a delim + return std::string(1, DefaultDelimiter); + } + if (leftEnd == 0) { + // r okay and not prefixed with delims, l empty + if (left.size() == 0 && rightStart == 0) + return right.substr(rightStart); + // r okay and prefixed with delims OR l full of delims + return DefaultDelimiter + right.substr(rightStart); + } + // normal concatenation + return left.substr(0, leftEnd) + DefaultDelimiter + right.substr(rightStart); +} diff --git a/src/lib/common/PathUtilities.h b/src/lib/common/PathUtilities.h new file mode 100644 index 00000000..993229b6 --- /dev/null +++ b/src/lib/common/PathUtilities.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +class PathUtilities +{ +public: + static std::string basename(const std::string& path); + static std::string concat(const std::string& left, const std::string& right); + +private: + // static class + PathUtilities() {} +}; From 129e61a33a786a6267474f2c7752e652fc91da45 Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Sat, 31 Mar 2018 22:48:59 -0400 Subject: [PATCH 14/18] server should look for config in the profile() dir rather than the personal() dir. removed personal() since it is no longer used. --- src/lib/barrier/ServerApp.cpp | 5 ++--- src/lib/common/DataDirectories.h | 4 ---- src/lib/common/DataDirectories_static.cpp | 1 - src/lib/common/unix/DataDirectories.cpp | 14 +------------- src/lib/common/win32/DataDirectories.cpp | 12 ------------ 5 files changed, 3 insertions(+), 33 deletions(-) diff --git a/src/lib/barrier/ServerApp.cpp b/src/lib/barrier/ServerApp.cpp index c6e31a5d..fb8ae0e4 100644 --- a/src/lib/barrier/ServerApp.cpp +++ b/src/lib/barrier/ServerApp.cpp @@ -144,7 +144,7 @@ ServerApp::help() << std::endl << "If no configuration file pathname is provided then the first of the" << std::endl << "following to load successfully sets the configuration:" << std::endl - << " " << PathUtilities::concat(DataDirectories::personal(), SYS_CONFIG_NAME) << std::endl + << " " << PathUtilities::concat(DataDirectories::profile(), SYS_CONFIG_NAME) << std::endl << " " << PathUtilities::concat(DataDirectories::systemconfig(), SYS_CONFIG_NAME) << std::endl; LOG((CLOG_PRINT "%s", buffer.str().c_str())); @@ -182,8 +182,7 @@ ServerApp::loadConfig() // load the default configuration if no explicit file given else { - // get the user's home directory - String path = DataDirectories::personal(); + String path = DataDirectories::profile(); if (!path.empty()) { // complete path path = PathUtilities::concat(path, USR_CONFIG_NAME); diff --git a/src/lib/common/DataDirectories.h b/src/lib/common/DataDirectories.h index edc604a6..8ff5402c 100644 --- a/src/lib/common/DataDirectories.h +++ b/src/lib/common/DataDirectories.h @@ -5,9 +5,6 @@ class DataDirectories { public: - static const std::string& personal(); - static const std::string& personal(const std::string& path); - static const std::string& profile(); static const std::string& profile(const std::string& path); @@ -21,7 +18,6 @@ private: // static class DataDirectories() {} - static std::string _personal; static std::string _profile; static std::string _global; static std::string _systemconfig; diff --git a/src/lib/common/DataDirectories_static.cpp b/src/lib/common/DataDirectories_static.cpp index 51ed9c7b..8a2f53e7 100644 --- a/src/lib/common/DataDirectories_static.cpp +++ b/src/lib/common/DataDirectories_static.cpp @@ -1,7 +1,6 @@ #include "DataDirectories.h" // static member -std::string DataDirectories::_personal; std::string DataDirectories::_profile; std::string DataDirectories::_global; std::string DataDirectories::_systemconfig; diff --git a/src/lib/common/unix/DataDirectories.cpp b/src/lib/common/unix/DataDirectories.cpp index 0dc8234e..57a56a55 100644 --- a/src/lib/common/unix/DataDirectories.cpp +++ b/src/lib/common/unix/DataDirectories.cpp @@ -44,22 +44,10 @@ static std::string unix_home() #endif // HAVE_GETPWUID_R -const std::string& DataDirectories::personal() -{ - if (_personal.empty()) - _personal = unix_home(); - return _personal; -} -const std::string& DataDirectories::personal(const std::string& path) -{ - _personal = path; - return _personal; -} - const std::string& DataDirectories::profile() { if (_profile.empty()) - _profile = personal() + ProfileSubdir; + _profile = unix_home() + ProfileSubdir; return _profile; } const std::string& DataDirectories::profile(const std::string& path) diff --git a/src/lib/common/win32/DataDirectories.cpp b/src/lib/common/win32/DataDirectories.cpp index 5fb36a41..6d2c14e2 100644 --- a/src/lib/common/win32/DataDirectories.cpp +++ b/src/lib/common/win32/DataDirectories.cpp @@ -23,18 +23,6 @@ std::string known_folder_path(const KNOWNFOLDERID& id) return path; } -const std::string& DataDirectories::personal() -{ - if (_personal.empty()) - _personal = known_folder_path(FOLDERID_Documents); - return _personal; -} -const std::string& DataDirectories::personal(const std::string& path) -{ - _personal = path; - return _personal; -} - const std::string& DataDirectories::profile() { if (_profile.empty()) From e6d0f40a36bf6703d613b6d16ba06a6cb4bf57bd Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Sun, 1 Apr 2018 12:43:55 -0400 Subject: [PATCH 15/18] add legal header to new files --- src/lib/common/DataDirectories.h | 17 +++++++++++++++++ src/lib/common/DataDirectories_static.cpp | 17 +++++++++++++++++ src/lib/common/PathUtilities.cpp | 17 +++++++++++++++++ src/lib/common/PathUtilities.h | 17 +++++++++++++++++ src/lib/common/unix/DataDirectories.cpp | 17 +++++++++++++++++ src/lib/common/win32/DataDirectories.cpp | 17 +++++++++++++++++ 6 files changed, 102 insertions(+) diff --git a/src/lib/common/DataDirectories.h b/src/lib/common/DataDirectories.h index 8ff5402c..6b990c20 100644 --- a/src/lib/common/DataDirectories.h +++ b/src/lib/common/DataDirectories.h @@ -1,3 +1,20 @@ +/* +* barrier -- mouse and keyboard sharing utility +* Copyright (C) 2018 Debauchee Open Source Group +* +* This package is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* found in the file LICENSE that should have accompanied this file. +* +* This package 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 this program. If not, see . +*/ + #pragma once #include diff --git a/src/lib/common/DataDirectories_static.cpp b/src/lib/common/DataDirectories_static.cpp index 8a2f53e7..48dccb68 100644 --- a/src/lib/common/DataDirectories_static.cpp +++ b/src/lib/common/DataDirectories_static.cpp @@ -1,3 +1,20 @@ +/* +* barrier -- mouse and keyboard sharing utility +* Copyright (C) 2018 Debauchee Open Source Group +* +* This package is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* found in the file LICENSE that should have accompanied this file. +* +* This package 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 this program. If not, see . +*/ + #include "DataDirectories.h" // static member diff --git a/src/lib/common/PathUtilities.cpp b/src/lib/common/PathUtilities.cpp index 9859e048..5fe814ff 100644 --- a/src/lib/common/PathUtilities.cpp +++ b/src/lib/common/PathUtilities.cpp @@ -1,3 +1,20 @@ +/* +* barrier -- mouse and keyboard sharing utility +* Copyright (C) 2018 Debauchee Open Source Group +* +* This package is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* found in the file LICENSE that should have accompanied this file. +* +* This package 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 this program. If not, see . +*/ + #include "PathUtilities.h" // keep the default platform delimiter as the first in the list diff --git a/src/lib/common/PathUtilities.h b/src/lib/common/PathUtilities.h index 993229b6..70b85b4c 100644 --- a/src/lib/common/PathUtilities.h +++ b/src/lib/common/PathUtilities.h @@ -1,3 +1,20 @@ +/* +* barrier -- mouse and keyboard sharing utility +* Copyright (C) 2018 Debauchee Open Source Group +* +* This package is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* found in the file LICENSE that should have accompanied this file. +* +* This package 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 this program. If not, see . +*/ + #pragma once #include diff --git a/src/lib/common/unix/DataDirectories.cpp b/src/lib/common/unix/DataDirectories.cpp index 57a56a55..4f829a9d 100644 --- a/src/lib/common/unix/DataDirectories.cpp +++ b/src/lib/common/unix/DataDirectories.cpp @@ -1,3 +1,20 @@ +/* +* barrier -- mouse and keyboard sharing utility +* Copyright (C) 2018 Debauchee Open Source Group +* +* This package is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* found in the file LICENSE that should have accompanied this file. +* +* This package 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 this program. If not, see . +*/ + #include "../DataDirectories.h" #include // sysconf diff --git a/src/lib/common/win32/DataDirectories.cpp b/src/lib/common/win32/DataDirectories.cpp index 6d2c14e2..15cb64e1 100644 --- a/src/lib/common/win32/DataDirectories.cpp +++ b/src/lib/common/win32/DataDirectories.cpp @@ -1,3 +1,20 @@ +/* +* barrier -- mouse and keyboard sharing utility +* Copyright (C) 2018 Debauchee Open Source Group +* +* This package is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* found in the file LICENSE that should have accompanied this file. +* +* This package 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 this program. If not, see . +*/ + #include "../DataDirectories.h" #include From 767188799e4d45bf8830ba71d03f41d671403ede Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Sun, 1 Apr 2018 13:59:14 -0400 Subject: [PATCH 16/18] add desktop name to DEBUG output. when is desktop ever NOT "Default" ? --- src/lib/platform/MSWindowsWatchdog.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/platform/MSWindowsWatchdog.cpp b/src/lib/platform/MSWindowsWatchdog.cpp index 1e34bcbf..32bf709f 100644 --- a/src/lib/platform/MSWindowsWatchdog.cpp +++ b/src/lib/platform/MSWindowsWatchdog.cpp @@ -55,6 +55,7 @@ std::string activeDesktopName() name = buffer; CloseDesktop(desk); } + LOG((CLOG_DEBUG "found desktop name: %.64s", name.c_str())); return name; } From 42a8f69050f0de2b77eb4c28df6ac11e0164b0be Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Sun, 1 Apr 2018 14:47:34 -0400 Subject: [PATCH 17/18] better comments in PathUtilities.cpp --- src/lib/common/PathUtilities.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/lib/common/PathUtilities.cpp b/src/lib/common/PathUtilities.cpp index 5fe814ff..a2ab38a2 100644 --- a/src/lib/common/PathUtilities.cpp +++ b/src/lib/common/PathUtilities.cpp @@ -15,6 +15,19 @@ * along with this program. If not, see . */ +/* + +These functions cover the vast majority of cases for different paths across +windows and unixes. They are not, however, fullproof and probably don't cover +fringe cases very well. The library below might be used as an alternative if +these implementations prove to be insufficient. As the library's readme states +it is simply a temporary band-aid until std::filesystem is integrated (C++17 +has it in std::experimental) and this class should also be treated as such. + +https://github.com/wjakob/filesystem/ + +*/ + #include "PathUtilities.h" // keep the default platform delimiter as the first in the list @@ -41,22 +54,22 @@ std::string PathUtilities::concat(const std::string& left, const std::string& ri ++leftEnd; auto rightStart = right.find_first_not_of(Delimiters, 0); if (rightStart == std::string::npos) { - // l/r empty + // both left/right are empty if (left.size() == 0 && right.size() == 0) return ""; - // r useless, l okay + // right is full of delims, left is okay if (leftEnd > 0) return left.substr(0, leftEnd); - // both useless but one has a delim + // both left/right useless but at least one has delims return std::string(1, DefaultDelimiter); } if (leftEnd == 0) { - // r okay and not prefixed with delims, l empty + // right is okay and not prefixed with delims, left is empty if (left.size() == 0 && rightStart == 0) return right.substr(rightStart); - // r okay and prefixed with delims OR l full of delims + // (right is okay and prefixed with delims) OR left is full of delims return DefaultDelimiter + right.substr(rightStart); } - // normal concatenation + // concatenation using both left and right return left.substr(0, leftEnd) + DefaultDelimiter + right.substr(rightStart); } From 9e7792e2ae168fbb81c7bb3eb738fe2e3f31016c Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Sun, 1 Apr 2018 14:57:22 -0400 Subject: [PATCH 18/18] add comment re C++17 to MSWindowsUtil.cpp --- src/lib/platform/MSWindowsUtil.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib/platform/MSWindowsUtil.cpp b/src/lib/platform/MSWindowsUtil.cpp index 87b1ddc6..b657906e 100644 --- a/src/lib/platform/MSWindowsUtil.cpp +++ b/src/lib/platform/MSWindowsUtil.cpp @@ -63,6 +63,15 @@ MSWindowsUtil::getErrorString(HINSTANCE hinstance, DWORD error, DWORD id) } } +/* + +This is a quick and dirty iterative CreateDirectory() wrapper that does zero +error checking. A much better cross-platform option exists in C++17 via +std::filesystem. If/when the project is updated to use 17 this function should +absolutley be replaced! + +*/ + void MSWindowsUtil::createDirectory(const std::string& path, bool stripLast) {