Catch exceptions whenever filesystem::exists is called. Hopefully definitely fixes all those cases that have been reported (bug 4052, bug 4440, bug 4534)

* src/support/filetools.{cpp,h}:
	- new helper function doesFileExist which calls fs::exists and catches exceptions.

* src/Buffer.cpp:
* srcConverterCache.cpp:
* src/Exporter.cpp:
* src/Format.cpp:
* src/LaTeX.cpp:
* src/LyX.cpp:
* src/LyXFunc.cpp:
* src/Session.cpp:
* src/TextClass.cpp:
* src/TextClassList.cpp:
* src/buffer_funcs.cpp:
* src/callback.cpp:
* src/frontends/Controllers/ControlGraphics.cpp:
* src/insets/InsetInclude.cpp:
* src/support/FileMonitor.cpp:
* src/tex2lyx/text.cpp:
	- use doesFileExist instead of plain fs::exists

* src/client/client.cpp:
	- catch exception when calling fs::exists

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/branches/BRANCH_1_5_X@22937 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Jürgen Spitzmüller 2008-02-11 08:23:28 +00:00
parent 3689e9ca67
commit 81876c4aed
19 changed files with 120 additions and 86 deletions

View File

@ -114,6 +114,7 @@ using support::changeExtension;
using support::cmd_ret;
using support::createBufferTmpDir;
using support::destroyDir;
using support::doesFileExist;
using support::FileName;
using support::getFormatFromContents;
using support::libFileSearch;
@ -372,8 +373,8 @@ pair<Buffer::LogType, string> const Buffer::getLogName() const
// If no Latex log or Build log is newer, show Build log
if (fs::exists(bname.toFilesystemEncoding()) &&
(!fs::exists(fname.toFilesystemEncoding()) ||
if (doesFileExist(bname) &&
(!doesFileExist(fname) ||
fs::last_write_time(fname.toFilesystemEncoding()) < fs::last_write_time(bname.toFilesystemEncoding()))) {
LYXERR(Debug::FILES) << "Log name calculated as: " << bname << endl;
return make_pair(Buffer::buildlog, bname.absFilename());
@ -792,7 +793,7 @@ bool Buffer::save() const
bool madeBackup = false;
// make a backup if the file already exists
if (lyxrc.make_backup && fs::exists(encodedFilename)) {
if (lyxrc.make_backup && doesFileExist(pimpl_->filename)) {
backupName = FileName(fileName() + '~');
if (!lyxrc.backupdir_path.empty()) {
string const mangledName =
@ -814,7 +815,7 @@ bool Buffer::save() const
}
// ask if the disk file has been externally modified (use checksum method)
if (fs::exists(encodedFilename) && isExternallyModified(checksum_method)) {
if (doesFileExist(pimpl_->filename) && isExternallyModified(checksum_method)) {
docstring const file = makeDisplayPath(fileName(), 20);
docstring text = bformat(_("Document %1$s has been externally modified. Are you sure "
"you want to overwrite this file?"), file);
@ -1579,7 +1580,7 @@ bool Buffer::isBakClean() const
bool Buffer::isExternallyModified(CheckMethod method) const
{
BOOST_ASSERT(fs::exists(pimpl_->filename.toFilesystemEncoding()));
BOOST_ASSERT(doesFileExist(pimpl_->filename));
// if method == timestamp, check timestamp before checksum
return (method == checksum_method
|| pimpl_->timestamp_ != fs::last_write_time(pimpl_->filename.toFilesystemEncoding()))
@ -1589,7 +1590,7 @@ bool Buffer::isExternallyModified(CheckMethod method) const
void Buffer::saveCheckSum(FileName const & file) const
{
if (fs::exists(file.toFilesystemEncoding())) {
if (doesFileExist(file)) {
pimpl_->timestamp_ =
fs::last_write_time(file.toFilesystemEncoding());
pimpl_->checksum_ = sum(file);

View File

@ -43,6 +43,7 @@ namespace fs = boost::filesystem;
namespace lyx {
using support::doesFileExist;
using support::FileName;
namespace {
@ -135,7 +136,7 @@ void ConverterCache::Impl::readIndex()
CacheItem item(orig_from_name, to_format, timestamp, checksum);
// Don't cache files that do not exist anymore
if (!fs::exists(orig_from_name.toFilesystemEncoding())) {
if (!doesFileExist(orig_from_name)) {
LYXERR(Debug::FILES) << "Not caching file `"
<< orig_from << "' (does not exist anymore)."
<< std::endl;
@ -146,7 +147,7 @@ void ConverterCache::Impl::readIndex()
// 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.
if (!fs::exists(item.cache_name.toFilesystemEncoding())) {
if (!doesFileExist(item.cache_name)) {
LYXERR(Debug::FILES) << "Not caching file `"
<< orig_from
<< "' (cached copy does not exist anymore)."
@ -228,7 +229,7 @@ void ConverterCache::init()
// We do this here and not in the constructor because package() gets
// initialized after all static variables.
cache_dir = FileName(addName(support::package().user_support().absFilename(), "cache"));
if (!fs::exists(cache_dir.toFilesystemEncoding()))
if (!doesFileExist(cache_dir))
if (support::mkdir(cache_dir, 0700) != 0) {
lyxerr << "Could not create cache directory `"
<< cache_dir << "'." << std::endl;

View File

@ -47,6 +47,7 @@ using support::addName;
using support::bformat;
using support::changeExtension;
using support::contains;
using support::doesFileExist;
using support::FileName;
using support::makeAbsPath;
using support::makeDisplayPath;
@ -78,7 +79,7 @@ vector<string> const Backends(Buffer const & buffer)
/// ask the user what to do if a file already exists
int checkOverwrite(FileName const & filename)
{
if (fs::exists(filename.toFilesystemEncoding())) {
if (doesFileExist(filename)) {
docstring text = bformat(_("The file %1$s already exists.\n\n"
"Do you want to overwrite that file?"),
makeDisplayPath(filename.absFilename()));
@ -243,7 +244,7 @@ bool Exporter::Export(Buffer * buffer, string const & format,
}
if (status == CANCEL) {
buffer->message(_("Document export cancelled."));
} else if (fs::exists(tmp_result_file.toFilesystemEncoding())) {
} else if (doesFileExist(tmp_result_file)) {
// Finally copy the main file
status = copyFile(format, tmp_result_file,
FileName(result_file), result_file,

View File

@ -38,6 +38,7 @@ using support::absolutePath;
using support::bformat;
using support::compare_ascii_no_case;
using support::contains;
using support::doesFileExist;
using support::FileName;
using support::libScriptSearch;
using support::makeDisplayPath;
@ -265,7 +266,7 @@ void Formats::setViewer(string const & name, string const & command)
bool Formats::view(Buffer const & buffer, FileName const & filename,
string const & format_name) const
{
if (filename.empty() || !fs::exists(filename.toFilesystemEncoding())) {
if (filename.empty() || !doesFileExist(filename)) {
Alert::error(_("Cannot view file"),
bformat(_("File does not exist: %1$s"),
from_utf8(filename.absFilename())));
@ -336,7 +337,7 @@ bool Formats::view(Buffer const & buffer, FileName const & filename,
bool Formats::edit(Buffer const & buffer, FileName const & filename,
string const & format_name) const
{
if (filename.empty() || !fs::exists(filename.toFilesystemEncoding())) {
if (filename.empty() || !doesFileExist(filename)) {
Alert::error(_("Cannot edit file"),
bformat(_("File does not exist: %1$s"),
from_utf8(filename.absFilename())));

View File

@ -56,6 +56,7 @@ using support::absolutePath;
using support::bformat;
using support::changeExtension;
using support::contains;
using support::doesFileExist;
using support::FileName;
using support::findtexfile;
using support::getcwd;
@ -213,7 +214,7 @@ int LaTeX::run(TeXErrors & terr)
// remake the dependency file.
//
bool had_depfile = fs::exists(depfile.toFilesystemEncoding());
bool had_depfile = doesFileExist(depfile);
bool run_bibtex = false;
FileName const aux_file(changeExtension(file.absFilename(), "aux"));
@ -230,7 +231,7 @@ int LaTeX::run(TeXErrors & terr)
// have aborted on error last time... in which cas we need
// to re-run latex and collect the error messages
// (even if they are the same).
if (!fs::exists(output_file.toFilesystemEncoding())) {
if (!doesFileExist(output_file)) {
LYXERR(Debug::DEPEND)
<< "re-running LaTeX because output file doesn't exist."
<< endl;
@ -291,7 +292,7 @@ int LaTeX::run(TeXErrors & terr)
// memoir (at least) writes an empty *idx file in the first place.
// A second latex run is needed.
FileName const idxfile(changeExtension(file.absFilename(), ".idx"));
rerun = fs::exists(idxfile.toFilesystemEncoding()) &&
rerun = doesFileExist(idxfile) &&
fs::is_empty(idxfile.toFilesystemEncoding());
// run makeindex
@ -480,7 +481,7 @@ LaTeX::scanAuxFiles(FileName const & file)
FileName const file2(basename
+ '.' + convert<string>(i)
+ ".aux");
if (!fs::exists(file2.toFilesystemEncoding()))
if (!doesFileExist(file2))
break;
result.push_back(scanAuxFile(file2));
}
@ -777,30 +778,9 @@ int LaTeX::scanLogFile(TeXErrors & terr)
namespace {
/**
* Wrapper around fs::exists that can handle invalid file names.
* In theory we could test with fs::native whether a filename is valid
* before calling fs::exists, but in practice it is unusable: On windows it
* does not allow spaces, and on unix it does not allow absolute file names.
* This function has the disadvantage that it catches also other errors than
* invalid names, but for dependency checking we can live with that.
*/
bool exists(FileName const & possible_name) {
try {
return fs::exists(possible_name.toFilesystemEncoding());
}
catch (fs::filesystem_error const & fe) {
LYXERR(Debug::DEPEND) << "Got error `" << fe.what()
<< "' while checking whether file `" << possible_name
<< "' exists." << endl;
return false;
}
}
bool insertIfExists(FileName const & absname, DepTable & head)
{
if (exists(absname) &&
if (doesFileExist(absname) &&
!fs::is_directory(absname.toFilesystemEncoding())) {
head.insert(absname, true);
return true;
@ -858,7 +838,7 @@ bool handleFoundFile(string const & ff, DepTable & head)
// check for spaces
while (contains(foundfile, ' ')) {
if (exists(absname))
if (doesFileExist(absname))
// everything o.k.
break;
else {
@ -866,7 +846,7 @@ bool handleFoundFile(string const & ff, DepTable & head)
// marks; those have to be removed
string unquoted = subst(foundfile, "\"", "");
absname = makeAbsPath(unquoted);
if (exists(absname))
if (doesFileExist(absname))
break;
// strip off part after last space and try again
string strippedfile;
@ -880,7 +860,7 @@ bool handleFoundFile(string const & ff, DepTable & head)
// (2) foundfile is in the tmpdir
// insert it into head
if (exists(absname) &&
if (doesFileExist(absname) &&
!fs::is_directory(absname.toFilesystemEncoding())) {
// FIXME: This regex contained glo, but glo is used by the old
// version of nomencl.sty. Do we need to put it back?

View File

@ -81,6 +81,7 @@ using support::changeExtension;
using support::createDirectory;
using support::createLyXTmpDir;
using support::destroyDir;
using support::doesFileExist;
using support::FileName;
using support::fileSearch;
using support::getEnv;
@ -964,7 +965,7 @@ bool LyX::init()
prependEnvPath("PATH", lyxrc.path_prefix);
FileName const document_path(lyxrc.document_path);
if (fs::exists(document_path.toFilesystemEncoding()) &&
if (doesFileExist(document_path) &&
fs::is_directory(document_path.toFilesystemEncoding()))
package().document_dir() = document_path;
@ -1109,11 +1110,11 @@ bool needsUpdate(string const & file)
firstrun = false;
}
string const absfile = FileName(addName(
package().user_support().absFilename(), file)).toFilesystemEncoding();
return (! fs::exists(absfile))
FileName absfile = FileName(addName(
package().user_support().absFilename(), file));
return (!doesFileExist(absfile))
|| (fs::last_write_time(configure_script)
> fs::last_write_time(absfile));
> fs::last_write_time(absfile.toFilesystemEncoding()));
}
}
@ -1124,7 +1125,8 @@ bool LyX::queryUserLyXDir(bool explicit_userdir)
// Does user directory exist?
string const user_support =
package().user_support().toFilesystemEncoding();
if (fs::exists(user_support) && fs::is_directory(user_support)) {
if (doesFileExist(package().user_support()) &&
fs::is_directory(user_support)) {
first_start = false;
return needsUpdate("lyxrc.defaults")

View File

@ -116,6 +116,7 @@ using support::addPath;
using support::bformat;
using support::changeExtension;
using support::contains;
using support::doesFileExist;
using support::FileFilterList;
using support::FileName;
using support::fileSearch;
@ -496,7 +497,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
enable = buf->lyxvc().inUse();
break;
case LFUN_BUFFER_RELOAD:
enable = !buf->isUnnamed() && fs::exists(buf->fileName())
enable = !buf->isUnnamed() && doesFileExist(FileName(buf->fileName()))
&& (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
break;
@ -1138,7 +1139,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
FileName const filename(makeAbsPath(target_name,
lyx_view_->buffer()->filePath()));
FileName const dvifile(makeAbsPath(dviname, path));
if (fs::exists(filename.toFilesystemEncoding())) {
if (doesFileExist(filename)) {
docstring text = bformat(
_("The file %1$s already exists.\n\n"
"Do you want to overwrite that file?"),
@ -2065,7 +2066,7 @@ void LyXFunc::open(string const & fname)
filename = fullname.absFilename();
// if the file doesn't exist, let the user create one
if (!fs::exists(fullname.toFilesystemEncoding())) {
if (!doesFileExist(fullname)) {
// the user specifically chose this name. Believe him.
Buffer * const b = newFile(filename, string(), true);
if (b)
@ -2153,7 +2154,7 @@ void LyXFunc::doImport(string const & argument)
// if the file exists already, and we didn't do
// -i lyx thefile.lyx, warn
if (fs::exists(lyxfile.toFilesystemEncoding()) && fullname != lyxfile) {
if (doesFileExist(lyxfile) && fullname != lyxfile) {
docstring const file = makeDisplayPath(lyxfile.absFilename(), 30);
docstring text = bformat(_("The document %1$s already exists.\n\n"
@ -2279,9 +2280,9 @@ void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
case LyXRC::RC_DISPLAY_GRAPHICS:
case LyXRC::RC_DOCUMENTPATH:
if (lyxrc_orig.document_path != lyxrc_new.document_path) {
string const encoded = FileName(
lyxrc_new.document_path).toFilesystemEncoding();
if (fs::exists(encoded) && fs::is_directory(encoded))
FileName encoded = FileName(lyxrc_new.document_path);
if (doesFileExist(encoded) &&
fs::is_directory(encoded.toFilesystemEncoding()))
support::package().document_dir() = FileName(lyxrc.document_path);
}
case LyXRC::RC_ESC_CHARS:

View File

@ -3,7 +3,7 @@
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Lars Gullik Bjønnes
* \author Lars Gullik Bjřnnes
* \author Bo Peng
*
* Full author contact details are available in file CREDITS.
@ -25,6 +25,7 @@
using lyx::support::absolutePath;
using lyx::support::addName;
using lyx::support::doesFileExist;
using lyx::support::FileName;
using lyx::support::package;
@ -78,7 +79,7 @@ void LastFilesSection::read(istream & is)
// read lastfiles
FileName const file(tmp);
if (fs::exists(file.toFilesystemEncoding()) &&
if (doesFileExist(file) &&
!fs::is_directory(file.toFilesystemEncoding()) &&
lastfiles.size() < num_lastfiles)
lastfiles.push_back(file);
@ -133,7 +134,7 @@ void LastOpenedSection::read(istream & is)
continue;
FileName const file(tmp);
if (fs::exists(file.toFilesystemEncoding()) &&
if (doesFileExist(file) &&
!fs::is_directory(file.toFilesystemEncoding()))
lastopened.push_back(file);
else
@ -188,7 +189,7 @@ void LastFilePosSection::read(istream & is)
if (!absolutePath(fname))
continue;
FileName const file(fname);
if (fs::exists(file.toFilesystemEncoding()) &&
if (doesFileExist(file) &&
!fs::is_directory(file.toFilesystemEncoding()) &&
lastfilepos.size() < num_lastfilepos)
lastfilepos[file] = boost::tie(pit, pos);
@ -269,7 +270,7 @@ void BookmarksSection::read(istream & is)
continue;
FileName const file(fname);
// only load valid bookmarks
if (fs::exists(file.toFilesystemEncoding()) &&
if (doesFileExist(file) &&
!fs::is_directory(file.toFilesystemEncoding()) &&
idx <= max_bookmarks)
bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);

View File

@ -37,6 +37,7 @@ namespace fs = boost::filesystem;
namespace lyx {
using support::doesFileExist;
using support::FileName;
using support::libFileSearch;
using support::makeDisplayPath;
@ -922,7 +923,7 @@ bool TextClass::load(string const & path) const
FileName layout_file;
if (!path.empty())
layout_file = FileName(addName(path, name_ + ".layout"));
if (layout_file.empty() || !fs::exists(layout_file.toFilesystemEncoding()))
if (layout_file.empty() || !doesFileExist(layout_file))
layout_file = libFileSearch("layouts", name_, "layout");
loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;

View File

@ -27,6 +27,7 @@
namespace lyx {
namespace fs = boost::filesystem;
using support::doesFileExist;
using support::FileName;
using support::addName;
using support::libFileSearch;
@ -183,7 +184,7 @@ TextClassList::addTextClass(std::string const & textclass, std::string const & p
// only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
// NOTE: latex class name is defined in textclass.layout, which can be different from textclass
FileName const layout_file(addName(path, textclass + ".layout"));
if (fs::exists(layout_file.toFilesystemEncoding())) {
if (doesFileExist(layout_file)) {
LYXERR(Debug::TCLASS) << "Adding class " << textclass << " from directory " << path << endl;
// Read .layout file and get description, real latex classname etc
//

View File

@ -50,6 +50,7 @@
#include <boost/filesystem/operations.hpp>
#include "support/textutils.h"
#include "support/filetools.h"
using std::min;
using std::string;
@ -60,6 +61,7 @@ using namespace std;
using support::bformat;
using support::FileName;
using support::doesFileExist;
using support::libFileSearch;
using support::makeAbsPath;
using support::makeDisplayPath;
@ -77,7 +79,7 @@ bool readFile(Buffer * const b, FileName const & s)
BOOST_ASSERT(b);
// File information about normal file
if (!fs::exists(s.toFilesystemEncoding())) {
if (!doesFileExist(s)) {
docstring const file = makeDisplayPath(s.absFilename(), 50);
docstring text = bformat(_("The specified document\n%1$s"
"\ncould not be read."), file);
@ -88,8 +90,7 @@ bool readFile(Buffer * const b, FileName const & s)
// Check if emergency save file exists and is newer.
FileName const e(s.absFilename() + ".emergency");
if (fs::exists(e.toFilesystemEncoding()) &&
fs::exists(s.toFilesystemEncoding()) &&
if (doesFileExist(e) && doesFileExist(s) &&
fs::last_write_time(e.toFilesystemEncoding()) > fs::last_write_time(s.toFilesystemEncoding()))
{
docstring const file = makeDisplayPath(s.absFilename(), 20);
@ -115,8 +116,7 @@ bool readFile(Buffer * const b, FileName const & s)
// Now check if autosave file is newer.
FileName const a(onlyPath(s.absFilename()) + '#' + onlyFilename(s.absFilename()) + '#');
if (fs::exists(a.toFilesystemEncoding()) &&
fs::exists(s.toFilesystemEncoding()) &&
if (doesFileExist(a) && doesFileExist(s) &&
fs::last_write_time(a.toFilesystemEncoding()) > fs::last_write_time(s.toFilesystemEncoding()))
{
docstring const file = makeDisplayPath(s.absFilename(), 20);

View File

@ -71,6 +71,7 @@ using boost::shared_ptr;
namespace lyx {
using support::bformat;
using support::doesFileExist;
using support::FileFilterList;
using support::FileName;
using support::ForkedProcess;
@ -176,7 +177,7 @@ bool writeAs(Buffer * buffer, string const & newname)
} else
fname = makeAbsPath(newname, onlyPath(oldname)).absFilename();
if (fs::exists(FileName(fname).toFilesystemEncoding())) {
if (doesFileExist(FileName(fname))) {
docstring const file = makeDisplayPath(fname, 30);
docstring text = bformat(_("The document %1$s already "
"exists.\n\nDo you want to "

View File

@ -80,6 +80,25 @@ string itoa(unsigned int i)
}
namespace {
bool fileExists(fs::path path)
{
try {
// fs::exists can throw an exception
// f.ex. if the drive was unmounted.
return fs::exists(path);
} catch (fs::filesystem_error const & fe){
lyxerr << "error while checking file existence: "
<< path << endl;
lyxerr << fe.what() << endl;
return false;
}
}
} // namespace anon
/// Returns the absolute pathnames of all lyx local sockets in
/// file system encoding.
/// Parts stolen from lyx::support::DirList().
@ -89,7 +108,7 @@ vector<fs::path> lyxSockets(string const & dir, string const & pid)
fs::path dirpath(dir);
if (!fs::exists(dirpath) || !fs::is_directory(dirpath)) {
if (!fileExists(dirpath) || !fs::is_directory(dirpath)) {
lyxerr << dir << " does not exist or is not a directory."
<< endl;
return dirlist;
@ -101,7 +120,7 @@ vector<fs::path> lyxSockets(string const & dir, string const & pid)
for (; beg != end; ++beg) {
if (prefixIs(beg->leaf(), "lyx_tmpdir" + pid)) {
fs::path lyxsocket = beg->path() / "lyxsocket";
if (fs::exists(lyxsocket)) {
if (fileExists(lyxsocket)) {
dirlist.push_back(lyxsocket);
}
}

View File

@ -43,6 +43,7 @@ namespace fs = boost::filesystem;
namespace lyx {
using support::addName;
using support::doesFileExist;
using support::FileFilterList;
using support::FileName;
using support::isFileReadable;
@ -89,7 +90,7 @@ docstring const ControlGraphics::browse(docstring const & in_name) const
// Does user clipart directory exist?
string clipdir = addName(package().user_support().absFilename(), "clipart");
string const encoded_clipdir = FileName(clipdir).toFilesystemEncoding();
if (!(fs::exists(encoded_clipdir) && fs::is_directory(encoded_clipdir)))
if (!(doesFileExist(FileName(clipdir)) && fs::is_directory(encoded_clipdir)))
// No - bail out to system clipart directory
clipdir = addName(package().system_support().absFilename(), "clipart");
pair<docstring, docstring> dir1(_("Clipart|#C#c"), from_utf8(clipdir));

View File

@ -62,6 +62,7 @@ using support::bformat;
using support::changeExtension;
using support::contains;
using support::copy;
using support::doesFileExist;
using support::DocFileName;
using support::FileName;
using support::getFileContents;
@ -402,7 +403,7 @@ bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
Buffer * buf = theBufferList().getBuffer(included_file.absFilename());
if (!buf) {
// the readonly flag can/will be wrong, not anymore I think.
if (!fs::exists(included_file.toFilesystemEncoding()))
if (!doesFileExist(included_file))
return false;
if (use_gui) {
lyx::dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN,

View File

@ -11,6 +11,7 @@
#include <config.h>
#include "support/FileMonitor.h"
#include "support/filetools.h"
#include "support/FileName.h"
#include "support/lyxlib.h"
@ -92,7 +93,7 @@ void FileMonitor::start() const
if (monitoring())
return;
if (!fs::exists(pimpl_->filename_.toFilesystemEncoding()))
if (!doesFileExist(pimpl_->filename_))
return;
pimpl_->timestamp_ = fs::last_write_time(pimpl_->filename_.toFilesystemEncoding());
@ -157,7 +158,7 @@ void FileMonitor::Impl::monitorFile()
{
bool changed = false;
if (!fs::exists(filename_.toFilesystemEncoding())) {
if (!doesFileExist(filename_)) {
changed = timestamp_ || checksum_;
timestamp_ = 0;
checksum_ = 0;

View File

@ -175,7 +175,22 @@ bool isFileReadable(FileName const & filename)
lyxerr << fe.what() << endl;
return false;
}
}
bool doesFileExist(FileName const & filename)
{
std::string const path = filename.toFilesystemEncoding();
try {
// fs::exists can throw an exception
// f.ex. if the drive was unmounted.
return fs::exists(path);
} catch (fs::filesystem_error const & fe){
lyxerr << "doesFileExist() error with path: "
<< path << endl;
lyxerr << fe.what() << endl;
return false;
}
}
@ -240,7 +255,7 @@ vector<FileName> const dirList(FileName const & dir, string const & ext)
vector<FileName> dirlist;
string const encoded_dir = dir.toFilesystemEncoding();
if (!(fs::exists(encoded_dir) && fs::is_directory(encoded_dir))) {
if (!(doesFileExist(dir) && fs::is_directory(encoded_dir))) {
LYXERR(Debug::FILES)
<< "Directory \"" << dir
<< "\" does not exist to DirList." << endl;
@ -643,7 +658,7 @@ string const normalizePath(string const & path)
string const getFileContents(FileName const & fname)
{
string const encodedname = fname.toFilesystemEncoding();
if (fs::exists(encodedname)) {
if (doesFileExist(fname)) {
ifstream ifs(encodedname.c_str());
ostringstream ofs;
if (ifs && ofs) {
@ -1135,7 +1150,7 @@ FileName const findtexfile(string const & fil, string const & /*format*/)
// If the file can be found directly, we just return a
// absolute path version of it.
FileName const absfile(makeAbsPath(fil));
if (fs::exists(absfile.toFilesystemEncoding()))
if (doesFileExist(absfile))
return absfile;
// No we try to find it using kpsewhich.
@ -1180,7 +1195,7 @@ void removeAutosaveFile(string const & filename)
a += onlyFilename(filename);
a += '#';
FileName const autosave(a);
if (fs::exists(autosave.toFilesystemEncoding()))
if (doesFileExist(autosave))
unlink(autosave);
}
@ -1249,15 +1264,15 @@ int compare_timestamps(FileName const & filename1, FileName const & filename2)
string const file1 = filename1.toFilesystemEncoding();
string const file2 = filename2.toFilesystemEncoding();
int cmp = 0;
if (fs::exists(file1) && fs::exists(file2)) {
if (doesFileExist(filename1) && doesFileExist(filename2)) {
double const tmp = difftime(fs::last_write_time(file1),
fs::last_write_time(file2));
if (tmp != 0)
cmp = tmp > 0 ? 1 : -1;
} else if (fs::exists(file1)) {
} else if (doesFileExist(filename1)) {
cmp = 1;
} else if (fs::exists(file2)) {
} else if (doesFileExist(filename2)) {
cmp = -1;
}

View File

@ -88,6 +88,11 @@ bool isDirWriteable(FileName const & path);
*/
bool isFileReadable(FileName const & path);
/** Does the file exist ?
Returns true if the file `path' exists.
*/
bool doesFileExist(FileName const & path);
///
bool isLyXFilename(std::string const & filename);

View File

@ -44,6 +44,7 @@ namespace lyx {
using support::addExtension;
using support::changeExtension;
using support::doesFileExist;
using support::FileName;
using support::makeAbsPath;
using support::makeRelPath;
@ -364,7 +365,7 @@ string find_file(string const & name, string const & path,
// expects utf8)
for (char const * const * what = extensions; *what; ++what) {
string const trial = addExtension(name, *what);
if (fs::exists(makeAbsPath(trial, path).toFilesystemEncoding()))
if (doesFileExist(makeAbsPath(trial, path)))
return trial;
}
return string();
@ -1581,7 +1582,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
// therefore path is only used for testing
// FIXME UNICODE encoding of name and path may be
// wrong (makeAbsPath expects utf8)
if (!fs::exists(makeAbsPath(name, path).toFilesystemEncoding())) {
if (!doesFileExist(makeAbsPath(name, path))) {
// The file extension is probably missing.
// Now try to find it out.
string const dvips_name =
@ -1613,7 +1614,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
// FIXME UNICODE encoding of name and path may be
// wrong (makeAbsPath expects utf8)
if (fs::exists(makeAbsPath(name, path).toFilesystemEncoding()))
if (doesFileExist(makeAbsPath(name, path)))
fix_relative_filename(name);
else
cerr << "Warning: Could not find graphics file '"
@ -2272,7 +2273,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
// FIXME UNICODE encoding of filename and path may be
// wrong (makeAbsPath expects utf8)
if ((t.cs() == "include" || t.cs() == "input") &&
!fs::exists(makeAbsPath(filename, path).toFilesystemEncoding())) {
!doesFileExist(makeAbsPath(filename, path))) {
// The file extension is probably missing.
// Now try to find it out.
string const tex_name =
@ -2283,7 +2284,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
}
// FIXME UNICODE encoding of filename and path may be
// wrong (makeAbsPath expects utf8)
if (fs::exists(makeAbsPath(filename, path).toFilesystemEncoding())) {
if (doesFileExist(makeAbsPath(filename, path))) {
string const abstexname =
makeAbsPath(filename, path).absFilename();
string const abslyxname =