lyxlib.h:sum -> FileName::checksum()

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21874 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2007-11-30 09:23:44 +00:00
parent 334a76e1d7
commit 377ae30bd8
16 changed files with 61 additions and 60 deletions

View File

@ -145,7 +145,6 @@ using support::split;
using support::subst;
using support::tempName;
using support::trim;
using support::sum;
using support::suffixIs;
namespace Alert = frontend::Alert;
@ -760,7 +759,7 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
// when it has to be converted to the current format.
if (!pimpl_->checksum_) {
// Save the timestamp and checksum of disk file. If filename is an
// emergency file, save the timestamp and sum of the original lyx file
// emergency file, save the timestamp and checksum of the original lyx file
// because isExternallyModified will check for this file. (BUG4193)
string diskfile = filename.absFilename();
if (suffixIs(diskfile, ".emergency"))
@ -1624,7 +1623,7 @@ bool Buffer::isExternallyModified(CheckMethod method) const
// if method == timestamp, check timestamp before checksum
return (method == checksum_method
|| pimpl_->timestamp_ != pimpl_->filename.lastModified())
&& pimpl_->checksum_ != sum(pimpl_->filename);
&& pimpl_->checksum_ != pimpl_->filename.checksum();
}
@ -1632,7 +1631,7 @@ void Buffer::saveCheckSum(FileName const & file) const
{
if (file.exists()) {
pimpl_->timestamp_ = file.lastModified();
pimpl_->checksum_ = sum(file);
pimpl_->checksum_ = file.checksum();
} else {
// in the case of save to a new file.
pimpl_->timestamp_ = 0;

View File

@ -92,7 +92,7 @@ public:
* system.
*/
enum CheckMethod {
checksum_method, ///< Use file check sum
checksum_method, ///< Use file checksum
timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
};

View File

@ -284,7 +284,7 @@ void ConverterCache::add(FileName const & orig_from, string const & to_format,
}
// Maybe the contents is still the same?
item->timestamp = timestamp;
unsigned long const checksum = support::sum(orig_from);
unsigned long const checksum = orig_from.checksum();
if (checksum == item->checksum) {
LYXERR(Debug::FILES, "Same checksum.");
return;
@ -297,7 +297,7 @@ void ConverterCache::add(FileName const & orig_from, string const & to_format,
}
} else {
CacheItem new_item(orig_from, to_format, timestamp,
support::sum(orig_from));
orig_from.checksum());
if (mover.copy(converted_file, new_item.cache_name,
support::onlyFilename(new_item.cache_name.absFilename()), 0600)) {
FormatCache & format_cache = pimpl_->cache[orig_from];
@ -389,7 +389,7 @@ bool ConverterCache::inCache(FileName const & orig_from,
LYXERR(Debug::FILES, "identical timestamp.");
return true;
}
if (item->checksum == support::sum(orig_from)) {
if (item->checksum == orig_from.checksum()) {
item->timestamp = timestamp;
LYXERR(Debug::FILES, "identical checksum.");
return true;

View File

@ -38,7 +38,6 @@ using support::FileName;
using support::ltrim;
using support::onlyFilename;
using support::suffixIs;
using support::sum;
inline
bool DepTable::dep_info::changed() const
@ -54,7 +53,7 @@ void DepTable::insert(FileName const & f, bool upd)
di.crc_prev = 0;
if (upd) {
LYXERR(Debug::DEPEND, " CRC...");
di.crc_cur = sum(f);
di.crc_cur = f.checksum();
LYXERR(Debug::DEPEND, "done.");
struct stat f_info;
stat(f.toFilesystemEncoding().c_str(), &f_info);
@ -87,7 +86,7 @@ void DepTable::update()
} else {
di.crc_prev = di.crc_cur;
LYXERR(Debug::DEPEND, itr->first << " CRC... ");
di.crc_cur = sum(itr->first);
di.crc_cur = itr->first.checksum();
LYXERR(Debug::DEPEND, "done");
}
} else {

View File

@ -62,7 +62,6 @@ using support::makeRelPath;
using support::changeExtension;
using support::bformat;
using support::prefixIs;
using support::sum;
using support::makedir;
@ -138,7 +137,7 @@ bool EmbeddedFile::extract(Buffer const * buf) const
// if external file already exists ...
if (ext.exists()) {
// no need to copy if the files are the same
if (sum(*this) == sum(FileName(emb_file)))
if (checksum() == FileName(emb_file).checksum())
return true;
// otherwise, ask if overwrite
int ret = Alert::prompt(
@ -181,7 +180,7 @@ bool EmbeddedFile::updateFromExternalFile(Buffer const * buf) const
// if embedded file already exists ...
if (emb.exists()) {
// no need to copy if the files are the same
if (sum(*this) == sum(FileName(emb_file)))
if (checksum() == FileName(emb_file).checksum())
return true;
// other wise, ask if overwrite
int const ret = Alert::prompt(

View File

@ -84,24 +84,17 @@ string itoa(unsigned int i)
/// Returns the absolute pathnames of all lyx local sockets in
/// file system encoding.
/// Parts stolen from lyx::support::DirList().
vector<fs::path> lyxSockets(string const & dir, string const & pid)
vector<FileName> lyxSockets(string const & dir, string const & pid)
{
vector<fs::path> dirlist;
FileName dirpath(dir);
fs::path dirpath(dir);
vector<FileName> dirlist = dirpath.dirList();
vector<FileName>::iterator it = dirlist.begin();
vector<FileName>::iterator const end = dirlist.end();
if (!fs::exists(dirpath) || !fs::is_directory(dirpath)) {
lyxerr << dir << " does not exist or is not a directory."
<< endl;
return dirlist;
}
fs::directory_iterator beg((fs::path(dir)));
fs::directory_iterator end;
for (; beg != end; ++beg) {
if (prefixIs(beg->leaf(), "lyx_tmpdir" + pid)) {
fs::path lyxsocket = beg->path() / "lyxsocket";
for (; it != end; ++it) {
if (prefixIs(it->baseName(), "lyx_tmpdir" + pid)) {
FileName lyxsocket(it->abs() + "lyxsocket";
if (fs::exists(lyxsocket)) {
dirlist.push_back(lyxsocket);
}
@ -562,16 +555,16 @@ int main(int argc, char * argv[])
} else {
// We have to look for an address.
// serverPid can be empty.
vector<fs::path> addrs = support::lyxSockets(to_filesystem8bit(cmdline::mainTmp), cmdline::serverPid);
vector<fs::path>::const_iterator addr = addrs.begin();
vector<fs::path>::const_iterator end = addrs.end();
vector<FileName> addrs = support::lyxSockets(to_filesystem8bit(cmdline::mainTmp), cmdline::serverPid);
vector<FileName>::const_iterator addr = addrs.begin();
vector<FileName>::const_iterator end = addrs.end();
for (; addr != end; ++addr) {
// Caution: addr->string() is in filesystem encoding
server.reset(new LyXDataSocket(FileName(to_utf8(from_filesystem8bit(addr->string())))));
server.reset(new LyXDataSocket(*addr));
if (server->connected())
break;
lyxerr << "lyxclient: " << "Could not connect to "
<< addr->string() << endl;
<< addr->absFileName() << endl;
}
if (addr == end) {
lyxerr << "lyxclient: No suitable server found."

View File

@ -63,7 +63,7 @@ public:
void startMonitoring() const;
///
bool monitoring() const;
/** Returns the check sum of filename() so that, for example, you can
/** Returns the check checksum of filename() so that, for example, you can
* ascertain whether to output a new PostScript version of the file
* for a LaTeX run.
*/

View File

@ -76,7 +76,7 @@ public:
void startMonitoring() const;
///
bool monitoring() const;
/** Returns the check sum of filename() so that, for example, you can
/** Returns the check checksum of filename() so that, for example, you can
* ascertain whether to output a new PostScript version of the file
* for a LaTeX run.
*/

View File

@ -255,8 +255,8 @@ void updateExternal(InsetExternalParams const & params,
support::makeAbsPath(params.filename.mangledFilename(),
masterBuffer->temppath()));
if (!params.filename.empty() && !params.filename.isDirectory()) {
unsigned long const from_checksum = support::sum(params.filename);
unsigned long const temp_checksum = support::sum(temp_file);
unsigned long const from_checksum = params.filename.checksum();
unsigned long const temp_checksum = temp_file.checksum();
if (from_checksum != temp_checksum) {
Mover const & mover = getMover(from_format);

View File

@ -78,7 +78,7 @@ TODO
#include "support/convert.h"
#include "support/docstream.h"
#include "support/filetools.h"
#include "support/lyxlib.h" // sum
#include "support/lyxlib.h"
#include "support/lstrings.h"
#include "support/os.h"
#include "support/Systemcall.h"
@ -495,8 +495,8 @@ enum GraphicsCopyStatus {
std::pair<GraphicsCopyStatus, FileName> const
copyFileIfNeeded(FileName const & file_in, FileName const & file_out)
{
unsigned long const checksum_in = support::sum(file_in);
unsigned long const checksum_out = support::sum(file_out);
unsigned long const checksum_in = file_in.checksum();
unsigned long const checksum_out = file_out.checksum();
if (checksum_in == checksum_out)
// Nothing to do...

View File

@ -73,7 +73,6 @@ using support::onlyFilename;
using support::onlyPath;
using support::prefixIs;
using support::subst;
using support::sum;
using std::find;
using std::string;
@ -463,8 +462,8 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
// to the temp dir, so that .aux files etc. are not created
// in the original dir. Files included by this file will be
// found via input@path, see ../Buffer.cpp.
unsigned long const checksum_in = sum(included_file);
unsigned long const checksum_out = sum(writefile);
unsigned long const checksum_in = included_file.checksum();
unsigned long const checksum_out = writefile.checksum();
if (checksum_in != checksum_out) {
if (!copy(included_file, writefile)) {

View File

@ -94,7 +94,7 @@ void FileMonitor::start() const
return;
pimpl_->timestamp_ = pimpl_->filename_.lastModified();
pimpl_->checksum_ = sum(pimpl_->filename_);
pimpl_->checksum_ = pimpl_->filename_.checksum();
if (pimpl_->timestamp_ && pimpl_->checksum_) {
pimpl_->timer_.start();
@ -124,7 +124,7 @@ unsigned long FileMonitor::checksum() const
// If we aren't actively monitoring the file, then recompute the
// checksum explicitly.
if (!pimpl_->timer_.running() && !pimpl_->filename_.empty())
return sum(pimpl_->filename_);
return pimpl_->filename_.checksum();
return pimpl_->checksum_;
}
@ -166,7 +166,7 @@ void FileMonitor::Impl::monitorFile()
if (new_timestamp != timestamp_) {
timestamp_ = new_timestamp;
unsigned long const new_checksum = sum(filename_);
unsigned long const new_checksum = filename_.checksum();
if (new_checksum != checksum_) {
checksum_ = new_checksum;
changed = true;

View File

@ -236,6 +236,23 @@ std::time_t FileName::lastModified() const
}
extern unsigned long sum(char const * file);
unsigned long FileName::checksum() const
{
if (!exists()) {
LYXERR0("File \"" << absFilename() << "\" does not exist!");
return 0;
}
// a directory may be passed here so we need to test it. (bug 3622)
if (isDirectory()) {
LYXERR0('\\' << absFilename() << "\" is a directory!");
return 0;
}
return sum(absFilename().c_str());
}
bool FileName::removeFile() const
{
bool const success = QFile::remove(d->fi.absoluteFilePath());

View File

@ -71,6 +71,8 @@ public:
bool isFileEmpty() const;
/// returns time of last write access
std::time_t lastModified() const;
/// generates a checksum of a file
unsigned long checksum() const;
/// return true when file is readable but not writabel
bool isReadOnly() const;
/// return true when it names a directory

View File

@ -37,8 +37,6 @@ bool rename(FileName const & from, FileName const & to);
/// copy a file, returns false it it fails
bool copy(FileName const & from, FileName const & to,
unsigned long int mode = (unsigned long int)-1);
/// generates a checksum of a file
unsigned long sum(FileName const & file);
/// FIXME: some point to this hmm ?
int kill(int pid, int sig);
/// FIXME: same here

View File

@ -52,11 +52,11 @@ template struct boost::detail::crc_table_t<32, 0x04C11DB7, true>;
namespace lyx {
namespace support {
unsigned long sum(FileName const & file)
unsigned long sum(char const * file)
{
LYXERR(Debug::FILES, "lyx::sum() using mmap (lightning fast)");
int fd = open(file.toFilesystemEncoding().c_str(), O_RDONLY);
int fd = open(file, O_RDONLY);
if (!fd)
return 0;
@ -114,15 +114,11 @@ using std::ifstream;
#if HAVE_DECL_ISTREAMBUF_ITERATOR
using std::istreambuf_iterator;
unsigned long sum(FileName const & file)
unsigned long sum(char const * file)
{
LYXERR(Debug::FILES, "lyx::sum() using istreambuf_iterator (fast)");
// a directory may be passed here so we need to test it. (bug 3622)
if (file.isDirectory())
return 0;
string filename = file.toFilesystemEncoding();
ifstream ifs(filename.c_str(), std::ios_base::in | std::ios_base::binary);
ifstream ifs(file, std::ios_base::in | std::ios_base::binary);
if (!ifs)
return 0;
@ -136,7 +132,7 @@ unsigned long sum(FileName const & file)
using std::istream_iterator;
using std::ios;
unsigned long sum(FileName const & file)
unsigned long sum(char const * file)
{
LYXERR(Debug::FILES, "lyx::sum() using istream_iterator (slow as a snail)");
@ -144,8 +140,7 @@ unsigned long sum(FileName const & file)
if (file.isDirectory())
return 0;
string filename = file.toFilesystemEncoding();
ifstream ifs(filename.c_str(), std::ios_base::in | std::ios_base::binary);
ifstream ifs(file, std::ios_base::in | std::ios_base::binary);
if (!ifs)
return 0;