From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33843) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bk5BR-0001Y0-ML for qemu-devel@nongnu.org; Wed, 14 Sep 2016 04:04:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bk5BL-00060H-No for qemu-devel@nongnu.org; Wed, 14 Sep 2016 04:04:40 -0400 From: Michael Tokarev Date: Wed, 14 Sep 2016 10:58:44 +0300 Message-Id: <73e37a96a0485d8f5f2edb4ff86ebae6bd7b8135.1473839869.git.mjt@msgid.tls.msk.ru> In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 18/27] curl: Operate on zero-length file List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Tom=C3=A1=C5=A1=20Golembiovsk=C3=BD?= , qemu-trivial@nongnu.org, Michael Tokarev From: Tom=C3=A1=C5=A1 Golembiovsk=C3=BD 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=C3=A1=C5=A1 Golembiovsk=C3=BD Signed-off-by: Michael Tokarev --- block/curl.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/block/curl.c b/block/curl.c index 426fb4d..571f24c 100644 --- a/block/curl.c +++ b/block/curl.c @@ -675,11 +675,28 @@ static int curl_open(BlockDriverState *bs, QDict *o= ptions, 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 =3D (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 >=3D 0x071304 + if (d < 0) { + pstrcpy(state->errmsg, CURL_ERROR_SIZE, + "Server didn't report file size."); + goto out; + } +#else + if (d <=3D 0) { + pstrcpy(state->errmsg, CURL_ERROR_SIZE, + "Unknown file size or zero-length file."); + goto out; + } +#endif + + s->len =3D (size_t)d; + if ((!strncasecmp(s->url, "http://", strlen("http://")) || !strncasecmp(s->url, "https://", strlen("https://"))) && !s->accept_range) { --=20 2.1.4