From 7b8663fa8b9bd3ebc5fbd20cfc5029c82dac24d0 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 21 Dec 2008 08:48:06 +0000 Subject: [PATCH] support for wide character paths for the GeoIP databases. Fixes #431 --- docs/manual.rst | 6 ++++++ include/libtorrent/aux_/session_impl.hpp | 2 ++ include/libtorrent/session.hpp | 2 ++ src/GeoIP.c | 14 ++++++++++++++ src/session.cpp | 10 ++++++++++ src/session_impl.cpp | 20 ++++++++++++++++++++ 6 files changed, 54 insertions(+) diff --git a/docs/manual.rst b/docs/manual.rst index dce5ebf9c..f5497bf11 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -134,7 +134,9 @@ The ``session`` class has the following synopsis:: int num_connections() const; bool load_asnum_db(char const* file); + bool load_asnum_db(wchar_t const* file); bool load_country_db(char const* file); + bool load_country_db(wchar_t const* file); int as_for_ip(address const& adr); void load_state(entry const& ses_state); @@ -470,7 +472,9 @@ load_asnum_db() load_country_db() int as_for_ip() :: bool load_asnum_db(char const* file); + bool load_asnum_db(wchar_t const* file); bool load_country_db(char const* file); + bool load_country_db(wchar_t const* file); int as_for_ip(address const& adr); These functions are not available if ``TORRENT_DISABLE_GEO_IP`` is defined. They @@ -480,6 +484,8 @@ respectively. This will be used to look up which AS and country peers belong to. ``as_for_ip`` returns the AS number for the IP address specified. If the IP is not in the database or the ASN database is not loaded, 0 is returned. +The ``wchar_t`` overloads are for wide character paths. + .. _`MaxMind ASN database`: http://www.maxmind.com/app/asnum .. _`MaxMind GeoIP database`: http://www.maxmind.com/app/geolitecountry diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index d1495c959..903bfb551 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -285,9 +285,11 @@ namespace libtorrent int as_for_ip(address const& a); std::pair* lookup_as(int as); bool load_asnum_db(char const* file); + bool load_asnum_db(wchar_t const* file); bool has_asnum_db() const { return m_asnum_db; } bool load_country_db(char const* file); + bool load_country_db(wchar_t const* file); bool has_country_db() const { return m_country_db; } char const* country_for_ip(address const& a); #endif diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index a14c10efb..9d26a882d 100644 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -289,7 +289,9 @@ namespace libtorrent #ifndef TORRENT_DISABLE_GEO_IP int as_for_ip(address const& addr); bool load_asnum_db(char const* file); + bool load_asnum_db(wchar_t const* file); bool load_country_db(char const* file); + bool load_country_db(wchar_t const* file); #endif void load_state(entry const& ses_state); diff --git a/src/GeoIP.c b/src/GeoIP.c index 562cdc9a2..a249a3ebd 100644 --- a/src/GeoIP.c +++ b/src/GeoIP.c @@ -344,7 +344,11 @@ int _check_mtime(GeoIP *gi) { } /* refresh filehandle */ fclose(gi->GeoIPDatabase); +#ifdef WIN32 + gi->GeoIPDatabase = _wfopen(libtorrent::safe_convert(gi->file_path).c_str(),L"rb"); +#else gi->GeoIPDatabase = fopen(gi->file_path,"rb"); +#endif if (gi->GeoIPDatabase == NULL) { fprintf(stderr,"Error Opening file %s when reloading\n",gi->file_path); return -1; @@ -524,6 +528,12 @@ GeoIP* GeoIP_new (int flags) { return gi; } */ + +namespace libtorrent +{ + std::wstring safe_convert(std::string const& s); +} + GeoIP* GeoIP_open (const char * filename, int flags) { struct stat buf; GeoIP * gi; @@ -539,7 +549,11 @@ GeoIP* GeoIP_open (const char * filename, int flags) { return NULL; } strncpy(gi->file_path, filename, len); +#ifdef WIN32 + gi->GeoIPDatabase = _wfopen(libtorrent::safe_convert(filename).c_str(),L"rb"); +#else gi->GeoIPDatabase = fopen(filename,"rb"); +#endif if (gi->GeoIPDatabase == NULL) { fprintf(stderr,"Error Opening file %s\n",filename); free(gi->file_path); diff --git a/src/session.cpp b/src/session.cpp index 06b19f6eb..9db0ec52e 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -231,11 +231,21 @@ namespace libtorrent return m_impl->load_asnum_db(file); } + bool session::load_asnum_db(wchar_t const* file) + { + return m_impl->load_asnum_db(file); + } + bool session::load_country_db(char const* file) { return m_impl->load_country_db(file); } + bool session::load_country_db(wchar_t const* file) + { + return m_impl->load_country_db(file); + } + int session::as_for_ip(address const& addr) { aux::session_impl::mutex_t::scoped_lock l(m_impl->m_mutex); diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 48d0d6c73..3ca3b7067 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -341,6 +341,16 @@ namespace aux { return m_asnum_db; } + bool session_impl::load_asnum_db(wchar_t const* file) + { + mutex_t::scoped_lock l(m_mutex); + if (m_asnum_db) GeoIP_delete(m_asnum_db); + std::string utf8; + wchar_utf8(file, utf8); + m_asnum_db = GeoIP_open(utf8, GEOIP_STANDARD); + return m_asnum_db; + } + bool session_impl::load_country_db(char const* file) { mutex_t::scoped_lock l(m_mutex); @@ -349,6 +359,16 @@ namespace aux { return m_country_db; } + bool session_impl::load_country_db(wchar_t const* file) + { + mutex_t::scoped_lock l(m_mutex); + if (m_country_db) GeoIP_delete(m_country_db); + std::string utf8; + wchar_utf8(file, utf8); + m_country_db = GeoIP_open(utf8, GEOIP_STANDARD); + return m_country_db; + } + #endif void session_impl::load_state(entry const& ses_state)