mirror of https://github.com/aria2/aria2
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.hpull/1/head
parent
3aff2b565e
commit
9b933ca406
|
@ -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>
|
2009-12-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Upcase info hash in Magnet URI.
|
Upcase info hash in Magnet URI.
|
||||||
|
|
|
@ -37,6 +37,7 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
#include "File.h"
|
#include "File.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -61,23 +62,7 @@ void Signature::setBody(const std::string& body)
|
||||||
|
|
||||||
bool Signature::save(const std::string& filepath) const
|
bool Signature::save(const std::string& filepath) const
|
||||||
{
|
{
|
||||||
if(File(filepath).exists()) {
|
return util::saveAs(filepath, _body);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
19
src/util.cc
19
src/util.cc
|
@ -981,6 +981,25 @@ void generateRandomData(unsigned char* data, size_t length)
|
||||||
#endif // HAVE_LIBSSL
|
#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 util
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -348,6 +348,13 @@ OutputIterator split(const std::string& src, OutputIterator out,
|
||||||
|
|
||||||
void generateRandomData(unsigned char* data, size_t length);
|
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 util
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
Loading…
Reference in New Issue