diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-08-05 22:44:54 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-08-05 22:44:54 +0900 |
| commit | 168094560deb5585b3f489e7583c96cd5d71f911 (patch) | |
| tree | df3356eea1b39c2470748e7b50c6778a36a209c1 | |
| parent | 292bbb3679a16c47926db384f75eab6fcc2785ff (diff) | |
Rewritten CookieStorage using stdio instead of stream
| -rw-r--r-- | src/CookieStorage.cc | 52 | ||||
| -rw-r--r-- | src/CookieStorage.h | 3 | ||||
| -rw-r--r-- | test/CookieStorageTest.cc | 2 |
3 files changed, 36 insertions, 21 deletions
diff --git a/src/CookieStorage.cc b/src/CookieStorage.cc index ee19cb98..b22336e1 100644 --- a/src/CookieStorage.cc +++ b/src/CookieStorage.cc @@ -35,8 +35,8 @@ #include "CookieStorage.h" #include <cstring> +#include <cstdio> #include <algorithm> -#include <fstream> #include "util.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(); } -void CookieStorage::DomainEntry::writeCookie(std::ostream& o) const +bool CookieStorage::DomainEntry::writeCookie(FILE* fp) const { for(std::deque<Cookie>::const_iterator i = cookies_.begin(), 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 @@ -331,15 +336,17 @@ size_t CookieStorage::size() const bool CookieStorage::load(const std::string& filename, time_t now) { char header[16]; // "SQLite format 3" plus \0 - std::ifstream s(filename.c_str(), std::ios::binary); - if(!s) { - A2_LOG_ERROR(fmt("Failed to open cookie file %s", filename.c_str())); + FILE* fp = a2fopen(utf8ToWChar(filename).c_str(), "rb"); + if(!fp) { + A2_LOG_ERROR(fmt("Failed to open cookie file %s", + utf8ToNative(filename).c_str())); return false; } - s.get(header, sizeof(header)); - if(!s) { + size_t r = fread(header, 1, sizeof(header), fp); + fclose(fp); + if(r != sizeof(header)) { A2_LOG_ERROR(fmt("Failed to read header of cookie file %s", - filename.c_str())); + utf8ToNative(filename).c_str())); return false; } try { @@ -367,7 +374,8 @@ bool CookieStorage::load(const std::string& filename, time_t now) } return true; } 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; } } @@ -376,18 +384,24 @@ bool CookieStorage::saveNsFormat(const std::string& filename) { std::string tempfilename = filename+"__temp"; { - std::ofstream o(tempfilename.c_str(), std::ios::binary); - if(!o) { - A2_LOG_ERROR(fmt("Cannot create cookie file %s", filename.c_str())); + FILE* fp = a2fopen(utf8ToWChar(tempfilename).c_str(), "wb"); + if(!fp) { + A2_LOG_ERROR(fmt("Cannot create cookie file %s", + utf8ToNative(filename).c_str())); return false; } for(std::deque<DomainEntry>::const_iterator i = domains_.begin(), 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(!o) { - A2_LOG_ERROR(fmt("Failed to save cookies to %s", filename.c_str())); + if(fclose(fp) == EOF) { + A2_LOG_ERROR(fmt("Failed to save cookies to %s", + utf8ToNative(filename).c_str())); return false; } } @@ -395,8 +409,8 @@ bool CookieStorage::saveNsFormat(const std::string& filename) return true; } else { A2_LOG_ERROR(fmt("Could not rename file %s as %s", - tempfilename.c_str(), - filename.c_str())); + utf8ToNative(tempfilename).c_str(), + utf8ToNative(filename).c_str())); return false; } } diff --git a/src/CookieStorage.h b/src/CookieStorage.h index b523dc3f..3dbe9927 100644 --- a/src/CookieStorage.h +++ b/src/CookieStorage.h @@ -37,6 +37,7 @@ #include "common.h" +#include <cstdio> #include <string> #include <deque> #include <vector> @@ -107,7 +108,7 @@ public: return lastAccessTime_; } - void writeCookie(std::ostream& o) const; + bool writeCookie(FILE* fp) const; bool contains(const Cookie& cookie) const; diff --git a/test/CookieStorageTest.cc b/test/CookieStorageTest.cc index f56f1807..48f407ed 100644 --- a/test/CookieStorageTest.cc +++ b/test/CookieStorageTest.cc @@ -356,7 +356,7 @@ void CookieStorageTest::testSaveNsFormat() "/config",true)), now); st.store(Cookie(createCookie("uid", "tujikawa", now, "domain.org", true, "/",false)), now); - st.saveNsFormat(filename); + CPPUNIT_ASSERT(st.saveNsFormat(filename)); CookieStorage loadst; loadst.load(filename, now); CPPUNIT_ASSERT_EQUAL((size_t)2, loadst.size()); |
