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"
23 #include "support/convert.h"
24 #include "support/filetools.h"
25 #include "support/lyxlib.h"
26 #include "support/lyxtime.h"
27 #include "support/Package.h"
29 #include <boost/assert.hpp>
30 #include <boost/crc.hpp>
31 #include <boost/current_function.hpp>
43 using support::FileName;
44 using support::addName;
48 unsigned long do_crc(string const & s)
50 boost::crc_32_type crc;
51 crc = std::for_each(s.begin(), s.end(), crc);
52 return crc.checksum();
56 static FileName cache_dir;
62 CacheItem(FileName const & orig_from, string const & to_format,
63 time_t t, unsigned long c)
64 : timestamp(t), checksum(c)
66 std::ostringstream os;
67 os << std::setw(10) << std::setfill('0') << do_crc(orig_from.absFilename())
69 cache_name = FileName(addName(cache_dir.absFilename(), os.str()));
70 LYXERR(Debug::FILES) << "Add file cache item " << orig_from
71 << ' ' << to_format << ' ' << cache_name
72 << ' ' << timestamp << ' ' << checksum
78 unsigned long checksum;
84 /** The cache contains one item per orig file and target format, so use a
85 * nested map to find the cache item quickly by filename and format.
87 typedef std::map<string, CacheItem> FormatCacheType;
90 /// Format of the source file
92 /// Cache target format -> item to quickly find the item by format
93 FormatCacheType cache;
95 typedef std::map<FileName, FormatCache> CacheType;
98 class ConverterCache::Impl {
105 CacheItem * find(FileName const & from, string const & format);
110 void ConverterCache::Impl::readIndex()
112 time_t const now = current_time();
113 FileName const index(addName(cache_dir.absFilename(), "index"));
114 std::ifstream is(index.toFilesystemEncoding().c_str());
120 string const orig_from = lex.getString();
123 string const to_format = lex.getString();
126 time_t const timestamp =
127 convert<unsigned long>(lex.getString());
130 unsigned long const checksum =
131 convert<unsigned long>(lex.getString());
132 FileName const orig_from_name(orig_from);
133 CacheItem item(orig_from_name, to_format, timestamp, checksum);
135 // Don't cache files that do not exist anymore
136 if (!orig_from_name.exists()) {
137 LYXERR(Debug::FILES) << "Not caching file `"
138 << orig_from << "' (does not exist anymore)."
140 support::unlink(item.cache_name);
144 // Don't add items that are not in the cache anymore
145 // This can happen if two instances of LyX are running
146 // at the same time and update the index file independantly.
147 if (!item.cache_name.exists()) {
148 LYXERR(Debug::FILES) << "Not caching file `"
150 << "' (cached copy does not exist anymore)."
155 // Delete the cached file if it is too old
156 if (difftime(now, item.cache_name.lastModified())
157 > lyxrc.converter_cache_maxage) {
158 LYXERR(Debug::FILES) << "Not caching file `"
159 << orig_from << "' (too old)." << std::endl;
160 support::unlink(item.cache_name);
164 FormatCache & format_cache = cache[orig_from_name];
165 if (format_cache.from_format.empty())
166 format_cache.from_format =
167 formats.getFormatFromFile(orig_from_name);
168 format_cache.cache[to_format] = item;
174 void ConverterCache::Impl::writeIndex()
176 FileName const index(addName(cache_dir.absFilename(), "index"));
177 std::ofstream os(index.toFilesystemEncoding().c_str());
179 if (!lyx::support::chmod(index, 0600))
181 os.open(index.toFilesystemEncoding().c_str());
182 CacheType::iterator it1 = cache.begin();
183 CacheType::iterator const end1 = cache.end();
184 for (; it1 != end1; ++it1) {
185 FormatCacheType const & format_cache = it1->second.cache;
186 FormatCacheType::const_iterator it2 = format_cache.begin();
187 FormatCacheType::const_iterator const end2 = format_cache.end();
188 for (; it2 != end2; ++it2)
189 os << Lexer::quoteString(it1->first.absFilename())
190 << ' ' << it2->first << ' '
191 << it2->second.timestamp << ' '
192 << it2->second.checksum << '\n';
198 CacheItem * ConverterCache::Impl::find(FileName const & from,
199 string const & format)
201 if (!lyxrc.use_converter_cache)
203 CacheType::iterator const it1 = cache.find(from);
204 if (it1 == cache.end())
206 FormatCacheType & format_cache = it1->second.cache;
207 FormatCacheType::iterator const it2 = format_cache.find(format);
208 if (it2 == format_cache.end())
210 return &(it2->second);
214 /////////////////////////////////////////////////////////////////////
218 /////////////////////////////////////////////////////////////////////
220 ConverterCache::ConverterCache()
225 ConverterCache::~ConverterCache()
227 if (!lyxrc.use_converter_cache)
229 pimpl_->writeIndex();
234 ConverterCache & ConverterCache::get()
236 // Now return the cache
237 static ConverterCache singleton;
242 void ConverterCache::init()
244 if (!lyxrc.use_converter_cache)
246 // We do this here and not in the constructor because package() gets
247 // initialized after all static variables.
248 cache_dir = FileName(addName(support::package().user_support().absFilename(), "cache"));
249 if (!cache_dir.exists())
250 if (support::mkdir(cache_dir, 0700) != 0) {
251 lyxerr << "Could not create cache directory `"
252 << cache_dir << "'." << std::endl;
255 get().pimpl_->readIndex();
259 void ConverterCache::add(FileName const & orig_from, string const & to_format,
260 FileName const & converted_file) const
262 if (!lyxrc.use_converter_cache || orig_from.empty() ||
263 converted_file.empty())
265 LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
266 << ' ' << to_format << ' ' << converted_file
269 // FIXME: Should not hardcode this (see bug 3819 for details)
270 if (to_format == "pstex") {
271 FileName const converted_eps(support::changeExtension(converted_file.absFilename(), "eps"));
272 add(orig_from, "eps", converted_eps);
273 } else if (to_format == "pdftex") {
274 FileName const converted_pdf(support::changeExtension(converted_file.absFilename(), "pdf"));
275 add(orig_from, "pdf", converted_pdf);
278 // Is the file in the cache already?
279 CacheItem * item = pimpl_->find(orig_from, to_format);
281 time_t const timestamp = orig_from.lastModified();
282 Mover const & mover = getMover(to_format);
284 LYXERR(Debug::FILES) << "ConverterCache::add(" << orig_from << "):\n"
285 "The file is already in the cache."
287 // First test for timestamp
288 if (timestamp == item->timestamp) {
289 LYXERR(Debug::FILES) << "Same timestamp."
293 // Maybe the contents is still the same?
294 item->timestamp = timestamp;
295 unsigned long const checksum = support::sum(orig_from);
296 if (checksum == item->checksum) {
297 LYXERR(Debug::FILES) << "Same checksum."
301 item->checksum = checksum;
303 if (!mover.copy(converted_file, item->cache_name,
304 support::onlyFilename(item->cache_name.absFilename()), 0600)) {
305 LYXERR(Debug::FILES) << "ConverterCache::add("
306 << orig_from << "):\n"
307 "Could not copy file."
311 CacheItem new_item(orig_from, to_format, timestamp,
312 support::sum(orig_from));
313 if (mover.copy(converted_file, new_item.cache_name,
314 support::onlyFilename(new_item.cache_name.absFilename()), 0600)) {
315 FormatCache & format_cache = pimpl_->cache[orig_from];
316 if (format_cache.from_format.empty())
317 format_cache.from_format =
318 formats.getFormatFromFile(orig_from);
319 format_cache.cache[to_format] = new_item;
321 LYXERR(Debug::FILES) << "ConverterCache::add("
322 << orig_from << "):\n"
323 "Could not copy file."
329 void ConverterCache::remove(FileName const & orig_from,
330 string const & to_format) const
332 if (!lyxrc.use_converter_cache || orig_from.empty())
334 LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
335 << ' ' << to_format << std::endl;
337 CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
338 if (it1 == pimpl_->cache.end())
340 FormatCacheType & format_cache = it1->second.cache;
341 FormatCacheType::iterator const it2 = format_cache.find(to_format);
342 if (it2 == format_cache.end())
345 format_cache.erase(it2);
346 if (format_cache.empty())
347 pimpl_->cache.erase(it1);
351 void ConverterCache::remove_all(string const & from_format,
352 string const & to_format) const
354 if (!lyxrc.use_converter_cache)
356 CacheType::iterator it1 = pimpl_->cache.begin();
357 while (it1 != pimpl_->cache.end()) {
358 if (it1->second.from_format != from_format) {
362 FormatCacheType & format_cache = it1->second.cache;
363 FormatCacheType::iterator it2 = format_cache.begin();
364 while (it2 != format_cache.end()) {
365 if (it2->first == to_format) {
367 << "Removing file cache item "
369 << ' ' << to_format << std::endl;
370 support::unlink(it2->second.cache_name);
371 format_cache.erase(it2);
372 // Have to start over again since items in a
373 // map are not ordered
374 it2 = format_cache.begin();
378 if (format_cache.empty()) {
379 pimpl_->cache.erase(it1);
380 // Have to start over again since items in a map are
382 it1 = pimpl_->cache.begin();
386 pimpl_->writeIndex();
390 bool ConverterCache::inCache(FileName const & orig_from,
391 string const & to_format) const
393 if (!lyxrc.use_converter_cache || orig_from.empty())
395 LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
396 << ' ' << to_format << std::endl;
398 CacheItem * const item = pimpl_->find(orig_from, to_format);
400 LYXERR(Debug::FILES) << "not in cache." << std::endl;
403 time_t const timestamp = orig_from.lastModified();
404 if (item->timestamp == timestamp) {
405 LYXERR(Debug::FILES) << "identical timestamp." << std::endl;
408 if (item->checksum == support::sum(orig_from)) {
409 item->timestamp = timestamp;
410 LYXERR(Debug::FILES) << "identical checksum." << std::endl;
413 LYXERR(Debug::FILES) << "in cache, but too old." << std::endl;
418 FileName const & ConverterCache::cacheName(FileName const & orig_from,
419 string const & to_format) const
421 LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
422 << ' ' << to_format << std::endl;
424 CacheItem * const item = pimpl_->find(orig_from, to_format);
426 return item->cache_name;
430 bool ConverterCache::copy(FileName const & orig_from, string const & to_format,
431 FileName const & dest) const
433 if (!lyxrc.use_converter_cache || orig_from.empty() || dest.empty())
435 LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
436 << ' ' << to_format << ' ' << dest << std::endl;
438 // FIXME: Should not hardcode this (see bug 3819 for details)
439 if (to_format == "pstex") {
440 FileName const dest_eps(support::changeExtension(dest.absFilename(), "eps"));
441 if (!copy(orig_from, "eps", dest_eps))
443 } else if (to_format == "pdftex") {
444 FileName const dest_pdf(support::changeExtension(dest.absFilename(), "pdf"));
445 if (!copy(orig_from, "pdf", dest_pdf))
449 CacheItem * const item = pimpl_->find(orig_from, to_format);
451 Mover const & mover = getMover(to_format);
452 return mover.copy(item->cache_name, dest,
453 support::onlyFilename(dest.absFilename()));