mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-11 15:27:47 +00:00
Use virFileAbsPath instead of manually creating the absolute path
Removes multiple 4kb stack allocations. Removes TODO comments as suggested by Daniel P. Berrange.
This commit is contained in:
parent
1901d091f1
commit
a16de3594f
118
src/libvirt.c
118
src/libvirt.c
@ -2238,7 +2238,6 @@ error:
|
|||||||
int
|
int
|
||||||
virDomainSave(virDomainPtr domain, const char *to)
|
virDomainSave(virDomainPtr domain, const char *to)
|
||||||
{
|
{
|
||||||
char filepath[4096];
|
|
||||||
virConnectPtr conn;
|
virConnectPtr conn;
|
||||||
|
|
||||||
VIR_DOMAIN_DEBUG(domain, "to=%s", to);
|
VIR_DOMAIN_DEBUG(domain, "to=%s", to);
|
||||||
@ -2260,29 +2259,21 @@ virDomainSave(virDomainPtr domain, const char *to)
|
|||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* We must absolutize the file path as the save is done out of process
|
|
||||||
* TODO: check for URI when libxml2 is linked in.
|
|
||||||
*/
|
|
||||||
if (to[0] != '/') {
|
|
||||||
unsigned int len, t;
|
|
||||||
|
|
||||||
t = strlen(to);
|
|
||||||
if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
|
|
||||||
return -1;
|
|
||||||
len = strlen(filepath);
|
|
||||||
/* that should be covered by getcwd() semantic, but be 100% sure */
|
|
||||||
if (len > sizeof(filepath) - (t + 3))
|
|
||||||
return -1;
|
|
||||||
filepath[len] = '/';
|
|
||||||
strcpy(&filepath[len + 1], to);
|
|
||||||
to = &filepath[0];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (conn->driver->domainSave) {
|
if (conn->driver->domainSave) {
|
||||||
int ret;
|
int ret;
|
||||||
ret = conn->driver->domainSave (domain, to);
|
char *absolute_to;
|
||||||
|
|
||||||
|
/* We must absolutize the file path as the save is done out of process */
|
||||||
|
if (virFileAbsPath(to, &absolute_to) < 0) {
|
||||||
|
virLibConnError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
_("could not build absolute output file path"));
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = conn->driver->domainSave(domain, absolute_to);
|
||||||
|
|
||||||
|
VIR_FREE(absolute_to);
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto error;
|
goto error;
|
||||||
return ret;
|
return ret;
|
||||||
@ -2298,7 +2289,7 @@ error:
|
|||||||
/**
|
/**
|
||||||
* virDomainRestore:
|
* virDomainRestore:
|
||||||
* @conn: pointer to the hypervisor connection
|
* @conn: pointer to the hypervisor connection
|
||||||
* @from: path to the
|
* @from: path to the input file
|
||||||
*
|
*
|
||||||
* This method will restore a domain saved to disk by virDomainSave().
|
* This method will restore a domain saved to disk by virDomainSave().
|
||||||
*
|
*
|
||||||
@ -2307,7 +2298,6 @@ error:
|
|||||||
int
|
int
|
||||||
virDomainRestore(virConnectPtr conn, const char *from)
|
virDomainRestore(virConnectPtr conn, const char *from)
|
||||||
{
|
{
|
||||||
char filepath[4096];
|
|
||||||
VIR_DEBUG("conn=%p, from=%s", conn, from);
|
VIR_DEBUG("conn=%p, from=%s", conn, from);
|
||||||
|
|
||||||
virResetLastError();
|
virResetLastError();
|
||||||
@ -2326,34 +2316,21 @@ virDomainRestore(virConnectPtr conn, const char *from)
|
|||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* We must absolutize the file path as the restore is done out of process
|
|
||||||
* TODO: check for URI when libxml2 is linked in.
|
|
||||||
*/
|
|
||||||
if (from[0] != '/') {
|
|
||||||
unsigned int len, t;
|
|
||||||
|
|
||||||
t = strlen(from);
|
|
||||||
if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL) {
|
|
||||||
virLibConnError(VIR_ERR_SYSTEM_ERROR,
|
|
||||||
_("cannot get working directory"));
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
len = strlen(filepath);
|
|
||||||
/* that should be covered by getcwd() semantic, but be 100% sure */
|
|
||||||
if (len > sizeof(filepath) - (t + 3)) {
|
|
||||||
virLibConnError(VIR_ERR_INTERNAL_ERROR,
|
|
||||||
_("path too long"));
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
filepath[len] = '/';
|
|
||||||
strcpy(&filepath[len + 1], from);
|
|
||||||
from = &filepath[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (conn->driver->domainRestore) {
|
if (conn->driver->domainRestore) {
|
||||||
int ret;
|
int ret;
|
||||||
ret = conn->driver->domainRestore (conn, from);
|
char *absolute_from;
|
||||||
|
|
||||||
|
/* We must absolutize the file path as the restore is done out of process */
|
||||||
|
if (virFileAbsPath(from, &absolute_from) < 0) {
|
||||||
|
virLibConnError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
_("could not build absolute input file path"));
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = conn->driver->domainRestore(conn, absolute_from);
|
||||||
|
|
||||||
|
VIR_FREE(absolute_from);
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto error;
|
goto error;
|
||||||
return ret;
|
return ret;
|
||||||
@ -2381,7 +2358,6 @@ error:
|
|||||||
int
|
int
|
||||||
virDomainCoreDump(virDomainPtr domain, const char *to, int flags)
|
virDomainCoreDump(virDomainPtr domain, const char *to, int flags)
|
||||||
{
|
{
|
||||||
char filepath[4096];
|
|
||||||
virConnectPtr conn;
|
virConnectPtr conn;
|
||||||
|
|
||||||
VIR_DOMAIN_DEBUG(domain, "to=%s, flags=%d", to, flags);
|
VIR_DOMAIN_DEBUG(domain, "to=%s, flags=%d", to, flags);
|
||||||
@ -2403,35 +2379,21 @@ virDomainCoreDump(virDomainPtr domain, const char *to, int flags)
|
|||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* We must absolutize the file path as the save is done out of process
|
|
||||||
* TODO: check for URI when libxml2 is linked in.
|
|
||||||
*/
|
|
||||||
if (to[0] != '/') {
|
|
||||||
unsigned int len, t;
|
|
||||||
|
|
||||||
t = strlen(to);
|
|
||||||
if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL) {
|
|
||||||
virLibDomainError(VIR_ERR_SYSTEM_ERROR,
|
|
||||||
_("cannot get current directory"));
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
len = strlen(filepath);
|
|
||||||
/* that should be covered by getcwd() semantic, but be 100% sure */
|
|
||||||
if (len > sizeof(filepath) - (t + 3)) {
|
|
||||||
virLibDomainError(VIR_ERR_INTERNAL_ERROR,
|
|
||||||
_("path too long"));
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
filepath[len] = '/';
|
|
||||||
strcpy(&filepath[len + 1], to);
|
|
||||||
to = &filepath[0];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (conn->driver->domainCoreDump) {
|
if (conn->driver->domainCoreDump) {
|
||||||
int ret;
|
int ret;
|
||||||
ret = conn->driver->domainCoreDump (domain, to, flags);
|
char *absolute_to;
|
||||||
|
|
||||||
|
/* We must absolutize the file path as the save is done out of process */
|
||||||
|
if (virFileAbsPath(to, &absolute_to) < 0) {
|
||||||
|
virLibConnError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
_("could not build absolute core file path"));
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = conn->driver->domainCoreDump(domain, absolute_to, flags);
|
||||||
|
|
||||||
|
VIR_FREE(absolute_to);
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto error;
|
goto error;
|
||||||
return ret;
|
return ret;
|
||||||
|
Loading…
Reference in New Issue
Block a user