]> WPIA git - cassiopeia.git/blobdiff - src/util.cpp
fmt: Whitespace, indentation, generic source formatting
[cassiopeia.git] / src / util.cpp
index ed7b42e61ff87a407c981ff791cd92d05459136a..9e24b378d834c6e3ad6a356b3f9a48b533fb1751 100644 (file)
@@ -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 "";
@@ -134,7 +138,6 @@ std::pair<bool, time_t> addMonths( std::time_t t, int32_t count ) {
     }
 
     return std::pair<bool, time_t>( true, res );
-
 }
 
 std::pair<bool, time_t> parseMonthInterval( std::time_t t, const std::string& date ) {
@@ -157,6 +160,7 @@ std::pair<bool, time_t> parseMonthInterval( std::time_t t, const std::string& da
         return std::pair<bool, time_t>( false, 0 );
     }
 }
+
 std::pair<bool, time_t> parseYearInterval( std::time_t t, const std::string& date ) {
     if( date[date.size() - 1] != 'y' ) {
         return  std::pair<bool, time_t>( false, 0 );
@@ -177,3 +181,25 @@ std::pair<bool, time_t> parseYearInterval( std::time_t t, const std::string& dat
         return std::pair<bool, time_t>( false, 0 );
     }
 }
+
+std::shared_ptr<std::ofstream> 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<std::ofstream>( 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;
+}