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/lyxlib.h"
26 #include "support/lyxtime.h"
27 #include "support/Package.h"
29 #include <boost/assert.hpp>
30 #include <boost/crc.hpp>
39 using namespace lyx::support;
45 unsigned long do_crc(string const & s)
47 boost::crc_32_type crc;
48 crc = for_each(s.begin(), s.end(), crc);
49 return crc.checksum();
53 static FileName cache_dir;
59 CacheItem(FileName const & orig_from, string const & to_format,
60 time_t t, unsigned long c)
61 : timestamp(t), checksum(c)
64 os << setw(10) << setfill('0') << do_crc(orig_from.absFilename())
66 cache_name = FileName(addName(cache_dir.absFilename(), os.str()));
67 LYXERR(Debug::FILES, "Add file cache item " << orig_from
68 << ' ' << to_format << ' ' << cache_name
69 << ' ' << long(timestamp) << ' ' << checksum << '.');
74 unsigned long checksum;
80 /** The cache contains one item per orig file and target format, so use a
81 * nested map to find the cache item quickly by filename and format.
83 typedef map<string, CacheItem> FormatCacheType;
86 /// Format of the source file
88 /// Cache target format -> item to quickly find the item by format
89 FormatCacheType cache;
91 typedef map<FileName, FormatCache> CacheType;
94 class ConverterCache::Impl {
101 CacheItem * find(FileName const & from, string const & format);
106 void ConverterCache::Impl::readIndex()
108 time_t const now = current_time();
109 FileName const index(addName(cache_dir.absFilename(), "index"));
110 ifstream is(index.toFilesystemEncoding().c_str());
116 string const orig_from = lex.getString();
119 string const to_format = lex.getString();
122 time_t const timestamp =
123 convert<unsigned long>(lex.getString());
126 unsigned long const checksum =
127 convert<unsigned long>(lex.getString());
128 FileName const orig_from_name(orig_from);
129 CacheItem item(orig_from_name, to_format, timestamp, checksum);
131 // Don't cache files that do not exist anymore
132 if (!orig_from_name.exists()) {
133 LYXERR(Debug::FILES, "Not caching file `"
134 << orig_from << "' (does not exist anymore).");
135 item.cache_name.removeFile();
139 // Don't add items that are not in the cache anymore
140 // This can happen if two instances of LyX are running
141 // at the same time and update the index file independantly.
142 if (!item.cache_name.exists()) {
143 LYXERR(Debug::FILES, "Not caching file `" << orig_from
144 << "' (cached copy does not exist anymore).");
148 // Delete the cached file if it is too old
149 if (difftime(now, item.cache_name.lastModified())
150 > lyxrc.converter_cache_maxage) {
151 LYXERR(Debug::FILES, "Not caching file `"
152 << orig_from << "' (too old).");
153 item.cache_name.removeFile();
157 FormatCache & format_cache = cache[orig_from_name];
158 if (format_cache.from_format.empty())
159 format_cache.from_format =
160 formats.getFormatFromFile(orig_from_name);
161 format_cache.cache[to_format] = item;
167 void ConverterCache::Impl::writeIndex()
169 FileName const index(addName(cache_dir.absFilename(), "index"));
170 ofstream os(index.toFilesystemEncoding().c_str());
172 if (!chmod(index, 0600))
174 os.open(index.toFilesystemEncoding().c_str());
175 CacheType::iterator it1 = cache.begin();
176 CacheType::iterator const end1 = cache.end();
177 for (; it1 != end1; ++it1) {
178 FormatCacheType const & format_cache = it1->second.cache;
179 FormatCacheType::const_iterator it2 = format_cache.begin();
180 FormatCacheType::const_iterator const end2 = format_cache.end();
181 for (; it2 != end2; ++it2)
182 os << Lexer::quoteString(it1->first.absFilename())
183 << ' ' << it2->first << ' '
184 << it2->second.timestamp << ' '
185 << it2->second.checksum << '\n';
191 CacheItem * ConverterCache::Impl::find(FileName const & from,
192 string const & format)
194 if (!lyxrc.use_converter_cache)
196 CacheType::iterator const it1 = cache.find(from);
197 if (it1 == cache.end())
199 FormatCacheType & format_cache = it1->second.cache;
200 FormatCacheType::iterator const it2 = format_cache.find(format);
201 if (it2 == format_cache.end())
203 return &(it2->second);
207 /////////////////////////////////////////////////////////////////////
211 /////////////////////////////////////////////////////////////////////
213 ConverterCache::ConverterCache()
218 ConverterCache::~ConverterCache()
220 if (!lyxrc.use_converter_cache)
222 pimpl_->writeIndex();
227 ConverterCache & ConverterCache::get()
229 // Now return the cache
230 static ConverterCache singleton;
235 void ConverterCache::init()
237 if (!lyxrc.use_converter_cache)
239 // We do this here and not in the constructor because package() gets
240 // initialized after all static variables.
241 cache_dir = FileName(addName(package().user_support().absFilename(), "cache"));
242 if (!cache_dir.exists())
243 if (support::mkdir(cache_dir, 0700) != 0) {
244 lyxerr << "Could not create cache directory `"
245 << cache_dir << "'." << endl;
248 get().pimpl_->readIndex();
252 static bool changeMode(FileName const & fname, unsigned long int mode)
254 if (mode == (unsigned long int)-1)
257 ofstream ofs(fname.toFilesystemEncoding().c_str(), ios::binary | ios::out | ios::trunc);
261 if (!chmod(fname, mode))
267 void ConverterCache::add(FileName const & orig_from, string const & to_format,
268 FileName const & converted_file) const
270 if (!lyxrc.use_converter_cache || orig_from.empty() ||
271 converted_file.empty())
273 LYXERR(Debug::FILES, ' ' << orig_from
274 << ' ' << to_format << ' ' << converted_file);
276 // FIXME: Should not hardcode this (see bug 3819 for details)
277 if (to_format == "pstex") {
278 FileName const converted_eps(changeExtension(converted_file.absFilename(), "eps"));
279 add(orig_from, "eps", converted_eps);
280 } else if (to_format == "pdftex") {
281 FileName const converted_pdf(changeExtension(converted_file.absFilename(), "pdf"));
282 add(orig_from, "pdf", converted_pdf);
285 // Is the file in the cache already?
286 CacheItem * item = pimpl_->find(orig_from, to_format);
288 time_t const timestamp = orig_from.lastModified();
289 Mover const & mover = getMover(to_format);
291 LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
292 "The file is already in the cache.");
293 // First test for timestamp
294 if (timestamp == item->timestamp) {
295 LYXERR(Debug::FILES, "Same timestamp.");
298 // Maybe the contents is still the same?
299 item->timestamp = timestamp;
300 unsigned long const checksum = orig_from.checksum();
301 if (checksum == item->checksum) {
302 LYXERR(Debug::FILES, "Same checksum.");
305 item->checksum = checksum;
306 if (!mover.copy(converted_file, item->cache_name,
307 onlyFilename(item->cache_name.absFilename()))) {
308 LYXERR(Debug::FILES, "Could not copy file " << orig_from << " to "
309 << item->cache_name);
310 } else if (!changeMode(item->cache_name, 0600)) {
311 LYXERR(Debug::FILES, "Could not change file mode"
312 << item->cache_name);
315 CacheItem new_item(orig_from, to_format, timestamp,
316 orig_from.checksum());
317 // FIXME: The original code used to chmod the new file to 600.
318 // See SpecialisedMover::do_copy().
319 if (mover.copy(converted_file, new_item.cache_name,
320 onlyFilename(new_item.cache_name.absFilename()))) {
321 if (!changeMode(item->cache_name, 0600)) {
322 LYXERR(Debug::FILES, "Could not change file mode"
323 << item->cache_name);
325 FormatCache & format_cache = pimpl_->cache[orig_from];
326 if (format_cache.from_format.empty())
327 format_cache.from_format =
328 formats.getFormatFromFile(orig_from);
329 format_cache.cache[to_format] = new_item;
331 LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
332 "Could not copy file.");
337 void ConverterCache::remove(FileName const & orig_from,
338 string const & to_format) const
340 if (!lyxrc.use_converter_cache || orig_from.empty())
342 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
344 CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
345 if (it1 == pimpl_->cache.end())
347 FormatCacheType & format_cache = it1->second.cache;
348 FormatCacheType::iterator const it2 = format_cache.find(to_format);
349 if (it2 == format_cache.end())
352 format_cache.erase(it2);
353 if (format_cache.empty())
354 pimpl_->cache.erase(it1);
358 void ConverterCache::remove_all(string const & from_format,
359 string const & to_format) const
361 if (!lyxrc.use_converter_cache)
363 CacheType::iterator it1 = pimpl_->cache.begin();
364 while (it1 != pimpl_->cache.end()) {
365 if (it1->second.from_format != from_format) {
369 FormatCacheType & format_cache = it1->second.cache;
370 FormatCacheType::iterator it2 = format_cache.begin();
371 while (it2 != format_cache.end()) {
372 if (it2->first == to_format) {
373 LYXERR(Debug::FILES, "Removing file cache item "
374 << it1->first << ' ' << to_format);
375 it2->second.cache_name.removeFile();
376 format_cache.erase(it2);
377 // Have to start over again since items in a
378 // map are not ordered
379 it2 = format_cache.begin();
384 if (format_cache.empty()) {
385 pimpl_->cache.erase(it1);
386 // Have to start over again since items in a map are
388 it1 = pimpl_->cache.begin();
393 pimpl_->writeIndex();
397 bool ConverterCache::inCache(FileName const & orig_from,
398 string const & to_format) const
400 if (!lyxrc.use_converter_cache || orig_from.empty())
402 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
404 CacheItem * const item = pimpl_->find(orig_from, to_format);
406 LYXERR(Debug::FILES, "not in cache.");
409 time_t const timestamp = orig_from.lastModified();
410 if (item->timestamp == timestamp) {
411 LYXERR(Debug::FILES, "identical timestamp.");
414 if (item->checksum == orig_from.checksum()) {
415 item->timestamp = timestamp;
416 LYXERR(Debug::FILES, "identical checksum.");
419 LYXERR(Debug::FILES, "in cache, but too old.");
424 FileName const & ConverterCache::cacheName(FileName const & orig_from,
425 string const & to_format) const
427 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
429 CacheItem * const item = pimpl_->find(orig_from, to_format);
431 return item->cache_name;
435 bool ConverterCache::copy(FileName const & orig_from, string const & to_format,
436 FileName const & dest) const
438 if (!lyxrc.use_converter_cache || orig_from.empty() || dest.empty())
440 LYXERR(Debug::FILES, orig_from << ' ' << to_format << ' ' << dest);
442 // FIXME: Should not hardcode this (see bug 3819 for details)
443 if (to_format == "pstex") {
444 FileName const dest_eps(changeExtension(dest.absFilename(), "eps"));
445 if (!copy(orig_from, "eps", dest_eps))
447 } else if (to_format == "pdftex") {
448 FileName const dest_pdf(changeExtension(dest.absFilename(), "pdf"));
449 if (!copy(orig_from, "pdf", dest_pdf))
453 CacheItem * const item = pimpl_->find(orig_from, to_format);
455 Mover const & mover = getMover(to_format);
456 return mover.copy(item->cache_name, dest,
457 onlyFilename(dest.absFilename()));