From nobody Sun Oct 5 19:22:55 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1543615823047354.9779950285225; Fri, 30 Nov 2018 14:10:23 -0800 (PST) Received: from localhost ([::1]:34848 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gSqzM-0001yb-V1 for importer@patchew.org; Fri, 30 Nov 2018 17:10:21 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55995) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gSqtI-0004P0-To for qemu-devel@nongnu.org; Fri, 30 Nov 2018 17:04:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gSqtH-0004Ff-7a for qemu-devel@nongnu.org; Fri, 30 Nov 2018 17:04:04 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52186) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gSqtE-0003px-7x; Fri, 30 Nov 2018 17:04:00 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8FE2B3DE0D; Fri, 30 Nov 2018 22:03:59 +0000 (UTC) Received: from red.redhat.com (ovpn-117-105.phx2.redhat.com [10.3.117.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id F2DF81057072; Fri, 30 Nov 2018 22:03:58 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Fri, 30 Nov 2018 16:03:37 -0600 Message-Id: <20181130220344.3350618-9-eblake@redhat.com> In-Reply-To: <20181130220344.3350618-1-eblake@redhat.com> References: <20181130220344.3350618-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Fri, 30 Nov 2018 22:03:59 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 08/14] nbd/client: Refactor nbd_receive_list() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: nsoffer@redhat.com, vsementsov@virtuozzo.com, jsnow@redhat.com, rjones@redhat.com, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add some parameters to make this function reusable in upcoming export listing, where we will want to capture the name and description rather than compare against a user-supplied name. No change in semantics to the existing caller. Signed-off-by: Eric Blake Reviewed-by: Richard W.M. Jones --- nbd/client.c | 66 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index 1dc8f83e19a..27785c55d0a 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -232,18 +232,21 @@ static int nbd_handle_reply_err(QIOChannel *ioc, NBDO= ptionReply *reply, return result; } -/* Process another portion of the NBD_OPT_LIST reply. Set *@match if - * the current reply matches @want or if the server does not support - * NBD_OPT_LIST, otherwise leave @match alone. Return 0 if iteration - * is complete, positive if more replies are expected, or negative - * with @errp set if an unrecoverable error occurred. */ +/* Process another portion of the NBD_OPT_LIST reply. If @want, then + * set *@match if the current reply matches @want or if the server + * does not support NBD_OPT_LIST, otherwise leave @match alone. + * Otherwise, @nameout and @description are malloc'd to contain + * NUL-terminated copies of the reply. Return 0 if iteration is + * complete, positive if more replies are expected, or negative with + * @errp set if an unrecoverable error occurred. */ static int nbd_receive_list(QIOChannel *ioc, const char *want, bool *match, - Error **errp) + char **nameout, char **description, Error **er= rp) { NBDOptionReply reply; uint32_t len; uint32_t namelen; - char name[NBD_MAX_NAME_SIZE + 1]; + char array[NBD_MAX_NAME_SIZE + 1]; + char *name =3D array; int error; if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) { @@ -253,7 +256,12 @@ static int nbd_receive_list(QIOChannel *ioc, const cha= r *want, bool *match, if (error <=3D 0) { /* The server did not support NBD_OPT_LIST, so set *match on * the assumption that any name will be accepted. */ - *match =3D true; + if (want) { + *match =3D true; + } else if (!error) { + error_setg(errp, "Server does not support export lists"); + error =3D -1; + } return error; } len =3D reply.length; @@ -290,30 +298,49 @@ static int nbd_receive_list(QIOChannel *ioc, const ch= ar *want, bool *match, nbd_send_opt_abort(ioc); return -1; } - if (namelen !=3D strlen(want)) { - if (nbd_drop(ioc, len, errp) < 0) { - error_prepend(errp, - "failed to skip export name with wrong length: "= ); - nbd_send_opt_abort(ioc); - return -1; + if (want) { + if (namelen !=3D strlen(want)) { + if (nbd_drop(ioc, len, errp) < 0) { + error_prepend(errp, + "failed to skip export name with wrong lengt= h: "); + nbd_send_opt_abort(ioc); + return -1; + } + return 1; } - return 1; + assert(namelen < sizeof(array)); + } else { + assert(nameout); + *nameout =3D name =3D g_new(char, namelen + 1); } - assert(namelen < sizeof(name)); if (nbd_read(ioc, name, namelen, errp) < 0) { error_prepend(errp, "failed to read export name: "); nbd_send_opt_abort(ioc); + if (!want) { + free(name); + } return -1; } name[namelen] =3D '\0'; len -=3D namelen; - if (nbd_drop(ioc, len, errp) < 0) { + if (!want) { + assert(description); + *description =3D g_new(char, len + 1); + if (nbd_read(ioc, *description, len, errp) < 0) { + error_prepend(errp, "failed to read export description: "); + nbd_send_opt_abort(ioc); + free(name); + free(*description); + return -1; + } + (*description)[len] =3D '\0'; + } else if (nbd_drop(ioc, len, errp) < 0) { error_prepend(errp, "failed to read export description: "); nbd_send_opt_abort(ioc); return -1; } - if (!strcmp(name, want)) { + if (want && !strcmp(name, want)) { *match =3D true; } return 1; @@ -498,7 +525,8 @@ static int nbd_receive_query_exports(QIOChannel *ioc, } while (1) { - int ret =3D nbd_receive_list(ioc, wantname, &foundExport, errp); + int ret =3D nbd_receive_list(ioc, wantname, &foundExport, + NULL, NULL, errp); if (ret < 0) { /* Server gave unexpected reply */ --=20 2.17.2