2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2012-2016 Symless Ltd.
|
2012-09-04 02:09:56 +00:00
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:55:14 +00:00
|
|
|
#include "arch/unix/ArchFileUnix.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
|
2024-11-01 12:59:17 +00:00
|
|
|
#include "common/constants.h"
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <cstring>
|
2024-10-14 15:36:02 +00:00
|
|
|
#include <filesystem>
|
2012-06-10 16:50:54 +00:00
|
|
|
#include <pwd.h>
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <stdio.h>
|
2012-06-10 16:50:54 +00:00
|
|
|
#include <sys/types.h>
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <unistd.h>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// ArchFileUnix
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
ArchFileUnix::ArchFileUnix()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
ArchFileUnix::~ArchFileUnix()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
const char *ArchFileUnix::getBasename(const char *pathname)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (pathname == NULL) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *basename = strrchr(pathname, '/');
|
|
|
|
|
if (basename != NULL) {
|
|
|
|
|
return basename + 1;
|
|
|
|
|
} else {
|
|
|
|
|
return pathname;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
std::string ArchFileUnix::getUserDirectory()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
char *buffer = NULL;
|
|
|
|
|
std::string dir;
|
2012-06-10 16:50:54 +00:00
|
|
|
#if HAVE_GETPWUID_R
|
2024-07-02 19:07:06 +00:00
|
|
|
struct passwd pwent;
|
|
|
|
|
struct passwd *pwentp{};
|
2012-06-10 16:50:54 +00:00
|
|
|
#if defined(_SC_GETPW_R_SIZE_MAX)
|
2024-07-02 19:07:06 +00:00
|
|
|
long size = sysconf(_SC_GETPW_R_SIZE_MAX);
|
|
|
|
|
if (size == -1) {
|
|
|
|
|
size = BUFSIZ;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
#else
|
2024-07-02 19:07:06 +00:00
|
|
|
long size = BUFSIZ;
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
2024-07-02 19:07:06 +00:00
|
|
|
buffer = new char[size];
|
|
|
|
|
getpwuid_r(getuid(), &pwent, buffer, size, &pwentp);
|
2012-06-10 16:50:54 +00:00
|
|
|
#else
|
2024-07-02 19:07:06 +00:00
|
|
|
struct passwd *pwentp = getpwuid(getuid());
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
2024-07-02 19:07:06 +00:00
|
|
|
if (pwentp != NULL && pwentp->pw_dir != NULL) {
|
|
|
|
|
dir = pwentp->pw_dir;
|
|
|
|
|
}
|
|
|
|
|
delete[] buffer;
|
|
|
|
|
return dir;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
std::string ArchFileUnix::getSystemDirectory()
|
|
|
|
|
{
|
|
|
|
|
return "/etc";
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
std::string ArchFileUnix::getInstalledDirectory()
|
|
|
|
|
{
|
2014-10-27 14:11:43 +00:00
|
|
|
#if WINAPI_XWINDOWS
|
2024-07-02 19:07:06 +00:00
|
|
|
return "/usr/bin";
|
2014-10-27 14:11:43 +00:00
|
|
|
#else
|
2024-11-01 12:59:17 +00:00
|
|
|
std::string rtn = "/Applications/";
|
|
|
|
|
rtn.append(kAppName).append(".app/Contents/MacOS");
|
|
|
|
|
return rtn;
|
2014-10-27 14:11:43 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
std::string ArchFileUnix::getLogDirectory()
|
|
|
|
|
{
|
|
|
|
|
return "/var/log";
|
|
|
|
|
}
|
2014-10-27 14:11:43 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
std::string ArchFileUnix::getPluginDirectory()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (!m_pluginDirectory.empty()) {
|
|
|
|
|
return m_pluginDirectory;
|
|
|
|
|
}
|
2015-03-04 11:15:07 +00:00
|
|
|
|
2015-02-09 15:37:47 +00:00
|
|
|
#if WINAPI_XWINDOWS
|
2024-07-02 19:07:06 +00:00
|
|
|
return getProfileDirectory().append("/plugins");
|
2015-02-09 15:37:47 +00:00
|
|
|
#else
|
2024-07-02 19:07:06 +00:00
|
|
|
return getProfileDirectory().append("/Plugins");
|
2015-02-11 17:28:56 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
std::string ArchFileUnix::getProfileDirectory()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (!m_profileDirectory.empty()) {
|
2024-10-14 15:36:02 +00:00
|
|
|
return m_profileDirectory;
|
2024-07-02 19:07:06 +00:00
|
|
|
} else {
|
2024-10-14 15:36:02 +00:00
|
|
|
const std::filesystem::path homeDir = getUserDirectory();
|
2015-02-11 17:28:56 +00:00
|
|
|
#if WINAPI_XWINDOWS
|
2024-10-14 15:36:02 +00:00
|
|
|
const auto xdgDir = std::getenv("XDG_CONFIG_HOME");
|
|
|
|
|
if (xdgDir != nullptr) {
|
2024-11-01 12:59:17 +00:00
|
|
|
return std::filesystem::path(xdgDir) / kAppName;
|
2024-10-14 15:36:02 +00:00
|
|
|
} else {
|
2024-11-01 12:59:17 +00:00
|
|
|
return homeDir / ".config" / kAppName;
|
2024-10-14 15:36:02 +00:00
|
|
|
}
|
2015-02-11 17:28:56 +00:00
|
|
|
#else
|
2024-11-01 12:59:17 +00:00
|
|
|
return homeDir / "Library" / kAppName;
|
2015-02-09 15:37:47 +00:00
|
|
|
#endif
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2015-02-09 15:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
std::string ArchFileUnix::concatPath(const std::string &prefix, const std::string &suffix)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
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;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
2015-03-04 11:15:07 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ArchFileUnix::setProfileDirectory(const String &s)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
m_profileDirectory = s;
|
2015-03-04 11:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ArchFileUnix::setPluginDirectory(const String &s)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
m_pluginDirectory = s;
|
2015-03-04 11:15:07 +00:00
|
|
|
}
|