Rewritten CookieStorage using BufferedFile

pull/1/head
Tatsuhiro Tsujikawa 2011-08-06 21:53:59 +09:00
parent 410d88710b
commit c9f8cf75bf
2 changed files with 18 additions and 19 deletions

View File

@ -49,6 +49,7 @@
#include "A2STR.h" #include "A2STR.h"
#include "message.h" #include "message.h"
#include "cookie_helper.h" #include "cookie_helper.h"
#include "BufferedFile.h"
#ifdef HAVE_SQLITE3 #ifdef HAVE_SQLITE3
# include "Sqlite3CookieParserImpl.h" # include "Sqlite3CookieParserImpl.h"
#endif // HAVE_SQLITE3 #endif // HAVE_SQLITE3
@ -133,13 +134,13 @@ bool CookieStorage::DomainEntry::contains(const Cookie& cookie) const
return std::find(cookies_.begin(), cookies_.end(), cookie) != cookies_.end(); return std::find(cookies_.begin(), cookies_.end(), cookie) != cookies_.end();
} }
bool CookieStorage::DomainEntry::writeCookie(FILE* fp) const bool CookieStorage::DomainEntry::writeCookie(BufferedFile& fp) const
{ {
for(std::deque<Cookie>::const_iterator i = cookies_.begin(), for(std::deque<Cookie>::const_iterator i = cookies_.begin(),
eoi = cookies_.end(); i != eoi; ++i) { eoi = cookies_.end(); i != eoi; ++i) {
std::string data = (*i).toNsCookieFormat(); std::string data = (*i).toNsCookieFormat();
data += "\n"; data += "\n";
if(fwrite(data.data(), 1, data.size(), fp) != data.size()) { if(fp.write(data.data(), data.size()) != data.size()) {
return false; return false;
} }
} }
@ -336,21 +337,19 @@ size_t CookieStorage::size() const
bool CookieStorage::load(const std::string& filename, time_t now) bool CookieStorage::load(const std::string& filename, time_t now)
{ {
char header[16]; // "SQLite format 3" plus \0 char header[16]; // "SQLite format 3" plus \0
FILE* fp = a2fopen(utf8ToWChar(filename).c_str(), "rb"); size_t headlen;
if(!fp) { {
A2_LOG_ERROR(fmt("Failed to open cookie file %s", BufferedFile fp(filename, BufferedFile::READ);
if(!fp) {
A2_LOG_ERROR(fmt("Failed to open cookie file %s",
utf8ToNative(filename).c_str())); utf8ToNative(filename).c_str()));
return false; return false;
} }
size_t r = fread(header, 1, sizeof(header), fp); headlen = fp.read(header, sizeof(header));
fclose(fp);
if(r != sizeof(header)) {
A2_LOG_ERROR(fmt("Failed to read header of cookie file %s",
utf8ToNative(filename).c_str()));
return false;
} }
try { try {
if(std::string(header) == "SQLite format 3") { if(headlen &&
std::string(&header[0], &header[headlen-1]) == "SQLite format 3") {
#ifdef HAVE_SQLITE3 #ifdef HAVE_SQLITE3
std::vector<Cookie> cookies; std::vector<Cookie> cookies;
try { try {
@ -384,7 +383,7 @@ bool CookieStorage::saveNsFormat(const std::string& filename)
{ {
std::string tempfilename = filename+"__temp"; std::string tempfilename = filename+"__temp";
{ {
FILE* fp = a2fopen(utf8ToWChar(tempfilename).c_str(), "wb"); BufferedFile fp(tempfilename, BufferedFile::WRITE);
if(!fp) { if(!fp) {
A2_LOG_ERROR(fmt("Cannot create cookie file %s", A2_LOG_ERROR(fmt("Cannot create cookie file %s",
utf8ToNative(filename).c_str())); utf8ToNative(filename).c_str()));
@ -393,13 +392,12 @@ bool CookieStorage::saveNsFormat(const std::string& filename)
for(std::deque<DomainEntry>::const_iterator i = domains_.begin(), for(std::deque<DomainEntry>::const_iterator i = domains_.begin(),
eoi = domains_.end(); i != eoi; ++i) { eoi = domains_.end(); i != eoi; ++i) {
if(!(*i).writeCookie(fp)) { if(!(*i).writeCookie(fp)) {
fclose(fp);
A2_LOG_ERROR(fmt("Failed to save cookies to %s", A2_LOG_ERROR(fmt("Failed to save cookies to %s",
utf8ToNative(filename).c_str())); utf8ToNative(filename).c_str()));
return false; return false;
} }
} }
if(fclose(fp) == EOF) { if(fp.close() == EOF) {
A2_LOG_ERROR(fmt("Failed to save cookies to %s", A2_LOG_ERROR(fmt("Failed to save cookies to %s",
utf8ToNative(filename).c_str())); utf8ToNative(filename).c_str()));
return false; return false;

View File

@ -37,7 +37,6 @@
#include "common.h" #include "common.h"
#include <cstdio>
#include <string> #include <string>
#include <deque> #include <deque>
#include <vector> #include <vector>
@ -48,6 +47,8 @@
namespace aria2 { namespace aria2 {
class BufferedFile;
class CookieStorage { class CookieStorage {
public: public:
@ -108,7 +109,7 @@ public:
return lastAccessTime_; return lastAccessTime_;
} }
bool writeCookie(FILE* fp) const; bool writeCookie(BufferedFile& fp) const;
bool contains(const Cookie& cookie) const; bool contains(const Cookie& cookie) const;