2 * \file ConverterCache.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
15 #include "ConverterCache.h"
21 #include "support/filetools.h"
22 #include "support/lyxlib.h"
23 #include "support/lyxtime.h"
24 #include "support/Package.h"
26 #include <boost/crc.hpp>
27 #include <boost/filesystem/operations.hpp>
28 #include <boost/current_function.hpp>
35 using lyx::support::addName;
39 namespace fs = boost::filesystem;
43 using support::FileName;
47 unsigned long do_crc(string const & s)
49 boost::crc_32_type crc;
50 crc = std::for_each(s.begin(), s.end(), crc);
51 return crc.checksum();
55 static FileName cache_dir;
61 CacheItem(FileName const & orig_from, string const & to_format,
62 time_t t, unsigned long c)
63 : timestamp(t), checksum(c)
65 std::ostringstream os;
66 os << std::setw(10) << std::setfill('0') << do_crc(orig_from.absFilename())
68 cache_name = FileName(addName(cache_dir.absFilename(), os.str()));
69 LYXERR(Debug::FILES) << "Add file cache item " << orig_from
70 << ' ' << to_format << ' ' << cache_name
71 << ' ' << timestamp << ' ' << checksum
77 unsigned long checksum;
83 /** The cache contains one item per orig file and target format, so use a
84 * nested map to find the cache item quickly by filename and format.
86 typedef std::map<string, CacheItem> FormatCacheType;
87 typedef std::map<FileName, FormatCacheType> CacheType;
90 class ConverterCache::Impl {
97 CacheItem * find(FileName const & from, string const & format);
102 void ConverterCache::Impl::readIndex()
104 time_t const now = current_time();
105 FileName const index(addName(cache_dir.absFilename(), "index"));
106 std::ifstream is(index.toFilesystemEncoding().c_str());
111 unsigned long checksum;
112 if (!(is >> orig_from >> to_format >> timestamp >> checksum))
114 FileName const orig_from_name(orig_from);
115 CacheItem item(orig_from_name, to_format, timestamp, checksum);
117 // Don't cache files that do not exist anymore
118 if (!fs::exists(orig_from_name.toFilesystemEncoding())) {
119 LYXERR(Debug::FILES) << "Not caching file `"
120 << orig_from << "' (does not exist anymore)."
122 support::unlink(item.cache_name);
126 // Delete the cached file if it is too old
127 if (difftime(now, fs::last_write_time(item.cache_name.toFilesystemEncoding())) >
128 lyxrc.converter_cache_maxage) {
129 LYXERR(Debug::FILES) << "Not caching file `"
130 << orig_from << "' (too old)." << std::endl;
131 support::unlink(item.cache_name);
135 cache[orig_from_name][to_format] = item;
141 void ConverterCache::Impl::writeIndex()
143 FileName const index(addName(cache_dir.absFilename(), "index"));
144 std::ofstream os(index.toFilesystemEncoding().c_str());
146 if (!lyx::support::chmod(index, 0600))
148 os.open(index.toFilesystemEncoding().c_str());
149 CacheType::iterator it1 = cache.begin();
150 CacheType::iterator const end1 = cache.end();
151 for (; it1 != end1; ++it1) {
152 FormatCacheType::iterator it2 = it1->second.begin();
153 FormatCacheType::iterator const end2 = it1->second.end();
154 for (; it2 != end2; ++it2)
155 os << it1->first << ' ' << it2->first << ' '
156 << it2->second.timestamp << ' '
157 << it2->second.checksum << '\n';
163 CacheItem * ConverterCache::Impl::find(FileName const & from,
164 string const & format)
166 if (!lyxrc.use_converter_cache)
168 CacheType::iterator const it1 = cache.find(from);
169 if (it1 == cache.end())
171 FormatCacheType::iterator const it2 = it1->second.find(format);
172 if (it2 == it1->second.end())
174 return &(it2->second);
178 ConverterCache & ConverterCache::get()
180 // Now return the cache
181 static ConverterCache singleton;
186 void ConverterCache::init()
188 if (!lyxrc.use_converter_cache)
190 // We do this here and not in the constructor because package() gets
191 // initialized after all static variables.
192 cache_dir = FileName(addName(support::package().user_support().absFilename(), "cache"));
193 if (!fs::exists(cache_dir.toFilesystemEncoding()))
194 if (support::mkdir(cache_dir, 0700) != 0) {
195 lyxerr << "Could not create cache directory `"
196 << cache_dir << "'." << std::endl;
199 get().pimpl_->readIndex();
203 ConverterCache::ConverterCache()
208 ConverterCache::~ConverterCache()
210 if (!lyxrc.use_converter_cache)
212 pimpl_->writeIndex();
216 void ConverterCache::add(FileName const & orig_from, string const & to_format,
217 FileName const & converted_file) const
219 if (!lyxrc.use_converter_cache || orig_from.empty() ||
220 converted_file.empty())
222 LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
223 << ' ' << to_format << ' ' << converted_file
226 // Is the file in the cache already?
227 CacheItem * item = pimpl_->find(orig_from, to_format);
229 time_t const timestamp = fs::last_write_time(orig_from.toFilesystemEncoding());
230 Mover const & mover = getMover(to_format);
232 LYXERR(Debug::FILES) << "ConverterCache::add(" << orig_from << "):\n"
233 "The file is already in the cache."
235 // First test for timestamp
236 if (timestamp == item->timestamp) {
237 LYXERR(Debug::FILES) << "Same timestamp."
241 // Maybe the contents is still the same?
242 item->timestamp = timestamp;
243 unsigned long const checksum = support::sum(orig_from);
244 if (checksum == item->checksum) {
245 LYXERR(Debug::FILES) << "Same checksum."
249 item->checksum = checksum;
251 if (!mover.copy(converted_file, item->cache_name, 0600))
252 LYXERR(Debug::FILES) << "ConverterCache::add("
253 << orig_from << "):\n"
254 "Could not copy file."
257 CacheItem new_item = CacheItem(orig_from, to_format, timestamp,
258 support::sum(orig_from));
259 if (mover.copy(converted_file, new_item.cache_name, 0600))
260 pimpl_->cache[orig_from][to_format] = new_item;
262 LYXERR(Debug::FILES) << "ConverterCache::add("
263 << orig_from << "):\n"
264 "Could not copy file."
270 void ConverterCache::remove(FileName const & orig_from,
271 string const & to_format) const
273 if (!lyxrc.use_converter_cache || orig_from.empty())
275 LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
276 << ' ' << to_format << std::endl;
278 CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
279 if (it1 == pimpl_->cache.end())
281 FormatCacheType::iterator const it2 = it1->second.find(to_format);
282 if (it2 == it1->second.end())
285 it1->second.erase(it2);
286 if (it1->second.empty())
287 pimpl_->cache.erase(it1);
291 bool ConverterCache::inCache(FileName const & orig_from,
292 string const & to_format) const
294 if (!lyxrc.use_converter_cache || orig_from.empty())
296 LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
297 << ' ' << to_format << std::endl;
299 CacheItem * const item = pimpl_->find(orig_from, to_format);
301 LYXERR(Debug::FILES) << "not in cache." << std::endl;
304 time_t const timestamp = fs::last_write_time(orig_from.toFilesystemEncoding());
305 if (item->timestamp == timestamp) {
306 LYXERR(Debug::FILES) << "identical timestamp." << std::endl;
309 if (item->checksum == support::sum(orig_from)) {
310 item->timestamp = timestamp;
311 LYXERR(Debug::FILES) << "identical checksum." << std::endl;
314 LYXERR(Debug::FILES) << "in cache, but too old." << std::endl;
319 FileName const & ConverterCache::cacheName(FileName const & orig_from,
320 string const & to_format) const
322 LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
323 << ' ' << to_format << std::endl;
325 CacheItem * const item = pimpl_->find(orig_from, to_format);
327 return item->cache_name;
331 bool ConverterCache::copy(FileName const & orig_from, string const & to_format,
332 FileName const & dest) const
334 if (!lyxrc.use_converter_cache || orig_from.empty() || dest.empty())
336 LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
337 << ' ' << to_format << ' ' << dest << std::endl;
339 CacheItem * const item = pimpl_->find(orig_from, to_format);
341 Mover const & mover = getMover(to_format);
342 return mover.copy(item->cache_name, dest);