libpackage: Use libcurl directly for downloads.
- Initial step towards allowing listening for download progress.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2011, Oliver Tappe <[email protected]>
|
* Copyright 2011, Oliver Tappe <[email protected]>
|
||||||
|
* Copyright 2013, Rene Gollent <[email protected]>
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
#ifndef _PACKAGE__PRIVATE__FETCH_FILE_JOB_H_
|
#ifndef _PACKAGE__PRIVATE__FETCH_FILE_JOB_H_
|
||||||
@@ -7,6 +8,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <Entry.h>
|
#include <Entry.h>
|
||||||
|
#include <File.h>
|
||||||
#include <String.h>
|
#include <String.h>
|
||||||
|
|
||||||
#include <package/Job.h>
|
#include <package/Job.h>
|
||||||
@@ -31,9 +33,19 @@ protected:
|
|||||||
virtual status_t Execute();
|
virtual status_t Execute();
|
||||||
virtual void Cleanup(status_t jobResult);
|
virtual void Cleanup(status_t jobResult);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// libcurl callbacks
|
||||||
|
static int _ProgressCallback(void *clientp,
|
||||||
|
double dltotal, double dlnow,
|
||||||
|
double ultotal, double ulnow);
|
||||||
|
|
||||||
|
static size_t _WriteCallback(void *buffer, size_t size,
|
||||||
|
size_t nmemb, void *userp);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BString fFileURL;
|
BString fFileURL;
|
||||||
BEntry fTargetEntry;
|
BEntry fTargetEntry;
|
||||||
|
BFile fTargetFile;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -137,8 +137,7 @@ BuildPlatformSharedLibrary libpackage_build.so
|
|||||||
SolverRepository.cpp
|
SolverRepository.cpp
|
||||||
SolverResult.cpp
|
SolverResult.cpp
|
||||||
:
|
:
|
||||||
libshared_build.a $(HOST_LIBBE) z $(HOST_LIBSTDC++)
|
libshared_build.a $(HOST_LIBBE) curl z $(HOST_LIBSTDC++)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
HaikuSubInclude solver ;
|
HaikuSubInclude solver ;
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2011, Haiku, Inc. All Rights Reserved.
|
* Copyright 2011-2013, Haiku, Inc. All Rights Reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
* Oliver Tappe <[email protected]>
|
* Oliver Tappe <[email protected]>
|
||||||
|
* Rene Gollent <[email protected]>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <package/FetchFileJob.h>
|
#include <package/FetchFileJob.h>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdio.h>
|
||||||
|
#include <curl/curl.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
|
||||||
#include <Path.h>
|
#include <Path.h>
|
||||||
@@ -25,7 +27,8 @@ FetchFileJob::FetchFileJob(const BContext& context, const BString& title,
|
|||||||
:
|
:
|
||||||
inherited(context, title),
|
inherited(context, title),
|
||||||
fFileURL(fileURL),
|
fFileURL(fileURL),
|
||||||
fTargetEntry(targetEntry)
|
fTargetEntry(targetEntry),
|
||||||
|
fTargetFile(&targetEntry, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,22 +41,76 @@ FetchFileJob::~FetchFileJob()
|
|||||||
status_t
|
status_t
|
||||||
FetchFileJob::Execute()
|
FetchFileJob::Execute()
|
||||||
{
|
{
|
||||||
BPath targetPath;
|
status_t result = fTargetFile.InitCheck();
|
||||||
status_t result = fTargetEntry.GetPath(&targetPath);
|
|
||||||
if (result != B_OK)
|
if (result != B_OK)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
// TODO: implement for real, maybe using the "service kit"-GSOC HTTP-stuff?
|
CURL* handle = curl_easy_init();
|
||||||
BString cmd = BString("curl -# -o ") << targetPath.Path() << " "
|
|
||||||
<< fFileURL.String();
|
|
||||||
|
|
||||||
int cmdResult = system(cmd.String());
|
if (handle == NULL)
|
||||||
if (WIFSIGNALED(cmdResult)
|
return B_NO_MEMORY;
|
||||||
&& (WTERMSIG(cmdResult) == SIGINT || WTERMSIG(cmdResult) == SIGQUIT)) {
|
|
||||||
return B_CANCELED;
|
result = curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0);
|
||||||
|
|
||||||
|
result = curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION,
|
||||||
|
&_ProgressCallback);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
|
result = curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, this);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
return B_ERROR;
|
||||||
|
|
||||||
|
result = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION,
|
||||||
|
&_WriteCallback);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
return B_ERROR;
|
||||||
|
|
||||||
|
result = curl_easy_setopt(handle, CURLOPT_WRITEDATA, this);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
return B_ERROR;
|
||||||
|
|
||||||
|
result = curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION,
|
||||||
|
&_ProgressCallback);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
return B_ERROR;
|
||||||
|
|
||||||
|
result = curl_easy_setopt(handle, CURLOPT_URL, fFileURL.String());
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
return B_ERROR;
|
||||||
|
|
||||||
|
result = curl_easy_perform(handle);
|
||||||
|
curl_easy_cleanup(handle);
|
||||||
|
if (result != CURLE_OK) {
|
||||||
|
// TODO: map more curl error codes to ours for more
|
||||||
|
// precise error reporting
|
||||||
|
return B_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmdResult == 0 ? B_OK : B_ERROR;
|
/* if (WIFSIGNALED(cmdResult)
|
||||||
|
&& (WTERMSIG(cmdResult) == SIGINT || WTERMSIG(cmdResult) == SIGQUIT)) {
|
||||||
|
return B_CANCELED;
|
||||||
|
} */
|
||||||
|
|
||||||
|
return B_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
FetchFileJob::_ProgressCallback(void *clientp, double dltotal, double dlnow,
|
||||||
|
double ultotal, double ulnow)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
size_t
|
||||||
|
FetchFileJob::_WriteCallback(void *buffer, size_t size, size_t nmemb,
|
||||||
|
void *userp)
|
||||||
|
{
|
||||||
|
FetchFileJob* job = reinterpret_cast<FetchFileJob*>(userp);
|
||||||
|
ssize_t dataWritten = job->fTargetFile.Write(buffer, size * nmemb);
|
||||||
|
return size_t(dataWritten);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ HPKG_SOURCES =
|
|||||||
local architectureObject ;
|
local architectureObject ;
|
||||||
for architectureObject in [ MultiArchSubDirSetup ] {
|
for architectureObject in [ MultiArchSubDirSetup ] {
|
||||||
on $(architectureObject) {
|
on $(architectureObject) {
|
||||||
|
UseBuildFeatureHeaders curl ;
|
||||||
UseBuildFeatureHeaders zlib ;
|
UseBuildFeatureHeaders zlib ;
|
||||||
|
|
||||||
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package hpkg ] ;
|
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package hpkg ] ;
|
||||||
@@ -70,6 +71,8 @@ for architectureObject in [ MultiArchSubDirSetup ] {
|
|||||||
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package manager ] ;
|
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package manager ] ;
|
||||||
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package solver ] ;
|
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package solver ] ;
|
||||||
|
|
||||||
|
Includes [ FGristFiles FetchFileJob.cpp ]
|
||||||
|
: [ BuildFeatureAttribute curl : headers ] ;
|
||||||
Includes [ FGristFiles $(HPKG_SOURCES) ]
|
Includes [ FGristFiles $(HPKG_SOURCES) ]
|
||||||
: [ BuildFeatureAttribute zlib : headers ] ;
|
: [ BuildFeatureAttribute zlib : headers ] ;
|
||||||
|
|
||||||
@@ -130,6 +133,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
|
|||||||
:
|
:
|
||||||
[ MultiArchDefaultGristFiles libshared.a ]
|
[ MultiArchDefaultGristFiles libshared.a ]
|
||||||
be
|
be
|
||||||
|
[ BuildFeatureAttribute curl : library ]
|
||||||
[ BuildFeatureAttribute zlib : library ]
|
[ BuildFeatureAttribute zlib : library ]
|
||||||
$(TARGET_LIBSTDC++)
|
$(TARGET_LIBSTDC++)
|
||||||
;
|
;
|
||||||
|
|||||||
Reference in New Issue
Block a user