- BJobStateListener: Add progress state and corresponding hook. - FetchFileJob: Notify job progress hook on libcurl notifications. - UserInteractionHandler: Add hooks for download progress and checksum validation progress. - PackageManager: inherit from JobStateListener and watch for job notifications for internally generated jobs. Forward to corresponding UserInteractionHandler hooks as needed. - Adapt pkgman, HaikuDepot and package_daemon to above changes. Neither HaikuDepot nor package_daemon's progress hooks are wired up to do anything yet though.
150 lines
2.8 KiB
C++
150 lines
2.8 KiB
C++
/*
|
|
* Copyright 2011-2013, Haiku, Inc. All Rights Reserved.
|
|
* Distributed under the terms of the MIT License.
|
|
*
|
|
* Authors:
|
|
* Oliver Tappe <[email protected]>
|
|
* Rene Gollent <[email protected]>
|
|
*/
|
|
|
|
|
|
#include <package/FetchFileJob.h>
|
|
|
|
#include <stdio.h>
|
|
#include <curl/curl.h>
|
|
#include <sys/wait.h>
|
|
|
|
#include <Path.h>
|
|
|
|
|
|
namespace BPackageKit {
|
|
|
|
namespace BPrivate {
|
|
|
|
|
|
FetchFileJob::FetchFileJob(const BContext& context, const BString& title,
|
|
const BString& fileURL, const BEntry& targetEntry)
|
|
:
|
|
inherited(context, title),
|
|
fFileURL(fileURL),
|
|
fTargetEntry(targetEntry),
|
|
fTargetFile(&targetEntry, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY),
|
|
fDownloadProgress(0.0)
|
|
{
|
|
}
|
|
|
|
|
|
FetchFileJob::~FetchFileJob()
|
|
{
|
|
}
|
|
|
|
|
|
float
|
|
FetchFileJob::DownloadProgress() const
|
|
{
|
|
return fDownloadProgress;
|
|
}
|
|
|
|
|
|
const char*
|
|
FetchFileJob::DownloadURL() const
|
|
{
|
|
return fFileURL.String();
|
|
}
|
|
|
|
|
|
const char*
|
|
FetchFileJob::DownloadFileName() const
|
|
{
|
|
return fTargetEntry.Name();
|
|
}
|
|
|
|
|
|
status_t
|
|
FetchFileJob::Execute()
|
|
{
|
|
status_t result = fTargetFile.InitCheck();
|
|
if (result != B_OK)
|
|
return result;
|
|
|
|
CURL* handle = curl_easy_init();
|
|
|
|
if (handle == NULL)
|
|
return B_NO_MEMORY;
|
|
|
|
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 B_OK;
|
|
}
|
|
|
|
|
|
int
|
|
FetchFileJob::_ProgressCallback(void *userp, double dltotal, double dlnow,
|
|
double ultotal, double ulnow)
|
|
{
|
|
FetchFileJob* job = reinterpret_cast<FetchFileJob*>(userp);
|
|
if (dltotal != 0) {
|
|
job->fDownloadProgress = dlnow / dltotal;
|
|
job->NotifyStateListeners();
|
|
}
|
|
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);
|
|
}
|
|
|
|
|
|
void
|
|
FetchFileJob::Cleanup(status_t jobResult)
|
|
{
|
|
if (jobResult != B_OK)
|
|
fTargetEntry.Remove();
|
|
}
|
|
|
|
|
|
} // namespace BPrivate
|
|
|
|
} // namespace BPackageKit
|