commit fba06fcd00f9913bd15a04ba37daf01405f1ffd1
parent 31970325bb90ca414a7e2ec0da2d11692157e3cd
Author: Christopher A. Oliver <caowasteland@gmail.com>
Date: Wed, 28 Oct 2015 21:05:39 -0400
Kluge causing TmpFileMgr to respect TMPDIR
Diffstat:
2 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/src/Misc/TmpFileMgr.cpp b/src/Misc/TmpFileMgr.cpp
@@ -9,13 +9,32 @@
#include "Util.h"
#include "TmpFileMgr.h"
+// This could be done during instantiation, but Middleware was to be untouched.
+static char *tmpdir;
+static char *tmp_nam_prefix;
+
+#define TMPNAM "/zynaddsubfx_"
+
+static void set_tmpdir()
+{
+ if ((tmpdir=getenv("TMPDIR")) == NULL)
+ tmpdir=(char *)"/tmp";
+ tmp_nam_prefix=(char *)malloc(sizeof(TMPNAM) + strlen(tmpdir));
+ strcpy(tmp_nam_prefix, tmpdir);
+ strcat(tmp_nam_prefix, TMPNAM);
+}
+
std::string TmpFileMgr::get_tmp_nam() const
{
+ if (tmpdir == NULL)
+ set_tmpdir();
return tmp_nam_prefix + to_s(getpid());
}
void TmpFileMgr::create_tmp_file(unsigned server_port) const
{
+ if (tmpdir == NULL)
+ set_tmpdir();
std::string tmp_nam = get_tmp_nam();
if(0 == access(tmp_nam.c_str(), F_OK)) {
fprintf(stderr, "Error: Cannot overwrite file %s. "
@@ -26,20 +45,23 @@ void TmpFileMgr::create_tmp_file(unsigned server_port) const
if(!tmp_fp)
fprintf(stderr, "Warning: could not create new file %s.\n",
tmp_nam.c_str());
- else
+ else {
fprintf(tmp_fp, "%u", server_port);
- fclose(tmp_fp);
+ fclose(tmp_fp);
+ }
}
void TmpFileMgr::clean_up_tmp_nams() const
{
+ if (tmpdir == NULL)
+ set_tmpdir();
DIR *dir;
struct dirent *entry;
- if ((dir = opendir ("/tmp/")) != nullptr)
+ if ((dir = opendir (tmpdir)) != nullptr)
{
while ((entry = readdir (dir)) != nullptr)
{
- std::string name = std::string("/tmp/") + entry->d_name;
+ std::string name = std::string(tmpdir) + "/" + entry->d_name;
if(!name.compare(0, strlen(tmp_nam_prefix),tmp_nam_prefix))
{
std::string pid = name.substr(strlen(tmp_nam_prefix));
@@ -109,6 +131,6 @@ void TmpFileMgr::clean_up_tmp_nams() const
}
closedir (dir);
} else {
- fputs("Warning: can not read /tmp.\n", stderr);
+ fprintf(stderr, "Warning: can not read %s.\n", tmpdir);
}
}
diff --git a/src/Misc/TmpFileMgr.h b/src/Misc/TmpFileMgr.h
@@ -5,8 +5,6 @@
*/
class TmpFileMgr
{
- static constexpr const char* tmp_nam_prefix = "/tmp/zynaddsubfx_";
-
public:
//! returns file name to where UDP port is saved
std::string get_tmp_nam() const;