diff --git a/contrib/epee/include/file_io_utils.h b/contrib/epee/include/file_io_utils.h index 39a96942..8e5fa6fd 100644 --- a/contrib/epee/include/file_io_utils.h +++ b/contrib/epee/include/file_io_utils.h @@ -52,6 +52,7 @@ #endif #include "include_base_utils.h" +#include "string_coding.h" namespace epee { @@ -380,16 +381,18 @@ namespace file_io_utils typedef int native_filesystem_handle; #endif + // uses UTF-8 for unicode names for all systems inline bool open_and_lock_file(const std::string file_path, native_filesystem_handle& h_file) { #ifdef WIN32 - h_file = ::CreateFileA(file_path.c_str(), // name of the write - GENERIC_WRITE, // open for writing - 0, // do not share - NULL, // default security - OPEN_ALWAYS, // create new file only - FILE_ATTRIBUTE_NORMAL, // normal file - NULL); // no attr. template + std::wstring file_path_w = epee::string_encoding::utf8_to_wstring(file_path); + h_file = ::CreateFileW(file_path_w.c_str(), // name of the file + GENERIC_WRITE, // open for writing + 0, // do not share + NULL, // default security + OPEN_ALWAYS, // create new file only + FILE_ATTRIBUTE_NORMAL, // normal file + NULL); // no attr. template if (h_file == INVALID_HANDLE_VALUE) return false; else diff --git a/contrib/epee/include/misc_log_ex.h b/contrib/epee/include/misc_log_ex.h index 8f932e10..00a4c9ff 100644 --- a/contrib/epee/include/misc_log_ex.h +++ b/contrib/epee/include/misc_log_ex.h @@ -65,6 +65,7 @@ DISABLE_VS_WARNINGS(4100) #include "syncobj.h" #include "sync_locked_object.h" +#include "string_coding.h" #define LOG_LEVEL_SILENT -1 @@ -694,13 +695,13 @@ namespace log_space class file_output_stream : public ibase_log_stream { public: - typedef std::map named_log_streams; + typedef std::map named_log_streams; - file_output_stream( std::string default_log_file_name, std::string log_path ) + file_output_stream( const std::string& default_log_file_name, const std::string& log_path ) { m_default_log_filename = default_log_file_name; m_max_logfile_size = 0; - m_default_log_path = log_path; + m_default_log_path_w = epee::string_encoding::utf8_to_wstring(log_path); m_pdefault_file_stream = add_new_stream_and_open(default_log_file_name.c_str()); } @@ -718,20 +719,22 @@ namespace log_space } private: named_log_streams m_log_file_names; - std::string m_default_log_path; - std::ofstream* m_pdefault_file_stream; + std::wstring m_default_log_path_w; + boost::filesystem::ofstream* m_pdefault_file_stream; std::string m_log_rotate_cmd; std::string m_default_log_filename; uint64_t m_max_logfile_size; - std::ofstream* add_new_stream_and_open(const char* pstream_name) + // gets utf-8 encoded string + boost::filesystem::ofstream* add_new_stream_and_open(const char* pstream_name) { //log_space::rotate_log_file((m_default_log_path + "\\" + pstream_name).c_str()); boost::system::error_code ec; - boost::filesystem::create_directories(m_default_log_path, ec); - std::ofstream* pstream = (m_log_file_names[pstream_name] = new std::ofstream); - std::string target_path = m_default_log_path + "/" + pstream_name; + boost::filesystem::create_directories(m_default_log_path_w, ec); + boost::filesystem::ofstream* pstream = (m_log_file_names[pstream_name] = new boost::filesystem::ofstream); + std::wstring target_path = m_default_log_path_w + L"/" + epee::string_encoding::utf8_to_wstring(pstream_name); + pstream->open( target_path.c_str(), std::ios_base::out | std::ios::app /*ios_base::trunc */); if(pstream->fail()) return NULL; @@ -754,7 +757,7 @@ namespace log_space virtual bool out_buffer( const char* buffer, int buffer_len, int log_level, int color, const char* plog_name = NULL ) { - std::ofstream* m_target_file_stream = m_pdefault_file_stream; + boost::filesystem::ofstream* m_target_file_stream = m_pdefault_file_stream; if(plog_name) { //find named stream named_log_streams::iterator it = m_log_file_names.find(plog_name); @@ -769,9 +772,10 @@ namespace log_space m_target_file_stream->write(buffer, buffer_len ); m_target_file_stream->flush(); + /* if(m_max_logfile_size) { - std::ofstream::pos_type pt = m_target_file_stream->tellp(); + boost::filesystem::ofstream::pos_type pt = m_target_file_stream->tellp(); uint64_t current_sz = pt; if(current_sz > m_max_logfile_size) { @@ -818,12 +822,13 @@ namespace log_space misc_utils::call_sys_cmd(m_log_rotate_cmd_local_copy); } - m_target_file_stream->open( (m_default_log_path + "/" + log_file_name).c_str(), std::ios_base::out | std::ios::app /*ios_base::trunc */); + + m_target_file_stream->open( (m_default_log_path + "/" + log_file_name).c_str(), std::ios_base::out | std::ios::app / * ios_base::trunc * /); if(m_target_file_stream->fail()) return false; } } - + */ return true; } int get_type(){return LOGGER_FILE;} diff --git a/src/common/db_backend_lmdb.cpp b/src/common/db_backend_lmdb.cpp index fdb289ff..6c913f34 100644 --- a/src/common/db_backend_lmdb.cpp +++ b/src/common/db_backend_lmdb.cpp @@ -49,10 +49,6 @@ namespace tools CHECK_AND_ASSERT_MESS_LMDB_DB(res, false, "Unable to mdb_env_set_mapsize"); m_path = path_; -#ifdef WIN32 - m_path = epee::string_encoding::convert_ansii_to_utf8(m_path); -#endif - CHECK_AND_ASSERT_MES(tools::create_directories_if_necessary(m_path), false, "create_directories_if_necessary failed: " << m_path); res = mdb_env_open(m_penv, m_path.c_str(), MDB_NORDAHEAD , 0644); diff --git a/src/common/util.cpp b/src/common/util.cpp index 48fd7588..999c5401 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -24,6 +24,8 @@ using namespace epee; #include +#include "string_coding.h" + namespace tools { std::function signal_handler::m_handler; @@ -450,18 +452,22 @@ std::string get_nix_version_display_string() #ifdef WIN32 - std::string get_special_folder_path(int nfolder, bool iscreate) + std::wstring get_special_folder_path_w(int nfolder, bool iscreate) { - namespace fs = boost::filesystem; - char psz_path[MAX_PATH] = ""; + wchar_t psz_path[MAX_PATH] = L""; - if(SHGetSpecialFolderPathA(NULL, psz_path, nfolder, iscreate)) + if (SHGetSpecialFolderPathW(NULL, psz_path, nfolder, iscreate)) { return psz_path; } - LOG_ERROR("SHGetSpecialFolderPathA() failed, could not obtain requested path."); - return ""; + LOG_ERROR("SHGetSpecialFolderPathW(" << nfolder << ", " << iscreate << ") failed, could not obtain requested path."); + return L""; + } + + std::string get_special_folder_path_utf8(int nfolder, bool iscreate) + { + return epee::string_encoding::wstring_to_utf8(get_special_folder_path_w(nfolder, iscreate)); } #endif @@ -476,9 +482,9 @@ std::string get_nix_version_display_string() #ifdef WIN32 // Windows #ifdef _M_X64 - config_folder = get_special_folder_path(CSIDL_APPDATA, true) + "/" + CURRENCY_NAME_SHORT; + config_folder = get_special_folder_path_utf8(CSIDL_APPDATA, true) + "/" + CURRENCY_NAME_SHORT; #else - config_folder = get_special_folder_path(CSIDL_APPDATA, true) + "/" + CURRENCY_NAME_SHORT + "-x86"; + config_folder = get_special_folder_path_utf8(CSIDL_APPDATA, true) + "/" + CURRENCY_NAME_SHORT + "-x86"; #endif #else std::string pathRet; @@ -518,7 +524,7 @@ std::string get_nix_version_display_string() std::string wallets_dir; #ifdef WIN32 // Windows - wallets_dir = get_special_folder_path(CSIDL_PERSONAL, true) + "/" + CURRENCY_NAME_BASE; + wallets_dir = get_special_folder_path_utf8(CSIDL_PERSONAL, true) + "/" + CURRENCY_NAME_BASE; #else std::string pathRet; char* pszHome = getenv("HOME"); @@ -553,7 +559,7 @@ std::string get_nix_version_display_string() { namespace fs = boost::filesystem; boost::system::error_code ec; - fs::path fs_path(path); + fs::path fs_path = epee::string_encoding::utf8_to_wstring(path); if (fs::is_directory(fs_path, ec)) { return true; diff --git a/src/currency_core/currency_core.cpp b/src/currency_core/currency_core.cpp index 54b93443..a79dfb2a 100644 --- a/src/currency_core/currency_core.cpp +++ b/src/currency_core/currency_core.cpp @@ -18,6 +18,7 @@ using namespace epee; #include "currency_core/currency_config.h" #include "currency_format_utils.h" #include "misc_language.h" +#include "string_coding.h" #define MINIMUM_REQUIRED_FREE_SPACE_BYTES (1024 * 1024 * 100) @@ -717,10 +718,27 @@ namespace currency //----------------------------------------------------------------------------------------------- bool core::check_if_free_space_critically_low(uint64_t* p_available_space /* = nullptr */) { - boost::filesystem::space_info si = boost::filesystem::space(m_config_folder); - if (p_available_space != nullptr) - *p_available_space = si.available; - return si.available < MINIMUM_REQUIRED_FREE_SPACE_BYTES; + namespace fs = boost::filesystem; + + try + { + CHECK_AND_ASSERT_MES(tools::create_directories_if_necessary(m_config_folder), false, "create_directories_if_necessary failed: " << m_config_folder); + std::wstring config_folder_w = epee::string_encoding::utf8_to_wstring(m_config_folder); + fs::space_info si = fs::space(config_folder_w); + if (p_available_space != nullptr) + *p_available_space = si.available; + return si.available < MINIMUM_REQUIRED_FREE_SPACE_BYTES; + } + catch (std::exception& e) + { + LOG_ERROR("failed to determine free space: " << e.what()); + return false; + } + catch (...) + { + LOG_ERROR("failed to determine free space: unknown exception"); + return false; + } } void core::check_free_space()