diff --git a/slsReceiverSoftware/src/slsReceiverImplementation.cpp b/slsReceiverSoftware/src/slsReceiverImplementation.cpp index 0ee2fcec0..80e177e2c 100755 --- a/slsReceiverSoftware/src/slsReceiverImplementation.cpp +++ b/slsReceiverSoftware/src/slsReceiverImplementation.cpp @@ -13,6 +13,7 @@ #include "GeneralData.h" #include "Listener.h" #include "ZmqSocket.h" //just for the zmq port define +#include "file_utils.h" #include //eperm #include //system @@ -555,12 +556,8 @@ void slsReceiverImplementation::setFilePath(const char c[]) { FILE_LOG(logDEBUG3) << __SHORT_AT__ << " called"; if (strlen(c)) { - // check if filepath exists - struct stat st; - if (stat(c, &st) == 0) - strcpy(filePath, c); - else - FILE_LOG(logERROR) << "FilePath does not exist: " << c; + mkdir_p(c); //throws if it can't create + strcpy(filePath, c); } FILE_LOG(logINFO) << "File path: " << filePath; } diff --git a/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp b/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp index 64356337e..d62af13f6 100755 --- a/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp +++ b/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp @@ -808,6 +808,8 @@ int slsReceiverTCPIPInterface::set_file_dir(Interface &socket) { if (strlen(fPath) != 0) { FILE_LOG(logDEBUG1) << "Setting file path: " << fPath; + if(fPath[0] != '/') + throw RuntimeError("Receiver path needs to be absolute path"); impl()->setFilePath(fPath); } std::string s = impl()->getFilePath(); diff --git a/slsSupportLib/include/file_utils.h b/slsSupportLib/include/file_utils.h index 9be439e77..0b9ceadbb 100755 --- a/slsSupportLib/include/file_utils.h +++ b/slsSupportLib/include/file_utils.h @@ -52,5 +52,5 @@ int writeDataFile(std::string fname,int nch, short int *data); - - +// mkdir -p path implemented by recursive calls +void mkdir_p(const std::string& path, std::string dir=""); diff --git a/slsSupportLib/src/file_utils.cpp b/slsSupportLib/src/file_utils.cpp index 71897e5c0..863bda544 100755 --- a/slsSupportLib/src/file_utils.cpp +++ b/slsSupportLib/src/file_utils.cpp @@ -1,9 +1,12 @@ #include "file_utils.h" #include "logger.h" +#include "sls_detector_exceptions.h" #include #include - +#include +#include +#include int readDataFile(std::ifstream &infile, short int *data, int nch, int offset) { int ichan, iline=0; @@ -77,5 +80,25 @@ int writeDataFile(std::string fname,int nch, short int *data) { +void mkdir_p(const std::string& path, std::string dir) { + if (path.length() == 0) + return; + + size_t i = 0; + for (; i < path.length(); i++) { + dir += path[i]; + if (path[i] == '/') + break; + } + if(mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0){ + if (errno != EEXIST) + throw sls::RuntimeError("Could not create: " + dir); + } + + if (i + 1 < path.length()) + mkdir_p(path.substr(i + 1), dir); +} + +