2009-12-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Added util::saveAs() function. Use it in Signature::save().
	* src/Signature.cc
	* src/util.cc
	* src/util.h
pull/1/head
Tatsuhiro Tsujikawa 2009-12-23 13:16:57 +00:00
parent 3aff2b565e
commit 9b933ca406
4 changed files with 35 additions and 17 deletions

View File

@ -1,3 +1,10 @@
2009-12-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added util::saveAs() function. Use it in Signature::save().
* src/Signature.cc
* src/util.cc
* src/util.h
2009-12-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Upcase info hash in Magnet URI.

View File

@ -37,6 +37,7 @@
#include <fstream>
#include "File.h"
#include "util.h"
namespace aria2 {
@ -61,23 +62,7 @@ void Signature::setBody(const std::string& body)
bool Signature::save(const std::string& filepath) const
{
if(File(filepath).exists()) {
return false;
}
std::string tempFilepath = filepath;
tempFilepath += "__temp";
{
std::ofstream out(tempFilepath.c_str(), std::ios::binary);
if(!out) {
return false;
}
out << _body;
out.flush();
if(!out) {
return false;
}
}
return File(tempFilepath).renameTo(filepath);
return util::saveAs(filepath, _body);
}
} // namespace aria2

View File

@ -981,6 +981,25 @@ void generateRandomData(unsigned char* data, size_t length)
#endif // HAVE_LIBSSL
}
bool saveAs
(const std::string& filename, const std::string& data, bool overwrite)
{
if(!overwrite && File(filename).exists()) {
return false;
}
std::string tempFilename = strconcat(filename, "__temp");
std::ofstream out(tempFilename.c_str(), std::ios::binary);
if(!out) {
return false;
}
out << data;
out.flush();
if(!out) {
return false;
}
return File(tempFilename).renameTo(filename);
}
} // namespace util
} // namespace aria2

View File

@ -348,6 +348,13 @@ OutputIterator split(const std::string& src, OutputIterator out,
void generateRandomData(unsigned char* data, size_t length);
// Saves data to file whose name is filename. If overwrite is true,
// existing file is overwritten. Otherwise, this function doesn't do
// nothing. If data is saved successfully, return true. Otherwise
// returns false.
bool saveAs
(const std::string& filename, const std::string& data, bool overwrite=false);
} // namespace util
} // namespace aria2