]> git.lyx.org Git - lyx.git/blob - src/ConverterCache.cpp
chkconfig.ltx: sort fontnames
[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 "debug.h"
18 #include "LyXRC.h"
19 #include "Mover.h"
20
21 #include "support/filetools.h"
22 #include "support/lyxlib.h"
23 #include "support/lyxtime.h"
24 #include "support/Package.h"
25
26 #include <boost/crc.hpp>
27 #include <boost/filesystem/operations.hpp>
28 #include <boost/current_function.hpp>
29
30 #include <fstream>
31 #include <iomanip>
32 #include <map>
33 #include <sstream>
34
35 using lyx::support::addName;
36
37 using std::string;
38
39 namespace fs = boost::filesystem;
40
41 namespace lyx {
42
43 using support::FileName;
44
45 namespace {
46
47 unsigned long do_crc(string const & s)
48 {
49         boost::crc_32_type crc;
50         crc = std::for_each(s.begin(), s.end(), crc);
51         return crc.checksum();
52 }
53
54
55 static FileName cache_dir;
56
57
58 class CacheItem {
59 public:
60         CacheItem() {}
61         CacheItem(FileName const & orig_from, string const & to_format,
62                   time_t t, unsigned long c)
63                 : timestamp(t), checksum(c)
64         {
65                 std::ostringstream os;
66                 os << std::setw(10) << std::setfill('0') << do_crc(orig_from.absFilename())
67                    << '-' << to_format;
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                                      << ' ' << timestamp << ' ' << checksum
72                                      << '.' << std::endl;
73         }
74         ~CacheItem() {}
75         FileName cache_name;
76         time_t timestamp;
77         unsigned long checksum;
78 };
79
80 }
81
82
83 /** The cache contains one item per orig file and target format, so use a
84  *  nested map to find the cache item quickly by filename and format.
85  */
86 typedef std::map<string, CacheItem> FormatCacheType;
87 typedef std::map<FileName, FormatCacheType> CacheType;
88
89
90 class ConverterCache::Impl {
91 public:
92         ///
93         void readIndex();
94         ///
95         void writeIndex();
96         ///
97         CacheItem * find(FileName const & from, string const & format);
98         CacheType cache;
99 };
100
101
102 void ConverterCache::Impl::readIndex()
103 {
104         time_t const now = current_time();
105         FileName const index(addName(cache_dir.absFilename(), "index"));
106         std::ifstream is(index.toFilesystemEncoding().c_str());
107         while (is.good()) {
108                 string orig_from;
109                 string to_format;
110                 time_t timestamp;
111                 unsigned long checksum;
112                 if (!(is >> orig_from >> to_format >> timestamp >> checksum))
113                         return;
114                 FileName const orig_from_name(orig_from);
115                 CacheItem item(orig_from_name, to_format, timestamp, checksum);
116
117                 // Don't cache files that do not exist anymore
118                 if (!fs::exists(orig_from_name.toFilesystemEncoding())) {
119                         LYXERR(Debug::FILES) << "Not caching file `"
120                                 << orig_from << "' (does not exist anymore)."
121                                 << std::endl;
122                         support::unlink(item.cache_name);
123                         continue;
124                 }
125
126                 // Don't add items that are not in the cache anymore
127                 // This can happen if two instances of LyX are running
128                 // at the same time and update the index file independantly.
129                 if (!fs::exists(item.cache_name.toFilesystemEncoding())) {
130                         LYXERR(Debug::FILES) << "Not caching file `"
131                                 << orig_from
132                                 << "' (cached copy does not exist anymore)."
133                                 << std::endl;
134                         continue;
135                 }
136
137                 // Delete the cached file if it is too old
138                 if (difftime(now, fs::last_write_time(item.cache_name.toFilesystemEncoding())) >
139                     lyxrc.converter_cache_maxage) {
140                         LYXERR(Debug::FILES) << "Not caching file `"
141                                 << orig_from << "' (too old)." << std::endl;
142                         support::unlink(item.cache_name);
143                         continue;
144                 }
145
146                 cache[orig_from_name][to_format] = item;
147         }
148         is.close();
149 }
150
151
152 void ConverterCache::Impl::writeIndex()
153 {
154         FileName const index(addName(cache_dir.absFilename(), "index"));
155         std::ofstream os(index.toFilesystemEncoding().c_str());
156         os.close();
157         if (!lyx::support::chmod(index, 0600))
158                 return;
159         os.open(index.toFilesystemEncoding().c_str());
160         CacheType::iterator it1 = cache.begin();
161         CacheType::iterator const end1 = cache.end();
162         for (; it1 != end1; ++it1) {
163                 FormatCacheType::iterator it2 = it1->second.begin();
164                 FormatCacheType::iterator const end2 = it1->second.end();
165                 for (; it2 != end2; ++it2)
166                         os << it1->first << ' ' << it2->first << ' '
167                            << it2->second.timestamp << ' '
168                            << it2->second.checksum << '\n';
169         }
170         os.close();
171 }
172
173
174 CacheItem * ConverterCache::Impl::find(FileName const & from,
175                 string const & format)
176 {
177         if (!lyxrc.use_converter_cache)
178                 return 0;
179         CacheType::iterator const it1 = cache.find(from);
180         if (it1 == cache.end())
181                 return 0;
182         FormatCacheType::iterator const it2 = it1->second.find(format);
183         if (it2 == it1->second.end())
184                 return 0;
185         return &(it2->second);
186 }
187
188
189 ConverterCache & ConverterCache::get()
190 {
191         // Now return the cache
192         static ConverterCache singleton;
193         return singleton;
194 }
195
196
197 void ConverterCache::init()
198 {
199         if (!lyxrc.use_converter_cache)
200                 return;
201         // We do this here and not in the constructor because package() gets
202         // initialized after all static variables.
203         cache_dir = FileName(addName(support::package().user_support().absFilename(), "cache"));
204         if (!fs::exists(cache_dir.toFilesystemEncoding()))
205                 if (support::mkdir(cache_dir, 0700) != 0) {
206                         lyxerr << "Could not create cache directory `"
207                                << cache_dir << "'." << std::endl;
208                         exit(EXIT_FAILURE);
209                 }
210         get().pimpl_->readIndex();
211 }
212
213
214 ConverterCache::ConverterCache()
215         : pimpl_(new Impl)
216 {}
217
218
219 ConverterCache::~ConverterCache()
220 {
221         if (!lyxrc.use_converter_cache)
222                 return;
223         pimpl_->writeIndex();
224 }
225
226
227 void ConverterCache::add(FileName const & orig_from, string const & to_format,
228                 FileName const & converted_file) const
229 {
230         if (!lyxrc.use_converter_cache || orig_from.empty() ||
231             converted_file.empty())
232                 return;
233         LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
234                              << ' ' << to_format << ' ' << converted_file
235                              << std::endl;
236
237         // Is the file in the cache already?
238         CacheItem * item = pimpl_->find(orig_from, to_format);
239
240         time_t const timestamp = fs::last_write_time(orig_from.toFilesystemEncoding());
241         Mover const & mover = getMover(to_format);
242         if (item) {
243                 LYXERR(Debug::FILES) << "ConverterCache::add(" << orig_from << "):\n"
244                                         "The file is already in the cache."
245                                      << std::endl;
246                 // First test for timestamp
247                 if (timestamp == item->timestamp) {
248                         LYXERR(Debug::FILES) << "Same timestamp."
249                                              << std::endl;
250                         return;
251                 } else {
252                         // Maybe the contents is still the same?
253                         item->timestamp = timestamp;
254                         unsigned long const checksum = support::sum(orig_from);
255                         if (checksum == item->checksum) {
256                                 LYXERR(Debug::FILES) << "Same checksum."
257                                                      << std::endl;
258                                 return;
259                         }
260                         item->checksum = checksum;
261                 }
262                 if (!mover.copy(converted_file, item->cache_name, 0600))
263                         LYXERR(Debug::FILES) << "ConverterCache::add("
264                                              << orig_from << "):\n"
265                                                 "Could not copy file."
266                                              << std::endl;
267         } else {
268                 CacheItem new_item = CacheItem(orig_from, to_format, timestamp,
269                                 support::sum(orig_from));
270                 if (mover.copy(converted_file, new_item.cache_name, 0600))
271                         pimpl_->cache[orig_from][to_format] = new_item;
272                 else
273                         LYXERR(Debug::FILES) << "ConverterCache::add("
274                                              << orig_from << "):\n"
275                                                 "Could not copy file."
276                                              << std::endl;
277         }
278 }
279
280
281 void ConverterCache::remove(FileName const & orig_from,
282                 string const & to_format) const
283 {
284         if (!lyxrc.use_converter_cache || orig_from.empty())
285                 return;
286         LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
287                              << ' ' << to_format << std::endl;
288
289         CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
290         if (it1 == pimpl_->cache.end())
291                 return;
292         FormatCacheType::iterator const it2 = it1->second.find(to_format);
293         if (it2 == it1->second.end())
294                 return;
295
296         it1->second.erase(it2);
297         if (it1->second.empty())
298                 pimpl_->cache.erase(it1);
299 }
300
301
302 bool ConverterCache::inCache(FileName const & orig_from,
303                 string const & to_format) const
304 {
305         if (!lyxrc.use_converter_cache || orig_from.empty())
306                 return false;
307         LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
308                              << ' ' << to_format << std::endl;
309
310         CacheItem * const item = pimpl_->find(orig_from, to_format);
311         if (!item) {
312                 LYXERR(Debug::FILES) << "not in cache." << std::endl;
313                 return false;
314         }
315         time_t const timestamp = fs::last_write_time(orig_from.toFilesystemEncoding());
316         if (item->timestamp == timestamp) {
317                 LYXERR(Debug::FILES) << "identical timestamp." << std::endl;
318                 return true;
319         }
320         if (item->checksum == support::sum(orig_from)) {
321                 item->timestamp = timestamp;
322                 LYXERR(Debug::FILES) << "identical checksum." << std::endl;
323                 return true;
324         }
325         LYXERR(Debug::FILES) << "in cache, but too old." << std::endl;
326         return false;
327 }
328
329
330 FileName const & ConverterCache::cacheName(FileName const & orig_from,
331                 string const & to_format) const
332 {
333         LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
334                              << ' ' << to_format << std::endl;
335
336         CacheItem * const item = pimpl_->find(orig_from, to_format);
337         BOOST_ASSERT(item);
338         return item->cache_name;
339 }
340
341
342 bool ConverterCache::copy(FileName const & orig_from, string const & to_format,
343                 FileName const & dest) const
344 {
345         if (!lyxrc.use_converter_cache || orig_from.empty() || dest.empty())
346                 return false;
347         LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
348                              << ' ' << to_format << ' ' << dest << std::endl;
349
350         CacheItem * const item = pimpl_->find(orig_from, to_format);
351         BOOST_ASSERT(item);
352         Mover const & mover = getMover(to_format);
353         return mover.copy(item->cache_name, dest);
354 }
355
356 } // namespace lyx