Pino Toscano
2020-Jan-08 14:20 UTC
[Libguestfs] [nbdkit PATCH] curl: use CURLINFO_CONTENT_LENGTH_DOWNLOAD_T when available
Use CURLINFO_CONTENT_LENGTH_DOWNLOAD_T for curl_easy_getinfo() (added in
curl 7.55.0) to get the length of a remote file, instead of the old
CURLINFO_CONTENT_LENGTH_DOWNLOAD. This way the size is already a 64-bit
integer value, as opposed to a double (the old information).
---
plugins/curl/curl.c | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/plugins/curl/curl.c b/plugins/curl/curl.c
index 1b0d2b9..e0449b7 100644
--- a/plugins/curl/curl.c
+++ b/plugins/curl/curl.c
@@ -70,6 +70,10 @@ static long protocols = CURLPROTO_ALL;
/* Use '-D curl.verbose=1' to set. */
int curl_debug_verbose = 0;
+#if CURL_AT_LEAST_VERSION(7, 55, 0)
+#define HAVE_CURLINFO_CONTENT_LENGTH_DOWNLOAD_T
+#endif
+
static void
curl_load (void)
{
@@ -290,6 +294,9 @@ curl_open (int readonly)
struct curl_handle *h;
CURLcode r;
double d;
+#ifdef HAVE_CURLINFO_CONTENT_LENGTH_DOWNLOAD_T
+ curl_off_t o;
+#endif
h = calloc (1, sizeof *h);
if (h == NULL) {
@@ -377,6 +384,21 @@ curl_open (int readonly)
goto err;
}
+#ifdef HAVE_CURLINFO_CONTENT_LENGTH_DOWNLOAD_T
+ r = curl_easy_getinfo (h->c, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &o);
+ if (r != CURLE_OK) {
+ display_curl_error (h, r, "could not get length of remote file
[%s]", url);
+ goto err;
+ }
+
+ if (o == -1) {
+ nbdkit_error ("could not get length of remote file [%s], "
+ "is the URL correct?", url);
+ goto err;
+ }
+
+ h->exportsize = o;
+#else
r = curl_easy_getinfo (h->c, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
if (r != CURLE_OK) {
display_curl_error (h, r, "could not get length of remote file
[%s]", url);
@@ -390,6 +412,7 @@ curl_open (int readonly)
}
h->exportsize = (size_t) d;
+#endif
nbdkit_debug ("content length: %" PRIi64, h->exportsize);
if (strncasecmp (url, "http://", strlen ("http://")) == 0
||
--
2.24.1
Richard W.M. Jones
2020-Jan-09 13:08 UTC
Re: [Libguestfs] [nbdkit PATCH] curl: use CURLINFO_CONTENT_LENGTH_DOWNLOAD_T when available
[Adrian - ignore what I said in the previous email. As you can see there's a patch submitted which does the right thing aleady.] On Wed, Jan 08, 2020 at 03:20:48PM +0100, Pino Toscano wrote:> Use CURLINFO_CONTENT_LENGTH_DOWNLOAD_T for curl_easy_getinfo() (added in > curl 7.55.0) to get the length of a remote file, instead of the old > CURLINFO_CONTENT_LENGTH_DOWNLOAD. This way the size is already a 64-bit > integer value, as opposed to a double (the old information). > --- > plugins/curl/curl.c | 23 +++++++++++++++++++++++ > 1 file changed, 23 insertions(+) > > diff --git a/plugins/curl/curl.c b/plugins/curl/curl.c > index 1b0d2b9..e0449b7 100644 > --- a/plugins/curl/curl.c > +++ b/plugins/curl/curl.c > @@ -70,6 +70,10 @@ static long protocols = CURLPROTO_ALL; > /* Use '-D curl.verbose=1' to set. */ > int curl_debug_verbose = 0; > > +#if CURL_AT_LEAST_VERSION(7, 55, 0) > +#define HAVE_CURLINFO_CONTENT_LENGTH_DOWNLOAD_T > +#endif > + > static void > curl_load (void) > { > @@ -290,6 +294,9 @@ curl_open (int readonly) > struct curl_handle *h; > CURLcode r; > double d; > +#ifdef HAVE_CURLINFO_CONTENT_LENGTH_DOWNLOAD_T > + curl_off_t o; > +#endif > > h = calloc (1, sizeof *h); > if (h == NULL) { > @@ -377,6 +384,21 @@ curl_open (int readonly) > goto err; > } > > +#ifdef HAVE_CURLINFO_CONTENT_LENGTH_DOWNLOAD_T > + r = curl_easy_getinfo (h->c, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &o); > + if (r != CURLE_OK) { > + display_curl_error (h, r, "could not get length of remote file [%s]", url); > + goto err; > + } > + > + if (o == -1) { > + nbdkit_error ("could not get length of remote file [%s], " > + "is the URL correct?", url); > + goto err; > + } > + > + h->exportsize = o; > +#else > r = curl_easy_getinfo (h->c, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d); > if (r != CURLE_OK) { > display_curl_error (h, r, "could not get length of remote file [%s]", url); > @@ -390,6 +412,7 @@ curl_open (int readonly) > } > > h->exportsize = (size_t) d; > +#endif > nbdkit_debug ("content length: %" PRIi64, h->exportsize); > > if (strncasecmp (url, "http://", strlen ("http://")) == 0 || > --ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/