diff --git a/src/filesystem.cc b/src/filesystem.cc index bbf6baf..9895e84 100644 --- a/src/filesystem.cc +++ b/src/filesystem.cc @@ -22,6 +22,9 @@ std::string safe_get_env(const std::string &env) { return nullptr==ptr ? "" : std::string(ptr); } +/** + * Returns home folder, empty on error + */ std::string juci::filesystem::get_home_folder() { auto home=safe_get_env("HOME"); if(home.empty()) @@ -30,11 +33,24 @@ std::string juci::filesystem::get_home_folder() { if((status.permissions() & 0222)>=2) { return home; } else { - Singleton::terminal()->print("Invalid permissions. Cannot write in " + home + "\n"); - throw new std::exception; + JERROR("No write permissions in home, var:"); + DEBUG_VAR(home); + return ""; } } +/** + * Returns tmp folder, empty on error. + */ +std::string juci::filesystem::get_tmp_folder() { + boost::system::error_code code; + auto path = boost::filesystem::temp_directory_path(code); + if (code.value()!=0) { + return ""; + } + return path.string(); +} + int juci::filesystem::read(const std::string &path, Glib::RefPtr text_buffer) { std::ifstream input(path, std::ofstream::binary); diff --git a/src/filesystem.h b/src/filesystem.h index ef53fbe..955b1be 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -26,6 +26,7 @@ namespace juci { static bool write(const std::string &path, Glib::RefPtr text_buffer); static bool write(const boost::filesystem::path &path, Glib::RefPtr text_buffer) { return write(path.string(), text_buffer); } static std::string get_home_folder(); + static std::string get_tmp_folder(); }; } // namepace juci #endif // JUCI_SOURCEFILE_H_ diff --git a/src/singletons.cc b/src/singletons.cc index 9040d55..b8734ec 100644 --- a/src/singletons.cc +++ b/src/singletons.cc @@ -30,3 +30,26 @@ Gtk::Label *Singleton::info() { info_=std::unique_ptr(new Gtk::Label()); return info_.get(); } + +std::string Singleton::create_config_path(const std::string &subfolder) { + boost::filesystem::path home; + home = juci::filesystem::get_home_folder(); + if(home.empty()) { + Singleton::terminal()->print("Could not find/write to home directory. Using defaults, no settings will be saved."); + home = juci::filesystem::get_tmp_folder(); + if(home.empty()) { + std::string message("Please fix permissions of your home folder"); + std::cerr << message << std::endl; + JFATAL(message); + throw new std::exception; + } + } + home /= subfolder; + return home.string(); +} + +std::string Singleton::config_dir() { return create_config_path(".juci/config"); } +std::string Singleton::log_dir() { return create_config_path(".juci/log"); } +std::string Singleton::style_dir() { return create_config_path(".juci/styles"); } + + diff --git a/src/singletons.h b/src/singletons.h index de53031..968356a 100644 --- a/src/singletons.h +++ b/src/singletons.h @@ -11,6 +11,7 @@ #include #include #include "filesystem.h" +#include class Singleton { public: @@ -27,12 +28,10 @@ public: static std::unique_ptr directories_; static std::unique_ptr terminal_; }; - static std::string create_config_path(const std::string &subfolder) { - return juci::filesystem::get_home_folder() + subfolder; - } - static std::string config_dir() { return create_config_path("/.juci/config/"); } - static std::string log_dir() { return create_config_path("/.juci/log/"); } - static std::string style_dir() { return create_config_path("/.juci/styles/"); } + static std::string create_config_path(const std::string &subfolder); + static std::string config_dir(); + static std::string log_dir(); + static std::string style_dir(); static Terminal *terminal(); static Directories *directories(); static Gtk::Label *status();