summaryrefslogtreecommitdiff
path: root/block/curl.c
diff options
context:
space:
mode:
authorTomáš Golembiovský <tgolembi@redhat.com>2016-07-08 13:18:09 +0200
committerMichael Tokarev <mjt@tls.msk.ru>2016-09-15 15:32:22 +0300
commita41c457881b5463b18901a849b4289459ab1d231 (patch)
tree28cfd8bb295364bb773cd3b996097566dccae2f0 /block/curl.c
parentd4b84d564ee3eb7a58e4585d671fb3c220b6c3b9 (diff)
curl: Operate on zero-length file
Another attempt to fix the bug 1596870. When creating new disk backed by remote file accessed via HTTPS and the backing file has zero length, qemu-img terminates with uniformative error message: qemu-img: disk.qcow2: CURL: Error opening file: While it may not make much sense to operate on empty file, other block backends (e.g. raw backend for regular files) seem to allow it. This patch fixes it for the curl backend and improves the reported error. Signed-off-by: Tomáš Golembiovský <tgolembi@redhat.com> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
Diffstat (limited to 'block/curl.c')
-rw-r--r--block/curl.c25
1 files changed, 21 insertions, 4 deletions
diff --git a/block/curl.c b/block/curl.c
index 426fb4d674..571f24cac2 100644
--- a/block/curl.c
+++ b/block/curl.c
@@ -675,11 +675,28 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags,
curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
if (curl_easy_perform(state->curl))
goto out;
- curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
- if (d)
- s->len = (size_t)d;
- else if(!s->len)
+ if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
goto out;
+ }
+ /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
+ * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
+ * known and zero if it is realy zero-length file. */
+#if LIBCURL_VERSION_NUM >= 0x071304
+ if (d < 0) {
+ pstrcpy(state->errmsg, CURL_ERROR_SIZE,
+ "Server didn't report file size.");
+ goto out;
+ }
+#else
+ if (d <= 0) {
+ pstrcpy(state->errmsg, CURL_ERROR_SIZE,
+ "Unknown file size or zero-length file.");
+ goto out;
+ }
+#endif
+
+ s->len = (size_t)d;
+
if ((!strncasecmp(s->url, "http://", strlen("http://"))
|| !strncasecmp(s->url, "https://", strlen("https://")))
&& !s->accept_range) {