parent
bb40aab1ea
commit
c4026492d4
@ -1,78 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2013,2015,2018 by Jonathan Naylor G4KLX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <curl/curl.h>
|
||||
|
||||
#include "HostsFileDownloader.h"
|
||||
#include "Log.h"
|
||||
|
||||
size_t CHostsFileDownloader::write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
|
||||
{
|
||||
size_t written = fwrite(ptr, size, nmemb, stream);
|
||||
return written;
|
||||
}
|
||||
|
||||
|
||||
bool CHostsFileDownloader::download(const std::string & hostsFileURL, const std::string & hostFilePath)
|
||||
{
|
||||
CURL *curl;
|
||||
FILE *fp;
|
||||
bool ok = false;
|
||||
std::string outFileName;
|
||||
char outFileNameBuf[] = "/tmp/HostFile_XXXXXX";
|
||||
|
||||
CLog::logInfo("Downloading host file from %s", hostsFileURL.c_str());
|
||||
|
||||
curl = curl_easy_init();
|
||||
if (curl) {
|
||||
int filedes = mkstemp(outFileNameBuf);
|
||||
if(filedes > 0 && (fp = fdopen(filedes,"wb")) != nullptr) {
|
||||
curl_easy_setopt(curl, CURLOPT_URL, hostsFileURL.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
ok = res == 0;
|
||||
/* always cleanup */
|
||||
curl_easy_cleanup(curl);
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(ok) {
|
||||
outFileName = std::string(outFileNameBuf);
|
||||
|
||||
try
|
||||
{
|
||||
std::filesystem::copy_file(outFileName, hostFilePath, std::filesystem::copy_options::overwrite_existing);
|
||||
}
|
||||
catch(std::filesystem::filesystem_error& e)
|
||||
{
|
||||
ok = false;
|
||||
CLog::logError("Failed to copy host file to %s: %s", hostFilePath.c_str(), e.what());
|
||||
}
|
||||
|
||||
} else {
|
||||
CLog::logWarning("Failed to download Host file from %s, using previous file", hostsFileURL.c_str());
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class CHostsFileDownloader {
|
||||
public:
|
||||
static bool download(const std::string & hostsFileURL, const std::string & hostFilePath);
|
||||
private:
|
||||
static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream);
|
||||
};
|
||||
|
||||
Loading…
Reference in new issue