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"
|
2006-11-13 10:27:57 +00:00
|
|
|
|
2007-05-17 08:31:00 +00:00
|
|
|
#include "support/convert.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2006-11-13 10:27:57 +00:00
|
|
|
#include "support/filetools.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
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2006-11-13 10:27:57 +00:00
|
|
|
#include <boost/crc.hpp>
|
|
|
|
|
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>
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2006-11-13 10:27:57 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
unsigned long do_crc(string const & s)
|
|
|
|
{
|
|
|
|
boost::crc_32_type crc;
|
2007-12-12 19:28:07 +00:00
|
|
|
crc = for_each(s.begin(), s.end(), crc);
|
2006-11-13 10:27:57 +00:00
|
|
|
return crc.checksum();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-07 22:59:05 +00:00
|
|
|
// FIXME THREAD
|
|
|
|
// This should be OK because it is only assigned during init()
|
2006-11-26 21:30:39 +00:00
|
|
|
static FileName cache_dir;
|
2006-11-13 10:27:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CacheItem {
|
|
|
|
public:
|
2015-03-04 21:50:37 +00:00
|
|
|
CacheItem() : timestamp(0), checksum(0) {}
|
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)
|
|
|
|
{
|
2007-12-12 19:28:07 +00:00
|
|
|
ostringstream os;
|
2010-04-21 01:19:09 +00:00
|
|
|
os << setw(10) << setfill('0') << do_crc(orig_from.absFileName())
|
2006-11-13 10:27:57 +00:00
|
|
|
<< '-' << to_format;
|
2010-04-21 01:19:09 +00:00
|
|
|
cache_name = FileName(addName(cache_dir.absFileName(), os.str()));
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "Add file cache item " << orig_from
|
2007-05-28 22:27:45 +00:00
|
|
|
<< ' ' << to_format << ' ' << cache_name
|
2007-11-29 09:00:28 +00:00
|
|
|
<< ' ' << long(timestamp) << ' ' << checksum << '.');
|
2006-11-13 10:27:57 +00:00
|
|
|
}
|
2012-10-27 13:45:27 +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.
|
|
|
|
*/
|
2007-12-12 19:28:07 +00:00
|
|
|
typedef 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;
|
|
|
|
};
|
2007-12-12 19:28:07 +00:00
|
|
|
typedef 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();
|
2010-04-21 01:19:09 +00:00
|
|
|
FileName const index(addName(cache_dir.absFileName(), "index"));
|
2007-12-12 19:28:07 +00:00
|
|
|
ifstream is(index.toFilesystemEncoding().c_str());
|
2008-04-02 23:06:22 +00:00
|
|
|
Lexer lex;
|
2007-05-17 08:31:00 +00:00
|
|
|
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-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "Not caching file `"
|
|
|
|
<< orig_from << "' (does not exist anymore).");
|
2007-11-28 09:01:49 +00:00
|
|
|
item.cache_name.removeFile();
|
2006-11-13 10:27:57 +00:00
|
|
|
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-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "Not caching file `" << orig_from
|
|
|
|
<< "' (cached copy does not exist anymore).");
|
2007-05-15 11:34:26 +00:00
|
|
|
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-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "Not caching file `"
|
|
|
|
<< orig_from << "' (too old).");
|
2007-11-28 09:01:49 +00:00
|
|
|
item.cache_name.removeFile();
|
2006-11-13 10:27:57 +00:00
|
|
|
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 =
|
2016-07-28 15:52:14 +00:00
|
|
|
// FIXME perf: This very expensive function is called on all
|
|
|
|
// cached files on opening. This slows LyX startup a lot. It
|
|
|
|
// would be better if this information was retrieved in a
|
|
|
|
// delayed fashion.
|
2017-03-14 03:33:40 +00:00
|
|
|
theFormats().getFormatFromFile(orig_from_name);
|
2007-05-17 08:31:00 +00:00
|
|
|
format_cache.cache[to_format] = item;
|
2006-11-13 10:27:57 +00:00
|
|
|
}
|
|
|
|
is.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConverterCache::Impl::writeIndex()
|
|
|
|
{
|
2010-04-21 01:19:09 +00:00
|
|
|
FileName const index(addName(cache_dir.absFileName(), "index"));
|
2007-12-12 19:28:07 +00:00
|
|
|
ofstream os(index.toFilesystemEncoding().c_str());
|
2006-11-13 10:27:57 +00:00
|
|
|
os.close();
|
2007-12-13 11:24:10 +00:00
|
|
|
if (!index.changePermission(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)
|
2010-04-21 01:19:09 +00:00
|
|
|
os << Lexer::quoteString(it1->first.absFileName())
|
2007-05-17 08:31:00 +00:00
|
|
|
<< ' ' << 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()
|
|
|
|
{
|
|
|
|
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.
|
2010-04-21 01:19:09 +00:00
|
|
|
cache_dir = FileName(addName(package().user_support().absFileName(), "cache"));
|
2007-10-18 19:29:32 +00:00
|
|
|
if (!cache_dir.exists())
|
2007-12-14 22:35:23 +00:00
|
|
|
if (!cache_dir.createDirectory(0700)) {
|
2006-11-13 10:27:57 +00:00
|
|
|
lyxerr << "Could not create cache directory `"
|
2007-12-12 19:28:07 +00:00
|
|
|
<< cache_dir << "'." << endl;
|
2006-11-13 10:27:57 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
get().pimpl_->readIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-29 23:43:23 +00:00
|
|
|
void ConverterCache::writeIndex() const
|
2009-05-10 00:03:14 +00:00
|
|
|
{
|
2010-03-25 18:03:49 +00:00
|
|
|
if (!lyxrc.use_converter_cache
|
|
|
|
|| cache_dir.empty())
|
|
|
|
return;
|
2009-05-10 00:03:14 +00:00
|
|
|
pimpl_->writeIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-11-29 21:10:35 +00:00
|
|
|
LYXERR(Debug::FILES, ' ' << orig_from
|
2007-11-15 20:04:51 +00:00
|
|
|
<< ' ' << to_format << ' ' << converted_file);
|
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") {
|
2010-04-21 01:19:09 +00:00
|
|
|
FileName const converted_eps(changeExtension(converted_file.absFileName(), "eps"));
|
2007-07-10 09:13:21 +00:00
|
|
|
add(orig_from, "eps", converted_eps);
|
|
|
|
} else if (to_format == "pdftex") {
|
2010-04-21 01:19:09 +00:00
|
|
|
FileName const converted_pdf(changeExtension(converted_file.absFileName(), "pdf"));
|
2013-03-21 19:13:02 +00:00
|
|
|
add(orig_from, "pdf6", converted_pdf);
|
2007-07-10 09:13:21 +00:00
|
|
|
}
|
|
|
|
|
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-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
|
|
|
|
"The file is already in the cache.");
|
2006-11-13 10:27:57 +00:00
|
|
|
// First test for timestamp
|
|
|
|
if (timestamp == item->timestamp) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "Same timestamp.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Maybe the contents is still the same?
|
|
|
|
item->timestamp = timestamp;
|
2007-11-30 09:23:44 +00:00
|
|
|
unsigned long const checksum = orig_from.checksum();
|
2007-11-15 20:04:51 +00:00
|
|
|
if (checksum == item->checksum) {
|
|
|
|
LYXERR(Debug::FILES, "Same checksum.");
|
2006-11-13 10:27:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-11-15 20:04:51 +00:00
|
|
|
item->checksum = checksum;
|
2008-10-08 15:07:09 +00:00
|
|
|
if (!mover.copy(converted_file, item->cache_name,
|
2010-04-21 01:19:28 +00:00
|
|
|
onlyFileName(item->cache_name.absFileName()))) {
|
2007-12-13 08:20:45 +00:00
|
|
|
LYXERR(Debug::FILES, "Could not copy file " << orig_from << " to "
|
|
|
|
<< item->cache_name);
|
2007-12-13 10:59:31 +00:00
|
|
|
} else if (!item->cache_name.changePermission(0600)) {
|
2007-12-13 08:20:45 +00:00
|
|
|
LYXERR(Debug::FILES, "Could not change file mode"
|
|
|
|
<< item->cache_name);
|
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,
|
2007-11-30 09:23:44 +00:00
|
|
|
orig_from.checksum());
|
2008-10-08 15:07:09 +00:00
|
|
|
if (mover.copy(converted_file, new_item.cache_name,
|
2010-04-21 01:19:28 +00:00
|
|
|
onlyFileName(new_item.cache_name.absFileName()))) {
|
2007-12-13 10:59:31 +00:00
|
|
|
if (!new_item.cache_name.changePermission(0600)) {
|
2007-12-13 08:20:45 +00:00
|
|
|
LYXERR(Debug::FILES, "Could not change file mode"
|
2007-12-13 09:02:58 +00:00
|
|
|
<< new_item.cache_name);
|
2007-12-13 08:20:45 +00:00
|
|
|
}
|
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 =
|
2017-03-14 03:33:40 +00:00
|
|
|
theFormats().getFormatFromFile(orig_from);
|
2007-05-17 08:31:00 +00:00
|
|
|
format_cache.cache[to_format] = new_item;
|
|
|
|
} else
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "ConverterCache::add(" << orig_from << "):\n"
|
|
|
|
"Could not copy file.");
|
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-11-29 21:10:35 +00:00
|
|
|
LYXERR(Debug::FILES, orig_from << ' ' << to_format);
|
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) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "Removing file cache item "
|
|
|
|
<< it1->first << ' ' << to_format);
|
2007-11-28 09:01:49 +00:00
|
|
|
it2->second.cache_name.removeFile();
|
2007-05-17 08:31:00 +00:00
|
|
|
format_cache.erase(it2);
|
|
|
|
// Have to start over again since items in a
|
|
|
|
// map are not ordered
|
|
|
|
it2 = format_cache.begin();
|
2007-11-15 20:04:51 +00:00
|
|
|
} else {
|
2007-05-17 08:31:00 +00:00
|
|
|
++it2;
|
2007-11-15 20:04:51 +00:00
|
|
|
}
|
2007-05-17 08:31:00 +00:00
|
|
|
}
|
|
|
|
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();
|
2007-11-15 20:04:51 +00:00
|
|
|
} else {
|
2007-05-17 08:31:00 +00:00
|
|
|
++it1;
|
2007-11-15 20:04:51 +00:00
|
|
|
}
|
2007-05-17 08:31:00 +00:00
|
|
|
}
|
|
|
|
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-11-29 21:10:35 +00:00
|
|
|
LYXERR(Debug::FILES, orig_from << ' ' << to_format);
|
2006-11-13 10:27:57 +00:00
|
|
|
|
|
|
|
CacheItem * const item = pimpl_->find(orig_from, to_format);
|
|
|
|
if (!item) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "not in cache.");
|
2006-11-13 10:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-21 19:13:02 +00:00
|
|
|
|
|
|
|
// Special handling of pstex and pdftex formats: These are only
|
|
|
|
// considered to be in the cache if the corresponding graphics
|
|
|
|
// fiels are there as well. Otherwise copy() of the graphics below
|
|
|
|
// would fail.
|
|
|
|
// FIXME: Should not hardcode this (see bug 3819 for details)
|
|
|
|
if (to_format == "pstex") {
|
|
|
|
if (!inCache(orig_from, "eps"))
|
|
|
|
return false;
|
|
|
|
} else if (to_format == "pdftex") {
|
|
|
|
if (!inCache(orig_from, "pdf6"))
|
|
|
|
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-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "identical timestamp.");
|
2006-11-13 10:27:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-11-30 09:23:44 +00:00
|
|
|
if (item->checksum == orig_from.checksum()) {
|
2006-11-13 10:27:57 +00:00
|
|
|
item->timestamp = timestamp;
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "identical checksum.");
|
2006-11-13 10:27:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "in cache, but too old.");
|
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-11-29 21:10:35 +00:00
|
|
|
LYXERR(Debug::FILES, orig_from << ' ' << to_format);
|
2006-11-13 10:27:57 +00:00
|
|
|
|
|
|
|
CacheItem * const item = pimpl_->find(orig_from, to_format);
|
2013-04-25 21:27:10 +00:00
|
|
|
LASSERT(item, { static const FileName fn; return fn; });
|
2006-11-13 10:27:57 +00:00
|
|
|
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-11-29 21:10:35 +00:00
|
|
|
LYXERR(Debug::FILES, orig_from << ' ' << to_format << ' ' << dest);
|
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") {
|
2010-04-21 01:19:09 +00:00
|
|
|
FileName const dest_eps(changeExtension(dest.absFileName(), "eps"));
|
2007-07-10 09:13:21 +00:00
|
|
|
if (!copy(orig_from, "eps", dest_eps))
|
|
|
|
return false;
|
|
|
|
} else if (to_format == "pdftex") {
|
2010-04-21 01:19:09 +00:00
|
|
|
FileName const dest_pdf(changeExtension(dest.absFileName(), "pdf"));
|
2013-03-21 19:13:02 +00:00
|
|
|
if (!copy(orig_from, "pdf6", dest_pdf))
|
2007-07-10 09:13:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
CacheItem * const item = pimpl_->find(orig_from, to_format);
|
2013-04-25 21:27:10 +00:00
|
|
|
LASSERT(item, return false);
|
2007-01-18 08:42:53 +00:00
|
|
|
Mover const & mover = getMover(to_format);
|
2008-10-08 15:07:09 +00:00
|
|
|
return mover.copy(item->cache_name, dest,
|
2010-04-21 01:19:28 +00:00
|
|
|
onlyFileName(dest.absFileName()));
|
2006-11-13 10:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace lyx
|