2 * \file ConverterCache.C
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"
21 #include "support/filetools.h"
22 #include "support/lyxlib.h"
23 #include "support/lyxtime.h"
24 #include "support/package.h"
26 #include <boost/crc.hpp>
27 #include <boost/filesystem/operations.hpp>
34 using lyx::support::absolutePath;
35 using lyx::support::addName;
39 namespace fs = boost::filesystem;
45 unsigned long do_crc(string const & s)
47 boost::crc_32_type crc;
48 crc = std::for_each(s.begin(), s.end(), crc);
49 return crc.checksum();
53 static string cache_dir;
59 CacheItem(string const & orig_from, string const & to_format,
60 time_t t, unsigned long c)
61 : timestamp(t), checksum(c)
63 BOOST_ASSERT(absolutePath(orig_from));
64 std::ostringstream os;
65 os << std::setw(10) << std::setfill('0') << do_crc(orig_from)
67 cache_name = addName(cache_dir, os.str());
68 lyxerr[Debug::FILES] << "Add file cache item " << orig_from
69 << ' ' << to_format << ' ' << cache_name
70 << ' ' << 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;
86 typedef std::map<string, FormatCacheType> CacheType;
89 class ConverterCache::Impl {
96 CacheItem * find(string const & from, string const & format);
101 void ConverterCache::Impl::readIndex()
103 time_t const now = current_time();
104 string const index = addName(cache_dir, "index");
105 std::ifstream is(index.c_str());
110 unsigned long checksum;
111 if (!(is >> orig_from >> to_format >> timestamp >> checksum))
113 CacheItem item(orig_from, to_format, timestamp, checksum);
115 // Don't cache files that do not exist anymore
116 if (!fs::exists(orig_from)) {
117 lyxerr[Debug::FILES] << "Not caching file `"
118 << orig_from << "' (does not exist anymore)."
120 support::unlink(item.cache_name);
124 // Delete the cached file if it is too old
125 if (difftime(now, fs::last_write_time(item.cache_name)) >
126 lyxrc.converter_cache_maxage) {
127 lyxerr[Debug::FILES] << "Not caching file `"
128 << orig_from << "' (too old)." << std::endl;
129 support::unlink(item.cache_name);
133 cache[orig_from][to_format] = item;
139 void ConverterCache::Impl::writeIndex()
141 string const index = addName(cache_dir, "index");
142 std::ofstream os(index.c_str());
144 if (!lyx::support::chmod(index.c_str(), 0600))
146 os.open(index.c_str());
147 CacheType::iterator it1 = cache.begin();
148 CacheType::iterator const end1 = cache.end();
149 for (; it1 != end1; ++it1) {
150 FormatCacheType::iterator it2 = it1->second.begin();
151 FormatCacheType::iterator const end2 = it1->second.end();
152 for (; it2 != end2; ++it2)
153 os << it1->first << ' ' << it2->first << ' '
154 << it2->second.timestamp << ' '
155 << it2->second.checksum << '\n';
161 CacheItem * ConverterCache::Impl::find(string const & from,
162 string const & format)
164 if (!lyxrc.use_converter_cache)
166 CacheType::iterator const it1 = cache.find(from);
167 if (it1 == cache.end())
169 FormatCacheType::iterator const it2 = it1->second.find(format);
170 if (it2 == it1->second.end())
172 return &(it2->second);
176 ConverterCache & ConverterCache::get()
178 // Now return the cache
179 static ConverterCache singleton;
184 void ConverterCache::init()
186 if (!lyxrc.use_converter_cache)
188 // We do this here and not in the constructor because package() gets
189 // initialized after all static variables.
190 cache_dir = addName(support::package().user_support(), "cache");
191 if (!fs::exists(cache_dir))
192 if (support::mkdir(cache_dir, 0700) != 0) {
193 lyxerr << "Could not create cache directory `"
194 << cache_dir << "'." << std::endl;
197 get().pimpl_->readIndex();
201 ConverterCache::ConverterCache()
206 ConverterCache::~ConverterCache()
208 if (!lyxrc.use_converter_cache)
210 pimpl_->writeIndex();
214 void ConverterCache::add(string const & orig_from, string const & to_format,
215 string const & converted_file) const
217 if (!lyxrc.use_converter_cache)
219 lyxerr[Debug::FILES] << BOOST_CURRENT_FUNCTION << ' ' << orig_from
220 << ' ' << to_format << ' ' << converted_file
222 BOOST_ASSERT(absolutePath(orig_from));
223 BOOST_ASSERT(absolutePath(converted_file));
225 // Is the file in the cache already?
226 CacheItem * item = pimpl_->find(orig_from, to_format);
228 time_t const timestamp = fs::last_write_time(orig_from);
229 Mover const & mover = movers(to_format);
231 lyxerr[Debug::FILES] << "ConverterCache::add(" << orig_from << "):\n"
232 "The file is already in the cache."
234 // First test for timestamp
235 if (timestamp == item->timestamp) {
236 lyxerr[Debug::FILES] << "Same timestamp."
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."
248 item->checksum = checksum;
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."
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;
261 lyxerr[Debug::FILES] << "ConverterCache::add("
262 << orig_from << "):\n"
263 "Could not copy file."
269 void ConverterCache::remove(string const & orig_from,
270 string const & to_format) const
272 if (!lyxrc.use_converter_cache)
274 lyxerr[Debug::FILES] << BOOST_CURRENT_FUNCTION << ' ' << orig_from
275 << ' ' << to_format << std::endl;
276 BOOST_ASSERT(absolutePath(orig_from));
278 CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
279 if (it1 == pimpl_->cache.end())
281 FormatCacheType::iterator const it2 = it1->second.find(to_format);
282 if (it2 == it1->second.end())
285 it1->second.erase(it2);
286 if (it1->second.empty())
287 pimpl_->cache.erase(it1);
291 bool ConverterCache::inCache(string const & orig_from,
292 string const & to_format) const
294 if (!lyxrc.use_converter_cache)
296 lyxerr[Debug::FILES] << BOOST_CURRENT_FUNCTION << ' ' << orig_from
297 << ' ' << to_format << std::endl;
298 BOOST_ASSERT(absolutePath(orig_from));
300 CacheItem * const item = pimpl_->find(orig_from, to_format);
302 lyxerr[Debug::FILES] << "not in cache." << std::endl;
305 time_t const timestamp = fs::last_write_time(orig_from);
306 if (item->timestamp == timestamp) {
307 lyxerr[Debug::FILES] << "identical timestamp." << std::endl;
310 if (item->checksum == support::sum(orig_from)) {
311 item->timestamp = timestamp;
312 lyxerr[Debug::FILES] << "identical checksum." << std::endl;
315 lyxerr[Debug::FILES] << "in cache, but too old." << std::endl;
320 string const ConverterCache::cacheName(string const & orig_from,
321 string const & to_format) const
323 lyxerr[Debug::FILES] << BOOST_CURRENT_FUNCTION << ' ' << orig_from
324 << ' ' << to_format << std::endl;
325 BOOST_ASSERT(absolutePath(orig_from));
327 CacheItem * const item = pimpl_->find(orig_from, to_format);
329 return item->cache_name;
333 bool ConverterCache::copy(string const & orig_from, string const & to_format,
334 string const & dest) const
336 if (!lyxrc.use_converter_cache)
338 lyxerr[Debug::FILES] << BOOST_CURRENT_FUNCTION << ' ' << orig_from
339 << ' ' << to_format << ' ' << dest << std::endl;
340 BOOST_ASSERT(absolutePath(orig_from));
341 BOOST_ASSERT(absolutePath(dest));
343 CacheItem * const item = pimpl_->find(orig_from, to_format);
345 Mover const & mover = movers(to_format);
346 return mover.copy(item->cache_name, dest);