Rewritten CookieStorage using stdio instead of stream

pull/1/head
Tatsuhiro Tsujikawa 2011-08-05 22:44:54 +09:00
parent 292bbb3679
commit 168094560d
3 changed files with 36 additions and 21 deletions

View File

@ -35,8 +35,8 @@
#include "CookieStorage.h" #include "CookieStorage.h"
#include <cstring> #include <cstring>
#include <cstdio>
#include <algorithm> #include <algorithm>
#include <fstream>
#include "util.h" #include "util.h"
#include "LogFactory.h" #include "LogFactory.h"
@ -133,12 +133,17 @@ 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();
} }
void CookieStorage::DomainEntry::writeCookie(std::ostream& o) const bool CookieStorage::DomainEntry::writeCookie(FILE* 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) {
o << (*i).toNsCookieFormat() << "\n"; std::string data = (*i).toNsCookieFormat();
data += "\n";
if(fwrite(data.data(), 1, data.size(), fp) != data.size()) {
return false;
}
} }
return true;
} }
size_t CookieStorage::DomainEntry::countCookie() const size_t CookieStorage::DomainEntry::countCookie() const
@ -331,15 +336,17 @@ 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
std::ifstream s(filename.c_str(), std::ios::binary); FILE* fp = a2fopen(utf8ToWChar(filename).c_str(), "rb");
if(!s) { if(!fp) {
A2_LOG_ERROR(fmt("Failed to open cookie file %s", filename.c_str())); A2_LOG_ERROR(fmt("Failed to open cookie file %s",
utf8ToNative(filename).c_str()));
return false; return false;
} }
s.get(header, sizeof(header)); size_t r = fread(header, 1, sizeof(header), fp);
if(!s) { fclose(fp);
if(r != sizeof(header)) {
A2_LOG_ERROR(fmt("Failed to read header of cookie file %s", A2_LOG_ERROR(fmt("Failed to read header of cookie file %s",
filename.c_str())); utf8ToNative(filename).c_str()));
return false; return false;
} }
try { try {
@ -367,7 +374,8 @@ bool CookieStorage::load(const std::string& filename, time_t now)
} }
return true; return true;
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
A2_LOG_ERROR(fmt("Failed to load cookies from %s", filename.c_str())); A2_LOG_ERROR(fmt("Failed to load cookies from %s",
utf8ToNative(filename).c_str()));
return false; return false;
} }
} }
@ -376,18 +384,24 @@ bool CookieStorage::saveNsFormat(const std::string& filename)
{ {
std::string tempfilename = filename+"__temp"; std::string tempfilename = filename+"__temp";
{ {
std::ofstream o(tempfilename.c_str(), std::ios::binary); FILE* fp = a2fopen(utf8ToWChar(tempfilename).c_str(), "wb");
if(!o) { if(!fp) {
A2_LOG_ERROR(fmt("Cannot create cookie file %s", filename.c_str())); A2_LOG_ERROR(fmt("Cannot create cookie file %s",
utf8ToNative(filename).c_str()));
return false; return false;
} }
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) {
(*i).writeCookie(o); if(!(*i).writeCookie(fp)) {
fclose(fp);
A2_LOG_ERROR(fmt("Failed to save cookies to %s",
utf8ToNative(filename).c_str()));
return false;
}
} }
o.flush(); if(fclose(fp) == EOF) {
if(!o) { A2_LOG_ERROR(fmt("Failed to save cookies to %s",
A2_LOG_ERROR(fmt("Failed to save cookies to %s", filename.c_str())); utf8ToNative(filename).c_str()));
return false; return false;
} }
} }
@ -395,8 +409,8 @@ bool CookieStorage::saveNsFormat(const std::string& filename)
return true; return true;
} else { } else {
A2_LOG_ERROR(fmt("Could not rename file %s as %s", A2_LOG_ERROR(fmt("Could not rename file %s as %s",
tempfilename.c_str(), utf8ToNative(tempfilename).c_str(),
filename.c_str())); utf8ToNative(filename).c_str()));
return false; return false;
} }
} }

View File

@ -37,6 +37,7 @@
#include "common.h" #include "common.h"
#include <cstdio>
#include <string> #include <string>
#include <deque> #include <deque>
#include <vector> #include <vector>
@ -107,7 +108,7 @@ public:
return lastAccessTime_; return lastAccessTime_;
} }
void writeCookie(std::ostream& o) const; bool writeCookie(FILE* fp) const;
bool contains(const Cookie& cookie) const; bool contains(const Cookie& cookie) const;

View File

@ -356,7 +356,7 @@ void CookieStorageTest::testSaveNsFormat()
"/config",true)), now); "/config",true)), now);
st.store(Cookie(createCookie("uid", "tujikawa", now, "domain.org", true, st.store(Cookie(createCookie("uid", "tujikawa", now, "domain.org", true,
"/",false)), now); "/",false)), now);
st.saveNsFormat(filename); CPPUNIT_ASSERT(st.saveNsFormat(filename));
CookieStorage loadst; CookieStorage loadst;
loadst.load(filename, now); loadst.load(filename, now);
CPPUNIT_ASSERT_EQUAL((size_t)2, loadst.size()); CPPUNIT_ASSERT_EQUAL((size_t)2, loadst.size());