2
0
mirror of https://github.com/meganz/MEGAcmd synced 2025-08-31 05:45:11 +00:00

Use PlatformDirectories to get paths to user data folders.

* src/configurationmanager.cpp:
 - Add new methods ConfigurationManager::{getDataDir, getStateDir}.
 - Replace ConfigurationManager::getConfFolderSubdir with
 getDataFolderSubdir, as it is only used for creating SDK directories,
 which should be in the data directory.
 - ConfigurationManager::loadConfigDir: Use PlatformDirectories to get
 config folder.
 - ConfigurationManager::{lockExecutor/unlockExecution}: Store the
 lock file in the state folder.

* src/megacmd.cpp:
 - executeServer: Use data dir and subfolders in it for SDK storage.

* src/megacmdlogger.cpp:
 - Store logs in the state directory.
This commit is contained in:
Vibhav Pant
2024-01-30 22:03:15 +05:30
parent 1055634075
commit 1ac5536f29
5 changed files with 124 additions and 158 deletions

View File

@@ -17,6 +17,8 @@
*/ */
#include "configurationmanager.h" #include "configurationmanager.h"
#include "mega/types.h"
#include "megacmdcommonutils.h"
#include "megacmdversion.h" #include "megacmdversion.h"
#include "megacmdutils.h" #include "megacmdutils.h"
#include "listeners.h" #include "listeners.h"
@@ -84,53 +86,16 @@ static const char* const persistentmcmdconfigurationkeys[] =
void ConfigurationManager::loadConfigDir() void ConfigurationManager::loadConfigDir()
{ {
auto dirs = PlatformDirectories::getPlatformSpecificDirectories();
std::lock_guard<std::recursive_mutex> g(settingsMutex); std::lock_guard<std::recursive_mutex> g(settingsMutex);
configFolder = dirs->configDirPath();
#ifdef _WIN32 if (configFolder.empty())
TCHAR szPath[MAX_PATH];
if (!SUCCEEDED(GetModuleFileName(NULL, szPath , MAX_PATH)))
{ {
LOG_fatal << "Couldnt get EXECUTABLE folder"; LOG_fatal << "Could not get config directory path";
return;
} }
else
{
if (SUCCEEDED(PathRemoveFileSpec(szPath)))
{
if (PathAppend(szPath,TEXT(".megaCmd")))
{
MegaApi::utf16ToUtf8(szPath, lstrlen(szPath), &configFolder);
}
}
}
#else
const char *homedir = NULL;
homedir = getenv("HOME"); auto fsAccess = ::mega::make_unique<MegaFileSystemAccess>();
if (!homedir)
{
struct passwd pd;
struct passwd* pwdptr = &pd;
struct passwd* tempPwdPtr;
char pwdbuffer[200];
int pwdlinelen = sizeof( pwdbuffer );
if (( getpwuid_r(22, pwdptr, pwdbuffer, pwdlinelen, &tempPwdPtr)) != 0)
{
LOG_fatal << "Couldnt get HOME folder";
return;
}
else
{
homedir = pwdptr->pw_dir;
}
}
stringstream sconfigDir;
sconfigDir << homedir << "/" << ".megaCmd";
configFolder = sconfigDir.str();
#endif
MegaFileSystemAccess *fsAccess = new MegaFileSystemAccess();
fsAccess->setdefaultfolderpermissions(0700); fsAccess->setdefaultfolderpermissions(0700);
LocalPath localConfigFolder = LocalPath::fromAbsolutePath(configFolder); LocalPath localConfigFolder = LocalPath::fromAbsolutePath(configFolder);
constexpr bool isHidden = true; constexpr bool isHidden = true;
@@ -139,45 +104,70 @@ void ConfigurationManager::loadConfigDir()
{ {
LOG_err << "Config folder not created"; LOG_err << "Config folder not created";
} }
delete fsAccess;
} }
std::string ConfigurationManager::getDataDir()
std::string ConfigurationManager::getConfFolderSubdir(const string &utf8Name)
{ {
if (!configFolder.size()) auto dirs = PlatformDirectories::getPlatformSpecificDirectories();
auto data_dir = dirs->dataDirPath();
auto fsAccess = ::mega::make_unique<MegaFileSystemAccess>();
fsAccess->setdefaultfolderpermissions(0700);
LocalPath local_data_dir = LocalPath::fromAbsolutePath(data_dir);
if (!is_file_exist(data_dir.c_str()) && !fsAccess->mkdirlocal(local_data_dir, false, false))
{ {
ConfigurationManager::loadConfigDir(); LOG_err << "Data directory not created";
} }
return data_dir;
}
std::string ConfigurationManager::getStateDir()
{
auto dirs = PlatformDirectories::getPlatformSpecificDirectories();
auto state_dir = dirs->stateDirPath();
auto fsAccess = ::mega::make_unique<MegaFileSystemAccess>();
fsAccess->setdefaultfolderpermissions(0700);
LocalPath local_data_dir = LocalPath::fromAbsolutePath(state_dir);
if (!is_file_exist(state_dir.c_str()) && !fsAccess->mkdirlocal(local_data_dir, false, false))
{
LOG_err << "State directory not created";
}
return state_dir;
}
std::string ConfigurationManager::getDataFolderSubdir(const string &utf8Name)
{
auto dirs = PlatformDirectories::getPlatformSpecificDirectories();
auto data_dir = dirs->dataDirPath();
MegaFileSystemAccess fsAccess; MegaFileSystemAccess fsAccess;
fsAccess.setdefaultfolderpermissions(0700); fsAccess.setdefaultfolderpermissions(0700);
LocalPath confSubdir = LocalPath::fromAbsolutePath(configFolder); LocalPath dataSubDir = LocalPath::fromAbsolutePath(data_dir);
confSubdir.appendWithSeparator(LocalPath::fromRelativePath(utf8Name), true); dataSubDir.appendWithSeparator(LocalPath::fromRelativePath(utf8Name), true);
constexpr bool isHidden = true; constexpr bool isHidden = true;
constexpr bool reportExisting = false; constexpr bool reportExisting = false;
if (!is_file_exist(confSubdir.toPath(false).c_str()) && !fsAccess.mkdirlocal(confSubdir, isHidden, reportExisting)) if (!is_file_exist(dataSubDir.toPath(false).c_str()) && !fsAccess.mkdirlocal(dataSubDir, isHidden, reportExisting))
{ {
LOG_err << "Config subfolder not created"; LOG_err << "State subfolder not created";
} }
return confSubdir.toPath(false); return dataSubDir.toPath(false);
} }
void ConfigurationManager::saveSession(const char*session) void ConfigurationManager::saveSession(const char*session)
{ {
std::lock_guard<std::recursive_mutex> g(settingsMutex); std::lock_guard<std::recursive_mutex> g(settingsMutex);
auto data_dir = getDataDir();
stringstream sessionfile; stringstream sessionfile;
if (!configFolder.size()) if (!data_dir.empty())
{ {
loadConfigDir(); sessionfile << data_dir << "/" << "session";
}
if (configFolder.size())
{
sessionfile << configFolder << "/" << "session";
LOG_debug << "Session file: " << sessionfile.str(); LOG_debug << "Session file: " << sessionfile.str();
ofstream fo(sessionfile.str().c_str(), ios::out); ofstream fo(sessionfile.str().c_str(), ios::out);
@@ -191,7 +181,7 @@ void ConfigurationManager::saveSession(const char*session)
} }
else else
{ {
LOG_err << "Couldnt access configuration folder "; LOG_err << "Couldnt access data directory";
} }
} }
@@ -199,11 +189,11 @@ void ConfigurationManager::saveProperty(const char *property, const char *value)
{ {
std::lock_guard<std::recursive_mutex> g(settingsMutex); std::lock_guard<std::recursive_mutex> g(settingsMutex);
if (!configFolder.size()) if (configFolder.empty())
{ {
loadConfigDir(); loadConfigDir();
} }
if (configFolder.size()) if (!configFolder.empty())
{ {
bool found = false; bool found = false;
stringstream configFile; stringstream configFile;
@@ -325,11 +315,11 @@ void ConfigurationManager::saveSyncs(map<string, sync_struct *> *syncsmap)
std::lock_guard<std::recursive_mutex> g(settingsMutex); std::lock_guard<std::recursive_mutex> g(settingsMutex);
stringstream syncsfile; stringstream syncsfile;
if (!configFolder.size()) if (configFolder.empty())
{ {
loadConfigDir(); loadConfigDir();
} }
if (configFolder.size()) if (!configFolder.empty())
{ {
syncsfile << configFolder << "/" << "syncs"; syncsfile << configFolder << "/" << "syncs";
LOG_debug << "Syncs file: " << syncsfile.str(); LOG_debug << "Syncs file: " << syncsfile.str();
@@ -374,11 +364,11 @@ void ConfigurationManager::saveBackups(map<string, backup_struct *> *backupsmap)
std::lock_guard<std::recursive_mutex> g(settingsMutex); std::lock_guard<std::recursive_mutex> g(settingsMutex);
stringstream backupsfile; stringstream backupsfile;
if (!configFolder.size()) if (configFolder.empty())
{ {
loadConfigDir(); loadConfigDir();
} }
if (configFolder.size()) if (!configFolder.empty())
{ {
backupsfile << configFolder << "/" << "backups"; backupsfile << configFolder << "/" << "backups";
LOG_debug << "Backups file: " << backupsfile.str(); LOG_debug << "Backups file: " << backupsfile.str();
@@ -445,11 +435,11 @@ void ConfigurationManager::saveExcludedNames()
{ {
std::lock_guard<std::recursive_mutex> g(settingsMutex); std::lock_guard<std::recursive_mutex> g(settingsMutex);
stringstream excludedNamesFile; stringstream excludedNamesFile;
if (!configFolder.size()) if (configFolder.empty())
{ {
loadConfigDir(); loadConfigDir();
} }
if (configFolder.size()) if (!configFolder.empty())
{ {
excludedNamesFile << configFolder << "/" << "excluded"; excludedNamesFile << configFolder << "/" << "excluded";
LOG_debug << "Exclusion file: " << excludedNamesFile.str(); LOG_debug << "Exclusion file: " << excludedNamesFile.str();
@@ -475,11 +465,11 @@ void ConfigurationManager::loadExcludedNames()
{ {
std::lock_guard<std::recursive_mutex> g(settingsMutex); std::lock_guard<std::recursive_mutex> g(settingsMutex);
stringstream excludedNamesFile; stringstream excludedNamesFile;
if (!configFolder.size()) if (configFolder.empty())
{ {
loadConfigDir(); loadConfigDir();
} }
if (configFolder.size()) if (!configFolder.empty())
{ {
excludedNamesFile << configFolder << "/" << "excluded"; excludedNamesFile << configFolder << "/" << "excluded";
LOG_debug << "Excluded file: " << excludedNamesFile.str(); LOG_debug << "Excluded file: " << excludedNamesFile.str();
@@ -543,11 +533,11 @@ void ConfigurationManager::loadsyncs()
{ {
std::lock_guard<std::recursive_mutex> g(settingsMutex); std::lock_guard<std::recursive_mutex> g(settingsMutex);
stringstream syncsfile; stringstream syncsfile;
if (!configFolder.size()) if (configFolder.empty())
{ {
loadConfigDir(); loadConfigDir();
} }
if (configFolder.size()) if (!configFolder.empty())
{ {
syncsfile << configFolder << "/" << "syncs"; syncsfile << configFolder << "/" << "syncs";
LOG_debug << "Syncs file: " << syncsfile.str(); LOG_debug << "Syncs file: " << syncsfile.str();
@@ -620,11 +610,11 @@ void ConfigurationManager::loadbackups()
{ {
std::lock_guard<std::recursive_mutex> g(settingsMutex); std::lock_guard<std::recursive_mutex> g(settingsMutex);
stringstream backupsfile; stringstream backupsfile;
if (!configFolder.size()) if (configFolder.empty())
{ {
loadConfigDir(); loadConfigDir();
} }
if (configFolder.size()) if (!configFolder.empty())
{ {
backupsfile << configFolder << "/" << "backups"; backupsfile << configFolder << "/" << "backups";
LOG_debug << "Backups file: " << backupsfile.str(); LOG_debug << "Backups file: " << backupsfile.str();
@@ -694,15 +684,14 @@ void ConfigurationManager::loadConfiguration(bool debug)
{ {
std::lock_guard<std::recursive_mutex> g(settingsMutex); std::lock_guard<std::recursive_mutex> g(settingsMutex);
if (!configFolder.size()) // SESSION
stringstream sessionfile;
auto data_dir = getDataDir();
if (!data_dir.empty())
{ {
loadConfigDir(); sessionfile << data_dir << "/"
} << "session";
if (configFolder.size())
{
//SESSION
stringstream sessionfile;
sessionfile << configFolder << "/" << "session";
if (debug) if (debug)
{ {
@@ -724,8 +713,7 @@ void ConfigurationManager::loadConfiguration(bool debug)
fi.close(); fi.close();
} }
// Check if version has been updated.
//Check if version has been updated.
stringstream versionionfile; stringstream versionionfile;
versionionfile << configFolder << "/" << VERSION_FILE_NAME; versionionfile << configFolder << "/" << VERSION_FILE_NAME;
@@ -759,25 +747,22 @@ void ConfigurationManager::loadConfiguration(bool debug)
{ {
if (debug) if (debug)
{ {
std::cout << "Couldnt access configuration folder " << std::endl; std::cout << "Couldnt access data folder " << std::endl;
} }
} }
} }
bool ConfigurationManager::lockExecution() bool ConfigurationManager::lockExecution()
{ {
if (!configFolder.size()) auto state_dir = getStateDir();
{ if (!state_dir.empty())
loadConfigDir();
}
if (configFolder.size())
{ {
stringstream lockfile; stringstream lockfile;
#ifdef _WIN32 #ifdef _WIN32
lockfile << "\\\\?\\"; lockfile << "\\\\?\\";
lockfile << configFolder << "\\" << "lockMCMD"; lockfile << state_dir << "\\" << "lockMCMD";
#else #else
lockfile << configFolder << "/" << "lockMCMD"; lockfile << state_dir << "/" << "lockMCMD";
#endif #endif
LOG_err << "Lock file: " << lockfile.str(); LOG_err << "Lock file: " << lockfile.str();
@@ -821,21 +806,18 @@ bool ConfigurationManager::lockExecution()
} }
else else
{ {
LOG_err << "Couldnt access configuration folder "; LOG_err << "Couldnt access state folder ";
} }
return false; return false;
} }
void ConfigurationManager::unlockExecution() void ConfigurationManager::unlockExecution()
{ {
if (!configFolder.size()) auto state_dir = getStateDir();
{ if (!state_dir.empty())
loadConfigDir();
}
if (configFolder.size())
{ {
stringstream lockfile; stringstream lockfile;
lockfile << configFolder << "/" << "lockMCMD"; lockfile << state_dir << "/" << "lockMCMD";
#if !defined(_WIN32) && defined(LOCK_EX) && defined(LOCK_NB) #if !defined(_WIN32) && defined(LOCK_EX) && defined(LOCK_NB)
flock(fd, LOCK_UN | LOCK_NB); flock(fd, LOCK_UN | LOCK_NB);
@@ -845,7 +827,7 @@ void ConfigurationManager::unlockExecution()
} }
else else
{ {
LOG_err << "Couldnt access configuration folder "; LOG_err << "Couldnt access state folder ";
} }
} }
string ConfigurationManager::getConfigurationSValue(string propertyName) string ConfigurationManager::getConfigurationSValue(string propertyName)

View File

@@ -40,7 +40,6 @@ private:
static void loadConfigDir(); static void loadConfigDir();
static void removeSyncConfig(sync_struct *syncToRemove); static void removeSyncConfig(sync_struct *syncToRemove);
public: public:
static std::map<std::string, sync_struct *> oldConfiguredSyncs; //This will refer to old syncs from now on static std::map<std::string, sync_struct *> oldConfiguredSyncs; //This will refer to old syncs from now on
@@ -238,8 +237,10 @@ public:
static std::string getConfigFolder(); static std::string getConfigFolder();
// creates a subfolder within config dir and returns it (utf8) // creates a subfolder within the state dir and returns it (utf8)
static std::string getConfFolderSubdir(const std::string &utf8Name); static std::string getDataFolderSubdir(const std::string &utf8Name);
static std::string getStateDir();
static std::string getDataDir();
static bool getHasBeenUpdated(); static bool getHasBeenUpdated();

View File

@@ -5008,7 +5008,7 @@ int executeServer(int argc, char* argv[],
LOG_debug << "MEGAcmd version: " << MEGACMD_MAJOR_VERSION << "." << MEGACMD_MINOR_VERSION << "." << MEGACMD_MICRO_VERSION << "." << MEGACMD_BUILD_ID << ": code " << MEGACMD_CODE_VERSION; LOG_debug << "MEGAcmd version: " << MEGACMD_MAJOR_VERSION << "." << MEGACMD_MINOR_VERSION << "." << MEGACMD_MICRO_VERSION << "." << MEGACMD_BUILD_ID << ": code " << MEGACMD_CODE_VERSION;
api = new MegaApi("BdARkQSQ", (MegaGfxProcessor*)NULL, ConfigurationManager::getConfigFolder().c_str(), userAgent); api = new MegaApi("BdARkQSQ", (MegaGfxProcessor*)NULL, ConfigurationManager::getDataDir().c_str(), userAgent);
if (!debug_api_url.empty()) if (!debug_api_url.empty())
{ {
@@ -5020,7 +5020,7 @@ int executeServer(int argc, char* argv[],
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
MegaApi *apiFolder = new MegaApi("BdARkQSQ", (MegaGfxProcessor*)NULL, MegaApi *apiFolder = new MegaApi("BdARkQSQ", (MegaGfxProcessor*)NULL,
ConfigurationManager::getConfFolderSubdir(std::string("apiFolder_").append(std::to_string(i))).c_str() ConfigurationManager::getDataFolderSubdir(std::string("apiFolder_").append(std::to_string(i))).c_str()
, userAgent); , userAgent);
apiFolder->setLanguage(localecode.c_str()); apiFolder->setLanguage(localecode.c_str());
apiFolders.push(apiFolder); apiFolders.push(apiFolder);

View File

@@ -17,6 +17,7 @@
*/ */
#include "megacmdlogger.h" #include "megacmdlogger.h"
#include "megacmdcommonutils.h"
#include <map> #include <map>
@@ -307,13 +308,17 @@ OUTFSTREAMTYPE streamForDefaultFile()
} }
} }
#else #else
const char* home = getenv("HOME"); auto dirs = PlatformDirectories::getPlatformSpecificDirectories();
if (home) path = dirs->stateDirPath();
auto fsAccess = ::mega::make_unique<MegaFileSystemAccess>();
fsAccess->setdefaultfolderpermissions(0700);
LocalPath local_data_dir = LocalPath::fromAbsolutePath(path);
if (!fsAccess->mkdirlocal(local_data_dir, false, false))
{ {
path.append(home); LOG_err << "State directory not created";
path.append("/.megaCmd/");
path.append("/megacmdserver.log");
} }
path.append("/megacmdserver.log");
#endif #endif
return OUTFSTREAMTYPE(path); return OUTFSTREAMTYPE(path);

View File

@@ -20,6 +20,7 @@
*/ */
#include "megacmdshellcommunications.h" #include "megacmdshellcommunications.h"
#include "megacmdcommonutils.h"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@@ -105,61 +106,38 @@ void MegaCmdShellCommunications::closeSocket(SOCKET socket){
string createAndRetrieveConfigFolder() string createAndRetrieveConfigFolder()
{ {
string configFolder; auto dirs = PlatformDirectories::getPlatformSpecificDirectories();
#ifdef _WIN32 #ifdef _WIN32
return dirs->configDirPath();
TCHAR szPath[MAX_PATH];
if (!SUCCEEDED(GetModuleFileName(NULL, szPath , MAX_PATH)))
{
cerr << "Couldnt get EXECUTABLE folder" << endl;
}
else
{
if (SUCCEEDED(PathRemoveFileSpec(szPath)))
{
if (PathAppend(szPath,TEXT(".megaCmd")))
{
utf16ToUtf8(szPath, lstrlen(szPath), &configFolder);
}
}
}
//TODO: create folder (not required currently) //TODO: create folder (not required currently)
#else #else
const char *homedir = NULL; auto dir = dirs->configDirPath();
struct stat st = {};
homedir = getenv("HOME"); if (stat(dir.c_str(), &st) == -1)
if (!homedir)
{ {
struct passwd pd; mkdir(dir.c_str(), 0700);
struct passwd* pwdptr = &pd;
struct passwd* tempPwdPtr;
char pwdbuffer[200];
int pwdlinelen = sizeof( pwdbuffer );
if (( getpwuid_r(22, pwdptr, pwdbuffer, pwdlinelen, &tempPwdPtr)) != 0)
{
cerr << "Couldnt get HOME folder" << endl;
return "/tmp";
}
else
{
homedir = pwdptr->pw_dir;
}
} }
stringstream sconfigDir; return dir;
sconfigDir << homedir << "/" << ".megaCmd";
configFolder = sconfigDir.str();
struct stat st;
if (stat(configFolder.c_str(), &st) == -1) {
mkdir(configFolder.c_str(), 0700);
}
#endif #endif
}
return configFolder; string createAndRetrieveStateFolder()
{
auto dirs = PlatformDirectories::getPlatformSpecificDirectories();
#ifdef _WIN32
return dirs->stateDirPath();
//TODO: create folder (not required currently)
#else
auto dir = dirs->stateDirPath();
struct stat st = {};
if (stat(dir.c_str(), &st) == -1)
{
mkdir(dir.c_str(), 0700);
}
return dir;
#endif
} }
@@ -332,7 +310,7 @@ SOCKET MegaCmdShellCommunications::createSocket(int number, bool initializeserve
setsid(); //create new session so as not to receive parent's Ctrl+C setsid(); //create new session so as not to receive parent's Ctrl+C
// Give an indication of where the logs will be find: // Give an indication of where the logs will be find:
string pathtolog = createAndRetrieveConfigFolder()+"/megacmdserver.log"; string pathtolog = createAndRetrieveStateFolder()+"/megacmdserver.log";
CERR << "[Initiating MEGAcmd server in background. Log: " << pathtolog << "]" << endl; CERR << "[Initiating MEGAcmd server in background. Log: " << pathtolog << "]" << endl;
freopen(std::string(pathtolog).append(".out").c_str(),"w",stdout); freopen(std::string(pathtolog).append(".out").c_str(),"w",stdout);