Skip to content

Commit

Permalink
detect 'GeoIP/alias' correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cnbatch committed Jan 16, 2021
1 parent 538445b commit 46c75f0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
30 changes: 30 additions & 0 deletions geoip_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@ typedef size_t (*call_back_function_ptr)(char*, size_t, size_t, void*);
CURLcode download_geoip_block(const std::string &url, void *write_to, call_back_function_ptr call_back);
map<string, string> unpack_archive(shared_ptr<archive> current_archive_ptr);

bool access_or_create_folder(const std::string &folder_name, bool slience_mode)
{
fs::path target_folder = folder_name;
if (!fs::exists(target_folder))
{
if (!slience_mode) cout << target_folder << " not exist\n";
if (access_or_create_folder(target_folder.parent_path(), slience_mode))
{
if (access(target_folder.parent_path().c_str(), W_OK) != 0)
{
if (!slience_mode) cout << target_folder.parent_path() << " cannot write (no permission)\n";
return false;
}

if (fs::create_directory(target_folder))
{
if (!slience_mode) cout << target_folder << " created\n";
}
else
{
if (!slience_mode) cout << target_folder << " cannot create\n";
return false;
}
}
else return false;
}

return true;
}

bool download_geoip_block_file(const std::string &url, shared_ptr<FILE> temp_file)
{
auto res = download_geoip_block(url, temp_file.get(),
Expand Down
3 changes: 2 additions & 1 deletion geoip_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include <memory>
#include <vector>

bool access_or_create_folder(const std::string &folder_name, bool slience_mode);
bool download_geoip_block_file(const std::string &url, std::shared_ptr<FILE> temp_file);
std::vector<char> download_geoip_block_memory(const std::string &url);
std::map<std::string, std::string> unpack_archive(std::vector<char> &received_data);
std::map<std::string, std::string> unpack_archive(std::vector<char> &&received_data);
std::map<std::string, std::string> unpack_archive(std::shared_ptr<FILE> temp_file);
std::map<std::string, std::string> unpack_archive(std::shared_ptr<FILE> temp_file);
13 changes: 5 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main(int argc, char *argv[])
default:
cout << "Usage: \n";
cout << application_name << " [-option]\n";
cout << application_name << " (without parameter): the same as \"" << application_name << " -m\"\n";
cout << application_name << " <without parameter>: the same as \"" << application_name << " -m\"\n";
cout << application_name << " -m\t" "Download and Parse GeoIP file in memory directly (faster)\n";
cout << application_name << " -f\t" "Download and Parse GeoIP file in /tmp (save memory)\n";
cout << application_name << " -s\t" "Slience mode (no stdout message)\n";
Expand All @@ -46,14 +46,11 @@ int main(int argc, char *argv[])
}
}

if (!fs::exists("/usr/local/share/GeoIP/alias"))
if (!access_or_create_folder("/usr/local/share/GeoIP/alias", slience_mode))
{
if (!fs::create_directory("/usr/local/share/GeoIP/alias"))
{
if(!slience_mode)
cerr << "Cannot access '/usr/local/share/GeoIP/alias'.\n\n";
return -1;
}
if (!slience_mode)
cerr << "Cannot access '/usr/local/share/GeoIP/alias'.\n\n";
return -1;
}

string json_record = R"({"address_count":{0},"file_count":{1},"timestamp":"{2}","locations_filename":"GeoLite2-Country-Locations-en.csv","address_sources":{"IPv4":"GeoLite2-Country-Blocks-IPv4.csv","IPv6":"GeoLite2-Country-Blocks-IPv6.csv"}})";
Expand Down

0 comments on commit 46c75f0

Please sign in to comment.