X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Futil.cpp;h=c16ba1c6c8d541164fc5ff7fd540aadb74d1e7fa;hb=be10d9f19349b8d4915f125aa29db98d0048df3f;hp=ed7b42e61ff87a407c981ff791cd92d05459136a;hpb=87325777087f80af2065a09977ae6abf39bc50d1;p=cassiopeia.git diff --git a/src/util.cpp b/src/util.cpp index ed7b42e..c16ba1c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -30,6 +30,7 @@ std::string writeBackFile( const std::string& serial, const std::string& cert, c errno = 0; std::string filename = keydir; + if( 0 != mkdir( filename.c_str(), 0755 ) ) { if( EEXIST != errno ) { return ""; @@ -39,6 +40,7 @@ std::string writeBackFile( const std::string& serial, const std::string& cert, c } filename += "/crt"; + if( 0 != mkdir( filename.c_str(), 0755 ) ) { if( EEXIST != errno ) { return ""; @@ -46,6 +48,7 @@ std::string writeBackFile( const std::string& serial, const std::string& cert, c //! \FIXME: Check this is a directory } + std::string first; if( serial.length() < 3 ) { @@ -55,6 +58,7 @@ std::string writeBackFile( const std::string& serial, const std::string& cert, c } filename += "/" + first; + if( 0 != mkdir( filename.c_str(), 0755 ) ) { if( EEXIST != errno ) { return ""; @@ -177,3 +181,21 @@ std::pair parseYearInterval( std::time_t t, const std::string& dat return std::pair( false, 0 ); } } + +std::shared_ptr openLogfile( const std::string name) { + struct stat buffer; + std::string tname = name; + int ctr = 2; + while(stat (tname.c_str(), &buffer) == 0) { + tname = name + "_" + std::to_string(ctr++); + } + auto res = std::shared_ptr(new std::ofstream( tname ), + [](std::ofstream *p){ + p->close(); + delete p; + }); + if(! res->good() ){ + throw std::string("Failed to open file for logging: ") + name; + } + return res; +}