From d66e07ef1a525bafbdcc8f151387377a0cb7fb8a Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 25 Sep 2019 17:09:38 +0300 Subject: [PATCH] filesystem unicode refactoring WIP3 --- contrib/epee/include/file_io_utils.h | 35 ++++++++++++------- src/currency_core/blockchain_storage.cpp | 11 +++--- src/currency_core/tx_pool.cpp | 8 ++--- src/daemon/daemon.cpp | 4 +-- .../qt-daemon/application/daemon_backend.cpp | 4 +-- src/gui/qt-daemon/application/gui_utils.cpp | 6 ++-- src/gui/qt-daemon/application/mainwindow.cpp | 6 ++-- 7 files changed, 44 insertions(+), 30 deletions(-) diff --git a/contrib/epee/include/file_io_utils.h b/contrib/epee/include/file_io_utils.h index 8e5fa6fd..d7a35e8b 100644 --- a/contrib/epee/include/file_io_utils.h +++ b/contrib/epee/include/file_io_utils.h @@ -223,13 +223,23 @@ namespace file_io_utils return str_result; } #endif + + inline const std::wstring& convert_utf8_to_wstring_if_needed(const std::wstring& s) + { + return s; + } + + inline std::wstring convert_utf8_to_wstring_if_needed(const std::string& s) + { + return epee::string_encoding::utf8_to_wstring(s); + } template bool is_file_exist(const t_string& path) - { - boost::filesystem::path p(path); - return boost::filesystem::exists(p); - } + { + boost::filesystem::path p(convert_utf8_to_wstring_if_needed(path)); + return boost::filesystem::exists(p); + } /* inline @@ -265,7 +275,7 @@ namespace file_io_utils //std::ofstream fstream; boost::filesystem::ofstream fstream; fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit); - fstream.open(path_to_file, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc); + fstream.open(convert_utf8_to_wstring_if_needed(path_to_file), std::ios_base::binary | std::ios_base::out | std::ios_base::trunc); fstream << str; fstream.close(); return true; @@ -297,7 +307,7 @@ namespace file_io_utils { boost::filesystem::ifstream fstream; //fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit); - fstream.open(path_to_file, std::ios_base::binary | std::ios_base::in | std::ios::ate); + fstream.open(convert_utf8_to_wstring_if_needed(path_to_file), std::ios_base::binary | std::ios_base::in | std::ios::ate); if (!fstream.good()) return false; std::ifstream::pos_type file_size = fstream.tellg(); @@ -354,7 +364,7 @@ namespace file_io_utils bool get_file_time(const std::string& path_to_file, OUT time_t& ft) { boost::system::error_code ec; - ft = boost::filesystem::last_write_time(boost::filesystem::path(path_to_file), ec); + ft = boost::filesystem::last_write_time(epee::string_encoding::utf8_to_wstring(path_to_file), ec); if(!ec) return true; else @@ -365,7 +375,7 @@ namespace file_io_utils bool set_file_time(const std::string& path_to_file, const time_t& ft) { boost::system::error_code ec; - boost::filesystem::last_write_time(boost::filesystem::path(path_to_file), ft, ec); + boost::filesystem::last_write_time(epee::string_encoding::utf8_to_wstring(path_to_file), ft, ec); if(!ec) return true; else @@ -468,20 +478,21 @@ namespace file_io_utils bool copy_file(const std::string& source, const std::string& destination) { boost::system::error_code ec; - boost::filesystem::copy_file(source, destination, ec); + boost::filesystem::copy_file(epee::string_encoding::utf8_to_wstring(source), epee::string_encoding::utf8_to_wstring(destination), ec); if (ec) return false; else return true; } + inline bool append_string_to_file(const std::string& path_to_file, const std::string& str) { try { - std::ofstream fstream; + boost::filesystem::ofstream fstream; fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit); - fstream.open(path_to_file.c_str(), std::ios_base::binary | std::ios_base::out | std::ios_base::app); + fstream.open(epee::string_encoding::utf8_to_wstring(path_to_file), std::ios_base::binary | std::ios_base::out | std::ios_base::app); fstream << str; fstream.close(); return true; @@ -553,7 +564,7 @@ namespace file_io_utils { boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end - for ( boost::filesystem::directory_iterator itr( path ); itr != end_itr; ++itr ) + for ( boost::filesystem::directory_iterator itr( epee::string_encoding::utf8_to_wstring(path) ); itr != end_itr; ++itr ) { if ( only_files && boost::filesystem::is_directory(itr->status()) ) { diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index fd6bb3bd..4c3a185c 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -225,10 +225,10 @@ bool blockchain_storage::init(const std::string& config_folder, const boost::pro // remove old incompatible DB const std::string old_db_folder_path = m_config_folder + "/" CURRENCY_BLOCKCHAINDATA_FOLDERNAME_OLD; - if (boost::filesystem::exists(old_db_folder_path)) + if (boost::filesystem::exists(epee::string_encoding::utf8_to_wstring(old_db_folder_path))) { LOG_PRINT_YELLOW("Removing old DB in " << old_db_folder_path << "...", LOG_LEVEL_0); - boost::filesystem::remove_all(old_db_folder_path); + boost::filesystem::remove_all(epee::string_encoding::utf8_to_wstring(old_db_folder_path)); } const std::string db_folder_path = m_config_folder + "/" CURRENCY_BLOCKCHAINDATA_FOLDERNAME; @@ -242,7 +242,7 @@ bool blockchain_storage::init(const std::string& config_folder, const boost::pro { // if DB could not be opened -- try to remove the whole folder and re-open DB LOG_PRINT_YELLOW("Failed to initialize database in folder: " << db_folder_path << ", first attempt", LOG_LEVEL_0); - boost::filesystem::remove_all(db_folder_path); + boost::filesystem::remove_all(epee::string_encoding::utf8_to_wstring(db_folder_path)); res = m_db.open(db_folder_path, cache_size_l1); CHECK_AND_ASSERT_MES(res, false, "Failed to initialize database in folder: " << db_folder_path << ", second attempt"); } @@ -312,7 +312,7 @@ bool blockchain_storage::init(const std::string& config_folder, const boost::pro m_db_addr_to_alias.deinit(); m_db_per_block_gindex_incs.deinit(); m_db.close(); - size_t files_removed = boost::filesystem::remove_all(db_folder_path); + size_t files_removed = boost::filesystem::remove_all(epee::string_encoding::utf8_to_wstring(db_folder_path)); LOG_PRINT_L1(files_removed << " files at " << db_folder_path << " removed"); // try to re-create DB and re-init containers @@ -2644,7 +2644,8 @@ void blockchain_storage::print_blockchain_with_tx(uint64_t start_index, uint64_t { boost::filesystem::ofstream ss; ss.exceptions(/*std::ifstream::failbit |*/ std::ifstream::badbit); - ss.open(log_space::log_singletone::get_default_log_folder() + "/blockchain_with_tx.txt", std::ios_base::binary | std::ios_base::out | std::ios_base::trunc); + ss.open(epee::string_encoding::utf8_to_wstring(log_space::log_singletone::get_default_log_folder() + "/blockchain_with_tx.txt"), + std::ios_base::binary | std::ios_base::out | std::ios_base::trunc); CRITICAL_REGION_LOCAL(m_read_lock); diff --git a/src/currency_core/tx_pool.cpp b/src/currency_core/tx_pool.cpp index b8c56fa4..440bd9a7 100644 --- a/src/currency_core/tx_pool.cpp +++ b/src/currency_core/tx_pool.cpp @@ -1125,10 +1125,10 @@ namespace currency // remove old incompartible DB const std::string old_db_folder_path = m_config_folder + "/" CURRENCY_POOLDATA_FOLDERNAME_OLD; - if (boost::filesystem::exists(old_db_folder_path)) + if (boost::filesystem::exists(epee::string_encoding::utf8_to_wstring(old_db_folder_path))) { LOG_PRINT_YELLOW("Removing old DB in " << old_db_folder_path << "...", LOG_LEVEL_0); - boost::filesystem::remove_all(old_db_folder_path); + boost::filesystem::remove_all(epee::string_encoding::utf8_to_wstring(old_db_folder_path)); } const std::string db_folder_path = m_config_folder + "/" CURRENCY_POOLDATA_FOLDERNAME; @@ -1142,7 +1142,7 @@ namespace currency { // if DB could not be opened -- try to remove the whole folder and re-open DB LOG_PRINT_YELLOW("Failed to initialize database in folder: " << db_folder_path << ", first attempt", LOG_LEVEL_0); - boost::filesystem::remove_all(db_folder_path); + boost::filesystem::remove_all(epee::string_encoding::utf8_to_wstring(db_folder_path)); res = m_db.open(db_folder_path, cache_size_l1); CHECK_AND_ASSERT_MES(res, false, "Failed to initialize database in folder: " << db_folder_path << ", second attempt"); } @@ -1182,7 +1182,7 @@ namespace currency m_db_alias_addresses.deinit(); m_db_solo_options.deinit(); m_db.close(); - size_t files_removed = boost::filesystem::remove_all(db_folder_path); + size_t files_removed = boost::filesystem::remove_all(epee::string_encoding::utf8_to_wstring(db_folder_path)); LOG_PRINT_L1(files_removed << " files at " << db_folder_path << " removed"); // try to re-create DB and re-init containers diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index f41affd6..4b07ce89 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -172,8 +172,8 @@ int main(int argc, char* argv[]) std::string data_dir = command_line::get_arg(vm, command_line::arg_data_dir); std::string config = command_line::get_arg(vm, command_line::arg_config_file); - boost::filesystem::path data_dir_path(data_dir); - boost::filesystem::path config_path(config); + boost::filesystem::path data_dir_path(epee::string_encoding::utf8_to_wstring(data_dir)); + boost::filesystem::path config_path(epee::string_encoding::utf8_to_wstring(config)); if (!config_path.has_parent_path()) { config_path = data_dir_path / config_path; diff --git a/src/gui/qt-daemon/application/daemon_backend.cpp b/src/gui/qt-daemon/application/daemon_backend.cpp index bd62eeb0..2abb5319 100644 --- a/src/gui/qt-daemon/application/daemon_backend.cpp +++ b/src/gui/qt-daemon/application/daemon_backend.cpp @@ -154,8 +154,8 @@ bool daemon_backend::init(int argc, char* argv[], view::i_view* pview_handler) m_data_dir = command_line::get_arg(m_vm, command_line::arg_data_dir); std::string config = command_line::get_arg(m_vm, command_line::arg_config_file); - boost::filesystem::path data_dir_path(m_data_dir); - boost::filesystem::path config_path(config); + boost::filesystem::path data_dir_path(epee::string_encoding::utf8_to_wstring(m_data_dir)); + boost::filesystem::path config_path(epee::string_encoding::utf8_to_wstring(config)); if (!config_path.has_parent_path()) { config_path = data_dir_path / config_path; diff --git a/src/gui/qt-daemon/application/gui_utils.cpp b/src/gui/qt-daemon/application/gui_utils.cpp index 7ccfdb1e..1a7b66bc 100644 --- a/src/gui/qt-daemon/application/gui_utils.cpp +++ b/src/gui/qt-daemon/application/gui_utils.cpp @@ -75,14 +75,14 @@ namespace gui_tools { namespace fs = boost::filesystem; - char pszPath[MAX_PATH] = ""; + wchar_t pszPath[MAX_PATH] = L""; - if (SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate)) + if (SHGetSpecialFolderPathW(NULL, pszPath, nFolder, fCreate)) { return fs::path(pszPath); } - //LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n"); + //LogPrintf("SHGetSpecialFolderPathW() failed, could not obtain requested path.\n"); return fs::path(""); } #endif diff --git a/src/gui/qt-daemon/application/mainwindow.cpp b/src/gui/qt-daemon/application/mainwindow.cpp index 4777a0bb..06c3340e 100644 --- a/src/gui/qt-daemon/application/mainwindow.cpp +++ b/src/gui/qt-daemon/application/mainwindow.cpp @@ -1232,7 +1232,7 @@ QString MainWindow::have_secure_app_data() view::api_response ar = AUTO_VAL_INIT(ar); boost::system::error_code ec; - if (boost::filesystem::exists(m_backend.get_config_folder() + "/" + GUI_SECURE_CONFIG_FILENAME, ec)) + if (boost::filesystem::exists(epee::string_encoding::utf8_to_wstring(m_backend.get_config_folder() + "/" + GUI_SECURE_CONFIG_FILENAME), ec)) ar.error_code = API_RETURN_CODE_TRUE; else ar.error_code = API_RETURN_CODE_FALSE; @@ -1240,6 +1240,7 @@ QString MainWindow::have_secure_app_data() return MAKE_RESPONSE(ar); CATCH_ENTRY_FAIL_API_RESPONCE(); } + QString MainWindow::drop_secure_app_data() { TRY_ENTRY(); @@ -1247,13 +1248,14 @@ QString MainWindow::drop_secure_app_data() view::api_response ar = AUTO_VAL_INIT(ar); boost::system::error_code ec; - if (boost::filesystem::remove(m_backend.get_config_folder() + "/" + GUI_SECURE_CONFIG_FILENAME, ec)) + if (boost::filesystem::remove(epee::string_encoding::utf8_to_wstring(m_backend.get_config_folder() + "/" + GUI_SECURE_CONFIG_FILENAME), ec)) ar.error_code = API_RETURN_CODE_TRUE; else ar.error_code = API_RETURN_CODE_FALSE; return MAKE_RESPONSE(ar); CATCH_ENTRY_FAIL_API_RESPONCE(); } + QString MainWindow::get_all_aliases() { TRY_ENTRY();