From 347badf770666fe5e2df91727c119eaed3de9f25 Mon Sep 17 00:00:00 2001 From: Patrick Brosi Date: Tue, 14 May 2019 00:11:15 +0200 Subject: [PATCH] better tmp dir selection --- src/pfaedle/Def.h | 5 ++- src/pfaedle/PfaedleMain.cpp | 46 ++++++++++++++++------------ src/pfaedle/config/MotConfigReader.h | 29 ++---------------- src/pfaedle/gtfs/ShapeContainer.h | 1 - src/pfaedle/gtfs/ShapeContainer.tpp | 2 ++ src/pfaedle/osm/OsmIdSet.cpp | 5 ++- src/pfaedle/osm/OsmIdSet.h | 1 - src/util/Misc.h | 46 ++++++++++++++++++++++++++++ 8 files changed, 83 insertions(+), 52 deletions(-) diff --git a/src/pfaedle/Def.h b/src/pfaedle/Def.h index cde8959..7b8e083 100644 --- a/src/pfaedle/Def.h +++ b/src/pfaedle/Def.h @@ -6,7 +6,9 @@ #define PFAEDLE_DEF_H_ #include +#include #include "util/log/Log.h" +#include "util/Misc.h" #include "util/geo/Geo.h" #include "util/geo/PolyLine.h" @@ -34,6 +36,7 @@ namespace pfaedle { // _____________________________________________________________________________ inline std::string getTmpFName(std::string dir, std::string postf) { if (postf.size()) postf = "-" + postf; + if (!dir.size()) dir = util::getTmpDir(); if (dir.size() && dir.back() != '/') dir = dir + "/"; std::string f = dir + ".pfaedle-tmp" + postf; @@ -55,6 +58,6 @@ inline std::string getTmpFName(std::string dir, std::string postf) { return f; } -} +} // namespace pfaedle #endif // PFAEDLE_DEF_H_ diff --git a/src/pfaedle/PfaedleMain.cpp b/src/pfaedle/PfaedleMain.cpp index 2b37aea..6a81f88 100644 --- a/src/pfaedle/PfaedleMain.cpp +++ b/src/pfaedle/PfaedleMain.cpp @@ -30,6 +30,29 @@ #include "util/geo/output/GeoJsonOutput.h" #include "util/json/Writer.h" #include "util/log/Log.h" +#include "util/Misc.h" + +#ifndef HOME_VAR +#define HOME_VAR "HOME" +#endif +#ifndef XDG_DATA_HOME_SUFFIX +#define XDG_DATA_HOME_SUFFIX "/.local/share" +#endif +#ifndef XDG_CONFIG_HOME_SUFFIX +#define XDG_CONFIG_HOME_SUFFIX "/.config" +#endif +#ifndef XDG_CACHE_HOME_SUFFIX +#define XDG_CACHE_HOME_SUFFIX "/.cache" +#endif +#ifndef XDG_DATA_DIRS_DEFAULT +#define XDG_DATA_DIRS_DEFAULT "/usr/local/share" +#endif +#ifndef XDG_CONFIG_DIRS_DEFAULT +#define XDG_CONFIG_DIRS_DEFAULT "/etc" +#endif +#ifndef CFG_FILE_NAME +#define CFG_FILE_NAME "pfaedle.cfg" +#endif using pfaedle::router::MOTs; using pfaedle::osm::BBoxIdx; @@ -316,25 +339,6 @@ std::vector getCfgPaths(const Config& cfg) { if (cfg.configPaths.size()) return cfg.configPaths; std::vector ret; - // parse implicit paths - const char* homedir = 0; - char* buf = 0; - - if ((homedir = getenv("HOME")) == 0) { - homedir = ""; - struct passwd pwd; - struct passwd* result; - size_t bufsize; - bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); - if (bufsize == static_cast(-1)) bufsize = 0x4000; - buf = static_cast(malloc(bufsize)); - if (buf != 0) { - getpwuid_r(getuid(), &pwd, buf, bufsize, &result); - if (result != NULL) homedir = result->pw_dir; - } - } - - if (buf) free(buf); // install prefix global configuration path, if available { @@ -352,7 +356,7 @@ std::vector getCfgPaths(const Config& cfg) { // local user configuration path, if available { - auto path = std::string(homedir) + XDG_CONFIG_HOME_SUFFIX + "/" + + auto path = util::getHomeDir() + XDG_CONFIG_HOME_SUFFIX + "/" + "pfaedle" + "/" + CFG_FILE_NAME; std::ifstream is(path); @@ -363,6 +367,8 @@ std::vector getCfgPaths(const Config& cfg) { } } + // free this here, as we use homedir in the block above + // CWD { char cwd[PATH_MAX]; diff --git a/src/pfaedle/config/MotConfigReader.h b/src/pfaedle/config/MotConfigReader.h index 2e7fee2..51716ff 100644 --- a/src/pfaedle/config/MotConfigReader.h +++ b/src/pfaedle/config/MotConfigReader.h @@ -5,36 +5,13 @@ #ifndef PFAEDLE_CONFIG_MOTCONFIGREADER_H_ #define PFAEDLE_CONFIG_MOTCONFIGREADER_H_ -#include "pfaedle/_config.h" - -#ifndef HOME_VAR -#define HOME_VAR "HOME" -#endif -#ifndef XDG_DATA_HOME_SUFFIX -#define XDG_DATA_HOME_SUFFIX "/.local/share" -#endif -#ifndef XDG_CONFIG_HOME_SUFFIX -#define XDG_CONFIG_HOME_SUFFIX "/.config" -#endif -#ifndef XDG_CACHE_HOME_SUFFIX -#define XDG_CACHE_HOME_SUFFIX "/.cache" -#endif -#ifndef XDG_DATA_DIRS_DEFAULT -#define XDG_DATA_DIRS_DEFAULT "/usr/local/share" -#endif -#ifndef XDG_CONFIG_DIRS_DEFAULT -#define XDG_CONFIG_DIRS_DEFAULT "/etc" -#endif -#ifndef CFG_FILE_NAME -#define CFG_FILE_NAME "pfaedle.cfg" -#endif - -#include -#include #include #include +#include +#include #include "ad/cppgtfs/gtfs/Route.h" #include "configparser/ConfigFileParser.h" +#include "pfaedle/_config.h" #include "pfaedle/config/MotConfig.h" #include "pfaedle/osm/OsmBuilder.h" diff --git a/src/pfaedle/gtfs/ShapeContainer.h b/src/pfaedle/gtfs/ShapeContainer.h index 0e25cb1..1128031 100644 --- a/src/pfaedle/gtfs/ShapeContainer.h +++ b/src/pfaedle/gtfs/ShapeContainer.h @@ -11,7 +11,6 @@ #include #include #include -#include #include "ad/cppgtfs/gtfs/Shape.h" #include "ad/cppgtfs/gtfs/flat/Shape.h" #include "pfaedle/Def.h" diff --git a/src/pfaedle/gtfs/ShapeContainer.tpp b/src/pfaedle/gtfs/ShapeContainer.tpp index e4e5395..b55d646 100644 --- a/src/pfaedle/gtfs/ShapeContainer.tpp +++ b/src/pfaedle/gtfs/ShapeContainer.tpp @@ -2,6 +2,8 @@ // Chair of Algorithms and Data Structures. // Authors: Patrick Brosi +#include + // ____________________________________________________________________________ template ShapeContainer::ShapeContainer() { diff --git a/src/pfaedle/osm/OsmIdSet.cpp b/src/pfaedle/osm/OsmIdSet.cpp index 63a12fc..8b23bf9 100644 --- a/src/pfaedle/osm/OsmIdSet.cpp +++ b/src/pfaedle/osm/OsmIdSet.cpp @@ -271,15 +271,14 @@ uint32_t OsmIdSet::hash(uint32_t in, int i) const { // _____________________________________________________________________________ int OsmIdSet::openTmpFile() const { - const std::string& fname = getTmpFName(_tmpPath, ""); + const std::string& fname = getTmpFName("", ""); int file = open(fname.c_str(), O_RDWR | O_CREAT, 0666); // immediately unlink unlink(fname.c_str()); if (file < 0) { - std::cerr << "Could not open temporary file " << fname - << std::endl; + std::cerr << "Could not open temporary file " << fname << std::endl; exit(1); } diff --git a/src/pfaedle/osm/OsmIdSet.h b/src/pfaedle/osm/OsmIdSet.h index 8eb703e..d19f9a1 100644 --- a/src/pfaedle/osm/OsmIdSet.h +++ b/src/pfaedle/osm/OsmIdSet.h @@ -34,7 +34,6 @@ static const size_t OBUFFER_S = 8 * 1024 * 1024; class OsmIdSet { public: OsmIdSet(); - OsmIdSet(const std::string& tmpPath) : _tmpPath(tmpPath) {}; ~OsmIdSet(); // Add an OSM id diff --git a/src/util/Misc.h b/src/util/Misc.h index 17b267e..6cd667d 100644 --- a/src/util/Misc.h +++ b/src/util/Misc.h @@ -6,8 +6,12 @@ #define UTIL_MISC_H_ #include +#include #include #include +#include +#include +#include #define UNUSED(expr) do { (void)(expr); } while (0) #define TIME() std::chrono::high_resolution_clock::now() @@ -85,6 +89,48 @@ inline double atof(const char* p, uint8_t mn) { // _____________________________________________________________________________ inline double atof(const char* p) { return atof(p, 38); } +// _____________________________________________________________________________ +inline std::string getHomeDir() { + // parse implicit paths + const char* homedir = 0; + char* buf = 0; + + if ((homedir = getenv("HOME")) == 0) { + homedir = ""; + struct passwd pwd; + struct passwd* result; + size_t bufsize; + bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); + if (bufsize == static_cast(-1)) bufsize = 0x4000; + buf = static_cast(malloc(bufsize)); + if (buf != 0) { + getpwuid_r(getuid(), &pwd, buf, bufsize, &result); + if (result != NULL) homedir = result->pw_dir; + } + } + + std::string ret(homedir); + if (buf) free(buf); + + return ret; +} + +// _____________________________________________________________________________ +inline std::string getTmpDir() { + // first, check if an env variable is set + const char* tmpdir = getenv("TMPDIR"); + if (std::strlen(tmpdir)) return std::string(tmpdir); + + // second, check if /tmp is writable + if (access("/tmp/", W_OK) == 0) return "/tmp"; + + // third, check if the cwd is writable + if (access(".", W_OK) == 0) return "."; + + // lastly, return the users home directory as a fallback + return getHomeDir(); +} + } // namespace util #endif // UTIL_MISC_H_