zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

commit d86da8194c7afdad7e752b1c442d2b90e11e447c
parent fba06fcd00f9913bd15a04ba37daf01405f1ffd1
Author: Christopher A. Oliver <caowasteland@gmail.com>
Date:   Wed, 28 Oct 2015 21:55:32 -0400

TmpFileMgr removed.  Zynaddsubfx writes the port to stdout anyhow.

Never write a file in /tmp.  What if it's a link?

Diffstat:
Msrc/Misc/CMakeLists.txt | 1-
Msrc/Misc/MiddleWare.cpp | 13+------------
Dsrc/Misc/TmpFileMgr.cpp | 136-------------------------------------------------------------------------------
Dsrc/Misc/TmpFileMgr.h | 17-----------------
4 files changed, 1 insertion(+), 166 deletions(-)

diff --git a/src/Misc/CMakeLists.txt b/src/Misc/CMakeLists.txt @@ -14,7 +14,6 @@ set(zynaddsubfx_misc_SRCS Misc/MiddleWare.cpp Misc/PresetExtractor.cpp Misc/Allocator.cpp - Misc/TmpFileMgr.cpp ) diff --git a/src/Misc/MiddleWare.cpp b/src/Misc/MiddleWare.cpp @@ -19,7 +19,6 @@ #include <map> #include "Util.h" -#include "TmpFileMgr.h" #include "Master.h" #include "Part.h" #include "PresetExtractor.h" @@ -465,7 +464,7 @@ namespace Nio } /* Implementation */ -class MiddleWareImpl : TmpFileMgr +class MiddleWareImpl { MiddleWare *parent; @@ -858,14 +857,6 @@ MiddleWareImpl::MiddleWareImpl(MiddleWare *mw, SYNTH_T synth_, fprintf(stderr, "lo server could not be started :-/\n"); -#ifndef PLUGINVERSION - if(!isPlugin()) { - clean_up_tmp_nams(); - if(server) - create_tmp_file((unsigned)lo_server_get_port(server)); - } -#endif - //dummy callback for starters cb = [](void*, const char*){}; idle = 0; @@ -906,8 +897,6 @@ void DummyDataObj::reply(const char *msg) } MiddleWareImpl::~MiddleWareImpl(void) { - remove(get_tmp_nam().c_str()); - warnMemoryLeaks(); if(server) diff --git a/src/Misc/TmpFileMgr.cpp b/src/Misc/TmpFileMgr.cpp @@ -1,136 +0,0 @@ -#include <cstdlib> -#include <cstring> -#include <string> -#include <cstdio> -#include <fstream> -#include <unistd.h> -#include <dirent.h> - -#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. " - "You should probably remove it.", tmp_nam.c_str()); - exit(EXIT_FAILURE); - } - FILE* tmp_fp = fopen(tmp_nam.c_str(), "w"); - if(!tmp_fp) - fprintf(stderr, "Warning: could not create new file %s.\n", - tmp_nam.c_str()); - else { - fprintf(tmp_fp, "%u", server_port); - fclose(tmp_fp); - } -} - -void TmpFileMgr::clean_up_tmp_nams() const -{ - if (tmpdir == NULL) - set_tmpdir(); - DIR *dir; - struct dirent *entry; - if ((dir = opendir (tmpdir)) != nullptr) - { - while ((entry = readdir (dir)) != nullptr) - { - 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)); - std::string proc_file = "/proc/" + std::move(pid) + - "/comm"; - - std::ifstream ifs(proc_file); - bool remove = false; - - if(!ifs.good()) - { - fprintf(stderr, "Note: trying to remove %s - the " - "process does not exist anymore.\n", - name.c_str()); - remove = true; - } - else - { - std::string comm_name; - ifs >> comm_name; - if(comm_name == "zynaddsubfx") - fprintf(stderr, "Note: detected running " - "zynaddsubfx with PID %s.\n", - name.c_str() + strlen(tmp_nam_prefix)); - else { - fprintf(stderr, "Note: trying to remove %s - the " - "PID is owned by\n another " - "process: %s\n", - name.c_str(), comm_name.c_str()); - remove = true; - } - } - - - if(remove) - { - // make sure this file contains only one unsigned - unsigned udp_port; - std::ifstream ifs2(name); - if(!ifs2.good()) - fprintf(stderr, "Warning: could not open %s.\n", - name.c_str()); - else - { - ifs2 >> udp_port; - if(ifs.good()) - fprintf(stderr, "Warning: could not remove " - "%s, \n it has not been " - "written by zynaddsubfx\n", - name.c_str()); - else - { - if(std::remove(name.c_str()) != 0) - fprintf(stderr, "Warning: can not remove " - "%s.\n", name.c_str()); - } - } - } - - /* one might want to connect to zyn here, - but it is not necessary: - lo_address la = lo_address_new(nullptr, udp_port.c_str()); - if(lo_send(la, "/echo", nullptr) <= 0) - fputs("Note: found crashed file %s\n", stderr); - lo_address_free(la);*/ - } - } - closedir (dir); - } else { - fprintf(stderr, "Warning: can not read %s.\n", tmpdir); - } -} diff --git a/src/Misc/TmpFileMgr.h b/src/Misc/TmpFileMgr.h @@ -1,17 +0,0 @@ -#pragma once - -/** - This file provides routines for using zyn's tmp files. -*/ -class TmpFileMgr -{ -public: - //! returns file name to where UDP port is saved - std::string get_tmp_nam() const; - - //! creates a tmp file with given UDP port information - void create_tmp_file(unsigned server_port) const; - - //! cleans up as many tmp files as possible - void clean_up_tmp_nams() const; -};