]> WPIA git - cassiopeia.git/blobdiff - src/util.cpp
fix: Ambiguity when compiling
[cassiopeia.git] / src / util.cpp
index 5cf30da1d2dac01576e6ce5dce6dbaa4f845add4..c16ba1c6c8d541164fc5ff7fd540aadb74d1e7fa 100644 (file)
@@ -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 ) {
@@ -28,14 +30,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 +58,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 );
 
@@ -162,3 +181,21 @@ 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;
+}