]> WPIA git - cassiopeia.git/blobdiff - src/util.cpp
upd: remarks from FJW
[cassiopeia.git] / src / util.cpp
index 5cf30da1d2dac01576e6ce5dce6dbaa4f845add4..d2667fad891951775a7d3abbb5cf89a76716fe15 100644 (file)
@@ -9,17 +9,15 @@
 #include <stdexcept>
 
 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
 }
 
 std::string readFile( const std::string& name ) {
     std::ifstream t( name );
     std::string res = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
-    t.close();
 
     return res;
 }
@@ -28,14 +26,25 @@ 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 ) ) {
-        return "";
+        if( EEXIST != errno ) {
+            return "";
+        }
+
+        //! \FIXME: Check this is a directory
     }
 
     filename += "/crt";
+
     if( 0 != mkdir( filename.c_str(), 0755 ) ) {
-        return "";
+        if( EEXIST != errno ) {
+            return "";
+        }
+
+        //! \FIXME: Check this is a directory
     }
+
     std::string first;
 
     if( serial.length() < 3 ) {
@@ -45,9 +54,15 @@ std::string writeBackFile( const std::string& serial, const std::string& cert, c
     }
 
     filename += "/" + first;
+
     if( 0 != mkdir( filename.c_str(), 0755 ) ) {
-        return "";
+        if( EEXIST != errno ) {
+            return "";
+        }
+
+        //! \FIXME: Check this is a directory
     }
+
     filename += "/" + serial + ".crt";
     writeFile( filename, cert );
 
@@ -94,7 +109,7 @@ std::pair<bool, time_t> parseDate( const std::string& date ) {
     std::string checkS( check, siz );
 
     if( checkS != date ) {
-        return std::pair<bool, time_t>( false, 0 );
+        return { false, 0 };
     }
 
     return std::pair<bool, time_t>( true, res );
@@ -119,7 +134,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 ) {
@@ -142,6 +156,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 );
@@ -162,3 +177,29 @@ std::pair<bool, time_t> parseYearInterval( std::time_t t, const std::string& dat
         return std::pair<bool, time_t>( false, 0 );
     }
 }
+
+std::unique_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 = make_unique<std::ofstream>( tname );
+
+    if( ! res->good() ) {
+        throw 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 );
+}