diff --git a/daemon/src/audio/audiorecord.cpp b/daemon/src/audio/audiorecord.cpp index 0b26d29a21c52aac65adfa3cb8f6f351655b3cee..b68d65aa427294ec1e387b6f723c4c065caeaa33 100644 --- a/daemon/src/audio/audiorecord.cpp +++ b/daemon/src/audio/audiorecord.cpp @@ -135,7 +135,7 @@ void AudioRecord::setRecordingOption(FILE_TYPE type, int sndSmplRate, const std: // use HOME directory if path is empty, or if path does not exist if (path.empty() or not fileutils::check_dir(path.c_str())) { - filePath = HOMEDIR; + filePath = fileutils::get_home_dir(); } else { filePath = path; } diff --git a/daemon/src/audio/audiortp/audio_zrtp_session.cpp b/daemon/src/audio/audiortp/audio_zrtp_session.cpp index 80bd1058f4630d04c3865b7f94628df7f423bd47..2c244038ab575b56f9fe96160297a1d1c18df3ae 100644 --- a/daemon/src/audio/audiortp/audio_zrtp_session.cpp +++ b/daemon/src/audio/audiortp/audio_zrtp_session.cpp @@ -68,7 +68,7 @@ void AudioZrtpSession::initializeZid() if (not cache_home.empty()) { zidCompleteFilename = cache_home + DIR_SEPARATOR_STR + zidFilename_; } else { - zidCompleteFilename = std::string(HOMEDIR) + DIR_SEPARATOR_STR + + zidCompleteFilename = fileutils::get_home_dir() + DIR_SEPARATOR_STR + ".cache" + DIR_SEPARATOR_STR + PACKAGE + DIR_SEPARATOR_STR + zidFilename_; } diff --git a/daemon/src/audio/codecs/audiocodecfactory.cpp b/daemon/src/audio/codecs/audiocodecfactory.cpp index f38c3487b793cd964a2db6fbe107281ad558b9d4..3389c65be6453840b03284c8185d3f2d2dbdf7be 100644 --- a/daemon/src/audio/codecs/audiocodecfactory.cpp +++ b/daemon/src/audio/codecs/audiocodecfactory.cpp @@ -159,7 +159,7 @@ AudioCodecFactory::scanCodecDirectory() std::vector<sfl::AudioCodec*> codecs; std::vector<std::string> dirToScan; - dirToScan.push_back(std::string(HOMEDIR) + DIR_SEPARATOR_STR "." PACKAGE "/"); + dirToScan.push_back(fileutils::get_home_dir() + DIR_SEPARATOR_STR "." PACKAGE "/"); dirToScan.push_back(CODECS_DIR "/"); const char *envDir = getenv("CODECS_PATH"); diff --git a/daemon/src/fileutils.cpp b/daemon/src/fileutils.cpp index e7bcb3ccff38a40583a419d3d317d2d9fb87214b..3263e89557fc097e1d69225f3f8799294b3216b8 100644 --- a/daemon/src/fileutils.cpp +++ b/daemon/src/fileutils.cpp @@ -91,25 +91,6 @@ const char *get_data_dir() } namespace { -std::string -get_home_dir() -{ - // 1) try getting user's home directory from the environment - const std::string home(HOMEDIR); - if (not home.empty()) - return home; - - // 2) try getting it from getpwuid_r (i.e. /etc/passwd) - const long max = sysconf(_SC_GETPW_R_SIZE_MAX); - if (max != -1) { - char buf[max]; - struct passwd pwbuf, *pw; - if (getpwuid_r(getuid(), &pwbuf, buf, sizeof(buf), &pw) == 0 and pw != NULL) - return std::string(pw->pw_dir); - } else { - return ""; - } -} /* Lock a file region */ int @@ -191,4 +172,23 @@ FileHandle::~FileHandle() } } +std::string +get_home_dir() +{ + // 1) try getting user's home directory from the environment + const std::string home(PROTECTED_GETENV("HOME")); + if (not home.empty()) + return home; + + // 2) try getting it from getpwuid_r (i.e. /etc/passwd) + const long max = sysconf(_SC_GETPW_R_SIZE_MAX); + if (max != -1) { + char buf[max]; + struct passwd pwbuf, *pw; + if (getpwuid_r(getuid(), &pwbuf, buf, sizeof(buf), &pw) == 0 and pw != NULL) + return pw->pw_dir; + } else { + return ""; + } +} } diff --git a/daemon/src/fileutils.h b/daemon/src/fileutils.h index e4fcf9031a5ee79508944d67dd55eeebc036128d..272404df8e1091c19caaf3fe543c41a9a8185598 100644 --- a/daemon/src/fileutils.h +++ b/daemon/src/fileutils.h @@ -34,7 +34,6 @@ #define PROTECTED_GETENV(str) ({char *envvar_ = getenv((str)); \ envvar_ ? envvar_ : "";}) -#define HOMEDIR (PROTECTED_GETENV("HOME")) #define XDG_DATA_HOME (PROTECTED_GETENV("XDG_DATA_HOME")) #define XDG_CONFIG_HOME (PROTECTED_GETENV("XDG_CONFIG_HOME")) #define XDG_CACHE_HOME (PROTECTED_GETENV("XDG_CACHE_HOME")) @@ -46,6 +45,7 @@ #define DIR_SEPARATOR_CH = '/' // Directory separator string namespace fileutils { + std::string get_home_dir(); bool check_dir(const char *path); void set_program_dir(char *program_path); const char *get_program_dir(); diff --git a/daemon/src/history/history.cpp b/daemon/src/history/history.cpp index 51c7db32824632f2f1bb13e9462fc803203a958b..9952a171ac0716922ad50eca18dc9a9ca8dee114 100644 --- a/daemon/src/history/history.cpp +++ b/daemon/src/history/history.cpp @@ -97,11 +97,12 @@ void History::addEntry(const HistoryItem &item, int oldest) void History::ensurePath() { if (path_.empty()) { - string xdg_data = string(HOMEDIR) + DIR_SEPARATOR_STR + ".local/share/sflphone"; + const string xdg_data = fileutils::get_home_dir() + DIR_SEPARATOR_STR + + ".local/share/sflphone"; string userdata; // If the environment variable is set (not null and not empty), we'll use it to save the history - // Else we 'll the standard one, ie: XDG_DATA_HOME = $HOMEDIR/.local/share/sflphone + // Else we 'll the standard one, ie: XDG_DATA_HOME = $HOME/.local/share/sflphone string xdg_env(XDG_DATA_HOME); (not xdg_env.empty()) ? userdata = xdg_env : userdata = xdg_data; diff --git a/daemon/src/managerimpl.cpp b/daemon/src/managerimpl.cpp index ffab7a88a9099341f1f7bd607f9e0a2eed1028fc..f91a363761c00acbbfa1810b80455de353131987 100644 --- a/daemon/src/managerimpl.cpp +++ b/daemon/src/managerimpl.cpp @@ -1857,7 +1857,7 @@ ManagerImpl::getTelephoneFile() */ std::string ManagerImpl::createConfigFile() const { - std::string configdir = std::string(HOMEDIR) + DIR_SEPARATOR_STR + + std::string configdir = fileutils::get_home_dir() + DIR_SEPARATOR_STR + ".config" + DIR_SEPARATOR_STR + PACKAGE; const std::string xdg_env(XDG_CONFIG_HOME); diff --git a/daemon/src/preferences.cpp b/daemon/src/preferences.cpp index 756a4d6b8da3cadd7866ad3fdd2c0ab2b03e65ce..d6c35e295cb350f34ce525ddfc9802f8c8e851cc 100644 --- a/daemon/src/preferences.cpp +++ b/daemon/src/preferences.cpp @@ -429,10 +429,9 @@ void AudioPreference::unserialize(const Conf::YamlNode &map) map.getValue(AUDIO_API_KEY, &audioApi_); std::string tmpRecordPath; map.getValue(RECORDPATH_KEY, &tmpRecordPath); - if (not setRecordPath(tmpRecordPath)) { - DEBUG("Setting record path to %s", HOMEDIR); - setRecordPath(HOMEDIR); - } + if (not setRecordPath(tmpRecordPath)) + setRecordPath(fileutils::get_home_dir()); + map.getValue(ALWAYS_RECORDING_KEY, &alwaysRecording_); map.getValue(VOLUMEMIC_KEY, &volumemic_); map.getValue(VOLUMESPKR_KEY, &volumespkr_);