From 69a53ac5b488d6562ebf75b98f143000ba70d623 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sat, 20 Apr 2013 21:22:34 +0200 Subject: [PATCH] Add DownloadFileRequest Downloads a file and optionally checks its checksum. --- .../build/os/package/DownloadFileRequest.h | 1 + headers/os/package/DownloadFileRequest.h | 41 +++++++++ src/build/libpackage/Jamfile | 1 + src/kits/package/DownloadFileRequest.cpp | 83 +++++++++++++++++++ src/kits/package/Jamfile | 1 + 5 files changed, 127 insertions(+) create mode 100644 headers/build/os/package/DownloadFileRequest.h create mode 100644 headers/os/package/DownloadFileRequest.h create mode 100644 src/kits/package/DownloadFileRequest.cpp diff --git a/headers/build/os/package/DownloadFileRequest.h b/headers/build/os/package/DownloadFileRequest.h new file mode 100644 index 0000000000..2dd8722084 --- /dev/null +++ b/headers/build/os/package/DownloadFileRequest.h @@ -0,0 +1 @@ +#include <../os/package/DownloadFileRequest.h> diff --git a/headers/os/package/DownloadFileRequest.h b/headers/os/package/DownloadFileRequest.h new file mode 100644 index 0000000000..cf42d53f28 --- /dev/null +++ b/headers/os/package/DownloadFileRequest.h @@ -0,0 +1,41 @@ +/* + * Copyright 2013, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef _PACKAGE__DOWNLOAD_FILE_REQUEST_H_ +#define _PACKAGE__DOWNLOAD_FILE_REQUEST_H_ + + +#include +#include + +#include +#include + + +namespace BPackageKit { + + +class DownloadFileRequest : public BRequest { + typedef BRequest inherited; + +public: + DownloadFileRequest(const BContext& context, + const BString& fileURL, + const BEntry& targetEntry, + const BString& checksum = BString()); + virtual ~DownloadFileRequest(); + + virtual status_t CreateInitialJobs(); + +private: + BString fFileURL; + BEntry fTargetEntry; + BString fChecksum; +}; + + +} // namespace BPackageKit + + +#endif // _PACKAGE__DOWNLOAD_FILE_REQUEST_H_ diff --git a/src/build/libpackage/Jamfile b/src/build/libpackage/Jamfile index 1d99a84d1b..d3f8dfcfc8 100644 --- a/src/build/libpackage/Jamfile +++ b/src/build/libpackage/Jamfile @@ -73,6 +73,7 @@ BuildPlatformSharedLibrary libpackage_build.so BlockBufferCacheNoLock.cpp ChecksumAccessors.cpp Context.cpp + DownloadFileRequest.cpp DropRepositoryRequest.cpp FetchFileJob.cpp InstallationLocationInfo.cpp diff --git a/src/kits/package/DownloadFileRequest.cpp b/src/kits/package/DownloadFileRequest.cpp new file mode 100644 index 0000000000..81b4064447 --- /dev/null +++ b/src/kits/package/DownloadFileRequest.cpp @@ -0,0 +1,83 @@ +/* + * Copyright 2013, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Ingo Weinhold + */ + + +#include + +#include +#include + + +namespace BPackageKit { + + +using namespace BPrivate; + + +DownloadFileRequest::DownloadFileRequest(const BContext& context, + const BString& fileURL, const BEntry& targetEntry, const BString& checksum) + : + inherited(context), + fFileURL(fileURL), + fTargetEntry(targetEntry), + fChecksum(checksum) +{ + if (fInitStatus == B_OK) { + if (fFileURL.IsEmpty()) + fInitStatus = B_BAD_VALUE; + else + fInitStatus = targetEntry.InitCheck(); + } +} + + +DownloadFileRequest::~DownloadFileRequest() +{ +} + + +status_t +DownloadFileRequest::CreateInitialJobs() +{ + status_t error = InitCheck(); + if (error != B_OK) + return B_NO_INIT; + + // create the download job + FetchFileJob* fetchJob = new (std::nothrow) FetchFileJob(fContext, + BString("Downloading ") << fFileURL, fFileURL, fTargetEntry); + if (fetchJob == NULL) + return B_NO_MEMORY; + + if ((error = QueueJob(fetchJob)) != B_OK) { + delete fetchJob; + return error; + } + + // create the checksum validation job + if (fChecksum.IsEmpty()) + return B_OK; + + ValidateChecksumJob* validateJob = new (std::nothrow) ValidateChecksumJob( + fContext, BString("Validating checksum for ") << fFileURL, + new (std::nothrow) StringChecksumAccessor(fChecksum), + new (std::nothrow) GeneralFileChecksumAccessor(fTargetEntry, true)); + + if (validateJob == NULL) + return B_NO_MEMORY; + + if ((error = QueueJob(validateJob)) != B_OK) { + delete validateJob; + return error; + } + + return B_OK; +} + + +} // namespace BPackageKit diff --git a/src/kits/package/Jamfile b/src/kits/package/Jamfile index 6df66e5cb2..525947eeca 100644 --- a/src/kits/package/Jamfile +++ b/src/kits/package/Jamfile @@ -53,6 +53,7 @@ SharedLibrary libpackage.so ChecksumAccessors.cpp Context.cpp DaemonClient.cpp + DownloadFileRequest.cpp DropRepositoryRequest.cpp FetchFileJob.cpp InstallationLocationInfo.cpp