better tmp dir selection

This commit is contained in:
Patrick Brosi 2019-05-14 00:11:15 +02:00
parent 9d19e344d3
commit 347badf770
8 changed files with 83 additions and 52 deletions

View file

@ -6,7 +6,9 @@
#define PFAEDLE_DEF_H_ #define PFAEDLE_DEF_H_
#include <unistd.h> #include <unistd.h>
#include <string>
#include "util/log/Log.h" #include "util/log/Log.h"
#include "util/Misc.h"
#include "util/geo/Geo.h" #include "util/geo/Geo.h"
#include "util/geo/PolyLine.h" #include "util/geo/PolyLine.h"
@ -34,6 +36,7 @@ namespace pfaedle {
// _____________________________________________________________________________ // _____________________________________________________________________________
inline std::string getTmpFName(std::string dir, std::string postf) { inline std::string getTmpFName(std::string dir, std::string postf) {
if (postf.size()) postf = "-" + postf; if (postf.size()) postf = "-" + postf;
if (!dir.size()) dir = util::getTmpDir();
if (dir.size() && dir.back() != '/') dir = dir + "/"; if (dir.size() && dir.back() != '/') dir = dir + "/";
std::string f = dir + ".pfaedle-tmp" + postf; std::string f = dir + ".pfaedle-tmp" + postf;
@ -55,6 +58,6 @@ inline std::string getTmpFName(std::string dir, std::string postf) {
return f; return f;
} }
} } // namespace pfaedle
#endif // PFAEDLE_DEF_H_ #endif // PFAEDLE_DEF_H_

View file

@ -30,6 +30,29 @@
#include "util/geo/output/GeoJsonOutput.h" #include "util/geo/output/GeoJsonOutput.h"
#include "util/json/Writer.h" #include "util/json/Writer.h"
#include "util/log/Log.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::router::MOTs;
using pfaedle::osm::BBoxIdx; using pfaedle::osm::BBoxIdx;
@ -316,25 +339,6 @@ std::vector<std::string> getCfgPaths(const Config& cfg) {
if (cfg.configPaths.size()) return cfg.configPaths; if (cfg.configPaths.size()) return cfg.configPaths;
std::vector<std::string> ret; std::vector<std::string> 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<size_t>(-1)) bufsize = 0x4000;
buf = static_cast<char*>(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 // install prefix global configuration path, if available
{ {
@ -352,7 +356,7 @@ std::vector<std::string> getCfgPaths(const Config& cfg) {
// local user configuration path, if available // 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; "pfaedle" + "/" + CFG_FILE_NAME;
std::ifstream is(path); std::ifstream is(path);
@ -363,6 +367,8 @@ std::vector<std::string> getCfgPaths(const Config& cfg) {
} }
} }
// free this here, as we use homedir in the block above
// CWD // CWD
{ {
char cwd[PATH_MAX]; char cwd[PATH_MAX];

View file

@ -5,36 +5,13 @@
#ifndef PFAEDLE_CONFIG_MOTCONFIGREADER_H_ #ifndef PFAEDLE_CONFIG_MOTCONFIGREADER_H_
#define 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 <unordered_map>
#include <vector>
#include <set> #include <set>
#include <string> #include <string>
#include <unordered_map>
#include <vector>
#include "ad/cppgtfs/gtfs/Route.h" #include "ad/cppgtfs/gtfs/Route.h"
#include "configparser/ConfigFileParser.h" #include "configparser/ConfigFileParser.h"
#include "pfaedle/_config.h"
#include "pfaedle/config/MotConfig.h" #include "pfaedle/config/MotConfig.h"
#include "pfaedle/osm/OsmBuilder.h" #include "pfaedle/osm/OsmBuilder.h"

View file

@ -11,7 +11,6 @@
#include <set> #include <set>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <string>
#include "ad/cppgtfs/gtfs/Shape.h" #include "ad/cppgtfs/gtfs/Shape.h"
#include "ad/cppgtfs/gtfs/flat/Shape.h" #include "ad/cppgtfs/gtfs/flat/Shape.h"
#include "pfaedle/Def.h" #include "pfaedle/Def.h"

View file

@ -2,6 +2,8 @@
// Chair of Algorithms and Data Structures. // Chair of Algorithms and Data Structures.
// Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de> // Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
#include <string>
// ____________________________________________________________________________ // ____________________________________________________________________________
template <typename T> template <typename T>
ShapeContainer<T>::ShapeContainer() { ShapeContainer<T>::ShapeContainer() {

View file

@ -271,15 +271,14 @@ uint32_t OsmIdSet::hash(uint32_t in, int i) const {
// _____________________________________________________________________________ // _____________________________________________________________________________
int OsmIdSet::openTmpFile() 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); int file = open(fname.c_str(), O_RDWR | O_CREAT, 0666);
// immediately unlink // immediately unlink
unlink(fname.c_str()); unlink(fname.c_str());
if (file < 0) { if (file < 0) {
std::cerr << "Could not open temporary file " << fname std::cerr << "Could not open temporary file " << fname << std::endl;
<< std::endl;
exit(1); exit(1);
} }

View file

@ -34,7 +34,6 @@ static const size_t OBUFFER_S = 8 * 1024 * 1024;
class OsmIdSet { class OsmIdSet {
public: public:
OsmIdSet(); OsmIdSet();
OsmIdSet(const std::string& tmpPath) : _tmpPath(tmpPath) {};
~OsmIdSet(); ~OsmIdSet();
// Add an OSM id // Add an OSM id

View file

@ -6,8 +6,12 @@
#define UTIL_MISC_H_ #define UTIL_MISC_H_
#include <cmath> #include <cmath>
#include <cstring>
#include <chrono> #include <chrono>
#include <sstream> #include <sstream>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#define UNUSED(expr) do { (void)(expr); } while (0) #define UNUSED(expr) do { (void)(expr); } while (0)
#define TIME() std::chrono::high_resolution_clock::now() #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 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<size_t>(-1)) bufsize = 0x4000;
buf = static_cast<char*>(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 } // namespace util
#endif // UTIL_MISC_H_ #endif // UTIL_MISC_H_