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"
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/lyxtime.h"
26 #include "support/Package.h"
28 #include "support/lassert.h"
29 #include <boost/crc.hpp>
38 using namespace lyx::support;
44 unsigned long do_crc(string const & s)
46 boost::crc_32_type crc;
47 crc = for_each(s.begin(), s.end(), crc);
48 return crc.checksum();
53 // This should be OK because it is only assigned during init()
54 static FileName cache_dir;
59 CacheItem() : timestamp(0), checksum(0) {}
60 CacheItem(FileName const & orig_from, string const & to_format,
61 time_t t, unsigned long c)
62 : timestamp(t), checksum(c)
65 os << setw(10) << setfill('0') << do_crc(orig_from.absFileName())
67 cache_name = FileName(addName(cache_dir.absFileName(), os.str()));
68 LYXERR(Debug::FILES, "Add file cache item " << orig_from
69 << ' ' << to_format << ' ' << cache_name
70 << ' ' << long(timestamp) << ' ' << checksum << '.');
76 unsigned long checksum;
82 /** The cache contains one item per orig file and target format, so use a
83 * nested map to find the cache item quickly by filename and format.
85 typedef map<string, CacheItem> FormatCacheType;
88 /// Format of the source file
90 /// Cache target format -> item to quickly find the item by format
91 FormatCacheType cache;
93 typedef map<FileName, FormatCache> CacheType;
96 class ConverterCache::Impl {
103 CacheItem * find(FileName const & from, string const & format);
108 void ConverterCache::Impl::readIndex()
110 time_t const now = current_time();
111 FileName const index(addName(cache_dir.absFileName(), "index"));
112 ifstream is(index.toFilesystemEncoding().c_str());
118 string const orig_from = lex.getString();
121 string const to_format = lex.getString();
124 time_t const timestamp =
125 convert<unsigned long>(lex.getString());
128 unsigned long const checksum =
129 convert<unsigned long>(lex.getString());
130 FileName const orig_from_name(orig_from);
131 CacheItem item(orig_from_name, to_format, timestamp, checksum);
133 // Don't cache files that do not exist anymore
134 if (!orig_from_name.exists()) {
135 LYXERR(Debug::FILES, "Not caching file `"
136 << orig_from << "' (does not exist anymore).");
137 item.cache_name.removeFile();
141 // Don't add items that are not in the cache anymore
142 // This can happen if two instances of LyX are running
143 // at the same time and update the index file independantly.
144 if (!item.cache_name.exists()) {
145 LYXERR(Debug::FILES, "Not caching file `" << orig_from
146 << "' (cached copy does not exist anymore).");
150 // Delete the cached file if it is too old
151 if (difftime(now, item.cache_name.lastModified())
152 > lyxrc.converter_cache_maxage) {
153 LYXERR(Debug::FILES, "Not caching file `"
154 << orig_from << "' (too old).");
155 item.cache_name.removeFile();
159 FormatCache & format_cache = cache[orig_from_name];
160 if (format_cache.from_format.empty())
161 format_cache.from_format =
162 // FIXME perf: This very expensive function is called on all
163 // cached files on opening. This slows LyX startup a lot. It
164 // would be better if this information was retrieved in a
166 theFormats().getFormatFromFile(orig_from_name);
167 format_cache.cache[to_format] = item;
173 void ConverterCache::Impl::writeIndex()
175 FileName const index(addName(cache_dir.absFileName(), "index"));
176 ofstream os(index.toFilesystemEncoding().c_str());
178 if (!index.changePermission(0600))
180 os.open(index.toFilesystemEncoding().c_str());
181 CacheType::iterator it1 = cache.begin();
182 CacheType::iterator const end1 = cache.end();
183 for (; it1 != end1; ++it1) {
184 FormatCacheType const & format_cache = it1->second.cache;
185 FormatCacheType::const_iterator it2 = format_cache.begin();
186 FormatCacheType::const_iterator const end2 = format_cache.end();
187 for (; it2 != end2; ++it2)
188 os << Lexer::quoteString(it1->first.absFileName())
189 << ' ' << it2->first << ' '
190 << it2->second.timestamp << ' '
191 << it2->second.checksum << '\n';
197 CacheItem * ConverterCache::Impl::find(FileName const & from,
198 string const & format)
200 if (!lyxrc.use_converter_cache)
202 CacheType::iterator const it1 = cache.find(from);
203 if (it1 == cache.end())
205 FormatCacheType & format_cache = it1->second.cache;
206 FormatCacheType::iterator const it2 = format_cache.find(format);
207 if (it2 == format_cache.end())
209 return &(it2->second);
213 /////////////////////////////////////////////////////////////////////
217 /////////////////////////////////////////////////////////////////////
219 ConverterCache::ConverterCache()
224 ConverterCache::~ConverterCache()
230 ConverterCache & ConverterCache::get()
232 // Now return the cache
233 static ConverterCache singleton;
238 void ConverterCache::init()
240 if (!lyxrc.use_converter_cache)
242 // We do this here and not in the constructor because package() gets
243 // initialized after all static variables.
244 cache_dir = FileName(addName(package().user_support().absFileName(), "cache"));
245 if (!cache_dir.exists())
246 if (!cache_dir.createDirectory(0700)) {
247 // FIXME This should really be displayed as a message. But the GUI
248 // does not exist yet.
249 lyxerr << "Could not create cache directory `"
250 << cache_dir << "'." << endl;
253 get().pimpl_->readIndex();
257 void ConverterCache::writeIndex() const
259 if (!lyxrc.use_converter_cache
260 || cache_dir.empty())
262 pimpl_->writeIndex();
266 void ConverterCache::add(FileName const & orig_from, string const & to_format,
267 FileName const & converted_file) const
269 if (!lyxrc.use_converter_cache || orig_from.empty() ||
270 converted_file.empty())
272 LYXERR(Debug::FILES, ' ' << orig_from
273 << ' ' << to_format << ' ' << converted_file);
275 // FIXME: Should not hardcode this (see bug 3819 for details)
276 if (to_format == "pstex") {
277 FileName const converted_eps(changeExtension(converted_file.absFileName(), "eps"));
278 add(orig_from, "eps", converted_eps);
279 } else if (to_format == "pdftex") {
280 FileName const converted_pdf(changeExtension(converted_file.absFileName(), "pdf"));
281 add(orig_from, "pdf6", converted_pdf);
284 // Is the file in the cache already?
285 CacheItem * item = pimpl_->find(orig_from, to_format);
287 time_t const timestamp = orig_from.lastModified();
288 Mover const & mover = getMover(to_format);
290 LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
291 "The file is already in the cache.");
292 // First test for timestamp
293 if (timestamp == item->timestamp) {
294 LYXERR(Debug::FILES, "Same timestamp.");
297 // Maybe the contents is still the same?
298 item->timestamp = timestamp;
299 unsigned long const checksum = orig_from.checksum();
300 if (checksum == item->checksum) {
301 LYXERR(Debug::FILES, "Same checksum.");
304 item->checksum = checksum;
305 if (!mover.copy(converted_file, item->cache_name,
306 onlyFileName(item->cache_name.absFileName()))) {
307 LYXERR(Debug::FILES, "Could not copy file " << orig_from << " to "
308 << item->cache_name);
309 } else if (!item->cache_name.changePermission(0600)) {
310 LYXERR(Debug::FILES, "Could not change file mode"
311 << item->cache_name);
314 CacheItem new_item(orig_from, to_format, timestamp,
315 orig_from.checksum());
316 if (mover.copy(converted_file, new_item.cache_name,
317 onlyFileName(new_item.cache_name.absFileName()))) {
318 if (!new_item.cache_name.changePermission(0600)) {
319 LYXERR(Debug::FILES, "Could not change file mode"
320 << new_item.cache_name);
322 FormatCache & format_cache = pimpl_->cache[orig_from];
323 if (format_cache.from_format.empty())
324 format_cache.from_format =
325 theFormats().getFormatFromFile(orig_from);
326 format_cache.cache[to_format] = new_item;
328 LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
329 "Could not copy file.");
334 void ConverterCache::remove(FileName const & orig_from,
335 string const & to_format) const
337 if (!lyxrc.use_converter_cache || orig_from.empty())
339 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
341 CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
342 if (it1 == pimpl_->cache.end())
344 FormatCacheType & format_cache = it1->second.cache;
345 FormatCacheType::iterator const it2 = format_cache.find(to_format);
346 if (it2 == format_cache.end())
349 format_cache.erase(it2);
350 if (format_cache.empty())
351 pimpl_->cache.erase(it1);
355 void ConverterCache::remove_all(string const & from_format,
356 string const & to_format) const
358 if (!lyxrc.use_converter_cache)
360 CacheType::iterator it1 = pimpl_->cache.begin();
361 while (it1 != pimpl_->cache.end()) {
362 if (it1->second.from_format != from_format) {
366 FormatCacheType & format_cache = it1->second.cache;
367 FormatCacheType::iterator it2 = format_cache.begin();
368 while (it2 != format_cache.end()) {
369 if (it2->first == to_format) {
370 LYXERR(Debug::FILES, "Removing file cache item "
371 << it1->first << ' ' << to_format);
372 it2->second.cache_name.removeFile();
373 format_cache.erase(it2);
374 // Have to start over again since items in a
375 // map are not ordered
376 it2 = format_cache.begin();
381 if (format_cache.empty()) {
382 pimpl_->cache.erase(it1);
383 // Have to start over again since items in a map are
385 it1 = pimpl_->cache.begin();
390 pimpl_->writeIndex();
394 bool ConverterCache::inCache(FileName const & orig_from,
395 string const & to_format) const
397 if (!lyxrc.use_converter_cache || orig_from.empty())
399 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
401 CacheItem * const item = pimpl_->find(orig_from, to_format);
403 LYXERR(Debug::FILES, "not in cache.");
407 // Special handling of pstex and pdftex formats: These are only
408 // considered to be in the cache if the corresponding graphics
409 // fiels are there as well. Otherwise copy() of the graphics below
411 // FIXME: Should not hardcode this (see bug 3819 for details)
412 if (to_format == "pstex") {
413 if (!inCache(orig_from, "eps"))
415 } else if (to_format == "pdftex") {
416 if (!inCache(orig_from, "pdf6"))
420 time_t const timestamp = orig_from.lastModified();
421 if (item->timestamp == timestamp) {
422 LYXERR(Debug::FILES, "identical timestamp.");
425 if (item->checksum == orig_from.checksum()) {
426 item->timestamp = timestamp;
427 LYXERR(Debug::FILES, "identical checksum.");
430 LYXERR(Debug::FILES, "in cache, but too old.");
435 FileName const & ConverterCache::cacheName(FileName const & orig_from,
436 string const & to_format) const
438 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
440 CacheItem * const item = pimpl_->find(orig_from, to_format);
441 LASSERT(item, { static const FileName fn; return fn; });
442 return item->cache_name;
446 bool ConverterCache::copy(FileName const & orig_from, string const & to_format,
447 FileName const & dest) const
449 if (!lyxrc.use_converter_cache || orig_from.empty() || dest.empty())
451 LYXERR(Debug::FILES, orig_from << ' ' << to_format << ' ' << dest);
453 // FIXME: Should not hardcode this (see bug 3819 for details)
454 if (to_format == "pstex") {
455 FileName const dest_eps(changeExtension(dest.absFileName(), "eps"));
456 if (!copy(orig_from, "eps", dest_eps))
458 } else if (to_format == "pdftex") {
459 FileName const dest_pdf(changeExtension(dest.absFileName(), "pdf"));
460 if (!copy(orig_from, "pdf6", dest_pdf))
464 CacheItem * const item = pimpl_->find(orig_from, to_format);
465 LASSERT(item, return false);
466 Mover const & mover = getMover(to_format);
467 return mover.copy(item->cache_name, dest,
468 onlyFileName(dest.absFileName()));