]> git.lyx.org Git - lyx.git/blob - src/ConverterCache.C
* support/os_unix.C (canAutoOpen, autoOpenFile): on Mac OS X, use
[lyx.git] / src / ConverterCache.C
1 /**
2  * \file ConverterCache.C
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                 // Delete the cached file if it is too old
127                 if (difftime(now, fs::last_write_time(item.cache_name.toFilesystemEncoding())) >
128                     lyxrc.converter_cache_maxage) {
129                         lyxerr[Debug::FILES] << "Not caching file `"
130                                 << orig_from << "' (too old)." << std::endl;
131                         support::unlink(item.cache_name);
132                         continue;
133                 }
134
135                 cache[orig_from_name][to_format] = item;
136         }
137         is.close();
138 }
139
140
141 void ConverterCache::Impl::writeIndex()
142 {
143         FileName const index(addName(cache_dir.absFilename(), "index"));
144         std::ofstream os(index.toFilesystemEncoding().c_str());
145         os.close();
146         if (!lyx::support::chmod(index, 0600))
147                 return;
148         os.open(index.toFilesystemEncoding().c_str());
149         CacheType::iterator it1 = cache.begin();
150         CacheType::iterator const end1 = cache.end();
151         for (; it1 != end1; ++it1) {
152                 FormatCacheType::iterator it2 = it1->second.begin();
153                 FormatCacheType::iterator const end2 = it1->second.end();
154                 for (; it2 != end2; ++it2)
155                         os << it1->first << ' ' << it2->first << ' '
156                            << it2->second.timestamp << ' '
157                            << it2->second.checksum << '\n';
158         }
159         os.close();
160 }
161
162
163 CacheItem * ConverterCache::Impl::find(FileName const & from,
164                 string const & format)
165 {
166         if (!lyxrc.use_converter_cache)
167                 return 0;
168         CacheType::iterator const it1 = cache.find(from);
169         if (it1 == cache.end())
170                 return 0;
171         FormatCacheType::iterator const it2 = it1->second.find(format);
172         if (it2 == it1->second.end())
173                 return 0;
174         return &(it2->second);
175 }
176
177
178 ConverterCache & ConverterCache::get()
179 {
180         // Now return the cache
181         static ConverterCache singleton;
182         return singleton;
183 }
184
185
186 void ConverterCache::init()
187 {
188         if (!lyxrc.use_converter_cache)
189                 return;
190         // We do this here and not in the constructor because package() gets
191         // initialized after all static variables.
192         cache_dir = FileName(addName(support::package().user_support(), "cache"));
193         if (!fs::exists(cache_dir.toFilesystemEncoding()))
194                 if (support::mkdir(cache_dir, 0700) != 0) {
195                         lyxerr << "Could not create cache directory `"
196                                << cache_dir << "'." << std::endl;
197                         exit(EXIT_FAILURE);
198                 }
199         get().pimpl_->readIndex();
200 }
201
202
203 ConverterCache::ConverterCache()
204         : pimpl_(new Impl)
205 {}
206
207
208 ConverterCache::~ConverterCache()
209 {
210         if (!lyxrc.use_converter_cache)
211                 return;
212         pimpl_->writeIndex();
213 }
214
215
216 void ConverterCache::add(FileName const & orig_from, string const & to_format,
217                 FileName const & converted_file) const
218 {
219         if (!lyxrc.use_converter_cache)
220                 return;
221         lyxerr[Debug::FILES] << BOOST_CURRENT_FUNCTION << ' ' << orig_from
222                              << ' ' << to_format << ' ' << converted_file
223                              << std::endl;
224
225         // Is the file in the cache already?
226         CacheItem * item = pimpl_->find(orig_from, to_format);
227
228         time_t const timestamp = fs::last_write_time(orig_from.toFilesystemEncoding());
229         Mover const & mover = movers(to_format);
230         if (item) {
231                 lyxerr[Debug::FILES] << "ConverterCache::add(" << orig_from << "):\n"
232                                         "The file is already in the cache."
233                                      << std::endl;
234                 // First test for timestamp
235                 if (timestamp == item->timestamp) {
236                         lyxerr[Debug::FILES] << "Same timestamp."
237                                              << std::endl;
238                         return;
239                 } else {
240                         // Maybe the contents is still the same?
241                         item->timestamp = timestamp;
242                         unsigned long const checksum = support::sum(orig_from);
243                         if (checksum == item->checksum) {
244                                 lyxerr[Debug::FILES] << "Same checksum."
245                                                      << std::endl;
246                                 return;
247                         }
248                         item->checksum = checksum;
249                 }
250                 if (!mover.copy(converted_file, item->cache_name, 0600))
251                         lyxerr[Debug::FILES] << "ConverterCache::add("
252                                              << orig_from << "):\n"
253                                                 "Could not copy file."
254                                              << std::endl;
255         } else {
256                 CacheItem new_item = CacheItem(orig_from, to_format, timestamp,
257                                 support::sum(orig_from));
258                 if (mover.copy(converted_file, new_item.cache_name, 0600))
259                         pimpl_->cache[orig_from][to_format] = new_item;
260                 else
261                         lyxerr[Debug::FILES] << "ConverterCache::add("
262                                              << orig_from << "):\n"
263                                                 "Could not copy file."
264                                              << std::endl;
265         }
266 }
267
268
269 void ConverterCache::remove(FileName const & orig_from,
270                 string const & to_format) const
271 {
272         if (!lyxrc.use_converter_cache)
273                 return;
274         lyxerr[Debug::FILES] << BOOST_CURRENT_FUNCTION << ' ' << orig_from
275                              << ' ' << to_format << std::endl;
276
277         CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
278         if (it1 == pimpl_->cache.end())
279                 return;
280         FormatCacheType::iterator const it2 = it1->second.find(to_format);
281         if (it2 == it1->second.end())
282                 return;
283
284         it1->second.erase(it2);
285         if (it1->second.empty())
286                 pimpl_->cache.erase(it1);
287 }
288
289
290 bool ConverterCache::inCache(FileName const & orig_from,
291                 string const & to_format) const
292 {
293         if (!lyxrc.use_converter_cache)
294                 return false;
295         lyxerr[Debug::FILES] << BOOST_CURRENT_FUNCTION << ' ' << orig_from
296                              << ' ' << to_format << std::endl;
297
298         CacheItem * const item = pimpl_->find(orig_from, to_format);
299         if (!item) {
300                 lyxerr[Debug::FILES] << "not in cache." << std::endl;
301                 return false;
302         }
303         time_t const timestamp = fs::last_write_time(orig_from.toFilesystemEncoding());
304         if (item->timestamp == timestamp) {
305                 lyxerr[Debug::FILES] << "identical timestamp." << std::endl;
306                 return true;
307         }
308         if (item->checksum == support::sum(orig_from)) {
309                 item->timestamp = timestamp;
310                 lyxerr[Debug::FILES] << "identical checksum." << std::endl;
311                 return true;
312         }
313         lyxerr[Debug::FILES] << "in cache, but too old." << std::endl;
314         return false;
315 }
316
317
318 FileName const & ConverterCache::cacheName(FileName const & orig_from,
319                 string const & to_format) const
320 {
321         lyxerr[Debug::FILES] << BOOST_CURRENT_FUNCTION << ' ' << orig_from
322                              << ' ' << to_format << std::endl;
323
324         CacheItem * const item = pimpl_->find(orig_from, to_format);
325         BOOST_ASSERT(item);
326         return item->cache_name;
327 }
328
329
330 bool ConverterCache::copy(FileName const & orig_from, string const & to_format,
331                 FileName const & dest) const
332 {
333         if (!lyxrc.use_converter_cache)
334                 return false;
335         lyxerr[Debug::FILES] << BOOST_CURRENT_FUNCTION << ' ' << orig_from
336                              << ' ' << to_format << ' ' << dest << std::endl;
337
338         CacheItem * const item = pimpl_->find(orig_from, to_format);
339         BOOST_ASSERT(item);
340         Mover const & mover = movers(to_format);
341         return mover.copy(item->cache_name, dest);
342 }
343
344 } // namespace lyx