Browse Source

No longer resolves symbolic links when finding current path

pipelines/280567345
eidheim 5 years ago
parent
commit
076ee4d7cf
  1. 6
      src/dialogs.cpp
  2. 28
      src/filesystem.cpp
  3. 3
      src/filesystem.hpp
  4. 7
      src/juci.cpp
  5. 14
      src/project.cpp

6
src/dialogs.cpp

@ -1,4 +1,5 @@
#include "dialogs.hpp" #include "dialogs.hpp"
#include "filesystem.hpp"
#include <cmath> #include <cmath>
Dialog::Message::Message(const std::string &text, std::function<void()> &&on_cancel, bool show_progress_bar) : Gtk::Window(Gtk::WindowType::WINDOW_POPUP) { Dialog::Message::Message(const std::string &text, std::function<void()> &&on_cancel, bool show_progress_bar) : Gtk::Window(Gtk::WindowType::WINDOW_POPUP) {
@ -84,9 +85,8 @@ std::string Dialog::gtk_dialog(const boost::filesystem::path &path, const std::s
else if(!path.empty()) else if(!path.empty())
gtk_file_chooser_set_current_folder(reinterpret_cast<GtkFileChooser *>(dialog.gobj()), path.string().c_str()); gtk_file_chooser_set_current_folder(reinterpret_cast<GtkFileChooser *>(dialog.gobj()), path.string().c_str());
else { else {
boost::system::error_code ec; auto current_path = filesystem::get_current_path();
auto current_path = boost::filesystem::current_path(ec); if(!current_path.empty())
if(!ec)
gtk_file_chooser_set_current_folder(reinterpret_cast<GtkFileChooser *>(dialog.gobj()), current_path.string().c_str()); gtk_file_chooser_set_current_folder(reinterpret_cast<GtkFileChooser *>(dialog.gobj()), current_path.string().c_str());
} }

28
src/filesystem.cpp

@ -1,4 +1,5 @@
#include "filesystem.hpp" #include "filesystem.hpp"
#include "process.hpp"
#include "utility.hpp" #include "utility.hpp"
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
@ -81,6 +82,33 @@ std::string filesystem::unescape_argument(const std::string &argument) {
return unescaped; return unescaped;
} }
boost::filesystem::path filesystem::get_current_path() noexcept {
static boost::filesystem::path current_path;
if(!current_path.empty())
return current_path;
#ifdef _WIN32
boost::system::error_code ec;
auto path = boost::filesystem::current_path(ec);
if(!ec) {
current_path = std::move(path);
return current_path;
}
return boost::filesystem::path();
#else
std::string path;
TinyProcessLib::Process process("pwd", "", [&path](const char *buffer, size_t length) {
path += std::string(buffer, length);
});
if(process.get_exit_status() == 0) {
if(!path.empty() && path.back() == '\n')
path.pop_back();
current_path = boost::filesystem::path(path);
return current_path;
}
return boost::filesystem::path();
#endif
}
boost::filesystem::path filesystem::get_home_path() noexcept { boost::filesystem::path filesystem::get_home_path() noexcept {
static boost::filesystem::path home_path; static boost::filesystem::path home_path;
if(!home_path.empty()) if(!home_path.empty())

3
src/filesystem.hpp

@ -16,6 +16,9 @@ public:
static std::string escape_argument(const std::string &argument); static std::string escape_argument(const std::string &argument);
static std::string unescape_argument(const std::string &argument); static std::string unescape_argument(const std::string &argument);
/// Does not resolve symbolic links. Returns empty path on failure
static boost::filesystem::path get_current_path() noexcept;
/// Returns empty path on failure
static boost::filesystem::path get_home_path() noexcept; static boost::filesystem::path get_home_path() noexcept;
/// Replaces home path with ~ /// Replaces home path with ~
static boost::filesystem::path get_short_path(const boost::filesystem::path &path) noexcept; static boost::filesystem::path get_short_path(const boost::filesystem::path &path) noexcept;

7
src/juci.cpp

@ -24,13 +24,12 @@ int Application::on_command_line(const Glib::RefPtr<Gio::ApplicationCommandLine>
char **argv = cmd->get_arguments(argc); char **argv = cmd->get_arguments(argc);
ctx.parse(argc, argv); ctx.parse(argc, argv);
if(argc >= 2) { if(argc >= 2) {
boost::system::error_code current_path_ec; auto current_path = filesystem::get_current_path();
auto current_path = boost::filesystem::current_path(current_path_ec); if(current_path.empty())
if(current_path_ec)
errors.emplace_back("\e[31mError\e[m: could not find current path\n"); errors.emplace_back("\e[31mError\e[m: could not find current path\n");
for(int c = 1; c < argc; c++) { for(int c = 1; c < argc; c++) {
boost::filesystem::path path(argv[c]); boost::filesystem::path path(argv[c]);
if(path.is_relative() && !current_path_ec) if(path.is_relative() && !current_path.empty())
path = current_path / path; path = current_path / path;
boost::system::error_code ec; boost::system::error_code ec;
if(boost::filesystem::exists(path, ec)) { if(boost::filesystem::exists(path, ec)) {

14
src/project.cpp

@ -35,11 +35,8 @@ boost::filesystem::path Project::get_preferably_view_folder() {
else if(!Directories::get().path.empty()) else if(!Directories::get().path.empty())
return Directories::get().path; return Directories::get().path;
else { else {
boost::system::error_code ec; auto current_path = filesystem::get_current_path();
auto current_path = boost::filesystem::current_path(ec); return !current_path.empty() ? current_path : boost::filesystem::path();
if(ec)
return boost::filesystem::path();
return current_path;
} }
} }
@ -49,11 +46,8 @@ boost::filesystem::path Project::get_preferably_directory_folder() {
else if(auto view = Notebook::get().get_current_view()) else if(auto view = Notebook::get().get_current_view())
return view->file_path.parent_path(); return view->file_path.parent_path();
else { else {
boost::system::error_code ec; auto current_path = filesystem::get_current_path();
auto current_path = boost::filesystem::current_path(ec); return !current_path.empty() ? current_path : boost::filesystem::path();
if(ec)
return boost::filesystem::path();
return current_path;
} }
} }

Loading…
Cancel
Save