From 377ae30bd8dd025e6e676691b9142c086aad87e9 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Fri, 30 Nov 2007 09:23:44 +0000 Subject: [PATCH] lyxlib.h:sum -> FileName::checksum() git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21874 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Buffer.cpp | 7 +++---- src/Buffer.h | 2 +- src/ConverterCache.cpp | 6 +++--- src/DepTable.cpp | 5 ++--- src/EmbeddedFiles.cpp | 5 ++--- src/client/client.cpp | 33 +++++++++++++------------------- src/graphics/GraphicsCacheItem.h | 2 +- src/graphics/GraphicsLoader.h | 2 +- src/insets/ExternalSupport.cpp | 4 ++-- src/insets/InsetGraphics.cpp | 6 +++--- src/insets/InsetInclude.cpp | 5 ++--- src/support/FileMonitor.cpp | 6 +++--- src/support/FileName.cpp | 17 ++++++++++++++++ src/support/FileName.h | 2 ++ src/support/lyxlib.h | 2 -- src/support/lyxsum.cpp | 17 ++++++---------- 16 files changed, 61 insertions(+), 60 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index a435455dd6..15bd4e468d 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -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; diff --git a/src/Buffer.h b/src/Buffer.h index d5703ba34a..9b8b8e22a1 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -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 }; diff --git a/src/ConverterCache.cpp b/src/ConverterCache.cpp index 2789b686e8..fe5e813fb5 100644 --- a/src/ConverterCache.cpp +++ b/src/ConverterCache.cpp @@ -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; diff --git a/src/DepTable.cpp b/src/DepTable.cpp index f93752d950..8ebb08aa06 100644 --- a/src/DepTable.cpp +++ b/src/DepTable.cpp @@ -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 { diff --git a/src/EmbeddedFiles.cpp b/src/EmbeddedFiles.cpp index d1d15cb1c9..e5d56e2437 100644 --- a/src/EmbeddedFiles.cpp +++ b/src/EmbeddedFiles.cpp @@ -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( diff --git a/src/client/client.cpp b/src/client/client.cpp index 5540f1a2f5..bf96e3e9b5 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -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 lyxSockets(string const & dir, string const & pid) +vector lyxSockets(string const & dir, string const & pid) { - vector dirlist; + FileName dirpath(dir); - fs::path dirpath(dir); + vector dirlist = dirpath.dirList(); + vector::iterator it = dirlist.begin(); + vector::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 addrs = support::lyxSockets(to_filesystem8bit(cmdline::mainTmp), cmdline::serverPid); - vector::const_iterator addr = addrs.begin(); - vector::const_iterator end = addrs.end(); + vector addrs = support::lyxSockets(to_filesystem8bit(cmdline::mainTmp), cmdline::serverPid); + vector::const_iterator addr = addrs.begin(); + vector::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." diff --git a/src/graphics/GraphicsCacheItem.h b/src/graphics/GraphicsCacheItem.h index 34dad4b265..3f8f7e100f 100644 --- a/src/graphics/GraphicsCacheItem.h +++ b/src/graphics/GraphicsCacheItem.h @@ -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. */ diff --git a/src/graphics/GraphicsLoader.h b/src/graphics/GraphicsLoader.h index b1c82d9067..c4ed587f66 100644 --- a/src/graphics/GraphicsLoader.h +++ b/src/graphics/GraphicsLoader.h @@ -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. */ diff --git a/src/insets/ExternalSupport.cpp b/src/insets/ExternalSupport.cpp index 2bb9665951..4da96e81bc 100644 --- a/src/insets/ExternalSupport.cpp +++ b/src/insets/ExternalSupport.cpp @@ -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); diff --git a/src/insets/InsetGraphics.cpp b/src/insets/InsetGraphics.cpp index 43d698c8d0..a5d6c61a42 100644 --- a/src/insets/InsetGraphics.cpp +++ b/src/insets/InsetGraphics.cpp @@ -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 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... diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index fb5fe5acbd..06b41c37c4 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -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)) { diff --git a/src/support/FileMonitor.cpp b/src/support/FileMonitor.cpp index 1abca468ed..d9571bcc01 100644 --- a/src/support/FileMonitor.cpp +++ b/src/support/FileMonitor.cpp @@ -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; diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index 115f97beb7..7c594da86c 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -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()); diff --git a/src/support/FileName.h b/src/support/FileName.h index 37180011e0..0151c7fa81 100644 --- a/src/support/FileName.h +++ b/src/support/FileName.h @@ -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 diff --git a/src/support/lyxlib.h b/src/support/lyxlib.h index f2b73eb8bb..f2cfaa6bdb 100644 --- a/src/support/lyxlib.h +++ b/src/support/lyxlib.h @@ -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 diff --git a/src/support/lyxsum.cpp b/src/support/lyxsum.cpp index 488395ac32..1463927a68 100644 --- a/src/support/lyxsum.cpp +++ b/src/support/lyxsum.cpp @@ -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;