Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/pqi/p3cfgmgr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,25 +327,29 @@ bool p3Config::saveConfig()
std::string signFname = Filename() + ".sgn";

std::cerr << "(II) Saving configuration file " << cfgFname << std::endl;

uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;
uint32_t stream_flags = BIN_FLAGS_WRITEABLE;
bool written = true;

if (!cleanup)
stream_flags |= BIN_FLAGS_NO_DELETE;

BinEncryptedFileInterface *cfg_bio = new BinEncryptedFileInterface(newCfgFname.c_str(), bioflags);
pqiSSLstore *stream = new pqiSSLstore(setupSerialiser(), RsPeerId(), cfg_bio, stream_flags);
bool ok = false;

try
{
if(!stream->encryptedSendItems(toSave))
throw std::runtime_error("(EE) Error while writing config file " + Filename() + ": file dropped!!");
RsFileHash strHash;

// Write within a scope to auto-delete stream (and close the file while deleting cfg_bio) when finished, which should be done before
// trying to rename the files on windows.
{
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;

BinEncryptedFileInterface *cfg_bio = new BinEncryptedFileInterface(newCfgFname.c_str(), bioflags);

uint32_t stream_flags = BIN_FLAGS_WRITEABLE | (cleanup?0:BIN_FLAGS_NO_DELETE);

pqiSSLstore stream(setupSerialiser(), RsPeerId(), cfg_bio, stream_flags);

if(!stream.encryptedSendItems(toSave))
throw std::runtime_error("(EE) Error while writing config file " + Filename() + ": file dropped!!");

/* store the hash */
RsFileHash strHash(cfg_bio->gethash());
/* store the hash */
strHash = cfg_bio->gethash();
}

/* sign data */
std::string signature;
Expand All @@ -363,9 +367,9 @@ bool p3Config::saveConfig()

// now rewrite current files to temp files
// rename back-up to current file
if(!RsDirUtil::renameFile(cfgFname, tmpCfgFname))
if(RsDirUtil::fileExists(cfgFname) && !RsDirUtil::renameFile(cfgFname, tmpCfgFname))
throw std::runtime_error("p3Config::backedUpFileSave() Failed to rename backup meta files: " + cfgFname + " to " + tmpCfgFname);
if(!RsDirUtil::renameFile(signFname, tmpSignFname))
if(RsDirUtil::fileExists(signFname) && !RsDirUtil::renameFile(signFname, tmpSignFname))
throw std::runtime_error("p3Config::backedUpFileSave() Failed to rename backup meta files: " + signFname + " to " + tmpSignFname);

// now rewrite current files to temp files; rename back-up to current file
Expand All @@ -383,7 +387,6 @@ bool p3Config::saveConfig()
RsErr() << e.what();
ok = false;
}
delete stream; // bio is taken care of in stream's destructor, also forces file to close

saveDone(); // callback to inherited class to unlock any Mutexes protecting saveList() data
return ok;
Expand Down
Loading