From 4bc603bbf3665ea523c4840438de70f2014e3e0c Mon Sep 17 00:00:00 2001 From: randomdan Date: Wed, 25 Apr 2012 18:43:27 +0000 Subject: Unlinking cache file invalidates cache (bug18) Fix possible race condition truncating cache file on update Correct config section name --- project2/files/presenterCache.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/project2/files/presenterCache.cpp b/project2/files/presenterCache.cpp index 8838568..e203fc4 100644 --- a/project2/files/presenterCache.cpp +++ b/project2/files/presenterCache.cpp @@ -16,6 +16,7 @@ SimpleSysCallException(OpenCacheFile); SimpleSysCallException(StatCacheFile); SimpleSysCallException(LockCacheFile); +SimpleSysCallException(TruncateCacheFile); SimpleSysCallException(CacheFileXAttr); template @@ -47,7 +48,12 @@ class FilePresenterCache : public PresenterCache, public StaticContent, public S readcachefd = safesys(-1, open(getCacheFile().string().c_str(), O_RDONLY)); } struct stat st; - safesys(-1, fstat(readcachefd, &st)); + safesys(-1, fstat(readcachefd, &st)); + if (st.st_nlink == 0) { + close(readcachefd); + readcachefd = 0; + readcachefd = safesys(-1, open(getCacheFile().string().c_str(), O_RDONLY)); + } if (st.st_mtime < time(NULL) - FilePresenterCache::CacheLife) { unlink(getCacheFile().string().c_str()); close(readcachefd); @@ -99,8 +105,9 @@ class FilePresenterCache : public PresenterCache, public StaticContent, public S std::ostream & writeCache(const std::string & ct, const std::string & enc) { int fd = safesys(-1, open(getCacheFile().string().c_str(), - O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)); + O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)); safesys(-1, ::flock(fd, LOCK_EX)); + safesys(-1, ::ftruncate(fd, 0)); safesys(-1, fsetxattr(fd, "user.content-type", ct.c_str(), ct.length(), 0)); safesys(-1, fsetxattr(fd, "user.encoding", enc.c_str(), enc.length(), 0)); writecache = new boost::iostreams::stream(fd, boost::iostreams::close_handle); @@ -148,7 +155,7 @@ time_t FilePresenterCache::CacheLife; class FilePresenterCacheLoader : public ElementLoader::For { public: FilePresenterCacheLoader() : - opts("XML Cache options") + opts("File Presenter Cache options") { opts ("pcache.file.store", Options::value(&FilePresenterCache::Store, "/tmp/project2.pcache"), -- cgit v1.2.3