]> git.lyx.org Git - lyx.git/blob - src/ConverterCache.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / ConverterCache.cpp
1 /**
2  * \file ConverterCache.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Baruch Even
7  * \author Angus Leeming
8  * \author Georg Baum
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "ConverterCache.h"
16
17 #include "Format.h"
18 #include "LyXRC.h"
19 #include "Mover.h"
20
21 #include "support/convert.h"
22 #include "support/debug.h"
23 #include "support/filetools.h"
24 #include "support/Lexer.h"
25 #include "support/lyxtime.h"
26 #include "support/Package.h"
27
28 #include "support/checksum.h"
29 #include "support/lassert.h"
30
31 #include <algorithm>
32 #include <fstream>
33 #include <iomanip>
34 #include <map>
35 #include <sstream>
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41
42 namespace {
43
44 // FIXME THREAD
45 // This should be OK because it is only assigned during init()
46 static FileName cache_dir;
47
48
49 class CacheItem {
50 public:
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)
55         {
56                 ostringstream os;
57                 os << setw(10) << setfill('0')
58                    << support::checksum(orig_from.absFileName())
59                    << '-' << to_format;
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 << '.');
64         }
65         ~CacheItem()
66         {}
67         FileName cache_name;
68         time_t timestamp;
69         unsigned long checksum;
70 };
71
72 } // namespace
73
74
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.
77  */
78 typedef map<string, CacheItem> FormatCacheType;
79 class FormatCache {
80 public:
81         /// Format of the source file
82         string from_format;
83         /// Cache target format -> item to quickly find the item by format
84         FormatCacheType cache;
85 };
86 typedef map<FileName, FormatCache> CacheType;
87
88
89 class ConverterCache::Impl {
90 public:
91         ///
92         void readIndex();
93         ///
94         void writeIndex();
95         ///
96         CacheItem * find(FileName const & from, string const & format);
97         CacheType cache;
98 };
99
100
101 void ConverterCache::Impl::readIndex()
102 {
103         time_t const now = current_time();
104         FileName const index(addName(cache_dir.absFileName(), "index"));
105         ifstream is(index.toFilesystemEncoding().c_str());
106         Lexer lex;
107         lex.setStream(is);
108         while (lex.isOK()) {
109                 if (!lex.next(true))
110                         break;
111                 string const orig_from = lex.getString();
112                 if (!lex.next())
113                         break;
114                 string const to_format = lex.getString();
115                 if (!lex.next())
116                         break;
117                 time_t const timestamp =
118                         convert<unsigned long>(lex.getString());
119                 if (!lex.next())
120                         break;
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);
125
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();
131                         continue;
132                 }
133
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).");
140                         continue;
141                 }
142
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();
149                         continue;
150                 }
151
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
158                                 // delayed fashion.
159                                 theFormats().getFormatFromFile(orig_from_name);
160                 format_cache.cache[to_format] = item;
161         }
162         is.close();
163 }
164
165
166 void ConverterCache::Impl::writeIndex()
167 {
168         FileName const index(addName(cache_dir.absFileName(), "index"));
169         ofstream os(index.toFilesystemEncoding().c_str());
170         os.close();
171         if (!index.changePermission(0600))
172                 return;
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';
185         }
186         os.close();
187 }
188
189
190 CacheItem * ConverterCache::Impl::find(FileName const & from,
191                 string const & format)
192 {
193         if (!lyxrc.use_converter_cache)
194                 return nullptr;
195         CacheType::iterator const it1 = cache.find(from);
196         if (it1 == cache.end())
197                 return nullptr;
198         FormatCacheType & format_cache = it1->second.cache;
199         FormatCacheType::iterator const it2 = format_cache.find(format);
200         if (it2 == format_cache.end())
201                 return nullptr;
202         return &(it2->second);
203 }
204
205
206 /////////////////////////////////////////////////////////////////////
207 //
208 // ConverterCache
209 //
210 /////////////////////////////////////////////////////////////////////
211
212 ConverterCache::ConverterCache()
213         : pimpl_(new Impl)
214 {}
215
216
217 ConverterCache::~ConverterCache()
218 {
219         delete pimpl_;
220 }
221
222
223 ConverterCache & ConverterCache::get()
224 {
225         // Now return the cache
226         static ConverterCache singleton;
227         return singleton;
228 }
229
230
231 void ConverterCache::init()
232 {
233         if (!lyxrc.use_converter_cache)
234                 return;
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;
244                         exit(EXIT_FAILURE);
245                 }
246         get().pimpl_->readIndex();
247 }
248
249
250 void ConverterCache::writeIndex() const
251 {
252         if (!lyxrc.use_converter_cache
253                   || cache_dir.empty())
254                 return;
255         pimpl_->writeIndex();
256 }
257
258
259 void ConverterCache::add(FileName const & orig_from, string const & to_format,
260                 FileName const & converted_file) const
261 {
262         if (!lyxrc.use_converter_cache || orig_from.empty() ||
263             converted_file.empty())
264                 return;
265         LYXERR(Debug::FILES, ' ' << orig_from
266                              << ' ' << to_format << ' ' << converted_file);
267
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);
275         }
276
277         // Is the file in the cache already?
278         CacheItem * item = pimpl_->find(orig_from, to_format);
279
280         time_t const timestamp = orig_from.lastModified();
281         Mover const & mover = getMover(to_format);
282         if (item) {
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.");
288                         return;
289                 }
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.");
295                         return;
296                 }
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);
305                 }
306         } else {
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);
314                         }
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;
320                 } else
321                         LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
322                                                 "Could not copy file.");
323         }
324 }
325
326
327 void ConverterCache::remove(FileName const & orig_from,
328                 string const & to_format) const
329 {
330         if (!lyxrc.use_converter_cache || orig_from.empty())
331                 return;
332         LYXERR(Debug::FILES, orig_from << ' ' << to_format);
333
334         CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
335         if (it1 == pimpl_->cache.end())
336                 return;
337         FormatCacheType & format_cache = it1->second.cache;
338         FormatCacheType::iterator const it2 = format_cache.find(to_format);
339         if (it2 == format_cache.end())
340                 return;
341
342         format_cache.erase(it2);
343         if (format_cache.empty())
344                 pimpl_->cache.erase(it1);
345 }
346
347
348 void ConverterCache::remove_all(string const & from_format,
349                 string const & to_format) const
350 {
351         if (!lyxrc.use_converter_cache)
352                 return;
353         CacheType::iterator it1 = pimpl_->cache.begin();
354         while (it1 != pimpl_->cache.end()) {
355                 if (it1->second.from_format != from_format) {
356                         ++it1;
357                         continue;
358                 }
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();
370                         } else {
371                                 ++it2;
372                         }
373                 }
374                 if (format_cache.empty()) {
375                         pimpl_->cache.erase(it1);
376                         // Have to start over again since items in a map are
377                         // not ordered
378                         it1 = pimpl_->cache.begin();
379                 } else {
380                         ++it1;
381                 }
382         }
383         pimpl_->writeIndex();
384 }
385
386
387 bool ConverterCache::inCache(FileName const & orig_from,
388                 string const & to_format) const
389 {
390         if (!lyxrc.use_converter_cache || orig_from.empty())
391                 return false;
392         LYXERR(Debug::FILES, orig_from << ' ' << to_format);
393
394         CacheItem * const item = pimpl_->find(orig_from, to_format);
395         if (!item) {
396                 LYXERR(Debug::FILES, "not in cache.");
397                 return false;
398         }
399
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
403         // would fail.
404         // FIXME: Should not hardcode this (see bug 3819 for details)
405         if (to_format == "pstex") {
406                 if (!inCache(orig_from, "eps"))
407                         return false;
408         } else if (to_format == "pdftex") {
409                 if (!inCache(orig_from, "pdf6"))
410                         return false;
411         }
412
413         time_t const timestamp = orig_from.lastModified();
414         if (item->timestamp == timestamp) {
415                 LYXERR(Debug::FILES, "identical timestamp.");
416                 return true;
417         }
418         if (item->checksum == orig_from.checksum()) {
419                 item->timestamp = timestamp;
420                 LYXERR(Debug::FILES, "identical checksum.");
421                 return true;
422         }
423         LYXERR(Debug::FILES, "in cache, but too old.");
424         return false;
425 }
426
427
428 FileName const & ConverterCache::cacheName(FileName const & orig_from,
429                 string const & to_format) const
430 {
431         LYXERR(Debug::FILES, orig_from << ' ' << to_format);
432
433         CacheItem * const item = pimpl_->find(orig_from, to_format);
434         LASSERT(item, { static const FileName fn; return fn; });
435         return item->cache_name;
436 }
437
438
439 bool ConverterCache::copy(FileName const & orig_from, string const & to_format,
440                 FileName const & dest) const
441 {
442         if (!lyxrc.use_converter_cache || orig_from.empty() || dest.empty())
443                 return false;
444         LYXERR(Debug::FILES, orig_from << ' ' << to_format << ' ' << dest);
445
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))
450                         return false;
451         } else if (to_format == "pdftex") {
452                 FileName const dest_pdf(changeExtension(dest.absFileName(), "pdf"));
453                 if (!copy(orig_from, "pdf6", dest_pdf))
454                         return false;
455         }
456
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()));
462 }
463
464 } // namespace lyx