Secured the output of .cfg files and cache files. Now closing RS or serializing errors while saving these files cannot corrupt them anymore.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1090 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2009-03-20 23:37:06 +00:00
parent 207983a9e5
commit 631e3b303e
4 changed files with 50 additions and 32 deletions

View File

@ -23,10 +23,11 @@
*
*/
#include "rsiface/rspeers.h"
#include "pqi/p3cfgmgr.h"
#include "pqi/p3authmgr.h"
#include "pqi/pqibin.h"
#include "pqi/pqiarchive.h"
#include "pqi/pqistreamer.h"
#include "serialiser/rsconfigitems.h"
@ -398,10 +399,9 @@ bool p3Config::loadConfiguration(std::string &loadHash)
uint32_t stream_flags = BIN_FLAGS_READABLE;
BinInterface *bio = new BinFileInterface(fname.c_str(), bioflags);
pqistreamer stream(setupSerialiser(), "CONFIG", bio, stream_flags);
pqiarchive stream(setupSerialiser(), bio, stream_flags);
RsItem *item = NULL;
stream.tick();
while(NULL != (item = stream.GetItem()))
{
#ifdef CONFIG_DEBUG
@ -411,7 +411,6 @@ bool p3Config::loadConfiguration(std::string &loadHash)
std::cerr << std::endl;
#endif
load.push_back(item);
stream.tick();
}
#ifdef CONFIG_DEBUG
@ -455,7 +454,9 @@ bool p3Config::saveConfiguration()
std::list<RsItem *> toSave = saveList(cleanup);
std::string fname = Filename();
std::string fnametmp = Filename()+".tmp";
std::cerr << "Writting p3config file " << fname.c_str() << std::endl ;
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::saveConfiguration() toSave " << toSave.size();
std::cerr << " Elements to File: " << fname;
@ -463,14 +464,18 @@ bool p3Config::saveConfiguration()
#endif
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;
uint32_t stream_flags = BIN_FLAGS_WRITEABLE;
if (!cleanup)
stream_flags |= BIN_FLAGS_NO_DELETE;
BinInterface *bio = new BinFileInterface(fname.c_str(), bioflags);
pqistreamer stream(setupSerialiser(), "CONFIG", bio, stream_flags);
BinInterface *bio = new BinFileInterface(fnametmp.c_str(), bioflags);
pqiarchive *stream = new pqiarchive(setupSerialiser(), bio, stream_flags);
std::list<RsItem *>::iterator it;
bool written = true ;
for(it = toSave.begin(); it != toSave.end(); it++)
{
#ifdef CONFIG_DEBUG
@ -479,19 +484,31 @@ bool p3Config::saveConfiguration()
(*it)->print(std::cerr, 0);
std::cerr << std::endl;
#endif
stream.SendItem(*it);
stream.tick();
if( (*it)->PeerId().length() == 0 ) // this is required by pqiarchive.
(*it)->PeerId(rsPeers->getOwnId()) ;
written = written && stream->SendItem(*it);
// std::cerr << "written = " << written << std::endl ;
}
/* final tick for anymore writing */
stream.tick();
/* store the hash */
setHash(bio->gethash());
saveDone(); /* callback to inherited class to unlock any Mutexes
* protecting saveList() data
*/
delete stream ;
if(!written)
return false ;
std::cerr << "renaming " << fnametmp.c_str() << " to " << fname.c_str() << std::endl ;
if(0 != rename(fnametmp.c_str(),fname.c_str()))
return false ;
std::cerr << "Successfully wrote p3config file " << fname.c_str() << std::endl ;
/* else okay */
return true;
}

View File

@ -143,6 +143,8 @@ int pqiarchive::SendItem(RsItem *si)
return -1;
}
// std::cerr << "SendItem: si->PeerId()=" << si->PeerId() << std::endl ;
int ret = writePkt(si);
return ret; /* 0 - failure, 1 - success*/
}
@ -246,6 +248,7 @@ int pqiarchive::status()
int pqiarchive::writePkt(RsItem *pqi)
{
// std::cerr << "writePkt, pqi->peerId()=" << pqi->PeerId() << std::endl ;
{
std::ostringstream out;
out << "pqiarchive::writePkt()";
@ -310,6 +313,8 @@ int pqiarchive::writePkt(RsItem *pqi)
std::ostringstream out;
out << "pqiarchive::writePkt() Invalid peerId Length!";
out << std::endl;
out << "Found " << pqi->PeerId().length() << " instead of " << PQI_PEERID_LENGTH << std::endl ;
out << "pqi->PeerId() = " << pqi->PeerId() << std::endl ;
out << "Caused By: " << std::endl;
pqi -> print(out);
pqioutput(PQL_ALERT, pqiarchivezone, out.str());

View File

@ -500,7 +500,7 @@ int pqistreamer::handleoutgoing()
int pqistreamer::handleincoming()
{
int readbytes = 0;
static const int max_failed_read_attempts = 600 ;
static const int max_failed_read_attempts = 2000 ;
{
std::ostringstream out;
@ -557,7 +557,7 @@ start_packet_read:
// Most likely it is that the packet is pending but could not be read by pqissl because of stream flow.
// So we return without an error, and leave the machine state in 'start_read'.
//
pqioutput(PQL_WARNING, pqistreamerzone, "pqistreamer::handleincoming() Error in bio read");
//pqioutput(PQL_WARNING, pqistreamerzone, "pqistreamer::handleincoming() Error in bio read");
// std::cerr << "given up 2, state = " << reading_state << std::endl ;
return 0;
}
@ -702,6 +702,7 @@ continue_packet:
}
}
failed_read_attempts = 0 ;
readbytes += extralen;
}

View File

@ -265,38 +265,33 @@ bool p3MsgService::saveConfiguration()
*/
std::string msgfile = Filename();
std::string msgfiletmp = Filename()+".tmp";
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
RsSerialiser *rss = new RsSerialiser();
rss->addSerialType(new RsMsgSerialiser());
BinFileInterface *out = new BinFileInterface(msgfile.c_str(), BIN_FLAGS_WRITEABLE | BIN_FLAGS_HASH_DATA);
pqiarchive *pa_out = new pqiarchive(rss, out, BIN_FLAGS_WRITEABLE | BIN_FLAGS_NO_DELETE);
bool written = false;
BinFileInterface *out = new BinFileInterface(msgfiletmp.c_str(), BIN_FLAGS_WRITEABLE | BIN_FLAGS_HASH_DATA);
pqiarchive *pa_out = new pqiarchive(rss, out, BIN_FLAGS_WRITEABLE | BIN_FLAGS_NO_DELETE);
bool written = true;
std::map<uint32_t, RsMsgItem *>::iterator mit;
for(mit = imsg.begin(); mit != imsg.end(); mit++)
{
if (pa_out -> SendItem(mit->second))
{
written = true;
}
}
written = written && pa_out -> SendItem(mit->second) ;
for(mit = msgOutgoing.begin(); mit != msgOutgoing.end(); mit++)
{
if (pa_out -> SendItem(mit->second))
{
written = true;
}
}
written = written && pa_out -> SendItem(mit->second) ;
setHash(out->gethash());
delete pa_out;
if(!written)
return false ;
if(0 != rename(msgfiletmp.c_str(),msgfile.c_str()))
return false ;
return true;
}