From nobody Tue Feb 10 23:00:59 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.74 as permitted sender) client-ip=170.10.133.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.133.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.133.74]) by mx.zohomail.com with SMTPS id 1651842704438901.6454145597936; Fri, 6 May 2022 06:11:44 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-576-SZbSHSY8PHyx5BkAjj43Bg-1; Fri, 06 May 2022 09:11:41 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A195B29DD980; Fri, 6 May 2022 13:11:34 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8DB9F1469392; Fri, 6 May 2022 13:11:34 +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 A2D2D1947BA5; Fri, 6 May 2022 13:11:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id CE36519451EF for ; Fri, 6 May 2022 13:11:31 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id AD75140D2831; Fri, 6 May 2022 13:11:31 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast02.extmail.prod.ext.rdu2.redhat.com [10.11.55.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A937740D2830 for ; Fri, 6 May 2022 13:11:31 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) (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 5BE3D803D45 for ; Fri, 6 May 2022 13:11:31 +0000 (UTC) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-473-XRWBt4B7Pti5WGCLzlmsuQ-1; Fri, 06 May 2022 09:11:29 -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-out2.suse.de (Postfix) with ESMTPS id 470241F91C; Fri, 6 May 2022 13:11:28 +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 0E63713AA2; Fri, 6 May 2022 13:11:28 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 8BvUAYAedWI1TAAAMHmgww (envelope-from ); Fri, 06 May 2022 13:11:28 +0000 X-MC-Unique: SZbSHSY8PHyx5BkAjj43Bg-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: XRWBt4B7Pti5WGCLzlmsuQ-1 From: Claudio Fontana To: =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= Subject: [libvirt RFCv7 05/40] virfile: change virFileDiskCopy arguments to extend beyond stdin, stdout Date: Fri, 6 May 2022 15:10:49 +0200 Message-Id: <20220506131124.14133-6-cfontana@suse.de> In-Reply-To: <20220506131124.14133-1-cfontana@suse.de> References: <20220506131124.14133-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.84 on 10.11.54.2 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.7 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: 1651842705523100001 Content-Type: text/plain; charset="utf-8"; x-default="true" Signed-off-by: Claudio Fontana --- src/util/iohelper.c | 17 +----------- src/util/virfile.c | 64 +++++++++++++++++++++++++++++++-------------- src/util/virfile.h | 2 +- 3 files changed, 46 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 4aa7be4740..8b07db6679 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -4670,19 +4670,46 @@ 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; =20 - if (fstat(fd, &sb) < 0) { + oflags =3D fcntl(disk_fd, F_GETFL); + + 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); @@ -4691,23 +4718,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; } @@ -4716,12 +4741,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; @@ -4743,9 +4768,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