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>
42 using support::FileName;
43 using support::addName;
47 unsigned long do_crc(string const & s)
49 boost::crc_32_type crc;
50 crc = std::for_each(s.begin(), s.end(), crc);
51 return crc.checksum();
55 static FileName cache_dir;
61 CacheItem(FileName const & orig_from, string const & to_format,
62 time_t t, unsigned long c)
63 : timestamp(t), checksum(c)
65 std::ostringstream os;
66 os << std::setw(10) << std::setfill('0') << do_crc(orig_from.absFilename())
68 cache_name = FileName(addName(cache_dir.absFilename(), os.str()));
69 LYXERR(Debug::FILES, "Add file cache item " << orig_from
70 << ' ' << to_format << ' ' << cache_name
71 << ' ' << 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 std::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 std::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 std::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 formats.getFormatFromFile(orig_from_name);
163 format_cache.cache[to_format] = item;
169 void ConverterCache::Impl::writeIndex()
171 FileName const index(addName(cache_dir.absFilename(), "index"));
172 std::ofstream os(index.toFilesystemEncoding().c_str());
174 if (!lyx::support::chmod(index, 0600))
176 os.open(index.toFilesystemEncoding().c_str());
177 CacheType::iterator it1 = cache.begin();
178 CacheType::iterator const end1 = cache.end();
179 for (; it1 != end1; ++it1) {
180 FormatCacheType const & format_cache = it1->second.cache;
181 FormatCacheType::const_iterator it2 = format_cache.begin();
182 FormatCacheType::const_iterator const end2 = format_cache.end();
183 for (; it2 != end2; ++it2)
184 os << Lexer::quoteString(it1->first.absFilename())
185 << ' ' << it2->first << ' '
186 << it2->second.timestamp << ' '
187 << it2->second.checksum << '\n';
193 CacheItem * ConverterCache::Impl::find(FileName const & from,
194 string const & format)
196 if (!lyxrc.use_converter_cache)
198 CacheType::iterator const it1 = cache.find(from);
199 if (it1 == cache.end())
201 FormatCacheType & format_cache = it1->second.cache;
202 FormatCacheType::iterator const it2 = format_cache.find(format);
203 if (it2 == format_cache.end())
205 return &(it2->second);
209 /////////////////////////////////////////////////////////////////////
213 /////////////////////////////////////////////////////////////////////
215 ConverterCache::ConverterCache()
220 ConverterCache::~ConverterCache()
222 if (!lyxrc.use_converter_cache)
224 pimpl_->writeIndex();
229 ConverterCache & ConverterCache::get()
231 // Now return the cache
232 static ConverterCache singleton;
237 void ConverterCache::init()
239 if (!lyxrc.use_converter_cache)
241 // We do this here and not in the constructor because package() gets
242 // initialized after all static variables.
243 cache_dir = FileName(addName(support::package().user_support().absFilename(), "cache"));
244 if (!cache_dir.exists())
245 if (support::mkdir(cache_dir, 0700) != 0) {
246 lyxerr << "Could not create cache directory `"
247 << cache_dir << "'." << std::endl;
250 get().pimpl_->readIndex();
254 void ConverterCache::add(FileName const & orig_from, string const & to_format,
255 FileName const & converted_file) const
257 if (!lyxrc.use_converter_cache || orig_from.empty() ||
258 converted_file.empty())
260 LYXERR(Debug::FILES, ' ' << orig_from
261 << ' ' << to_format << ' ' << converted_file);
263 // FIXME: Should not hardcode this (see bug 3819 for details)
264 if (to_format == "pstex") {
265 FileName const converted_eps(support::changeExtension(converted_file.absFilename(), "eps"));
266 add(orig_from, "eps", converted_eps);
267 } else if (to_format == "pdftex") {
268 FileName const converted_pdf(support::changeExtension(converted_file.absFilename(), "pdf"));
269 add(orig_from, "pdf", converted_pdf);
272 // Is the file in the cache already?
273 CacheItem * item = pimpl_->find(orig_from, to_format);
275 time_t const timestamp = orig_from.lastModified();
276 Mover const & mover = getMover(to_format);
278 LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
279 "The file is already in the cache.");
280 // First test for timestamp
281 if (timestamp == item->timestamp) {
282 LYXERR(Debug::FILES, "Same timestamp.");
285 // Maybe the contents is still the same?
286 item->timestamp = timestamp;
287 unsigned long const checksum = orig_from.checksum();
288 if (checksum == item->checksum) {
289 LYXERR(Debug::FILES, "Same checksum.");
292 item->checksum = checksum;
293 if (!mover.copy(converted_file, item->cache_name,
294 support::onlyFilename(item->cache_name.absFilename()), 0600)) {
295 LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
296 "Could not copy file.");
299 CacheItem new_item(orig_from, to_format, timestamp,
300 orig_from.checksum());
301 if (mover.copy(converted_file, new_item.cache_name,
302 support::onlyFilename(new_item.cache_name.absFilename()), 0600)) {
303 FormatCache & format_cache = pimpl_->cache[orig_from];
304 if (format_cache.from_format.empty())
305 format_cache.from_format =
306 formats.getFormatFromFile(orig_from);
307 format_cache.cache[to_format] = new_item;
309 LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
310 "Could not copy file.");
315 void ConverterCache::remove(FileName const & orig_from,
316 string const & to_format) const
318 if (!lyxrc.use_converter_cache || orig_from.empty())
320 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
322 CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
323 if (it1 == pimpl_->cache.end())
325 FormatCacheType & format_cache = it1->second.cache;
326 FormatCacheType::iterator const it2 = format_cache.find(to_format);
327 if (it2 == format_cache.end())
330 format_cache.erase(it2);
331 if (format_cache.empty())
332 pimpl_->cache.erase(it1);
336 void ConverterCache::remove_all(string const & from_format,
337 string const & to_format) const
339 if (!lyxrc.use_converter_cache)
341 CacheType::iterator it1 = pimpl_->cache.begin();
342 while (it1 != pimpl_->cache.end()) {
343 if (it1->second.from_format != from_format) {
347 FormatCacheType & format_cache = it1->second.cache;
348 FormatCacheType::iterator it2 = format_cache.begin();
349 while (it2 != format_cache.end()) {
350 if (it2->first == to_format) {
351 LYXERR(Debug::FILES, "Removing file cache item "
352 << it1->first << ' ' << to_format);
353 it2->second.cache_name.removeFile();
354 format_cache.erase(it2);
355 // Have to start over again since items in a
356 // map are not ordered
357 it2 = format_cache.begin();
362 if (format_cache.empty()) {
363 pimpl_->cache.erase(it1);
364 // Have to start over again since items in a map are
366 it1 = pimpl_->cache.begin();
371 pimpl_->writeIndex();
375 bool ConverterCache::inCache(FileName const & orig_from,
376 string const & to_format) const
378 if (!lyxrc.use_converter_cache || orig_from.empty())
380 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
382 CacheItem * const item = pimpl_->find(orig_from, to_format);
384 LYXERR(Debug::FILES, "not in cache.");
387 time_t const timestamp = orig_from.lastModified();
388 if (item->timestamp == timestamp) {
389 LYXERR(Debug::FILES, "identical timestamp.");
392 if (item->checksum == orig_from.checksum()) {
393 item->timestamp = timestamp;
394 LYXERR(Debug::FILES, "identical checksum.");
397 LYXERR(Debug::FILES, "in cache, but too old.");
402 FileName const & ConverterCache::cacheName(FileName const & orig_from,
403 string const & to_format) const
405 LYXERR(Debug::FILES, orig_from << ' ' << to_format);
407 CacheItem * const item = pimpl_->find(orig_from, to_format);
409 return item->cache_name;
413 bool ConverterCache::copy(FileName const & orig_from, string const & to_format,
414 FileName const & dest) const
416 if (!lyxrc.use_converter_cache || orig_from.empty() || dest.empty())
418 LYXERR(Debug::FILES, orig_from << ' ' << to_format << ' ' << dest);
420 // FIXME: Should not hardcode this (see bug 3819 for details)
421 if (to_format == "pstex") {
422 FileName const dest_eps(support::changeExtension(dest.absFilename(), "eps"));
423 if (!copy(orig_from, "eps", dest_eps))
425 } else if (to_format == "pdftex") {
426 FileName const dest_pdf(support::changeExtension(dest.absFilename(), "pdf"));
427 if (!copy(orig_from, "pdf", dest_pdf))
431 CacheItem * const item = pimpl_->find(orig_from, to_format);
433 Mover const & mover = getMover(to_format);
434 return mover.copy(item->cache_name, dest,
435 support::onlyFilename(dest.absFilename()));