X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Futil.cpp;h=c16ba1c6c8d541164fc5ff7fd540aadb74d1e7fa;hb=ea0b37d1586c82d6be3f8e3951f8d6a6bfef4cec;hp=20d46606762d1aec69db186a9c24843bf9c60c5a;hpb=28e331e99235c616e8ccd27b7b5083bae47a1c1b;p=cassiopeia.git diff --git a/src/util.cpp b/src/util.cpp index 20d4660..c16ba1c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -14,6 +14,8 @@ void writeFile( const std::string& name, const std::string& content ) { file.open( name ); file << content; file.close(); + + //! \FIXME: Error checking } std::string readFile( const std::string& name ) { @@ -25,10 +27,28 @@ std::string readFile( const std::string& name ) { } std::string writeBackFile( const std::string& serial, const std::string& cert, const std::string& keydir ) { + errno = 0; + std::string filename = keydir; - mkdir( filename.c_str(), 0755 ); + + if( 0 != mkdir( filename.c_str(), 0755 ) ) { + if( EEXIST != errno ) { + return ""; + } + + //! \FIXME: Check this is a directory + } + filename += "/crt"; - mkdir( filename.c_str(), 0755 ); + + if( 0 != mkdir( filename.c_str(), 0755 ) ) { + if( EEXIST != errno ) { + return ""; + } + + //! \FIXME: Check this is a directory + } + std::string first; if( serial.length() < 3 ) { @@ -38,12 +58,21 @@ std::string writeBackFile( const std::string& serial, const std::string& cert, c } filename += "/" + first; - mkdir( filename.c_str(), 0755 ); + + if( 0 != mkdir( filename.c_str(), 0755 ) ) { + if( EEXIST != errno ) { + return ""; + } + + //! \FIXME: Check this is a directory + } + filename += "/" + serial + ".crt"; writeFile( filename, cert ); return filename; } + bool isDigit( char c ) { return ( c >= '0' ) && ( c <= '9' ); } @@ -152,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; +}