From nobody Mon Feb 9 16:02:38 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.74 as permitted sender) client-ip=170.10.129.74; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-74.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.74 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=suse.de Return-Path: Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.129.74]) by mx.zohomail.com with SMTPS id 1651755705579679.1889989006373; Thu, 5 May 2022 06:01:45 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-613-YyZpJZxuPeq5JYYAnbX9kg-1; Thu, 05 May 2022 09:01:40 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 378A7803792; Thu, 5 May 2022 13:01:29 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1D970552CF6; Thu, 5 May 2022 13:01:29 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id D2C451947050; Thu, 5 May 2022 13:01:27 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id CB55A1947B87 for ; Thu, 5 May 2022 13:01:23 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id AEF5114C26D7; Thu, 5 May 2022 13:01:23 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast09.extmail.prod.ext.rdu2.redhat.com [10.11.55.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AAD3F14C1EAE for ; Thu, 5 May 2022 13:01:23 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 92558299E754 for ; Thu, 5 May 2022 13:01:23 +0000 (UTC) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-652-TdiakTOnMxWWgTB0wyHhfQ-1; Thu, 05 May 2022 09:01:21 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 38681219BF; Thu, 5 May 2022 12:52:49 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0207D13B11; Thu, 5 May 2022 12:52:48 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id sPZ4OqDIc2KKDAAAMHmgww (envelope-from ); Thu, 05 May 2022 12:52:48 +0000 X-MC-Unique: YyZpJZxuPeq5JYYAnbX9kg-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: TdiakTOnMxWWgTB0wyHhfQ-1 From: Claudio Fontana To: =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= Subject: [libvirt RFCv6 05/27] virfile: change virFileDiskCopy arguments to extend beyond stdin, stdout Date: Thu, 5 May 2022 14:52:19 +0200 Message-Id: <20220505125241.13291-6-cfontana@suse.de> In-Reply-To: <20220505125241.13291-1-cfontana@suse.de> References: <20220505125241.13291-1-cfontana@suse.de> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: libvir-list@redhat.com, "Dr . David Alan Gilbert" , Claudio Fontana Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1651756608000100001 Content-Type: text/plain; charset="utf-8"; x-default="true" Signed-off-by: Claudio Fontana --- src/util/iohelper.c | 17 +--------- src/util/virfile.c | 82 ++++++++++++++++++++++++++++++++++----------- src/util/virfile.h | 2 +- 3 files changed, 64 insertions(+), 37 deletions(-) diff --git a/src/util/iohelper.c b/src/util/iohelper.c index ce10ccb905..055540c8c4 100644 --- a/src/util/iohelper.c +++ b/src/util/iohelper.c @@ -58,7 +58,6 @@ int main(int argc, char **argv) { const char *path; - int oflags =3D -1; int fd =3D -1; =20 program_name =3D argv[0]; @@ -79,25 +78,11 @@ main(int argc, char **argv) program_name, argv[3]); exit(EXIT_FAILURE); } -#ifdef F_GETFL - oflags =3D fcntl(fd, F_GETFL); -#else - /* Stupid mingw. */ - if (fd =3D=3D STDIN_FILENO) - oflags =3D O_RDONLY; - else if (fd =3D=3D STDOUT_FILENO) - oflags =3D O_WRONLY; -#endif - if (oflags < 0) { - fprintf(stderr, _("%s: unable to determine access mode of fd %= d"), - program_name, fd); - exit(EXIT_FAILURE); - } } else { /* unknown argc pattern */ usage(EXIT_FAILURE); } =20 - if (fd < 0 || virFileDiskCopy(path, fd, oflags) < 0) + if (fd < 0 || virFileDiskCopy(fd, path, -1, "stdio") < 0) goto error; =20 return 0; diff --git a/src/util/virfile.c b/src/util/virfile.c index 30b0eee074..87d3e53819 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -4669,19 +4669,64 @@ runIOCopy(const struct runIOParams p) return total; } =20 +/** + * virFileDiskCopy: run IO to copy data between storage and a pipe or sock= et. + * + * @disk_fd: the already open regular file or block device + * @disk_path: the pathname corresponding to disk_fd (for error reportin= g) + * @remote_fd: the pipe or socket + * Use -1 to auto-choose between STDIN or STDOUT. + * @remote_path: the pathname corresponding to remote_fd (for error report= ing) + * + * Note that the direction of the transfer is detected based on the @disk_= fd + * file access mode (man 2 open). Therefore @disk_fd must be opened with + * O_RDONLY or O_WRONLY. O_RDWR is not supported. + * + * virFileDiskCopy always closes the file descriptor disk_fd, + * and any error during close(2) is reported and considered a failure. + * + * Returns: bytes transferred or < 0 on failure. + */ =20 off_t -virFileDiskCopy(const char *path, int fd, int oflags) +virFileDiskCopy(int disk_fd, const char *disk_path, int remote_fd, const c= har *remote_path) { int ret =3D -1; off_t total =3D 0; struct stat sb; struct runIOParams p; + int oflags =3D -1; + +#ifdef F_GETFL + oflags =3D fcntl(disk_fd, F_GETFL); +#else /* !F_GETFL */ + /* + * mingw still does not support F_GETFL, and does not seem willing to + * support it in the future. So we need a hack to get iohelper working, + * specifically possible only when using stdio. + */ + if (disk_fd =3D=3D STDIN_FILENO) { + oflags =3D O_RDONLY; + } else if (disk_fd =3D=3D STDOUT_FILENO) { + oflags =3D O_WRONLY; + } else { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, + _("%s: disk_fd must be stdin or stdout, due to lack= of F_GETFL (mingw)"), + __FUNCTION__); + goto cleanup; + } +#endif /* !F_GETFL */ =20 - if (fstat(fd, &sb) < 0) { + if (oflags < 0) { virReportSystemError(errno, - _("Unable to access file descriptor %d path %= s"), - fd, path); + _("unable to determine access mode of %s"), + disk_path); + goto cleanup; + } + if (fstat(disk_fd, &sb) < 0) { + virReportSystemError(errno, + _("unable to stat file descriptor %d path %s"= ), + disk_fd, disk_path); goto cleanup; } p.isBlockDev =3D S_ISBLK(sb.st_mode); @@ -4690,23 +4735,21 @@ virFileDiskCopy(const char *path, int fd, int oflag= s) switch (oflags & O_ACCMODE) { case O_RDONLY: p.isWrite =3D false; - p.fdin =3D fd; - p.fdinname =3D path; - p.fdout =3D STDOUT_FILENO; - p.fdoutname =3D "stdout"; + p.fdin =3D disk_fd; + p.fdinname =3D disk_path; + p.fdout =3D remote_fd >=3D 0 ? remote_fd : STDOUT_FILENO; + p.fdoutname =3D remote_path; break; case O_WRONLY: p.isWrite =3D true; - p.fdin =3D STDIN_FILENO; - p.fdinname =3D "stdin"; - p.fdout =3D fd; - p.fdoutname =3D path; + p.fdin =3D remote_fd >=3D 0 ? remote_fd : STDIN_FILENO; + p.fdinname =3D remote_path; + p.fdout =3D disk_fd; + p.fdoutname =3D disk_path; break; - case O_RDWR: default: - virReportSystemError(EINVAL, - _("Unable to process file with flags %d"), + virReportSystemError(EINVAL, _("Unable to process file with flags = %d"), (oflags & O_ACCMODE)); goto cleanup; } @@ -4715,12 +4758,12 @@ virFileDiskCopy(const char *path, int fd, int oflag= s) if (!p.isBlockDev && p.isDirect) { off_t off; if (p.isWrite) { - if ((off =3D lseek(fd, 0, SEEK_END)) !=3D 0) { + if ((off =3D lseek(disk_fd, 0, SEEK_END)) !=3D 0) { virReportSystemError(off < 0 ? errno : EINVAL, "%s", _("O_DIRECT write needs empty seekabl= e file")); goto cleanup; } - } else if ((off =3D lseek(fd, 0, SEEK_CUR)) !=3D 0) { + } else if ((off =3D lseek(disk_fd, 0, SEEK_CUR)) !=3D 0) { virReportSystemError(off < 0 ? errno : EINVAL, "%s", _("O_DIRECT read needs entire seekable fi= le")); goto cleanup; @@ -4742,9 +4785,8 @@ virFileDiskCopy(const char *path, int fd, int oflags) ret =3D 0; =20 cleanup: - if (VIR_CLOSE(fd) < 0 && - ret =3D=3D 0) { - virReportSystemError(errno, _("Unable to close %s"), path); + if (VIR_CLOSE(disk_fd) < 0 && ret =3D=3D 0) { + virReportSystemError(errno, _("Unable to close %s"), disk_path); ret =3D -1; } return ret; diff --git a/src/util/virfile.h b/src/util/virfile.h index 59efb760f3..8e378efe30 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -384,4 +384,4 @@ int virFileDataSync(int fd); int virFileSetCOW(const char *path, virTristateBool state); =20 -off_t virFileDiskCopy(const char *path, int fd, int oflags); +off_t virFileDiskCopy(int disk_fd, const char *disk_path, int remote_fd, c= onst char *remote_path); --=20 2.35.3