summaryrefslogtreecommitdiff
path: root/project2/files
diff options
context:
space:
mode:
Diffstat (limited to 'project2/files')
-rw-r--r--project2/files/presenterCache.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/project2/files/presenterCache.cpp b/project2/files/presenterCache.cpp
index 76a4e86..789dd95 100644
--- a/project2/files/presenterCache.cpp
+++ b/project2/files/presenterCache.cpp
@@ -43,7 +43,9 @@ class FilePresenterCache : public PresenterCache, public StaticContent, public S
bool check() const
{
try {
- readcachefd = safesys<OpenCacheFile>(-1, open(getCacheFile().string().c_str(), O_RDONLY));
+ if (readcachefd == 0) {
+ readcachefd = safesys<OpenCacheFile>(-1, open(getCacheFile().string().c_str(), O_RDONLY));
+ }
struct stat st;
safesys<CacheFileXAttr>(-1, fstat(readcachefd, &st));
if (st.st_mtime < time(NULL) - FilePresenterCache::CacheLife) {
@@ -52,12 +54,13 @@ class FilePresenterCache : public PresenterCache, public StaticContent, public S
readcachefd = 0;
return false;
}
- safesys<LockCacheFile>(-1, ::flock(readcachefd, LOCK_SH));
return true;
}
catch (...) {
- close(readcachefd);
- readcachefd = 0;
+ if (readcachefd) {
+ close(readcachefd);
+ readcachefd = 0;
+ }
return false;
}
}
@@ -76,9 +79,11 @@ class FilePresenterCache : public PresenterCache, public StaticContent, public S
}
void writeTo(std::ostream & o) const
{
+ safesys<LockCacheFile>(-1, ::flock(readcachefd, LOCK_SH));
boost::iostreams::stream<boost::iostreams::file_descriptor_source> cache(readcachefd, boost::iostreams::never_close_handle);
cache.seekg(0);
o << cache.rdbuf();
+ safesys<LockCacheFile>(-1, ::flock(readcachefd, LOCK_UN));
}
operator const StaticContent * () const {
return this;
@@ -95,6 +100,11 @@ class FilePresenterCache : public PresenterCache, public StaticContent, public S
writecache = new boost::iostreams::stream<boost::iostreams::file_descriptor_sink>(fd, boost::iostreams::close_handle);
return *writecache;
}
+ void flushCache()
+ {
+ delete writecache;
+ writecache = NULL;
+ }
std::ostream * writecache;
private: