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/checksum.h"
29 #include "support/lassert.h"
38 using namespace lyx::support;
45 // This should be OK because it is only assigned during init()
46 static FileName cache_dir;
51 CacheItem() : timestamp(0), checksum(0) {}
52 CacheItem(FileName const & orig_from, string const & to_format,
53 time_t t, unsigned long c)
54 : timestamp(t), checksum(c)
57 os << setw(10) << setfill('0')
58 << support::checksum(orig_from.absFileName())
60 cache_name = FileName(addName(cache_dir.absFileName(), os.str()));
61 LYXERR(Debug::FILES, "Add file cache item " << orig_from
62 << ' ' << to_format << ' ' << cache_name
63 << ' ' << long(timestamp) << ' ' << checksum << '.');
69 unsigned long checksum;
75 /** The cache contains one item per orig file and target format, so use a
76 * nested map to find the cache item quickly by filename and format.
78 typedef map<string, CacheItem> FormatCacheType;
81 /// Format of the source file
83 /// Cache target format -> item to quickly find the item by format
84 FormatCacheType cache;
86 typedef map<FileName, FormatCache> CacheType;
89 class ConverterCache::Impl {
96 CacheItem * find(FileName const & from, string const & format);
101 void ConverterCache::Impl::readIndex()
103 time_t const now = current_time();
104 FileName const index(addName(cache_dir.absFileName(), "index"));
105 ifstream is(index.toFilesystemEncoding().c_str());
111 string const orig_from = lex.getString();
114 string const to_format = lex.getString();
117 time_t const timestamp =
118 convert<unsigned long>(lex.getString());
121 unsigned long const checksum =
122 convert<unsigned long>(lex.getString());
123 FileName const orig_from_name(orig_from);
124 CacheItem item(orig_from_name, to_format, timestamp, checksum);
126 // Don't cache files that do not exist anymore
127 if (!orig_from_name.exists()) {
128 LYXERR(Debug::FILES, "Not caching file `"
129 << orig_from << "' (does not exist anymore).");
130 item.cache_name.removeFile();
134 // Don't add items that are not in the cache anymore
135 // This can happen if two instances of LyX are running
136 // at the same time and update the index file independantly.
137 if (!item.cache_name.exists()) {
138 LYXERR(Debug::FILES, "Not caching file `" << orig_from
139 << "' (cached copy does not exist anymore).");
143 // Delete the cached file if it is too old
144 if (difftime(now, item.cache_name.lastModified())
145 > lyxrc.converter_cache_maxage) {
146 LYXERR(Debug::FILES, "Not caching file `"
147 << orig_from << "' (too old).");
148 item.cache_name.removeFile();
152 FormatCache & format_cache = cache[orig_from_name];
153 if (format_cache.from_format.empty())
154 format_cache.from_format =
155 // FIXME perf: This very expensive function is called on all
156 // cached files on opening. This slows LyX startup a lot. It
157 // would be better if this information was retrieved in a
159 theFormats().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()
223 ConverterCache & ConverterCache::get()
225 // Now return the cache
226 static ConverterCache singleton;
231 void ConverterCache::init()
233 if (!lyxrc.use_converter_cache)
235 // We do this here and not in the constructor because package() gets
236 // initialized after all static variables.
237 cache_dir = FileName(addName(package().user_support().absFileName(), "cache"));
238 if (!cache_dir.exists())
239 if (!cache_dir.createDirectory(0700)) {
240 // FIXME This should really be displayed as a message. But the GUI
241 // does not exist yet.
242 lyxerr << "Could not create cache directory `"
243 << cache_dir << "'." << endl;
246 get().pimpl_->readIndex();
250 void ConverterCache::writeIndex() const
252 if (!lyxrc.use_converter_cache
253 || cache_dir.empty())
255 pimpl_->writeIndex();
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, ' ' << orig_from
266 << ' ' << to_format << ' ' << converted_file);
268 // FIXME: Should not hardcode this (see bug 3819 for details)
269 if (to_format == "pstex") {
270 FileName const converted_eps(changeExtension(converted_file.absFileName(), "eps"));
271 add(orig_from, "eps", converted_eps);
272 } else if (to_format == "pdftex") {
273 FileName const converted_pdf(changeExtension(converted_file.absFileName(), "pdf"));
274 add(orig_from, "pdf6", converted_pdf);
277 // Is the file in the cache already?
278 CacheItem * item = pimpl_->find(orig_from, to_format);
280 time_t const timestamp = orig_from.lastModified();
281 Mover const & mover = getMover(to_format);
283 LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
284 "The file is already in the cache.");
285 // First test for timestamp
286 if (timestamp == item->timestamp) {
287 LYXERR(Debug::FILES, "Same timestamp.");
290 // Maybe the contents is still the same?
291 item->timestamp = timestamp;
292 unsigned long const checksum = orig_from.checksum();
293 if (checksum == item->checksum) {
294 LYXERR(Debug::FILES, "Same checksum.");
297 item->checksum = checksum;
298 if (!mover.copy(converted_file, item->cache_name,
299 onlyFileName(item->cache_name.absFileName()))) {
300 LYXERR(Debug::FILES, "Could not copy file " << orig_from << " to "
301 << item->cache_name);
302 } else if (!item->cache_name.changePermission(0600)) {
303 LYXERR(Debug::FILES, "Could not change file mode"
304 << item->cache_name);
307 CacheItem new_item(orig_from, to_format, timestamp,
308 orig_from.checksum());
309 if (mover.copy(converted_file, new_item.cache_name,
310 onlyFileName(new_item.cache_name.absFileName()))) {
311 if (!new_item.cache_name.changePermission(0600)) {
312 LYXERR(Debug::FILES, "Could not change file mode"
313 << new_item.cache_name);
315 FormatCache & format_cache = pimpl_->cache[orig_from];
316 if (format_cache.from_format.empty())
317 format_cache.from_format =
318 theFormats().getFormatFromFile(orig_from);
319 format_cache.cache[to_format] = new_item;
321 LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
322 "Could not copy file.");
327 void ConverterCache::remove(FileName const & orig_from,
328 string const & to_format) const
330 if (!lyxrc.use_converter_cache || orig_from.empty())
332 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
334 CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
335 if (it1 == pimpl_->cache.end())
337 FormatCacheType & format_cache = it1->second.cache;
338 FormatCacheType::iterator const it2 = format_cache.find(to_format);
339 if (it2 == format_cache.end())
342 format_cache.erase(it2);
343 if (format_cache.empty())
344 pimpl_->cache.erase(it1);
348 void ConverterCache::remove_all(string const & from_format,
349 string const & to_format) const
351 if (!lyxrc.use_converter_cache)
353 CacheType::iterator it1 = pimpl_->cache.begin();
354 while (it1 != pimpl_->cache.end()) {
355 if (it1->second.from_format != from_format) {
359 FormatCacheType & format_cache = it1->second.cache;
360 FormatCacheType::iterator it2 = format_cache.begin();
361 while (it2 != format_cache.end()) {
362 if (it2->first == to_format) {
363 LYXERR(Debug::FILES, "Removing file cache item "
364 << it1->first << ' ' << to_format);
365 it2->second.cache_name.removeFile();
366 format_cache.erase(it2);
367 // Have to start over again since items in a
368 // map are not ordered
369 it2 = format_cache.begin();
374 if (format_cache.empty()) {
375 pimpl_->cache.erase(it1);
376 // Have to start over again since items in a map are
378 it1 = pimpl_->cache.begin();
383 pimpl_->writeIndex();
387 bool ConverterCache::inCache(FileName const & orig_from,
388 string const & to_format) const
390 if (!lyxrc.use_converter_cache || orig_from.empty())
392 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
394 CacheItem * const item = pimpl_->find(orig_from, to_format);
396 LYXERR(Debug::FILES, "not in cache.");
400 // Special handling of pstex and pdftex formats: These are only
401 // considered to be in the cache if the corresponding graphics
402 // fiels are there as well. Otherwise copy() of the graphics below
404 // FIXME: Should not hardcode this (see bug 3819 for details)
405 if (to_format == "pstex") {
406 if (!inCache(orig_from, "eps"))
408 } else if (to_format == "pdftex") {
409 if (!inCache(orig_from, "pdf6"))
413 time_t const timestamp = orig_from.lastModified();
414 if (item->timestamp == timestamp) {
415 LYXERR(Debug::FILES, "identical timestamp.");
418 if (item->checksum == orig_from.checksum()) {
419 item->timestamp = timestamp;
420 LYXERR(Debug::FILES, "identical checksum.");
423 LYXERR(Debug::FILES, "in cache, but too old.");
428 FileName const & ConverterCache::cacheName(FileName const & orig_from,
429 string const & to_format) const
431 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
433 CacheItem * const item = pimpl_->find(orig_from, to_format);
434 LASSERT(item, { static const FileName fn; return fn; });
435 return item->cache_name;
439 bool ConverterCache::copy(FileName const & orig_from, string const & to_format,
440 FileName const & dest) const
442 if (!lyxrc.use_converter_cache || orig_from.empty() || dest.empty())
444 LYXERR(Debug::FILES, orig_from << ' ' << to_format << ' ' << dest);
446 // FIXME: Should not hardcode this (see bug 3819 for details)
447 if (to_format == "pstex") {
448 FileName const dest_eps(changeExtension(dest.absFileName(), "eps"));
449 if (!copy(orig_from, "eps", dest_eps))
451 } else if (to_format == "pdftex") {
452 FileName const dest_pdf(changeExtension(dest.absFileName(), "pdf"));
453 if (!copy(orig_from, "pdf6", dest_pdf))
457 CacheItem * const item = pimpl_->find(orig_from, to_format);
458 LASSERT(item, return false);
459 Mover const & mover = getMover(to_format);
460 return mover.copy(item->cache_name, dest,
461 onlyFileName(dest.absFileName()));