From d5ac358ca540a5238a1da1f6011fff2c0d1b55d6 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 5 May 2020 12:40:22 +0200 Subject: [PATCH] Added filesystem::get_canonical_path instead of using boost::filesystem::canonical_path that always throws if boost::filesystem::current_path fails --- src/directories.cc | 4 +--- src/filesystem.cc | 9 +++++++++ src/filesystem.h | 3 +++ src/notebook.cc | 5 +---- src/source_diff.cc | 10 ++-------- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/directories.cc b/src/directories.cc index fa217d4..86577be 100644 --- a/src/directories.cc +++ b/src/directories.cc @@ -823,9 +823,7 @@ void Directories::colorize_path(boost::filesystem::path dir_path_, bool include_ auto name = Glib::Markup::escape_text(child.get_value(column_record.name)); auto path = child.get_value(column_record.path); // Use canonical path to follow symbolic links - auto canonical_path = boost::filesystem::canonical(path, ec); - if(ec) - canonical_path = path; + auto canonical_path = filesystem::get_canonical_path(path); Gdk::RGBA *color; if(status.modified.find(canonical_path.generic_string()) != status.modified.end()) diff --git a/src/filesystem.cc b/src/filesystem.cc index 6f7eda3..04d4a83 100644 --- a/src/filesystem.cc +++ b/src/filesystem.cc @@ -312,3 +312,12 @@ boost::filesystem::path filesystem::get_path_from_uri(const std::string &uri) { return unencoded; } + +boost::filesystem::path filesystem::get_canonical_path(const boost::filesystem::path &path) { + try { + return boost::filesystem::canonical(path); + } + catch(...) { + return path; + } +} diff --git a/src/filesystem.h b/src/filesystem.h index 1860015..de00bac 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -44,4 +44,7 @@ public: static std::string get_uri_from_path(const boost::filesystem::path &path); /// Get path from file uri static boost::filesystem::path get_path_from_uri(const std::string &uri); + + /// Returns path on error. Do not use boost::filesystem::canonical_path since it is bugged when current_folder() fails. + static boost::filesystem::path get_canonical_path(const boost::filesystem::path &path); }; diff --git a/src/notebook.cc b/src/notebook.cc index d099363..920e803 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -116,10 +116,7 @@ void Notebook::open(const boost::filesystem::path &file_path_, Position position // Use canonical path to follow symbolic links if(position == Position::infer) { - boost::system::error_code ec; - auto canonical_file_path = boost::filesystem::canonical(file_path, ec); - if(ec) - canonical_file_path = file_path; + auto canonical_file_path = filesystem::get_canonical_path(file_path); for(size_t c = 0; c < size(); c++) { bool equal; { diff --git a/src/source_diff.cc b/src/source_diff.cc index 7638308..96889ce 100644 --- a/src/source_diff.cc +++ b/src/source_diff.cc @@ -35,10 +35,7 @@ void Source::DiffView::Renderer::draw_vfunc(const Cairo::RefPtr } Source::DiffView::DiffView(const boost::filesystem::path &file_path, const Glib::RefPtr &language) : BaseView(file_path, language), renderer(new Renderer()) { - boost::system::error_code ec; - canonical_file_path = boost::filesystem::canonical(file_path, ec); - if(ec) - canonical_file_path = file_path; + canonical_file_path = filesystem::get_canonical_path(file_path); renderer->tag_added = get_buffer()->create_tag("git_added"); renderer->tag_modified = get_buffer()->create_tag("git_modified"); @@ -254,10 +251,7 @@ void Source::DiffView::rename(const boost::filesystem::path &path) { Source::BaseView::rename(path); LockGuard lock(canonical_file_path_mutex); - boost::system::error_code ec; - canonical_file_path = boost::filesystem::canonical(path, ec); - if(ec) - canonical_file_path = path; + canonical_file_path = filesystem::get_canonical_path(path); } void Source::DiffView::goto_next_diff() {