]> WPIA git - cassiopeia.git/blob - src/util.cpp
cln: Move code around, cleanup structure
[cassiopeia.git] / src / util.cpp
1 #include "util.h"
2
3 #include <sys/stat.h>
4
5 #include <fstream>
6
7 void writeFile( const std::string& name, const std::string& content ) {
8     std::ofstream file;
9
10     file.open( name );
11     file << content;
12     file.close();
13 }
14
15 std::string readFile( const std::string& name ) {
16     std::ifstream t( name );
17     std::string res = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
18     t.close();
19
20     return res;
21 }
22
23 std::string writeBackFile( const std::string& serial, const std::string& cert, const std::string& keydir ) {
24     std::string filename = keydir;
25     mkdir( filename.c_str(), 0755 );
26     filename += "/crt";
27     mkdir( filename.c_str(), 0755 );
28     std::string first;
29
30     if( serial.length() < 3 ) {
31         first = "0";
32     } else {
33         first = serial.substr( 0, serial.length() - 3 );
34     }
35
36     filename += "/" + first;
37     mkdir( filename.c_str(), 0755 );
38     filename += "/" + serial + ".crt";
39     writeFile( filename, cert );
40
41     return filename;
42 }