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 <boost/assert.hpp>
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();
52 static FileName cache_dir;
58 CacheItem(FileName const & orig_from, string const & to_format,
59 time_t t, unsigned long c)
60 : timestamp(t), checksum(c)
63 os << setw(10) << setfill('0') << do_crc(orig_from.absFilename())
65 cache_name = FileName(addName(cache_dir.absFilename(), os.str()));
66 LYXERR(Debug::FILES, "Add file cache item " << orig_from
67 << ' ' << to_format << ' ' << cache_name
68 << ' ' << long(timestamp) << ' ' << checksum << '.');
73 unsigned long checksum;
79 /** The cache contains one item per orig file and target format, so use a
80 * nested map to find the cache item quickly by filename and format.
82 typedef map<string, CacheItem> FormatCacheType;
85 /// Format of the source file
87 /// Cache target format -> item to quickly find the item by format
88 FormatCacheType cache;
90 typedef map<FileName, FormatCache> CacheType;
93 class ConverterCache::Impl {
100 CacheItem * find(FileName const & from, string const & format);
105 void ConverterCache::Impl::readIndex()
107 time_t const now = current_time();
108 FileName const index(addName(cache_dir.absFilename(), "index"));
109 ifstream is(index.toFilesystemEncoding().c_str());
115 string const orig_from = lex.getString();
118 string const to_format = lex.getString();
121 time_t const timestamp =
122 convert<unsigned long>(lex.getString());
125 unsigned long const checksum =
126 convert<unsigned long>(lex.getString());
127 FileName const orig_from_name(orig_from);
128 CacheItem item(orig_from_name, to_format, timestamp, checksum);
130 // Don't cache files that do not exist anymore
131 if (!orig_from_name.exists()) {
132 LYXERR(Debug::FILES, "Not caching file `"
133 << orig_from << "' (does not exist anymore).");
134 item.cache_name.removeFile();
138 // Don't add items that are not in the cache anymore
139 // This can happen if two instances of LyX are running
140 // at the same time and update the index file independantly.
141 if (!item.cache_name.exists()) {
142 LYXERR(Debug::FILES, "Not caching file `" << orig_from
143 << "' (cached copy does not exist anymore).");
147 // Delete the cached file if it is too old
148 if (difftime(now, item.cache_name.lastModified())
149 > lyxrc.converter_cache_maxage) {
150 LYXERR(Debug::FILES, "Not caching file `"
151 << orig_from << "' (too old).");
152 item.cache_name.removeFile();
156 FormatCache & format_cache = cache[orig_from_name];
157 if (format_cache.from_format.empty())
158 format_cache.from_format =
159 formats.getFormatFromFile(orig_from_name);
160 format_cache.cache[to_format] = item;
166 void ConverterCache::Impl::writeIndex()
168 FileName const index(addName(cache_dir.absFilename(), "index"));
169 ofstream os(index.toFilesystemEncoding().c_str());
171 if (!index.changePermission(0600))
173 os.open(index.toFilesystemEncoding().c_str());
174 CacheType::iterator it1 = cache.begin();
175 CacheType::iterator const end1 = cache.end();
176 for (; it1 != end1; ++it1) {
177 FormatCacheType const & format_cache = it1->second.cache;
178 FormatCacheType::const_iterator it2 = format_cache.begin();
179 FormatCacheType::const_iterator const end2 = format_cache.end();
180 for (; it2 != end2; ++it2)
181 os << Lexer::quoteString(it1->first.absFilename())
182 << ' ' << it2->first << ' '
183 << it2->second.timestamp << ' '
184 << it2->second.checksum << '\n';
190 CacheItem * ConverterCache::Impl::find(FileName const & from,
191 string const & format)
193 if (!lyxrc.use_converter_cache)
195 CacheType::iterator const it1 = cache.find(from);
196 if (it1 == cache.end())
198 FormatCacheType & format_cache = it1->second.cache;
199 FormatCacheType::iterator const it2 = format_cache.find(format);
200 if (it2 == format_cache.end())
202 return &(it2->second);
206 /////////////////////////////////////////////////////////////////////
210 /////////////////////////////////////////////////////////////////////
212 ConverterCache::ConverterCache()
217 ConverterCache::~ConverterCache()
219 if (!lyxrc.use_converter_cache)
221 pimpl_->writeIndex();
226 ConverterCache & ConverterCache::get()
228 // Now return the cache
229 static ConverterCache singleton;
234 void ConverterCache::init()
236 if (!lyxrc.use_converter_cache)
238 // We do this here and not in the constructor because package() gets
239 // initialized after all static variables.
240 cache_dir = FileName(addName(package().user_support().absFilename(), "cache"));
241 if (!cache_dir.exists())
242 if (!cache_dir.createDirectory(0700)) {
243 lyxerr << "Could not create cache directory `"
244 << cache_dir << "'." << endl;
247 get().pimpl_->readIndex();
251 void ConverterCache::add(FileName const & orig_from, string const & to_format,
252 FileName const & converted_file) const
254 if (!lyxrc.use_converter_cache || orig_from.empty() ||
255 converted_file.empty())
257 LYXERR(Debug::FILES, ' ' << orig_from
258 << ' ' << to_format << ' ' << converted_file);
260 // FIXME: Should not hardcode this (see bug 3819 for details)
261 if (to_format == "pstex") {
262 FileName const converted_eps(changeExtension(converted_file.absFilename(), "eps"));
263 add(orig_from, "eps", converted_eps);
264 } else if (to_format == "pdftex") {
265 FileName const converted_pdf(changeExtension(converted_file.absFilename(), "pdf"));
266 add(orig_from, "pdf", converted_pdf);
269 // Is the file in the cache already?
270 CacheItem * item = pimpl_->find(orig_from, to_format);
272 time_t const timestamp = orig_from.lastModified();
273 Mover const & mover = getMover(to_format);
275 LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
276 "The file is already in the cache.");
277 // First test for timestamp
278 if (timestamp == item->timestamp) {
279 LYXERR(Debug::FILES, "Same timestamp.");
282 // Maybe the contents is still the same?
283 item->timestamp = timestamp;
284 unsigned long const checksum = orig_from.checksum();
285 if (checksum == item->checksum) {
286 LYXERR(Debug::FILES, "Same checksum.");
289 item->checksum = checksum;
290 if (!mover.copy(converted_file, item->cache_name,
291 onlyFilename(item->cache_name.absFilename()))) {
292 LYXERR(Debug::FILES, "Could not copy file " << orig_from << " to "
293 << item->cache_name);
294 } else if (!item->cache_name.changePermission(0600)) {
295 LYXERR(Debug::FILES, "Could not change file mode"
296 << item->cache_name);
299 CacheItem new_item(orig_from, to_format, timestamp,
300 orig_from.checksum());
301 if (mover.copy(converted_file, new_item.cache_name,
302 onlyFilename(new_item.cache_name.absFilename()))) {
303 if (!new_item.cache_name.changePermission(0600)) {
304 LYXERR(Debug::FILES, "Could not change file mode"
305 << new_item.cache_name);
307 FormatCache & format_cache = pimpl_->cache[orig_from];
308 if (format_cache.from_format.empty())
309 format_cache.from_format =
310 formats.getFormatFromFile(orig_from);
311 format_cache.cache[to_format] = new_item;
313 LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
314 "Could not copy file.");
319 void ConverterCache::remove(FileName const & orig_from,
320 string const & to_format) const
322 if (!lyxrc.use_converter_cache || orig_from.empty())
324 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
326 CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
327 if (it1 == pimpl_->cache.end())
329 FormatCacheType & format_cache = it1->second.cache;
330 FormatCacheType::iterator const it2 = format_cache.find(to_format);
331 if (it2 == format_cache.end())
334 format_cache.erase(it2);
335 if (format_cache.empty())
336 pimpl_->cache.erase(it1);
340 void ConverterCache::remove_all(string const & from_format,
341 string const & to_format) const
343 if (!lyxrc.use_converter_cache)
345 CacheType::iterator it1 = pimpl_->cache.begin();
346 while (it1 != pimpl_->cache.end()) {
347 if (it1->second.from_format != from_format) {
351 FormatCacheType & format_cache = it1->second.cache;
352 FormatCacheType::iterator it2 = format_cache.begin();
353 while (it2 != format_cache.end()) {
354 if (it2->first == to_format) {
355 LYXERR(Debug::FILES, "Removing file cache item "
356 << it1->first << ' ' << to_format);
357 it2->second.cache_name.removeFile();
358 format_cache.erase(it2);
359 // Have to start over again since items in a
360 // map are not ordered
361 it2 = format_cache.begin();
366 if (format_cache.empty()) {
367 pimpl_->cache.erase(it1);
368 // Have to start over again since items in a map are
370 it1 = pimpl_->cache.begin();
375 pimpl_->writeIndex();
379 bool ConverterCache::inCache(FileName const & orig_from,
380 string const & to_format) const
382 if (!lyxrc.use_converter_cache || orig_from.empty())
384 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
386 CacheItem * const item = pimpl_->find(orig_from, to_format);
388 LYXERR(Debug::FILES, "not in cache.");
391 time_t const timestamp = orig_from.lastModified();
392 if (item->timestamp == timestamp) {
393 LYXERR(Debug::FILES, "identical timestamp.");
396 if (item->checksum == orig_from.checksum()) {
397 item->timestamp = timestamp;
398 LYXERR(Debug::FILES, "identical checksum.");
401 LYXERR(Debug::FILES, "in cache, but too old.");
406 FileName const & ConverterCache::cacheName(FileName const & orig_from,
407 string const & to_format) const
409 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
411 CacheItem * const item = pimpl_->find(orig_from, to_format);
413 return item->cache_name;
417 bool ConverterCache::copy(FileName const & orig_from, string const & to_format,
418 FileName const & dest) const
420 if (!lyxrc.use_converter_cache || orig_from.empty() || dest.empty())
422 LYXERR(Debug::FILES, orig_from << ' ' << to_format << ' ' << dest);
424 // FIXME: Should not hardcode this (see bug 3819 for details)
425 if (to_format == "pstex") {
426 FileName const dest_eps(changeExtension(dest.absFilename(), "eps"));
427 if (!copy(orig_from, "eps", dest_eps))
429 } else if (to_format == "pdftex") {
430 FileName const dest_pdf(changeExtension(dest.absFilename(), "pdf"));
431 if (!copy(orig_from, "pdf", dest_pdf))
435 CacheItem * const item = pimpl_->find(orig_from, to_format);
437 Mover const & mover = getMover(to_format);
438 return mover.copy(item->cache_name, dest,
439 onlyFilename(dest.absFilename()));