mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-01-05 21:01:11 -05:00
Fixed config forward compatibility problem
- note to self: C++ binary file not same as C binary file git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@2510 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
0b4bc0f2eb
commit
6054890725
@ -23,11 +23,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "fstream"
|
|
||||||
|
|
||||||
using std::ofstream;
|
|
||||||
using std::ifstream;
|
|
||||||
|
|
||||||
#include "util/rsdir.h"
|
#include "util/rsdir.h"
|
||||||
#include "rsiface/rspeers.h"
|
#include "rsiface/rspeers.h"
|
||||||
#include "pqi/p3cfgmgr.h"
|
#include "pqi/p3cfgmgr.h"
|
||||||
@ -39,7 +34,7 @@ using std::ifstream;
|
|||||||
|
|
||||||
#include "serialiser/rsconfigitems.h"
|
#include "serialiser/rsconfigitems.h"
|
||||||
|
|
||||||
//#define CONFIG_DEBUG 1
|
#define CONFIG_DEBUG 1
|
||||||
|
|
||||||
|
|
||||||
p3ConfigMgr::p3ConfigMgr(std::string dir, std::string fname, std::string signame)
|
p3ConfigMgr::p3ConfigMgr(std::string dir, std::string fname, std::string signame)
|
||||||
@ -139,32 +134,37 @@ void p3ConfigMgr::saveConfiguration()
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
/* construct filename */
|
/* construct filename */
|
||||||
std::string filename1 = basedir;
|
std::string fname = basedir;
|
||||||
std::string filename2 = basedir;
|
std::string sign_fname = basedir;
|
||||||
std::string filename1_tmp, filename2_tmp; // temporary file to do two pass save
|
std::string fname_backup, sign_fname_backup; // back up files
|
||||||
|
|
||||||
if (basedir != "")
|
if (basedir != "")
|
||||||
{
|
{
|
||||||
filename1 += "/";
|
fname += "/";
|
||||||
filename2 += "/";
|
sign_fname += "/";
|
||||||
}
|
}
|
||||||
filename1 += metasigfname;
|
|
||||||
filename2 += metafname;
|
fname += metafname;
|
||||||
filename1_tmp = filename1 + ".tmp";
|
sign_fname += metasigfname;
|
||||||
filename2_tmp = filename2 + ".tmp";
|
fname_backup = fname + ".tmp";
|
||||||
|
sign_fname_backup = sign_fname + ".tmp";
|
||||||
|
|
||||||
/* Write the data to a stream */
|
/* Write the data to a stream */
|
||||||
uint32_t bioflags = BIN_FLAGS_WRITEABLE;
|
uint32_t bioflags = BIN_FLAGS_WRITEABLE;
|
||||||
BinMemInterface *membio = new BinMemInterface(1000, bioflags);
|
BinMemInterface *configbio = new BinMemInterface(1000, bioflags);
|
||||||
RsSerialiser *rss = new RsSerialiser();
|
RsSerialiser *rss = new RsSerialiser();
|
||||||
rss->addSerialType(new RsGeneralConfigSerialiser());
|
rss->addSerialType(new RsGeneralConfigSerialiser());
|
||||||
pqistore store(rss, "CONFIG", membio, BIN_FLAGS_WRITEABLE);
|
pqistore store(rss, "CONFIG", configbio, BIN_FLAGS_WRITEABLE);
|
||||||
|
|
||||||
store.SendItem(item);
|
store.SendItem(item);
|
||||||
|
|
||||||
/* sign data */
|
/* sign data */
|
||||||
std::string signature;
|
std::string signature;
|
||||||
AuthSSL::getAuthSSL()->SignData(membio->memptr(), membio->memsize(), signature);
|
AuthSSL::getAuthSSL()->SignData(configbio->memptr(), configbio->memsize(), signature);
|
||||||
|
|
||||||
|
/* write signature to configuration */
|
||||||
|
BinMemInterface *signbio = new BinMemInterface(signature.c_str(),
|
||||||
|
signature.length(), BIN_FLAGS_READABLE);
|
||||||
|
|
||||||
#ifdef CONFIG_DEBUG
|
#ifdef CONFIG_DEBUG
|
||||||
std::cerr << "p3ConfigMgr::saveConfiguration() MetaFile Signature:";
|
std::cerr << "p3ConfigMgr::saveConfiguration() MetaFile Signature:";
|
||||||
@ -173,46 +173,105 @@ void p3ConfigMgr::saveConfiguration()
|
|||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!membio->writetofile(filename2_tmp.c_str()))
|
// begin two pass save
|
||||||
{
|
backedUpFileSave(fname, fname_backup, sign_fname, sign_fname_backup, configbio, signbio);
|
||||||
#ifdef CONFIG_DEBUG
|
|
||||||
std::cerr << "p3ConfigMgr::saveConfiguration() Failed to Write temp MetaFile " << filename2 ;
|
|
||||||
std::cerr << std::endl;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* write signature to configuration */
|
|
||||||
BinMemInterface *signbio = new BinMemInterface(signature.c_str(),
|
|
||||||
signature.length(), BIN_FLAGS_READABLE);
|
|
||||||
|
|
||||||
if (!signbio->writetofile(filename1_tmp.c_str()))
|
|
||||||
{
|
|
||||||
#ifdef CONFIG_DEBUG
|
|
||||||
std::cerr << "p3ConfigMgr::saveConfiguration() Failed to Write temp MetaSignFile" << filename1 ;
|
|
||||||
std::cerr << std::endl;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* if above written successfully now write actual file that will be read initially
|
|
||||||
* The aim is that if any of the file savings here fail at least one of these pairings of
|
|
||||||
* signature will be compatible to either config.tmp and config files
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (!membio->writetofile(filename1.c_str()) || !signbio->writetofile(filename2.c_str()))
|
|
||||||
{
|
|
||||||
#ifdef CONFIG_DEBUG
|
|
||||||
std::cerr << "p3ConfigMgr::saveConfiguration() Failed to Write MetaFile and MetaSignFile " << filename2 ;
|
|
||||||
std::cerr << std::endl;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
delete signbio;
|
delete signbio;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool p3ConfigMgr::backedUpFileSave(const std::string& fname, const std::string& fname_backup, const std::string& sign_fname,
|
||||||
|
const std::string& sign_fname_backup, BinMemInterface* configbio, BinMemInterface* signbio){
|
||||||
|
|
||||||
|
FILE *file = NULL, *sign = NULL;
|
||||||
|
int size_file, size_sign;
|
||||||
|
|
||||||
|
// begin two pass saving by writing to back up file instead
|
||||||
|
if (!configbio->writetofile(fname_backup.c_str()) || !signbio->writetofile(sign_fname_backup.c_str()))
|
||||||
|
{
|
||||||
|
#ifdef CONFIG_DEBUG
|
||||||
|
std::cerr << "p3ConfigMgr::backedupFileSave() Failed write to Backup MetaFiles " << fname_backup
|
||||||
|
<< " and " << sign_fname_backup;
|
||||||
|
std::cerr << std::endl;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef CONFIG_DEBUG
|
||||||
|
std::cerr << "p3Config::backedUpFileSave() Save file and keeps a back up " << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// open file from which to collect buffer
|
||||||
|
file = fopen(fname.c_str(), "rb");
|
||||||
|
sign = fopen(sign_fname.c_str(), "rb");
|
||||||
|
|
||||||
|
// if failed then just use back-up file: back-up and actual configs are the same files now
|
||||||
|
if((file == NULL) || (sign == NULL)){
|
||||||
|
#ifdef CONFIG_DEBUG
|
||||||
|
std::cerr << "p3Config::backedUpFileSave() failed to open meta files " << fname << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
file = fopen(fname_backup.c_str(), "rb");
|
||||||
|
sign = fopen(sign_fname_backup.c_str(), "rb");
|
||||||
|
|
||||||
|
if((file == NULL) || (sign == NULL)){
|
||||||
|
std::cerr << "p3Config::backedUpFileSave() failed to open backup meta files" << fname_backup << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//determine file size
|
||||||
|
fseek(file, 0L, SEEK_END);
|
||||||
|
size_file = ftell(file);
|
||||||
|
fseek(file, 0L, SEEK_SET);
|
||||||
|
|
||||||
|
fseek(sign, 0L, SEEK_END);
|
||||||
|
size_sign = ftell(sign);
|
||||||
|
fseek(sign, 0L, SEEK_SET);
|
||||||
|
|
||||||
|
//read this into a buffer
|
||||||
|
char* config_buff = new char[size_file];
|
||||||
|
fread(config_buff, size_file, 1, file);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
//read this into a buffer
|
||||||
|
char* sign_buff = new char[size_sign];
|
||||||
|
fread(sign_buff, size_sign, 1, sign);
|
||||||
|
fclose(sign);
|
||||||
|
|
||||||
|
// rename back-up to current file
|
||||||
|
if(!RsDirUtil::renameFile(fname_backup, fname) || !RsDirUtil::renameFile(sign_fname_backup, sign_fname)){
|
||||||
|
std::cerr << "p3Config::backedUpFileSave() Failed to rename backup meta files: " << std::endl
|
||||||
|
<< fname_backup << " to " << fname << std::endl
|
||||||
|
<< sign_fname_backup << " to " << sign_fname << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// now write actual back-up file
|
||||||
|
file = fopen(fname_backup.c_str(), "wb");
|
||||||
|
sign = fopen(sign_fname_backup.c_str(), "wb");
|
||||||
|
|
||||||
|
if((file == NULL) || (sign == NULL)){
|
||||||
|
#ifdef CONFIG_DEBUG
|
||||||
|
std::cerr << "p3Config::backedUpFileSave() fopen failed for file: " << fname_backup << std::endl;
|
||||||
|
#endif
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fwrite(config_buff, size_file, 1, file);
|
||||||
|
fwrite(sign_buff, size_sign, 1, sign);
|
||||||
|
fclose(file);
|
||||||
|
fclose(sign);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DEBUG
|
||||||
|
std::cerr << "p3Config::backedUpFileSave() finished backed up save. " << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
delete[] config_buff;
|
||||||
|
delete[] sign_buff;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void p3ConfigMgr::loadConfiguration()
|
void p3ConfigMgr::loadConfiguration()
|
||||||
@ -225,30 +284,27 @@ void p3ConfigMgr::loadConfiguration()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* construct filename */
|
/* construct filename */
|
||||||
std::string filename1 = basedir;
|
std::string fname = basedir;
|
||||||
std::string filename2 = basedir;
|
std::string sign_fname = basedir;
|
||||||
std::string filename1_tmp, filename2_tmp;
|
std::string fname_backup, sign_fname_backup;
|
||||||
|
|
||||||
if (basedir != "")
|
if (basedir != "")
|
||||||
{
|
{
|
||||||
filename1 += "/";
|
fname += "/";
|
||||||
filename2 += "/";
|
sign_fname += "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
filename1 += metasigfname;
|
fname += metafname;
|
||||||
filename2 += metafname;
|
sign_fname += metasigfname;
|
||||||
|
|
||||||
// temporary files
|
// temporary files
|
||||||
filename1_tmp = filename1 + ".tmp";
|
fname_backup = fname + ".tmp";
|
||||||
filename2_tmp = filename2 + ".tmp";
|
sign_fname_backup = sign_fname + ".tmp";
|
||||||
|
|
||||||
BinMemInterface* membio = new BinMemInterface(1000, BIN_FLAGS_READABLE);
|
BinMemInterface* membio = new BinMemInterface(1000, BIN_FLAGS_READABLE);
|
||||||
|
|
||||||
/*
|
// Will attempt to get signature first from meta file then if that fails try temporary meta files, these will correspond to temp configs
|
||||||
* Will attempt to get signature first from meta file then if that fails try temporary meta files, these will correspond to temp configs
|
bool pass = getSignAttempt(fname, sign_fname, membio);
|
||||||
*/
|
|
||||||
|
|
||||||
bool pass = getSignAttempt(filename2, filename1, membio);
|
|
||||||
|
|
||||||
// if first attempt fails then try and temporary files
|
// if first attempt fails then try and temporary files
|
||||||
if(!pass){
|
if(!pass){
|
||||||
@ -258,7 +314,7 @@ void p3ConfigMgr::loadConfiguration()
|
|||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
pass = getSignAttempt(filename2_tmp, filename1_tmp, membio);
|
pass = getSignAttempt(fname_backup, sign_fname_backup , membio);
|
||||||
|
|
||||||
if(!pass){
|
if(!pass){
|
||||||
#ifdef CONFIG_DEBUG
|
#ifdef CONFIG_DEBUG
|
||||||
@ -468,29 +524,30 @@ p3Config::p3Config(uint32_t t)
|
|||||||
bool p3Config::loadConfiguration(std::string &loadHash)
|
bool p3Config::loadConfiguration(std::string &loadHash)
|
||||||
{
|
{
|
||||||
|
|
||||||
std::string fname = Filename();
|
std::string cfg_fname = Filename();
|
||||||
std::string fnametmp = fname + ".tmp";
|
std::string cfg_fname_backup = cfg_fname + ".tmp";
|
||||||
std::string hashstr;
|
std::string hashstr;
|
||||||
std::list<RsItem *> load;
|
std::list<RsItem *> load;
|
||||||
|
|
||||||
#ifdef CONFIG_DEBUG
|
#ifdef CONFIG_DEBUG
|
||||||
std::string success_fname = fname;
|
std::string success_fname = cfg_fname;
|
||||||
std::cerr << "p3Config::loadConfiguration(): Attempting to load configuration file" << fname << std::endl;
|
std::cerr << "p3Config::loadConfiguration(): Attempting to load configuration file" << cfg_fname << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool pass = getHashAttempt(loadHash, hashstr, fname, load);
|
bool pass = getHashAttempt(loadHash, hashstr, cfg_fname, load);
|
||||||
|
|
||||||
if(!pass){
|
if(!pass){
|
||||||
pass = getHashAttempt(loadHash, hashstr, fnametmp, load);
|
pass = getHashAttempt(loadHash, hashstr, cfg_fname_backup, load);
|
||||||
#ifdef CONFIG_DEBUG
|
#ifdef CONFIG_DEBUG
|
||||||
success_fname = fnametmp;
|
std::cerr << "p3Config::loadConfiguration() ERROR: Failed to get Hash from " << success_fname << std::endl;
|
||||||
|
success_fname = cfg_fname_backup;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(!pass){
|
if(!pass){
|
||||||
#ifdef CONFIG_DEBUG
|
#ifdef CONFIG_DEBUG
|
||||||
std::cerr << "p3Config::loadConfiguratio() ERROR: Failed to get Hash" << std::endl;
|
std::cerr << "p3Config::loadConfiguration() ERROR: Failed to get Hash from " << success_fname << std::endl;
|
||||||
|
#endif
|
||||||
return false;
|
return false;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -499,15 +556,13 @@ bool p3Config::loadConfiguration(std::string &loadHash)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
setHash(hashstr);
|
setHash(hashstr);
|
||||||
|
|
||||||
// else okay
|
|
||||||
return loadList(load);
|
return loadList(load);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool p3Config::getHashAttempt(const std::string& loadHash, std::string& hashstr,const std::string& fname,
|
bool p3Config::getHashAttempt(const std::string& loadHash, std::string& hashstr,const std::string& cfg_fname,
|
||||||
std::list<RsItem *>& load){
|
std::list<RsItem *>& load){
|
||||||
|
|
||||||
std::list<RsItem *>::iterator it;
|
std::list<RsItem *>::iterator it;
|
||||||
@ -515,7 +570,7 @@ bool p3Config::getHashAttempt(const std::string& loadHash, std::string& hashstr,
|
|||||||
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_READABLE;
|
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_READABLE;
|
||||||
uint32_t stream_flags = BIN_FLAGS_READABLE;
|
uint32_t stream_flags = BIN_FLAGS_READABLE;
|
||||||
|
|
||||||
BinInterface *bio = new BinFileInterface(fname.c_str(), bioflags);
|
BinInterface *bio = new BinFileInterface(cfg_fname.c_str(), bioflags);
|
||||||
pqistore stream(setupSerialiser(), "CONFIG", bio, stream_flags);
|
pqistore stream(setupSerialiser(), "CONFIG", bio, stream_flags);
|
||||||
RsItem *item = NULL;
|
RsItem *item = NULL;
|
||||||
|
|
||||||
@ -532,7 +587,7 @@ bool p3Config::getHashAttempt(const std::string& loadHash, std::string& hashstr,
|
|||||||
|
|
||||||
#ifdef CONFIG_DEBUG
|
#ifdef CONFIG_DEBUG
|
||||||
std::cerr << "p3Config::loadConfiguration() loaded " << load.size();
|
std::cerr << "p3Config::loadConfiguration() loaded " << load.size();
|
||||||
std::cerr << " Elements from File: " << fname;
|
std::cerr << " Elements from File: " << cfg_fname;
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -570,31 +625,47 @@ bool p3Config::saveConfiguration()
|
|||||||
std::list<RsItem *> toSave = saveList(cleanup);
|
std::list<RsItem *> toSave = saveList(cleanup);
|
||||||
|
|
||||||
|
|
||||||
std::string fname = Filename(); // get configuration file name
|
std::string cfg_fname = Filename(); // get configuration file name
|
||||||
std::string fnametmp = Filename()+".tmp"; // temporary file for two pass save
|
std::string cfg_fname_backup = Filename()+".tmp"; // backup file for two pass save
|
||||||
std::string fnametmpold = fnametmp + "_old";
|
|
||||||
|
|
||||||
#ifdef CONFIG_DEBUG
|
#ifdef CONFIG_DEBUG
|
||||||
std::cerr << "Writting p3config file " << fname.c_str() << std::endl ;
|
std::cerr << "Writting p3config file " << cfg_fname << std::endl ;
|
||||||
std::cerr << "p3Config::saveConfiguration() toSave " << toSave.size();
|
std::cerr << "p3Config::saveConfiguration() toSave " << toSave.size();
|
||||||
std::cerr << " Elements to File: " << fname;
|
std::cerr << " Elements to File: " << cfg_fname;
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
saveDone(); // callback to inherited class to unlock any Mutexes protecting saveList() data
|
||||||
|
|
||||||
|
// saves current config and keeps back-up (old configuration)
|
||||||
|
if(!backedUpFileSave(cfg_fname, cfg_fname_backup, toSave, cleanup))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool p3Config::backedUpFileSave(const std::string& cfg_fname, const std::string& cfg_fname_backup, std::list<RsItem* >& itemList,
|
||||||
|
bool cleanup){
|
||||||
|
|
||||||
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;
|
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;
|
||||||
uint32_t stream_flags = BIN_FLAGS_WRITEABLE;
|
uint32_t stream_flags = true;
|
||||||
|
|
||||||
if (!cleanup)
|
if (!cleanup){
|
||||||
stream_flags |= BIN_FLAGS_NO_DELETE;
|
stream_flags |= BIN_FLAGS_NO_DELETE;
|
||||||
|
}else{
|
||||||
BinInterface *bio = new BinFileInterface(fnametmp.c_str(), bioflags);
|
stream_flags = BIN_FLAGS_WRITEABLE;
|
||||||
|
}
|
||||||
|
BinInterface *bio = new BinFileInterface(cfg_fname_backup.c_str(), bioflags);
|
||||||
pqistore *stream = new pqistore(setupSerialiser(), "CONFIG", bio, stream_flags);
|
pqistore *stream = new pqistore(setupSerialiser(), "CONFIG", bio, stream_flags);
|
||||||
|
|
||||||
std::list<RsItem *>::iterator it;
|
std::list<RsItem *>::iterator it;
|
||||||
|
|
||||||
bool written = true ;
|
bool written = true;
|
||||||
|
|
||||||
for(it = toSave.begin(); it != toSave.end(); it++)
|
for(it = itemList.begin(); it != itemList.end(); it++)
|
||||||
{
|
{
|
||||||
#ifdef CONFIG_DEBUG
|
#ifdef CONFIG_DEBUG
|
||||||
std::cerr << "p3Config::saveConfiguration() save item:";
|
std::cerr << "p3Config::saveConfiguration() save item:";
|
||||||
@ -603,71 +674,71 @@ bool p3Config::saveConfiguration()
|
|||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
#endif
|
#endif
|
||||||
written = written && stream->SendItem(*it);
|
written = written && stream->SendItem(*it);
|
||||||
|
|
||||||
// std::cerr << "written = " << written << std::endl ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* store the hash */
|
/* store the hash */
|
||||||
setHash(bio->gethash());
|
setHash(bio->gethash());
|
||||||
saveDone(); /* callback to inherited class to unlock any Mutexes
|
|
||||||
* protecting saveList() data
|
|
||||||
*/
|
|
||||||
|
|
||||||
// so early?
|
// bio is taken care of in stream's destructor
|
||||||
delete stream ;
|
delete stream;
|
||||||
|
|
||||||
/**
|
|
||||||
* ensures real old config is alway present in file system, leave of config as tmp file
|
|
||||||
*/
|
|
||||||
|
|
||||||
if(!written)
|
|
||||||
return false ;
|
|
||||||
|
|
||||||
#ifdef CONFIG_DEBUG
|
#ifdef CONFIG_DEBUG
|
||||||
std::cerr << "p3config::saveConfiguration() renaming " << fnametmp.c_str() << " to " << fname.c_str() << std::endl;
|
std::cerr << "p3Config::backedUpFileSave() Save file and keeps a back up " << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// first write old config to old-temp file
|
// open file from which to collect buffer
|
||||||
|
FILE* cfg_file = NULL;
|
||||||
ifstream ifstrm;
|
int size_file = 0;
|
||||||
ofstream ofstrm;
|
cfg_file = fopen(cfg_fname.c_str(), "rb");
|
||||||
ifstrm.open(fname.c_str(), std::ifstream::binary);
|
|
||||||
|
|
||||||
|
// if failed then just use back-up file: back-up and file are now the same files
|
||||||
|
if(cfg_file == NULL){
|
||||||
#ifdef CONFIG_DEBUG
|
#ifdef CONFIG_DEBUG
|
||||||
std::cerr << "p3config::saveConfiguration() Is file open: " << ifstrm.is_open() << std::endl;
|
std::cerr << "p3Config::backedUpFileSave() fopen failed for file: " << cfg_fname << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
fclose(cfg_file);
|
||||||
// if file does not exist then open temporay file already created
|
cfg_file = fopen(cfg_fname_backup.c_str(), "rb");
|
||||||
if(!ifstrm.is_open()){
|
if(cfg_file == NULL){
|
||||||
ifstrm.close();
|
std::cerr << "p3Config::backedUpFileSave() fopen failed for file:" << cfg_fname_backup << std::endl;
|
||||||
ifstrm.open(fnametmp.c_str(), std::ifstream::binary);
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine size of old config file
|
//determine file size
|
||||||
ifstrm.seekg(0, std::ios_base::end);
|
fseek(cfg_file, 0L, SEEK_END);
|
||||||
std::streampos length = ifstrm.tellg();
|
size_file = ftell(cfg_file);
|
||||||
ifstrm.seekg(0);
|
fseek(cfg_file, 0L, SEEK_SET);
|
||||||
|
|
||||||
//read this into a buffer
|
//read this into a data buffer
|
||||||
char* buff = new char[length];
|
char* buff = new char[size_file];
|
||||||
ifstrm.read(buff, length);
|
fread(buff, size_file, 1, cfg_file);
|
||||||
ifstrm.close();
|
fclose(cfg_file);
|
||||||
|
|
||||||
// write to config_old file
|
// rename back-up to current file
|
||||||
ofstrm.open(fnametmpold.c_str(), std::ofstream::binary);
|
if(!RsDirUtil::renameFile(cfg_fname_backup, cfg_fname)){
|
||||||
ofstrm.write(buff, length);
|
std::cerr << "p3Config::backedUpFileSave() Failed to rename file" << cfg_fname_backup << " to "
|
||||||
ofstrm.close();
|
<< cfg_fname << std::endl;
|
||||||
|
return false;
|
||||||
// now rename new temp config file to current(fname) and old config file to temp (fnametmp)
|
|
||||||
if(!RsDirUtil::renameFile(fnametmp, fname) || !RsDirUtil::renameFile(fnametmpold, fnametmp)){
|
|
||||||
std::cerr << "ERROR!" << std::endl;
|
|
||||||
#ifdef CONFIG_DEBUG
|
|
||||||
std::cerr << "p3Config::saveConfiguration(): Failed to Rename" << std::endl;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete buff;
|
// now write actual back-up file
|
||||||
// delete bio;
|
cfg_file = fopen(cfg_fname_backup.c_str(), "wb");
|
||||||
|
|
||||||
|
if(cfg_file == NULL){
|
||||||
|
#ifdef CONFIG_DEBUG
|
||||||
|
std::cerr << "p3Config::backedUpFileSave() fopen failed for file: " << cfg_fname_backup << std::endl;
|
||||||
|
#endif
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fwrite(buff, size_file, 1, cfg_file);
|
||||||
|
fclose(cfg_file);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DEBUG
|
||||||
|
std::cerr << "p3Config::backedUpFileSave() finished backed up save. " << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
delete[] buff;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,6 +97,11 @@ const uint32_t CONFIG_TYPE_CACHE = 0xff01;
|
|||||||
|
|
||||||
class p3ConfigMgr;
|
class p3ConfigMgr;
|
||||||
|
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Aim is that active objects in retroshare can dervie from this class
|
||||||
|
* and implement their method of saving and loading their configuration
|
||||||
|
*/
|
||||||
class pqiConfig
|
class pqiConfig
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -104,17 +109,17 @@ class pqiConfig
|
|||||||
virtual ~pqiConfig();
|
virtual ~pqiConfig();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param loadHash This is hash that will be compared to confirm that data has not been tampered with
|
* loadHash This is the hash that will be compared to confirm saved configuration has not
|
||||||
|
* been tampered with
|
||||||
*/
|
*/
|
||||||
virtual bool loadConfiguration(std::string &loadHash) = 0;
|
virtual bool loadConfiguration(std::string &loadHash) = 0;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param This should
|
|
||||||
*/
|
|
||||||
virtual bool saveConfiguration() = 0;
|
virtual bool saveConfiguration() = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The type of configuration, see ids where this class is declared
|
* The type of configuration, see ids where this class is declared
|
||||||
|
* @see p3cfgmgr.h
|
||||||
*/
|
*/
|
||||||
uint32_t Type();
|
uint32_t Type();
|
||||||
|
|
||||||
@ -131,6 +136,9 @@ std::string Hash();
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if configuration has changed
|
||||||
|
*/
|
||||||
void IndicateConfigChanged();
|
void IndicateConfigChanged();
|
||||||
void setHash(std::string h);
|
void setHash(std::string h);
|
||||||
|
|
||||||
@ -161,18 +169,23 @@ void setHash(std::string h);
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**** MUTEX NOTE
|
|
||||||
* None - because no-one calls any functions
|
|
||||||
* besides tick() when the system is running.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
/***********************************************************************************************/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* MUTEX NOTE
|
||||||
|
* None - because no-one calls any functions besides tick() when the system is running.
|
||||||
|
* Takes care of updating, saving configurations within retroshare and also
|
||||||
|
* adding new configurations
|
||||||
|
*/
|
||||||
class p3ConfigMgr
|
class p3ConfigMgr
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
p3ConfigMgr(std::string bdir, std::string fname, std::string signame);
|
p3ConfigMgr(std::string bdir, std::string fname, std::string signame);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called from Rs server to check configuration and update configurations
|
* checks and update all added configurations
|
||||||
|
* @see rsserver
|
||||||
*/
|
*/
|
||||||
void tick();
|
void tick();
|
||||||
|
|
||||||
@ -200,10 +213,25 @@ class p3ConfigMgr
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To be called from load configuration
|
* checks if signature and configuration file's signature matches
|
||||||
|
* @return false if signature file does not match configuration file's signature
|
||||||
*/
|
*/
|
||||||
bool getSignAttempt(std::string& metaConfigFname, std::string& metaSignFname, BinMemInterface* membio);
|
bool getSignAttempt(std::string& metaConfigFname, std::string& metaSignFname, BinMemInterface* membio);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* takes current configuration which is stored in back-up file, and moves it to actual config file
|
||||||
|
* then stores data that was in actual config file into back-up, Corresponding signatures and handled
|
||||||
|
* simlarly in parallel
|
||||||
|
*@param fname the name of the first configuration file checked
|
||||||
|
*@param fname_backup the seconf file, backup, checked if first file does not exist or is corrupted
|
||||||
|
*@param sign file name in which signature is kept
|
||||||
|
*@param sign_backup file name in which signature which correspond to backup config is kept
|
||||||
|
*@param configbio to write config to file
|
||||||
|
*@param signbio to write signature config to file
|
||||||
|
*/
|
||||||
|
bool backedUpFileSave(const std::string& fname, const std::string& fname_backup,const std::string& sign,
|
||||||
|
const std::string& sign_backup, BinMemInterface* configbio, BinMemInterface* signbio);
|
||||||
|
|
||||||
const std::string basedir;
|
const std::string basedir;
|
||||||
const std::string metafname;
|
const std::string metafname;
|
||||||
const std::string metasigfname;
|
const std::string metasigfname;
|
||||||
@ -214,6 +242,7 @@ bool mConfigSaveActive;
|
|||||||
std::map<uint32_t, pqiConfig *> configs;
|
std::map<uint32_t, pqiConfig *> configs;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/***************************************************************************************************/
|
||||||
|
|
||||||
class p3Config: public pqiConfig
|
class p3Config: public pqiConfig
|
||||||
{
|
{
|
||||||
@ -238,8 +267,19 @@ virtual void saveDone() { return; }
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
//helper function for retrieving correct hash using a temporary directory
|
/**
|
||||||
virtual bool getHashAttempt(const std::string& loadHash, std::string& hashstr, const std::string& fname, std::list<RsItem *>& load);
|
* takes current configuration which is stored in back-up file, and moves it to actual config file
|
||||||
|
* then stores data that was in actual config file into back-up, This is an rs specific solution
|
||||||
|
* @param fname the name of the first configuration file checked
|
||||||
|
* @param fname_backup the seconf file, backup, checked if first file does not exist or is corrupted
|
||||||
|
*/
|
||||||
|
bool backedUpFileSave(const std::string& fname, const std::string& fname_backup, std::list<RsItem* >& toSave,
|
||||||
|
bool cleanup);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* for retrieving hash files
|
||||||
|
*/
|
||||||
|
bool getHashAttempt(const std::string& loadHash, std::string& hashstr, const std::string& fname, std::list<RsItem *>& load);
|
||||||
|
|
||||||
|
|
||||||
}; /* end of p3Config */
|
}; /* end of p3Config */
|
||||||
|
@ -251,8 +251,14 @@ bool p3Blogs::BlogMessageSend(BlogMsgInfo &info)
|
|||||||
|
|
||||||
bool p3Blogs::BlogMessageReply(BlogMsgInfo& reply){
|
bool p3Blogs::BlogMessageReply(BlogMsgInfo& reply){
|
||||||
|
|
||||||
if(reply.msgIdReply.empty())
|
// ensure it has a value
|
||||||
|
if(isReply(reply)){
|
||||||
|
std::cerr << "p3Blogs::BlogMessageReply()" << " This is not a reply " << std::endl;
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// also check that msgId exists for group
|
||||||
|
|
||||||
return BlogMessageSend(reply);
|
return BlogMessageSend(reply);
|
||||||
}
|
}
|
||||||
@ -306,6 +312,23 @@ bool p3Blogs::blogSubscribe(std::string cId, bool subscribe)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool p3Blogs::deleteBlogMsg(std::string cId, std::string mId){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool p3Blogs::isBlogDeleted(std::string cId){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool p3Blogs::isBlogMsgDeleted(std::string cId, std::string mId){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool p3Blogs::deleteBlog(std::string cId){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/***************************************************************************************/
|
/***************************************************************************************/
|
||||||
/****************** Event Feedback (Overloaded form p3distrib) *************************/
|
/****************** Event Feedback (Overloaded form p3distrib) *************************/
|
||||||
/***************************************************************************************/
|
/***************************************************************************************/
|
||||||
|
@ -45,24 +45,75 @@ virtual ~p3Blogs();
|
|||||||
/****************************************/
|
/****************************************/
|
||||||
/********* rsBlogs Interface ***********/
|
/********* rsBlogs Interface ***********/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if any of the blogs has been changed
|
||||||
|
* @param blogIds list blogs by ids to be checked
|
||||||
|
* @return false if changed and vice versa
|
||||||
|
*/
|
||||||
virtual bool blogsChanged(std::list<std::string> &blogIds);
|
virtual bool blogsChanged(std::list<std::string> &blogIds);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates a new blog
|
||||||
|
*/
|
||||||
virtual std::string createBlog(std::wstring blogName, std::wstring blogDesc, uint32_t blogFlags);
|
virtual std::string createBlog(std::wstring blogName, std::wstring blogDesc, uint32_t blogFlags);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param cId the id for the blog
|
||||||
|
* @param BloogInfo blog information is stored here
|
||||||
|
*/
|
||||||
virtual bool getBlogInfo(std::string cId, BlogInfo &ci);
|
virtual bool getBlogInfo(std::string cId, BlogInfo &ci);
|
||||||
virtual bool getBlogList(std::list<BlogInfo> &chanList);
|
|
||||||
|
/**
|
||||||
|
* get list of blogs from the group peer belongs to
|
||||||
|
*/
|
||||||
|
virtual bool getBlogList(std::list<BlogInfo> &blogList);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of all the messages sent
|
||||||
|
* @param cId group id
|
||||||
|
* @param msgs
|
||||||
|
*/
|
||||||
virtual bool getBlogMsgList(std::string cId, std::list<BlogMsgSummary> &msgs);
|
virtual bool getBlogMsgList(std::string cId, std::list<BlogMsgSummary> &msgs);
|
||||||
virtual bool getBlogMessage(std::string cId, std::string mId, BlogMsgInfo &msg);
|
virtual bool getBlogMessage(std::string cId, std::string mId, BlogMsgInfo &msg);
|
||||||
|
|
||||||
virtual bool BlogMessageSend(BlogMsgInfo &info);
|
virtual bool BlogMessageSend(BlogMsgInfo &info);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* subscribes or unsubscribes peer to a blog message
|
||||||
|
* @param cId the id of the blog message peer wants to subscribe
|
||||||
|
* @param subscribe set to true to subscribe, false otherwise
|
||||||
|
*/
|
||||||
virtual bool blogSubscribe(std::string cId, bool subscribe);
|
virtual bool blogSubscribe(std::string cId, bool subscribe);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* send a reply to a blog msg, ensure msgIdReply has valid id to a message
|
||||||
|
*/
|
||||||
virtual bool BlogMessageReply(BlogMsgInfo &info);
|
virtual bool BlogMessageReply(BlogMsgInfo &info);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if a particular blog is a reply
|
||||||
|
* @return false if not a reply, true otherwise
|
||||||
|
*/
|
||||||
virtual bool isReply(BlogMsgInfo& info);
|
virtual bool isReply(BlogMsgInfo& info);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a blog by group id
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
virtual bool deleteBlog(std::string cId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a blog message
|
||||||
|
* @param cId the group id the blog message belongs to
|
||||||
|
* @param mId the message id within the group
|
||||||
|
*/
|
||||||
|
virtual bool deleteBlogMsg(std::string cId, std::string mId);
|
||||||
|
|
||||||
|
virtual bool isBlogDeleted(std::string cId);
|
||||||
|
|
||||||
|
virtual bool isBlogMsgDeleted(std::string cId, std::string mId);
|
||||||
/***************************************************************************************/
|
/***************************************************************************************/
|
||||||
/****************** Event Feedback (Overloaded form p3distrib) *************************/
|
/****************** Event Feedback (Overloaded form p3distrib) *************************/
|
||||||
/***************************************************************************************/
|
/***************************************************************************************/
|
||||||
|
@ -117,6 +117,10 @@ const uint32_t GROUP_KEY_DISTRIB_PRIVATE = 0x0020;
|
|||||||
const uint32_t GROUP_KEY_DISTRIB_ADMIN = 0x0040;
|
const uint32_t GROUP_KEY_DISTRIB_ADMIN = 0x0040;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* allows group members to access
|
||||||
|
*/
|
||||||
class GroupKey
|
class GroupKey
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -130,6 +134,8 @@ class GroupKey
|
|||||||
EVP_PKEY *key;
|
EVP_PKEY *key;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class GroupInfo
|
class GroupInfo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -36,6 +36,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#if defined(WIN32) || defined(__CYGWIN__)
|
#if defined(WIN32) || defined(__CYGWIN__)
|
||||||
#include "wtypes.h"
|
#include "wtypes.h"
|
||||||
|
Loading…
Reference in New Issue
Block a user