提交 732af77c 编写于 作者: J John Ferlan

qemu: Add helpers to handle stat data for qemuStorageLimitsRefresh

Split out the opening of the file and fetch of the stat buffer into a
helper qemuDomainStorageOpenStat. This will handle either opening the
local or remote storage.

Additionally split out the cleanup of that into a separate helper
qemuDomainStorageCloseStat which will either close the file or
call the virStorageFileDeinit function.
Signed-off-by: NJohn Ferlan <jferlan@redhat.com>
上级 7149d169
......@@ -11542,6 +11542,74 @@ qemuDomainMemoryPeek(virDomainPtr dom,
}
/**
* @driver: qemu driver data
* @cfg: driver configuration data
* @vm: domain object
* @src: storage source data
* @ret_fd: pointer to return open'd file descriptor
* @ret_sb: pointer to return stat buffer (local or remote)
*
* For local storage, open the file using qemuOpenFile and then use
* fstat() to grab the stat struct data for the caller.
*
* For remote storage, attempt to access the file and grab the stat
* struct data if the remote connection supports it.
*
* Returns 0 on success with @ret_fd and @ret_sb populated, -1 on failure
*/
static int
qemuDomainStorageOpenStat(virQEMUDriverPtr driver,
virQEMUDriverConfigPtr cfg,
virDomainObjPtr vm,
virStorageSourcePtr src,
int *ret_fd,
struct stat *ret_sb)
{
if (virStorageSourceIsLocalStorage(src)) {
if ((*ret_fd = qemuOpenFile(driver, vm, src->path, O_RDONLY,
NULL, NULL)) == -1)
return -1;
if (fstat(*ret_fd, ret_sb) < 0) {
virReportSystemError(errno, _("cannot stat file '%s'"), src->path);
VIR_FORCE_CLOSE(*ret_fd);
return -1;
}
} else {
if (virStorageFileInitAs(src, cfg->user, cfg->group) < 0)
return -1;
if (virStorageFileStat(src, ret_sb) < 0) {
virStorageFileDeinit(src);
virReportSystemError(errno, _("failed to stat remote file '%s'"),
NULLSTR(src->path));
return -1;
}
}
return 0;
}
/**
* @src: storage source data
* @fd: file descriptor to close for local
*
* If local, then just close the file descriptor.
* else remote, then tear down the storage driver backend connection.
*/
static void
qemuDomainStorageCloseStat(virStorageSourcePtr src,
int *fd)
{
if (virStorageSourceIsLocalStorage(src))
VIR_FORCE_CLOSE(*fd);
else
virStorageFileDeinit(src);
}
/**
* @driver: qemu driver data
* @cfg: driver configuration data
......@@ -11585,35 +11653,19 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
char *buf = NULL;
ssize_t len;
if (virStorageSourceIsLocalStorage(src)) {
if ((fd = qemuOpenFile(driver, vm, src->path, O_RDONLY,
NULL, NULL)) == -1)
goto cleanup;
if (fstat(fd, &sb) < 0) {
virReportSystemError(errno,
_("cannot stat file '%s'"), src->path);
goto cleanup;
}
if (qemuDomainStorageOpenStat(driver, cfg, vm, src, &fd, &sb) < 0)
goto cleanup;
if (virStorageSourceIsLocalStorage(src)) {
if ((len = virFileReadHeaderFD(fd, VIR_STORAGE_MAX_HEADER, &buf)) < 0) {
virReportSystemError(errno, _("cannot read header '%s'"),
src->path);
goto cleanup;
}
} else {
if (virStorageFileInitAs(src, cfg->user, cfg->group) < 0)
goto cleanup;
if ((len = virStorageFileReadHeader(src, VIR_STORAGE_MAX_HEADER,
&buf)) < 0)
goto cleanup;
if (virStorageFileStat(src, &sb) < 0) {
virReportSystemError(errno, _("failed to stat remote file '%s'"),
NULLSTR(src->path));
goto cleanup;
}
}
/* Get info for normal formats */
......@@ -11681,8 +11733,7 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
cleanup:
VIR_FREE(buf);
virStorageSourceFree(meta);
VIR_FORCE_CLOSE(fd);
virStorageFileDeinit(src);
qemuDomainStorageCloseStat(src, &fd);
return ret;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册