2006-11-13 10:27:57 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file ConverterCache.cpp
|
2006-11-13 10:27:57 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Baruch Even
|
|
|
|
* \author Angus Leeming
|
|
|
|
* \author Georg Baum
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "ConverterCache.h"
|
|
|
|
|
2007-05-17 08:31:00 +00:00
|
|
|
#include "Format.h"
|
|
|
|
#include "Lexer.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "LyXRC.h"
|
|
|
|
#include "Mover.h"
|
2007-05-17 08:31:00 +00:00
|
|
|
#include "debug.h"
|
2006-11-13 10:27:57 +00:00
|
|
|
|
2007-05-17 08:31:00 +00:00
|
|
|
#include "support/convert.h"
|
2006-11-13 10:27:57 +00:00
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
#include "support/lyxtime.h"
|
Rename files in src/support, step one.
src/support/package.h src/support/Package.h Package
src/support/package.C.in src/support/Package.C.in Package
src/support/path.h src/support/Path.h Path
src/support/fs_extras.h src/support/fs_extras.h NOCLASSES
src/support/RandomAccessList.h src/support/RandomAccessList.h RandomAccessList
src/support/lyxmanip.h src/support/lyxmanip.h NOCLASSES
src/support/rename.C src/support/rename.cpp NOCLASSES
src/support/abort.C src/support/abort.cpp NOCLASSES
src/support/lyxlib.h src/support/lyxlib.h NOCLASSES
src/support/ExceptionMessage.h src/support/ExceptionMessage.h ExceptionMessage
src/support/copy.C src/support/copy.cpp NOCLASSES
src/support/limited_stack.h src/support/limited_stack.h limited_stack
src/support/filefilterlist.C src/support/FileFilterList.cpp ['FileFilterList', 'Filter']
src/support/cow_ptr.h src/support/cow_ptr.h cow_ptr
src/support/os_unix.C src/support/os_unix.cpp NOCLASSES
src/support/socktools.h src/support/socktools.h NOCLASSES
src/support/forkedcontr.h src/support/ForkedcallsController.h ForkedcallsController
src/support/os.h src/support/os.h NOCLASSES
src/support/FileMonitor.h src/support/FileMonitor.h FileMonitor
src/support/copied_ptr.h src/support/copied_ptr.h copied_ptr
src/support/translator.h src/support/Translator.h Translator
src/support/filetools.C src/support/filetools.cpp NOCLASSES
src/support/unlink.C src/support/unlink.cpp NOCLASSES
src/support/os_win32.C src/support/os_win32.cpp GetFolderPath
src/support/lstrings.C src/support/lstrings.cpp NOCLASSES
src/support/qstring_helpers.C src/support/qstring_helpers.cpp NOCLASSES
src/support/getcwd.C src/support/getcwd.cpp NOCLASSES
src/support/systemcall.C src/support/Systemcall.cpp Systemcall
src/support/lyxalgo.h src/support/lyxalgo.h NOCLASSES
src/support/filefilterlist.h src/support/FileFilterList.h ['FileFilterList', 'Filter']
src/support/unicode.C src/support/unicode.cpp IconvProcessor
src/support/userinfo.C src/support/userinfo.cpp NOCLASSES
src/support/lyxtime.C src/support/lyxtime.cpp NOCLASSES
src/support/kill.C src/support/kill.cpp NOCLASSES
src/support/docstring.C src/support/docstring.cpp to_local8bit_failure
src/support/os_cygwin.C src/support/os_cygwin.cpp NOCLASSES
src/support/lyxsum.C src/support/lyxsum.cpp NOCLASSES
src/support/environment.C src/support/environment.cpp NOCLASSES
src/support/filetools.h src/support/filetools.h NOCLASSES
src/support/textutils.C src/support/textutils.cpp NOCLASSES
src/support/mkdir.C src/support/mkdir.cpp NOCLASSES
src/support/forkedcall.C src/support/Forkedcall.cpp ['ForkedProcess', 'Forkedcall']
src/support/tempname.C src/support/tempname.cpp NOCLASSES
src/support/os_win32.h src/support/os_win32.h GetFolderPath
src/support/types.h src/support/types.h NOCLASSES
src/support/lstrings.h src/support/lstrings.h NOCLASSES
src/support/forkedcallqueue.C src/support/ForkedCallQueue.cpp ForkedCallQueue
src/support/qstring_helpers.h src/support/qstring_helpers.h NOCLASSES
src/support/convert.C src/support/convert.cpp NOCLASSES
src/support/filename.C src/support/FileName.cpp ['FileName', 'DocFileName']
src/support/tests/convert.C src/support/tests/convert.cpp NOCLASSES
src/support/tests/filetools.C src/support/tests/filetools.cpp NOCLASSES
src/support/tests/lstrings.C src/support/tests/lstrings.cpp NOCLASSES
src/support/tests/boost.C src/support/tests/boost.cpp NOCLASSES
src/support/docstream.C src/support/docstream.cpp ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/std_istream.h src/support/std_istream.h NOCLASSES
src/support/systemcall.h src/support/Systemcall.h Systemcall
src/support/chdir.C src/support/chdir.cpp NOCLASSES
src/support/std_ostream.h src/support/std_ostream.h NOCLASSES
src/support/unicode.h src/support/unicode.h IconvProcessor
src/support/path.C src/support/Path.cpp Path
src/support/fs_extras.C src/support/fs_extras.cpp NOCLASSES
src/support/userinfo.h src/support/userinfo.h NOCLASSES
src/support/lyxtime.h src/support/lyxtime.h NOCLASSES
src/support/docstring.h src/support/docstring.h to_local8bit_failure
src/support/debugstream.h src/support/debugstream.h basic_debugstream
src/support/environment.h src/support/environment.h NOCLASSES
src/support/textutils.h src/support/textutils.h NOCLASSES
src/support/forkedcall.h src/support/Forkedcall.h ['ForkedProcess', 'Forkedcall']
src/support/socktools.C src/support/socktools.cpp NOCLASSES
src/support/forkedcallqueue.h src/support/ForkedCallQueue.h ForkedCallQueue
src/support/forkedcontr.C src/support/ForkedcallsController.cpp ForkedcallsController
src/support/os.C src/support/os.cpp NOCLASSES
src/support/convert.h src/support/convert.h NOCLASSES
src/support/filename.h src/support/FileName.h ['FileName', 'DocFileName']
src/support/docstream.h src/support/docstream.h ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/FileMonitor.C src/support/FileMonitor.cpp FileMonitor
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18024 a592a061-630c-0410-9148-cb99ea01b6c8
2007-04-26 05:12:52 +00:00
|
|
|
#include "support/Package.h"
|
2006-11-13 10:27:57 +00:00
|
|
|
|
2007-11-07 22:04:02 +00:00
|
|
|
#include <boost/assert.hpp>
|
2006-11-13 10:27:57 +00:00
|
|
|
#include <boost/crc.hpp>
|
2006-11-15 18:39:20 +00:00
|
|
|
#include <boost/current_function.hpp>
|
2006-11-13 10:27:57 +00:00
|
|
|
|
2007-11-07 23:39:12 +00:00
|
|
|
#include <algorithm>
|
2006-11-13 10:27:57 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
using support::FileName;
|
2007-11-07 22:04:02 +00:00
|
|
|
using support::addName;
|
2006-11-26 21:30:39 +00:00
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
unsigned long do_crc(string const & s)
|
|
|
|
{
|
|
|
|
boost::crc_32_type crc;
|
|
|
|
crc = std::for_each(s.begin(), s.end(), crc);
|
|
|
|
return crc.checksum();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
static FileName cache_dir;
|
2006-11-13 10:27:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CacheItem {
|
|
|
|
public:
|
|
|
|
CacheItem() {}
|
2006-11-26 21:30:39 +00:00
|
|
|
CacheItem(FileName const & orig_from, string const & to_format,
|
2007-05-28 22:27:45 +00:00
|
|
|
time_t t, unsigned long c)
|
2006-11-13 10:27:57 +00:00
|
|
|
: timestamp(t), checksum(c)
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2006-11-26 21:30:39 +00:00
|
|
|
os << std::setw(10) << std::setfill('0') << do_crc(orig_from.absFilename())
|
2006-11-13 10:27:57 +00:00
|
|
|
<< '-' << to_format;
|
2006-11-26 21:30:39 +00:00
|
|
|
cache_name = FileName(addName(cache_dir.absFilename(), os.str()));
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "Add file cache item " << orig_from
|
2007-05-28 22:27:45 +00:00
|
|
|
<< ' ' << to_format << ' ' << cache_name
|
|
|
|
<< ' ' << timestamp << ' ' << checksum
|
|
|
|
<< '.' << std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
}
|
|
|
|
~CacheItem() {}
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName cache_name;
|
2006-11-13 10:27:57 +00:00
|
|
|
time_t timestamp;
|
|
|
|
unsigned long checksum;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** The cache contains one item per orig file and target format, so use a
|
|
|
|
* nested map to find the cache item quickly by filename and format.
|
|
|
|
*/
|
|
|
|
typedef std::map<string, CacheItem> FormatCacheType;
|
2007-05-17 08:31:00 +00:00
|
|
|
class FormatCache {
|
|
|
|
public:
|
|
|
|
/// Format of the source file
|
|
|
|
string from_format;
|
|
|
|
/// Cache target format -> item to quickly find the item by format
|
|
|
|
FormatCacheType cache;
|
|
|
|
};
|
|
|
|
typedef std::map<FileName, FormatCache> CacheType;
|
2006-11-13 10:27:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConverterCache::Impl {
|
|
|
|
public:
|
|
|
|
///
|
|
|
|
void readIndex();
|
|
|
|
///
|
|
|
|
void writeIndex();
|
|
|
|
///
|
2006-11-26 21:30:39 +00:00
|
|
|
CacheItem * find(FileName const & from, string const & format);
|
2006-11-13 10:27:57 +00:00
|
|
|
CacheType cache;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void ConverterCache::Impl::readIndex()
|
|
|
|
{
|
|
|
|
time_t const now = current_time();
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const index(addName(cache_dir.absFilename(), "index"));
|
|
|
|
std::ifstream is(index.toFilesystemEncoding().c_str());
|
2007-05-17 08:31:00 +00:00
|
|
|
Lexer lex(0, 0);
|
|
|
|
lex.setStream(is);
|
|
|
|
while (lex.isOK()) {
|
|
|
|
if (!lex.next(true))
|
|
|
|
break;
|
|
|
|
string const orig_from = lex.getString();
|
|
|
|
if (!lex.next())
|
|
|
|
break;
|
|
|
|
string const to_format = lex.getString();
|
|
|
|
if (!lex.next())
|
|
|
|
break;
|
|
|
|
time_t const timestamp =
|
|
|
|
convert<unsigned long>(lex.getString());
|
|
|
|
if (!lex.next())
|
|
|
|
break;
|
|
|
|
unsigned long const checksum =
|
|
|
|
convert<unsigned long>(lex.getString());
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const orig_from_name(orig_from);
|
|
|
|
CacheItem item(orig_from_name, to_format, timestamp, checksum);
|
2006-11-13 10:27:57 +00:00
|
|
|
|
|
|
|
// Don't cache files that do not exist anymore
|
2007-10-18 19:29:32 +00:00
|
|
|
if (!orig_from_name.exists()) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "Not caching file `"
|
2006-11-13 10:27:57 +00:00
|
|
|
<< orig_from << "' (does not exist anymore)."
|
|
|
|
<< std::endl;
|
|
|
|
support::unlink(item.cache_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-05-15 11:34:26 +00:00
|
|
|
// Don't add items that are not in the cache anymore
|
|
|
|
// This can happen if two instances of LyX are running
|
|
|
|
// at the same time and update the index file independantly.
|
2007-10-18 19:29:32 +00:00
|
|
|
if (!item.cache_name.exists()) {
|
2007-05-15 11:34:26 +00:00
|
|
|
LYXERR(Debug::FILES) << "Not caching file `"
|
|
|
|
<< orig_from
|
|
|
|
<< "' (cached copy does not exist anymore)."
|
|
|
|
<< std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
// Delete the cached file if it is too old
|
2007-10-18 19:29:32 +00:00
|
|
|
if (difftime(now, item.cache_name.lastModified())
|
|
|
|
> lyxrc.converter_cache_maxage) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "Not caching file `"
|
2006-11-13 10:27:57 +00:00
|
|
|
<< orig_from << "' (too old)." << std::endl;
|
|
|
|
support::unlink(item.cache_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-05-17 08:31:00 +00:00
|
|
|
FormatCache & format_cache = cache[orig_from_name];
|
|
|
|
if (format_cache.from_format.empty())
|
|
|
|
format_cache.from_format =
|
|
|
|
formats.getFormatFromFile(orig_from_name);
|
|
|
|
format_cache.cache[to_format] = item;
|
2006-11-13 10:27:57 +00:00
|
|
|
}
|
|
|
|
is.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConverterCache::Impl::writeIndex()
|
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const index(addName(cache_dir.absFilename(), "index"));
|
|
|
|
std::ofstream os(index.toFilesystemEncoding().c_str());
|
2006-11-13 10:27:57 +00:00
|
|
|
os.close();
|
2006-11-26 21:30:39 +00:00
|
|
|
if (!lyx::support::chmod(index, 0600))
|
2006-11-13 10:27:57 +00:00
|
|
|
return;
|
2006-11-26 21:30:39 +00:00
|
|
|
os.open(index.toFilesystemEncoding().c_str());
|
2006-11-13 10:27:57 +00:00
|
|
|
CacheType::iterator it1 = cache.begin();
|
|
|
|
CacheType::iterator const end1 = cache.end();
|
|
|
|
for (; it1 != end1; ++it1) {
|
2007-05-17 08:31:00 +00:00
|
|
|
FormatCacheType const & format_cache = it1->second.cache;
|
|
|
|
FormatCacheType::const_iterator it2 = format_cache.begin();
|
|
|
|
FormatCacheType::const_iterator const end2 = format_cache.end();
|
2006-11-13 10:27:57 +00:00
|
|
|
for (; it2 != end2; ++it2)
|
2007-05-17 08:31:00 +00:00
|
|
|
os << Lexer::quoteString(it1->first.absFilename())
|
|
|
|
<< ' ' << it2->first << ' '
|
2006-11-13 10:27:57 +00:00
|
|
|
<< it2->second.timestamp << ' '
|
|
|
|
<< it2->second.checksum << '\n';
|
|
|
|
}
|
|
|
|
os.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
CacheItem * ConverterCache::Impl::find(FileName const & from,
|
2006-11-13 10:27:57 +00:00
|
|
|
string const & format)
|
|
|
|
{
|
|
|
|
if (!lyxrc.use_converter_cache)
|
|
|
|
return 0;
|
|
|
|
CacheType::iterator const it1 = cache.find(from);
|
|
|
|
if (it1 == cache.end())
|
|
|
|
return 0;
|
2007-05-17 08:31:00 +00:00
|
|
|
FormatCacheType & format_cache = it1->second.cache;
|
|
|
|
FormatCacheType::iterator const it2 = format_cache.find(format);
|
|
|
|
if (it2 == format_cache.end())
|
2006-11-13 10:27:57 +00:00
|
|
|
return 0;
|
|
|
|
return &(it2->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-07 21:25:22 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ConverterCache
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
ConverterCache::ConverterCache()
|
|
|
|
: pimpl_(new Impl)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
ConverterCache::~ConverterCache()
|
|
|
|
{
|
|
|
|
if (!lyxrc.use_converter_cache)
|
|
|
|
return;
|
|
|
|
pimpl_->writeIndex();
|
|
|
|
delete pimpl_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
ConverterCache & ConverterCache::get()
|
|
|
|
{
|
|
|
|
// Now return the cache
|
|
|
|
static ConverterCache singleton;
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConverterCache::init()
|
|
|
|
{
|
|
|
|
if (!lyxrc.use_converter_cache)
|
|
|
|
return;
|
|
|
|
// We do this here and not in the constructor because package() gets
|
|
|
|
// initialized after all static variables.
|
2007-04-06 13:09:54 +00:00
|
|
|
cache_dir = FileName(addName(support::package().user_support().absFilename(), "cache"));
|
2007-10-18 19:29:32 +00:00
|
|
|
if (!cache_dir.exists())
|
2006-11-13 10:27:57 +00:00
|
|
|
if (support::mkdir(cache_dir, 0700) != 0) {
|
|
|
|
lyxerr << "Could not create cache directory `"
|
|
|
|
<< cache_dir << "'." << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
get().pimpl_->readIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
void ConverterCache::add(FileName const & orig_from, string const & to_format,
|
|
|
|
FileName const & converted_file) const
|
2006-11-13 10:27:57 +00:00
|
|
|
{
|
2007-02-18 18:26:14 +00:00
|
|
|
if (!lyxrc.use_converter_cache || orig_from.empty() ||
|
|
|
|
converted_file.empty())
|
2006-11-13 10:27:57 +00:00
|
|
|
return;
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
|
2007-05-28 22:27:45 +00:00
|
|
|
<< ' ' << to_format << ' ' << converted_file
|
|
|
|
<< std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
|
2007-07-10 09:13:21 +00:00
|
|
|
// FIXME: Should not hardcode this (see bug 3819 for details)
|
|
|
|
if (to_format == "pstex") {
|
|
|
|
FileName const converted_eps(support::changeExtension(converted_file.absFilename(), "eps"));
|
|
|
|
add(orig_from, "eps", converted_eps);
|
|
|
|
} else if (to_format == "pdftex") {
|
|
|
|
FileName const converted_pdf(support::changeExtension(converted_file.absFilename(), "pdf"));
|
|
|
|
add(orig_from, "pdf", converted_pdf);
|
|
|
|
}
|
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
// Is the file in the cache already?
|
|
|
|
CacheItem * item = pimpl_->find(orig_from, to_format);
|
|
|
|
|
2007-10-18 19:29:32 +00:00
|
|
|
time_t const timestamp = orig_from.lastModified();
|
2007-01-18 08:42:53 +00:00
|
|
|
Mover const & mover = getMover(to_format);
|
2006-11-13 10:27:57 +00:00
|
|
|
if (item) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "ConverterCache::add(" << orig_from << "):\n"
|
2007-05-28 22:27:45 +00:00
|
|
|
"The file is already in the cache."
|
|
|
|
<< std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
// First test for timestamp
|
|
|
|
if (timestamp == item->timestamp) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "Same timestamp."
|
2007-05-28 22:27:45 +00:00
|
|
|
<< std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Maybe the contents is still the same?
|
|
|
|
item->timestamp = timestamp;
|
|
|
|
unsigned long const checksum = support::sum(orig_from);
|
|
|
|
if (checksum == item->checksum) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "Same checksum."
|
2007-05-28 22:27:45 +00:00
|
|
|
<< std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
item->checksum = checksum;
|
|
|
|
}
|
2007-07-10 09:13:21 +00:00
|
|
|
if (!mover.copy(converted_file, item->cache_name,
|
2007-09-16 10:36:57 +00:00
|
|
|
support::onlyFilename(item->cache_name.absFilename()), 0600)) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "ConverterCache::add("
|
2007-05-28 22:27:45 +00:00
|
|
|
<< orig_from << "):\n"
|
|
|
|
"Could not copy file."
|
|
|
|
<< std::endl;
|
2007-09-16 10:36:57 +00:00
|
|
|
}
|
2006-11-13 10:27:57 +00:00
|
|
|
} else {
|
2007-05-17 08:31:00 +00:00
|
|
|
CacheItem new_item(orig_from, to_format, timestamp,
|
2006-11-13 10:27:57 +00:00
|
|
|
support::sum(orig_from));
|
2007-07-10 09:13:21 +00:00
|
|
|
if (mover.copy(converted_file, new_item.cache_name,
|
|
|
|
support::onlyFilename(new_item.cache_name.absFilename()), 0600)) {
|
2007-05-17 08:31:00 +00:00
|
|
|
FormatCache & format_cache = pimpl_->cache[orig_from];
|
|
|
|
if (format_cache.from_format.empty())
|
|
|
|
format_cache.from_format =
|
|
|
|
formats.getFormatFromFile(orig_from);
|
|
|
|
format_cache.cache[to_format] = new_item;
|
|
|
|
} else
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "ConverterCache::add("
|
2007-05-28 22:27:45 +00:00
|
|
|
<< orig_from << "):\n"
|
|
|
|
"Could not copy file."
|
|
|
|
<< std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
void ConverterCache::remove(FileName const & orig_from,
|
2006-11-13 10:27:57 +00:00
|
|
|
string const & to_format) const
|
|
|
|
{
|
2007-02-18 18:26:14 +00:00
|
|
|
if (!lyxrc.use_converter_cache || orig_from.empty())
|
2006-11-13 10:27:57 +00:00
|
|
|
return;
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
|
2007-05-28 22:27:45 +00:00
|
|
|
<< ' ' << to_format << std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
|
|
|
|
CacheType::iterator const it1 = pimpl_->cache.find(orig_from);
|
|
|
|
if (it1 == pimpl_->cache.end())
|
|
|
|
return;
|
2007-05-17 08:31:00 +00:00
|
|
|
FormatCacheType & format_cache = it1->second.cache;
|
|
|
|
FormatCacheType::iterator const it2 = format_cache.find(to_format);
|
|
|
|
if (it2 == format_cache.end())
|
2006-11-13 10:27:57 +00:00
|
|
|
return;
|
|
|
|
|
2007-05-17 08:31:00 +00:00
|
|
|
format_cache.erase(it2);
|
|
|
|
if (format_cache.empty())
|
2006-11-13 10:27:57 +00:00
|
|
|
pimpl_->cache.erase(it1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-17 08:31:00 +00:00
|
|
|
void ConverterCache::remove_all(string const & from_format,
|
|
|
|
string const & to_format) const
|
|
|
|
{
|
|
|
|
if (!lyxrc.use_converter_cache)
|
|
|
|
return;
|
|
|
|
CacheType::iterator it1 = pimpl_->cache.begin();
|
|
|
|
while (it1 != pimpl_->cache.end()) {
|
|
|
|
if (it1->second.from_format != from_format) {
|
|
|
|
++it1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
FormatCacheType & format_cache = it1->second.cache;
|
|
|
|
FormatCacheType::iterator it2 = format_cache.begin();
|
|
|
|
while (it2 != format_cache.end()) {
|
|
|
|
if (it2->first == to_format) {
|
|
|
|
LYXERR(Debug::FILES)
|
|
|
|
<< "Removing file cache item "
|
|
|
|
<< it1->first
|
|
|
|
<< ' ' << to_format << std::endl;
|
|
|
|
support::unlink(it2->second.cache_name);
|
|
|
|
format_cache.erase(it2);
|
|
|
|
// Have to start over again since items in a
|
|
|
|
// map are not ordered
|
|
|
|
it2 = format_cache.begin();
|
|
|
|
} else
|
|
|
|
++it2;
|
|
|
|
}
|
|
|
|
if (format_cache.empty()) {
|
|
|
|
pimpl_->cache.erase(it1);
|
|
|
|
// Have to start over again since items in a map are
|
|
|
|
// not ordered
|
|
|
|
it1 = pimpl_->cache.begin();
|
|
|
|
} else
|
|
|
|
++it1;
|
|
|
|
}
|
|
|
|
pimpl_->writeIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
bool ConverterCache::inCache(FileName const & orig_from,
|
2006-11-13 10:27:57 +00:00
|
|
|
string const & to_format) const
|
|
|
|
{
|
2007-02-18 18:26:14 +00:00
|
|
|
if (!lyxrc.use_converter_cache || orig_from.empty())
|
2006-11-13 10:27:57 +00:00
|
|
|
return false;
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
|
2007-05-28 22:27:45 +00:00
|
|
|
<< ' ' << to_format << std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
|
|
|
|
CacheItem * const item = pimpl_->find(orig_from, to_format);
|
|
|
|
if (!item) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "not in cache." << std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-11-07 22:04:02 +00:00
|
|
|
time_t const timestamp = orig_from.lastModified();
|
2006-11-13 10:27:57 +00:00
|
|
|
if (item->timestamp == timestamp) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "identical timestamp." << std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (item->checksum == support::sum(orig_from)) {
|
|
|
|
item->timestamp = timestamp;
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "identical checksum." << std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << "in cache, but too old." << std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const & ConverterCache::cacheName(FileName const & orig_from,
|
2006-11-13 10:27:57 +00:00
|
|
|
string const & to_format) const
|
|
|
|
{
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
|
2007-05-28 22:27:45 +00:00
|
|
|
<< ' ' << to_format << std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
|
|
|
|
CacheItem * const item = pimpl_->find(orig_from, to_format);
|
|
|
|
BOOST_ASSERT(item);
|
|
|
|
return item->cache_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
bool ConverterCache::copy(FileName const & orig_from, string const & to_format,
|
|
|
|
FileName const & dest) const
|
2006-11-13 10:27:57 +00:00
|
|
|
{
|
2007-02-18 18:26:14 +00:00
|
|
|
if (!lyxrc.use_converter_cache || orig_from.empty() || dest.empty())
|
2006-11-13 10:27:57 +00:00
|
|
|
return false;
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::FILES) << BOOST_CURRENT_FUNCTION << ' ' << orig_from
|
2007-05-28 22:27:45 +00:00
|
|
|
<< ' ' << to_format << ' ' << dest << std::endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
|
2007-07-10 09:13:21 +00:00
|
|
|
// FIXME: Should not hardcode this (see bug 3819 for details)
|
|
|
|
if (to_format == "pstex") {
|
|
|
|
FileName const dest_eps(support::changeExtension(dest.absFilename(), "eps"));
|
|
|
|
if (!copy(orig_from, "eps", dest_eps))
|
|
|
|
return false;
|
|
|
|
} else if (to_format == "pdftex") {
|
|
|
|
FileName const dest_pdf(support::changeExtension(dest.absFilename(), "pdf"));
|
|
|
|
if (!copy(orig_from, "pdf", dest_pdf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
CacheItem * const item = pimpl_->find(orig_from, to_format);
|
|
|
|
BOOST_ASSERT(item);
|
2007-01-18 08:42:53 +00:00
|
|
|
Mover const & mover = getMover(to_format);
|
2007-07-10 09:13:21 +00:00
|
|
|
return mover.copy(item->cache_name, dest,
|
|
|
|
support::onlyFilename(dest.absFilename()));
|
2006-11-13 10:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace lyx
|