Fix for Android

- No std::to_string. We have to implement it with a ostringstream
- No pthread_cancel. So we use pthread_kill to send a signal to the thread.
This commit is contained in:
Matthieu Gautier 2018-10-24 10:44:23 +02:00
parent 910ce5f10d
commit 34021994cd
5 changed files with 35 additions and 8 deletions

View File

@ -64,6 +64,12 @@ std::string lcFirst(const std::string& word);
std::string toTitle(const std::string& word); std::string toTitle(const std::string& word);
std::string normalize(const std::string& word); std::string normalize(const std::string& word);
template<typename T>
std::string to_string(T value)
{
std::ostringstream oss;
oss << value;
return oss.str();
} }
} //namespace kiwix
#endif #endif

View File

@ -21,7 +21,7 @@ Aria2::Aria2():
m_downloadDir = getDataDirectory(); m_downloadDir = getDataDirectory();
std::vector<const char*> callCmd; std::vector<const char*> callCmd;
std::string rpc_port = "--rpc-listen-port=" + std::to_string(m_port); std::string rpc_port = "--rpc-listen-port=" + to_string(m_port);
std::string download_dir = "--dir=" + getDataDirectory(); std::string download_dir = "--dir=" + getDataDirectory();
std::string session_file = appendToDirectory(getDataDirectory(), "kiwix.session"); std::string session_file = appendToDirectory(getDataDirectory(), "kiwix.session");
std::string session = "--save-session=" + session_file; std::string session = "--save-session=" + session_file;
@ -32,7 +32,7 @@ Aria2::Aria2():
#else #else
pid_t pid = getpid(); pid_t pid = getpid();
#endif #endif
std::string stop_with_pid = "--stop-with-process=" + std::to_string(pid); std::string stop_with_pid = "--stop-with-process=" + to_string(pid);
std::string rpc_secret = "--rpc-secret=" + m_secret; std::string rpc_secret = "--rpc-secret=" + m_secret;
m_secret = "token:"+m_secret; m_secret = "token:"+m_secret;

View File

@ -72,12 +72,12 @@ void Download::updateStatus(bool follow)
} catch (InvalidRPCNode& e) { } } catch (InvalidRPCNode& e) { }
} }
m_status = status; m_status = status;
m_totalLength = std::stoull(structNode.getMember("totalLength").getValue().getAsS()); m_totalLength = structNode.getMember("totalLength").getValue().getAsI();
m_completedLength = std::stoull(structNode.getMember("completedLength").getValue().getAsS()); m_completedLength = structNode.getMember("completedLength").getValue().getAsI();
m_downloadSpeed = std::stoull(structNode.getMember("downloadSpeed").getValue().getAsS()); m_downloadSpeed = structNode.getMember("downloadSpeed").getValue().getAsI();
try { try {
auto verifiedLengthValue = structNode.getMember("verifiedLength").getValue(); auto verifiedLengthValue = structNode.getMember("verifiedLength").getValue();
m_verifiedLength = std::stoull(verifiedLengthValue.getAsS()); m_verifiedLength = verifiedLengthValue.getAsI();
} catch (InvalidRPCNode& e) { m_verifiedLength = 0; } } catch (InvalidRPCNode& e) { m_verifiedLength = 0; }
auto filesMember = structNode.getMember("files"); auto filesMember = structNode.getMember("files");
auto fileStruct = filesMember.getValue().getArray().getValue(0).getStruct(); auto fileStruct = filesMember.getValue().getArray().getValue(0).getStruct();

View File

@ -72,7 +72,7 @@ pugi::xml_node OPDSDumper::handleBook(Book book, pugi::xml_node root_node) {
acquisition_link.append_attribute("rel") = "http://opds-spec.org/acquisition/open-access"; acquisition_link.append_attribute("rel") = "http://opds-spec.org/acquisition/open-access";
acquisition_link.append_attribute("type") = "application/x-zim"; acquisition_link.append_attribute("type") = "application/x-zim";
acquisition_link.append_attribute("href") = book.getUrl().c_str(); acquisition_link.append_attribute("href") = book.getUrl().c_str();
acquisition_link.append_attribute("length") = std::to_string(book.getSize()).c_str(); acquisition_link.append_attribute("length") = to_string(book.getSize()).c_str();
} }
if (! book.getFaviconMimeType().empty() ) { if (! book.getFaviconMimeType().empty() ) {

View File

@ -7,6 +7,7 @@
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <stdlib.h>
UnixImpl::UnixImpl(): UnixImpl::UnixImpl():
m_pid(0), m_pid(0),
@ -19,11 +20,31 @@ UnixImpl::UnixImpl():
UnixImpl::~UnixImpl() UnixImpl::~UnixImpl()
{ {
kill(); kill();
// Android has no pthread_cancel :(
#ifdef __ANDROID__
pthread_kill(m_waitingThread, SIGUSR1);
#else
pthread_cancel(m_waitingThread); pthread_cancel(m_waitingThread);
#endif
} }
#ifdef __ANDROID__
void thread_exit_handler(int sig) {
pthread_exit(0);
}
#endif
void* UnixImpl::waitForPID(void* _self) void* UnixImpl::waitForPID(void* _self)
{ {
#ifdef __ANDROID__
struct sigaction actions;
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = thread_exit_handler;
sigaction(SIGUSR1, &actions, NULL);
#endif
UnixImpl* self = static_cast<UnixImpl*>(_self); UnixImpl* self = static_cast<UnixImpl*>(_self);
waitpid(self->m_pid, NULL, WEXITED); waitpid(self->m_pid, NULL, WEXITED);