2 * Copyright 2011-2015, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Axel Dörfler <axeld@pinc-software.de>
7 * Rene Gollent <rene@gollent.com>
8 * Oliver Tappe <zooey@hirschkaefer.de>
12 #include <package/FetchFileJob.h>
15 #ifndef HAIKU_BOOTSTRAP_BUILD
16 #include <curl/curl.h>
23 namespace BPackageKit
{
28 FetchFileJob::FetchFileJob(const BContext
& context
, const BString
& title
,
29 const BString
& fileURL
, const BEntry
& targetEntry
)
31 inherited(context
, title
),
33 fTargetEntry(targetEntry
),
34 fTargetFile(&targetEntry
, B_CREATE_FILE
| B_ERASE_FILE
| B_WRITE_ONLY
),
35 fDownloadProgress(0.0)
40 FetchFileJob::~FetchFileJob()
46 FetchFileJob::DownloadProgress() const
48 return fDownloadProgress
;
53 FetchFileJob::DownloadURL() const
55 return fFileURL
.String();
60 FetchFileJob::DownloadFileName() const
62 return fTargetEntry
.Name();
67 FetchFileJob::DownloadBytes() const
74 FetchFileJob::DownloadTotalBytes() const
81 FetchFileJob::Execute()
83 status_t result
= fTargetFile
.InitCheck();
87 #ifndef HAIKU_BOOTSTRAP_BUILD
88 CURL
* handle
= curl_easy_init();
93 result
= curl_easy_setopt(handle
, CURLOPT_NOPROGRESS
, 0);
95 result
= curl_easy_setopt(handle
, CURLOPT_XFERINFOFUNCTION
,
97 if (result
!= CURLE_OK
)
100 result
= curl_easy_setopt(handle
, CURLOPT_PROGRESSDATA
, this);
101 if (result
!= CURLE_OK
)
104 result
= curl_easy_setopt(handle
, CURLOPT_WRITEFUNCTION
,
106 if (result
!= CURLE_OK
)
109 result
= curl_easy_setopt(handle
, CURLOPT_WRITEDATA
, this);
110 if (result
!= CURLE_OK
)
113 result
= curl_easy_setopt(handle
, CURLOPT_FOLLOWLOCATION
, 1);
114 if (result
!= CURLE_OK
)
117 result
= curl_easy_setopt(handle
, CURLOPT_URL
, fFileURL
.String());
118 if (result
!= CURLE_OK
)
121 result
= curl_easy_perform(handle
);
122 curl_easy_cleanup(handle
);
123 if (result
!= CURLE_OK
) {
124 // TODO: map more curl error codes to ours for more
125 // precise error reporting
128 #endif /* !HAIKU_BOOTSTRAP_BUILD */
135 FetchFileJob::_TransferCallback(void* _job
, off_t downloadTotal
,
136 off_t downloaded
, off_t uploadTotal
, off_t uploaded
)
138 FetchFileJob
* job
= reinterpret_cast<FetchFileJob
*>(_job
);
139 if (downloadTotal
!= 0) {
140 job
->fBytes
= downloaded
;
141 job
->fTotalBytes
= downloadTotal
;
142 job
->fDownloadProgress
= (float)downloaded
/ downloadTotal
;
143 job
->NotifyStateListeners();
150 FetchFileJob::_WriteCallback(void *buffer
, size_t size
, size_t nmemb
,
153 FetchFileJob
* job
= reinterpret_cast<FetchFileJob
*>(userp
);
154 ssize_t dataWritten
= job
->fTargetFile
.Write(buffer
, size
* nmemb
);
155 return size_t(dataWritten
);
160 FetchFileJob::Cleanup(status_t jobResult
)
162 if (jobResult
!= B_OK
)
163 fTargetEntry
.Remove();
167 } // namespace BPrivate
169 } // namespace BPackageKit