diff --git a/cpp/src/phonenumbers/shortnumberinfo.cc b/cpp/src/phonenumbers/shortnumberinfo.cc index 6c1daeb95..cd81061c5 100644 --- a/cpp/src/phonenumbers/shortnumberinfo.cc +++ b/cpp/src/phonenumbers/shortnumberinfo.cc @@ -19,9 +19,10 @@ #include #include +#include + #include "phonenumbers/default_logger.h" #include "phonenumbers/matcher_api.h" -#include "phonenumbers/phonemetadata.pb.h" #include "phonenumbers/phonenumberutil.h" #include "phonenumbers/regex_based_matcher.h" #include "phonenumbers/region_code.h" @@ -37,6 +38,9 @@ using std::string; #ifdef ISTREAM_DATA_PROVIDER + +const string METADATA_SHORT_FILE_NAME = "metadata_short.dat"; + bool LoadMetadataFromFile(string fileName, PhoneMetadataCollection* metadata) { std::fstream input(fileName.c_str(), std::ios::in | std::ios::binary); if (!input) { @@ -54,9 +58,45 @@ void ShortNumberInfo::ClearMetadata(){ regions_where_emergency_numbers_must_be_exact_->clear(); } -void ShortNumberInfo::ReloadMetadata() { +bool UpdateShortMetadataFile(const string& filepath) { + + boost::filesystem::path path_to = boost::filesystem::current_path(); + path_to += "/"; + path_to += METADATA_SHORT_FILE_NAME; + boost::filesystem::path path_bk = path_to; + path_bk += ".bak"; + boost::system::error_code ec; + + boost::filesystem::copy(path_to.string(), path_bk.string(), ec); + if (ec) { + LOG(DFATAL) << "Could not create backup copy of metadata file." << ec.message(); + return false; + } + boost::filesystem::rename(filepath, path_to.string(), ec); + if(ec) { + LOG(DFATAL) << "Could not rename metadata file." << ec.message(); + return false; + } + boost::filesystem::remove(path_bk, ec); + if(ec) { + LOG(DFATAL) << "Could not remove metadata file." << ec.message(); + } + return true; +} + +bool ShortNumberInfo::ReloadMetadata(const string& filename) { ClearMetadata(); - LoadMetadata(); + PhoneMetadataCollection metadata_collection; + if (!LoadMetadataFromFile(filename, &metadata_collection)) { + LOG(DFATAL) << "Could not parse metadata from file."; + return false; + } + LoadMetadataFromCollection(metadata_collection); + if(!UpdateShortMetadataFile(filename)){ + LOG(DFATAL) << "Could not update metadata file."; + return false; + } + return true; } #else bool LoadCompiledInMetadata(PhoneMetadataCollection* metadata) { @@ -68,20 +108,7 @@ bool LoadCompiledInMetadata(PhoneMetadataCollection* metadata) { } #endif // ISTREAM_DATA_PROVIDER -void ShortNumberInfo::LoadMetadata() { - PhoneMetadataCollection metadata_collection; -#ifdef ISTREAM_DATA_PROVIDER - const string metadataInputFile = "metadata_short.dat"; - if (!LoadMetadataFromFile(metadataInputFile, &metadata_collection)) { - LOG(DFATAL) << "Could not parse metadata from file."; - return; - } -#else - if (!LoadCompiledInMetadata(&metadata_collection)) { - LOG(DFATAL) << "Could not parse compiled-in metadata."; - return; - } -#endif // ISTREAM_DATA_PROVIDER +void ShortNumberInfo::LoadMetadataFromCollection(const PhoneMetadataCollection& metadata_collection) { for (const auto& metadata : metadata_collection.metadata()) { const string& region_code = metadata.id(); region_to_short_metadata_map_->insert(std::make_pair(region_code, metadata)); @@ -96,7 +123,20 @@ ShortNumberInfo::ShortNumberInfo() matcher_api_(new RegexBasedMatcher()), region_to_short_metadata_map_(new absl::flat_hash_map()), regions_where_emergency_numbers_must_be_exact_(new absl::flat_hash_set()) { - LoadMetadata(); + + PhoneMetadataCollection metadata_collection; +#ifdef ISTREAM_DATA_PROVIDER + if (!LoadMetadataFromFile(METADATA_SHORT_FILE_NAME, &metadata_collection)) { + LOG(DFATAL) << "Could not parse metadata from file."; + return; + } +#else + if (!LoadCompiledInMetadata(&metadata_collection)) { + LOG(DFATAL) << "Could not parse compiled-in metadata."; + return; + } +#endif // ISTREAM_DATA_PROVIDER + LoadMetadataFromCollection(metadata_collection); } ShortNumberInfo::~ShortNumberInfo() {} diff --git a/cpp/src/phonenumbers/shortnumberinfo.h b/cpp/src/phonenumbers/shortnumberinfo.h index b67858e08..2339ae827 100644 --- a/cpp/src/phonenumbers/shortnumberinfo.h +++ b/cpp/src/phonenumbers/shortnumberinfo.h @@ -23,7 +23,7 @@ #include #include #include - +#include "phonenumbers/phonemetadata.pb.h" #include "phonenumbers/base/basictypes.h" #include "phonenumbers/base/memory/scoped_ptr.h" #include "absl/container/flat_hash_set.h" @@ -43,6 +43,7 @@ class PhoneNumber; class PhoneNumberUtil; class ShortNumberInfo { + void LoadMetadataFromCollection(const PhoneMetadataCollection& metadata_collection); public: ShortNumberInfo(); ~ShortNumberInfo(); @@ -50,7 +51,7 @@ class ShortNumberInfo { private: void ClearMetadata(); public: - void ReloadMetadata(); + bool ReloadMetadata(const string& filename); #endif // ISTREAM_DATA_PROVIDER // Cost categories of short numbers. enum ShortNumberCost { diff --git a/cpp/test/phonenumbers/shortnumberinfo_test.cc b/cpp/test/phonenumbers/shortnumberinfo_test.cc index 83dc4f918..ca5a504e1 100644 --- a/cpp/test/phonenumbers/shortnumberinfo_test.cc +++ b/cpp/test/phonenumbers/shortnumberinfo_test.cc @@ -47,7 +47,7 @@ class ShortNumberInfoTest : public testing::Test { } const PhoneNumberUtil phone_util_; - const ShortNumberInfo short_info_; + ShortNumberInfo short_info_; private: DISALLOW_COPY_AND_ASSIGN(ShortNumberInfoTest); @@ -96,6 +96,11 @@ TEST_F(ShortNumberInfoTest, IsValidShortNumber) { EXPECT_TRUE(short_info_.IsValidShortNumber(shared_number)); } +TEST_F(ShortNumberInfoTest, ReloadMetadata) { + const string filename = "metadata_short.dat"; + EXPECT_TRUE(short_info_.ReloadMetadata(filename)); +} + TEST_F(ShortNumberInfoTest, IsCarrierSpecific) { PhoneNumber carrier_specific_number; carrier_specific_number.set_country_code(1);