summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--project2/files/presenterCache.cpp13
1 files 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 <class E>
@@ -47,7 +48,12 @@ class FilePresenterCache : public PresenterCache, public StaticContent, public S
readcachefd = safesys<OpenCacheFile>(-1, open(getCacheFile().string().c_str(), O_RDONLY));
}
struct stat st;
- safesys<CacheFileXAttr>(-1, fstat(readcachefd, &st));
+ safesys<StatCacheFile>(-1, fstat(readcachefd, &st));
+ if (st.st_nlink == 0) {
+ close(readcachefd);
+ readcachefd = 0;
+ readcachefd = safesys<OpenCacheFile>(-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<OpenCacheFile>(-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<LockCacheFile>(-1, ::flock(fd, LOCK_EX));
+ safesys<TruncateCacheFile>(-1, ::ftruncate(fd, 0));
safesys<CacheFileXAttr>(-1, fsetxattr(fd, "user.content-type", ct.c_str(), ct.length(), 0));
safesys<CacheFileXAttr>(-1, fsetxattr(fd, "user.encoding", enc.c_str(), enc.length(), 0));
writecache = new boost::iostreams::stream<boost::iostreams::file_descriptor_sink>(fd, boost::iostreams::close_handle);
@@ -148,7 +155,7 @@ time_t FilePresenterCache::CacheLife;
class FilePresenterCacheLoader : public ElementLoader::For<FilePresenterCache> {
public:
FilePresenterCacheLoader() :
- opts("XML Cache options")
+ opts("File Presenter Cache options")
{
opts
("pcache.file.store", Options::value(&FilePresenterCache::Store, "/tmp/project2.pcache"),