X-Git-Url: http://code.wpia.club/?a=blobdiff_plain;f=src%2Futil.cpp;h=224613e7caff236cab5df05f5027a930d664c668;hb=HEAD;hp=ed7b42e61ff87a407c981ff791cd92d05459136a;hpb=f69f31caeda734d6d9c8ab00e693671ac7512bea;p=cassiopeia.git diff --git a/src/util.cpp b/src/util.cpp index ed7b42e..224613e 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -9,11 +9,8 @@ #include void writeFile( const std::string& name, const std::string& content ) { - std::ofstream file; - - file.open( name ); + std::ofstream file( name ); file << content; - file.close(); //! \FIXME: Error checking } @@ -21,7 +18,6 @@ void writeFile( const std::string& name, const std::string& content ) { std::string readFile( const std::string& name ) { std::ifstream t( name ); std::string res = std::string( std::istreambuf_iterator( t ), std::istreambuf_iterator() ); - t.close(); return res; } @@ -30,15 +26,17 @@ 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 ""; + throw std::runtime_error( "Storage location could not be determined" ); } //! \FIXME: Check this is a directory } filename += "/crt"; + if( 0 != mkdir( filename.c_str(), 0755 ) ) { if( EEXIST != errno ) { return ""; @@ -46,6 +44,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 +54,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 ""; @@ -89,13 +89,14 @@ std::pair parseDate( const std::string& date ) { return std::pair( false, 0 ); } - std::tm t; + std::tm t = {}; t.tm_sec = 0; t.tm_min = 0; t.tm_hour = 0; - t.tm_year = std::stoi( date.substr( 0, 4 ) ) - 1900; - t.tm_mon = std::stoi( date.substr( 5, 2 ) ) - 1; t.tm_mday = std::stoi( date.substr( 8, 2 ) ); + t.tm_mon = std::stoi( date.substr( 5, 2 ) ) - 1; + t.tm_year = std::stoi( date.substr( 0, 4 ) ) - 1900; + setenv( "TZ", "UTC", 1 ); tzset(); std::time_t res = mktime( &t ); @@ -109,14 +110,14 @@ std::pair parseDate( const std::string& date ) { std::string checkS( check, siz ); if( checkS != date ) { - return std::pair( false, 0 ); + return { false, 0 }; } return std::pair( true, res ); } std::pair addMonths( std::time_t t, int32_t count ) { - std::tm* parsed = gmtime( &t ); + std::tm *parsed = gmtime( &t ); if( !parsed || count <= 0 || count > 24 ) { // FIXED MAX-Validity-Length return std::pair( false, 0 ); @@ -134,7 +135,6 @@ std::pair addMonths( std::time_t t, int32_t count ) { } return std::pair( true, res ); - } std::pair parseMonthInterval( std::time_t t, const std::string& date ) { @@ -144,7 +144,7 @@ std::pair parseMonthInterval( std::time_t t, const std::string& da try { size_t end = 0; - int num = std::stoi( date.substr( 0, date.size() - 1 ) , &end ); + int num = std::stoi( date.substr( 0, date.size() - 1 ), &end ); if( end != date.size() - 1 ) { return std::pair( false, 0 ); @@ -157,6 +157,7 @@ std::pair parseMonthInterval( std::time_t t, const std::string& da return std::pair( false, 0 ); } } + std::pair parseYearInterval( std::time_t t, const std::string& date ) { if( date[date.size() - 1] != 'y' ) { return std::pair( false, 0 ); @@ -177,3 +178,31 @@ std::pair parseYearInterval( std::time_t t, const std::string& dat return std::pair( false, 0 ); } } + +std::unique_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::make_unique( tname ); + + if( ! res->good() ) { + throw std::runtime_error( std::string( "Failed to open file for logging: " ) + name ); + } + + return res; +} + +std::string timestamp() { + time_t c_time; + + if( time( &c_time ) == -1 ) { + throw std::runtime_error( "Error while fetching time?" ); + } + + return std::to_string( c_time ); +}