From nobody Mon Feb 9 14:00:54 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1498225338668147.511295253046; Fri, 23 Jun 2017 06:42:18 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 93C4FB8153; Fri, 23 Jun 2017 13:42:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 647CD6FB82; Fri, 23 Jun 2017 13:42:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 171D6262; Fri, 23 Jun 2017 13:42:15 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v5NDXKae025155 for ; Fri, 23 Jun 2017 09:33:20 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4A35E6C40E; Fri, 23 Jun 2017 13:33:20 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 92A356C41C; Fri, 23 Jun 2017 13:33:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 93C4FB8153 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 93C4FB8153 From: Peter Krempa To: libvir-list@redhat.com Date: Fri, 23 Jun 2017 15:33:16 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 4/6] storage: Make virStorageFileReadHeader more universal X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 23 Jun 2017 13:42:16 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Allow specifying offset to read an arbitrary position in the file. This warrants a rename to virStorageFileRead. Reviewed-by: John Ferlan --- src/qemu/qemu_driver.c | 3 +-- src/storage/storage_backend.h | 9 +++++---- src/storage/storage_backend_fs.c | 20 +++++++++++++------ src/storage/storage_backend_gluster.c | 37 ++++++++++++++++++++++---------= ---- src/storage/storage_source.c | 30 +++++++++++++++------------- src/storage/storage_source.h | 7 ++++--- 6 files changed, 63 insertions(+), 43 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 052c7f0fc..4c4a6a036 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -11584,8 +11584,7 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver, goto cleanup; } } else { - if ((len =3D virStorageFileReadHeader(src, VIR_STORAGE_MAX_HEADER, - &buf)) < 0) + if ((len =3D virStorageFileRead(src, 0, VIR_STORAGE_MAX_HEADER, &b= uf)) < 0) goto cleanup; } diff --git a/src/storage/storage_backend.h b/src/storage/storage_backend.h index 21ac84552..193cf134d 100644 --- a/src/storage/storage_backend.h +++ b/src/storage/storage_backend.h @@ -154,9 +154,10 @@ typedef int struct stat *st); typedef ssize_t -(*virStorageFileBackendReadHeader)(virStorageSourcePtr src, - ssize_t max_len, - char **buf); +(*virStorageFileBackendRead)(virStorageSourcePtr src, + size_t offset, + size_t len, + char **buf); typedef const char * (*virStorageFileBackendGetUniqueIdentifier)(virStorageSourcePtr src); @@ -186,7 +187,7 @@ struct _virStorageFileBackend { * error on failure. */ virStorageFileBackendInit backendInit; virStorageFileBackendDeinit backendDeinit; - virStorageFileBackendReadHeader storageFileReadHeader; + virStorageFileBackendRead storageFileRead; virStorageFileBackendGetUniqueIdentifier storageFileGetUniqueIdentifie= r; /* The following group of callbacks is expected to set errno diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend= _fs.c index bf1d7de43..847dbc9e0 100644 --- a/src/storage/storage_backend_fs.c +++ b/src/storage/storage_backend_fs.c @@ -779,9 +779,10 @@ virStorageFileBackendFileStat(virStorageSourcePtr src, static ssize_t -virStorageFileBackendFileReadHeader(virStorageSourcePtr src, - ssize_t max_len, - char **buf) +virStorageFileBackendFileRead(virStorageSourcePtr src, + size_t offset, + size_t len, + char **buf) { int fd =3D -1; ssize_t ret =3D -1; @@ -793,7 +794,14 @@ virStorageFileBackendFileReadHeader(virStorageSourcePt= r src, return -1; } - if ((ret =3D virFileReadHeaderFD(fd, max_len, buf)) < 0) { + if (offset > 0) { + if (lseek(fd, offset, SEEK_SET) =3D=3D (off_t) -1) { + virReportSystemError(errno, _("cannot seek into '%s'"), src->p= ath); + goto cleanup; + } + } + + if ((ret =3D virFileReadHeaderFD(fd, len, buf)) < 0) { virReportSystemError(errno, _("cannot read header '%s'"), src->path); goto cleanup; @@ -850,7 +858,7 @@ virStorageFileBackend virStorageFileBackendFile =3D { .storageFileCreate =3D virStorageFileBackendFileCreate, .storageFileUnlink =3D virStorageFileBackendFileUnlink, .storageFileStat =3D virStorageFileBackendFileStat, - .storageFileReadHeader =3D virStorageFileBackendFileReadHeader, + .storageFileRead =3D virStorageFileBackendFileRead, .storageFileAccess =3D virStorageFileBackendFileAccess, .storageFileChown =3D virStorageFileBackendFileChown, @@ -865,7 +873,7 @@ virStorageFileBackend virStorageFileBackendBlock =3D { .backendDeinit =3D virStorageFileBackendFileDeinit, .storageFileStat =3D virStorageFileBackendFileStat, - .storageFileReadHeader =3D virStorageFileBackendFileReadHeader, + .storageFileRead =3D virStorageFileBackendFileRead, .storageFileAccess =3D virStorageFileBackendFileAccess, .storageFileChown =3D virStorageFileBackendFileChown, diff --git a/src/storage/storage_backend_gluster.c b/src/storage/storage_ba= ckend_gluster.c index 93dce4042..8ea7e603c 100644 --- a/src/storage/storage_backend_gluster.c +++ b/src/storage/storage_backend_gluster.c @@ -151,20 +151,20 @@ virStorageBackendGlusterOpen(virStoragePoolObjPtr poo= l) static ssize_t -virStorageBackendGlusterReadHeader(glfs_fd_t *fd, - const char *name, - ssize_t maxlen, - char **buf) +virStorageBackendGlusterRead(glfs_fd_t *fd, + const char *name, + size_t len, + char **buf) { char *s; size_t nread =3D 0; - if (VIR_ALLOC_N(*buf, maxlen) < 0) + if (VIR_ALLOC_N(*buf, len) < 0) return -1; s =3D *buf; - while (maxlen) { - ssize_t r =3D glfs_read(fd, s, maxlen, 0); + while (len) { + ssize_t r =3D glfs_read(fd, s, len, 0); if (r < 0 && errno =3D=3D EINTR) continue; if (r < 0) { @@ -175,7 +175,7 @@ virStorageBackendGlusterReadHeader(glfs_fd_t *fd, if (r =3D=3D 0) return nread; s +=3D r; - maxlen -=3D r; + len -=3D r; nread +=3D r; } return nread; @@ -292,7 +292,7 @@ virStorageBackendGlusterRefreshVol(virStorageBackendGlu= sterStatePtr state, goto cleanup; } - if ((len =3D virStorageBackendGlusterReadHeader(fd, name, len, &header= )) < 0) + if ((len =3D virStorageBackendGlusterRead(fd, name, len, &header)) < 0) goto cleanup; if (!(meta =3D virStorageFileGetMetadataFromBuf(name, header, len, @@ -721,9 +721,10 @@ virStorageFileBackendGlusterStat(virStorageSourcePtr s= rc, static ssize_t -virStorageFileBackendGlusterReadHeader(virStorageSourcePtr src, - ssize_t max_len, - char **buf) +virStorageFileBackendGlusterRead(virStorageSourcePtr src, + size_t offset, + size_t len, + char **buf) { virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; glfs_fd_t *fd =3D NULL; @@ -737,8 +738,16 @@ virStorageFileBackendGlusterReadHeader(virStorageSourc= ePtr src, return -1; } - ret =3D virStorageBackendGlusterReadHeader(fd, src->path, max_len, buf= ); + if (offset > 0) { + if (glfs_lseek(fd, offset, SEEK_SET) =3D=3D (off_t) -1) { + virReportSystemError(errno, _("cannot seek into '%s'"), src->p= ath); + goto cleanup; + } + } + + ret =3D virStorageBackendGlusterRead(fd, src->path, len, buf); + cleanup: if (fd) glfs_close(fd); @@ -851,7 +860,7 @@ virStorageFileBackend virStorageFileBackendGluster =3D { .storageFileCreate =3D virStorageFileBackendGlusterCreate, .storageFileUnlink =3D virStorageFileBackendGlusterUnlink, .storageFileStat =3D virStorageFileBackendGlusterStat, - .storageFileReadHeader =3D virStorageFileBackendGlusterReadHeader, + .storageFileRead =3D virStorageFileBackendGlusterRead, .storageFileAccess =3D virStorageFileBackendGlusterAccess, .storageFileChown =3D virStorageFileBackendGlusterChown, diff --git a/src/storage/storage_source.c b/src/storage/storage_source.c index c91d629c8..130c2d2f3 100644 --- a/src/storage/storage_source.c +++ b/src/storage/storage_source.c @@ -64,7 +64,7 @@ virStorageFileSupportsBackingChainTraversal(virStorageSou= rcePtr src) } return backend->storageFileGetUniqueIdentifier && - backend->storageFileReadHeader && + backend->storageFileRead && backend->storageFileAccess; } @@ -263,10 +263,11 @@ virStorageFileStat(virStorageSourcePtr src, /** - * virStorageFileReadHeader: read the beginning bytes of a file into a buf= fer + * virStorageFileRead: read the beginning bytes of a file into a buffer * * @src: file structure pointing to the file - * @max_len: maximum number of bytes read from the storage file + * @offset: number of bytes to skip in the storage file + * @len: maximum number of bytes read from the storage file * @buf: buffer to read the data into. buffer shall be freed by caller) * * Returns the count of bytes read on success and -1 on failure, -2 if the @@ -274,9 +275,10 @@ virStorageFileStat(virStorageSourcePtr src, * Libvirt error is reported on failure. */ ssize_t -virStorageFileReadHeader(virStorageSourcePtr src, - ssize_t max_len, - char **buf) +virStorageFileRead(virStorageSourcePtr src, + size_t offset, + size_t len, + char **buf) { ssize_t ret; @@ -286,18 +288,18 @@ virStorageFileReadHeader(virStorageSourcePtr src, return -1; } - if (!src->drv->backend->storageFileReadHeader) { + if (!src->drv->backend->storageFileRead) { virReportError(VIR_ERR_INTERNAL_ERROR, - _("storage file header reading is not supported for= " + _("storage file reading is not supported for " "storage type %s (protocol: %s)"), virStorageTypeToString(src->type), virStorageNetProtocolTypeToString(src->protocol)); return -2; } - ret =3D src->drv->backend->storageFileReadHeader(src, max_len, buf); + ret =3D src->drv->backend->storageFileRead(src, offset, len, buf); - VIR_DEBUG("read of storage header %p: ret=3D%zd", src, ret); + VIR_DEBUG("read of storage %p: ret=3D%zd", src, ret); return ret; } @@ -444,8 +446,8 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr sr= c, if (virHashAddEntry(cycle, uniqueName, (void *)1) < 0) goto cleanup; - if ((headerLen =3D virStorageFileReadHeader(src, VIR_STORAGE_MAX_HEADE= R, - &buf)) < 0) + if ((headerLen =3D virStorageFileRead(src, 0, VIR_STORAGE_MAX_HEADER, + &buf)) < 0) goto cleanup; if (virStorageFileGetMetadataInternal(src, buf, headerLen, @@ -565,8 +567,8 @@ virStorageFileGetBackingStoreStr(virStorageSourcePtr sr= c) if (virStorageFileAccess(src, F_OK) < 0) return NULL; - if ((headerLen =3D virStorageFileReadHeader(src, VIR_STORAGE_MAX_HEADE= R, - &buf)) < 0) + if ((headerLen =3D virStorageFileRead(src, 0, VIR_STORAGE_MAX_HEADER, + &buf)) < 0) return NULL; if (!(tmp =3D virStorageSourceCopy(src, false))) diff --git a/src/storage/storage_source.h b/src/storage/storage_source.h index 6b3362244..6462baf6a 100644 --- a/src/storage/storage_source.h +++ b/src/storage/storage_source.h @@ -32,9 +32,10 @@ int virStorageFileCreate(virStorageSourcePtr src); int virStorageFileUnlink(virStorageSourcePtr src); int virStorageFileStat(virStorageSourcePtr src, struct stat *stat); -ssize_t virStorageFileReadHeader(virStorageSourcePtr src, - ssize_t max_len, - char **buf); +ssize_t virStorageFileRead(virStorageSourcePtr src, + size_t offset, + size_t len, + char **buf); const char *virStorageFileGetUniqueIdentifier(virStorageSourcePtr src); int virStorageFileAccess(virStorageSourcePtr src, int mode); int virStorageFileChown(const virStorageSource *src, uid_t uid, gid_t gid); --=20 2.12.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list