mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 11:31:16 +00:00
storage: Introduce virStoragePoolObjListForEach
Create an API to walk the pools->objs[] list in order to perform a callback function for each element of the objs array that doesn't care about whether the action succeeds or fails as the desire is to run the code over every element in the array rather than fail as soon as or if one fails.
This commit is contained in:
parent
770aa08e48
commit
d0258dd9d6
@ -230,6 +230,35 @@ virStoragePoolObjListFree(virStoragePoolObjListPtr pools)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* virStoragePoolObjListForEach
|
||||
* @pools: Pointer to pools object
|
||||
* @iter: Callback iteration helper
|
||||
* @opaque: Opaque data to use as argument to helper
|
||||
*
|
||||
* For each object in @pools, call the @iter helper using @opaque as
|
||||
* an argument. This function doesn't care whether the @iter fails or
|
||||
* not as it's being used for Autostart and UpdateAllState callers
|
||||
* that want to iterate over all the @pools objects not stopping if
|
||||
* one happens to fail.
|
||||
*/
|
||||
void
|
||||
virStoragePoolObjListForEach(virStoragePoolObjListPtr pools,
|
||||
virStoragePoolObjListIterator iter,
|
||||
const void *opaque)
|
||||
{
|
||||
size_t i;
|
||||
virStoragePoolObjPtr obj;
|
||||
|
||||
for (i = 0; i < pools->count; i++) {
|
||||
obj = pools->objs[i];
|
||||
virStoragePoolObjLock(obj);
|
||||
iter(obj, opaque);
|
||||
virStoragePoolObjUnlock(obj);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
|
||||
virStoragePoolObjPtr obj)
|
||||
|
@ -226,6 +226,15 @@ virStoragePoolObjFree(virStoragePoolObjPtr obj);
|
||||
void
|
||||
virStoragePoolObjListFree(virStoragePoolObjListPtr pools);
|
||||
|
||||
typedef void
|
||||
(*virStoragePoolObjListIterator)(virStoragePoolObjPtr obj,
|
||||
const void *opaque);
|
||||
|
||||
void
|
||||
virStoragePoolObjListForEach(virStoragePoolObjListPtr pools,
|
||||
virStoragePoolObjListIterator iter,
|
||||
const void *opaque);
|
||||
|
||||
void
|
||||
virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
|
||||
virStoragePoolObjPtr obj);
|
||||
|
@ -1090,6 +1090,7 @@ virStoragePoolObjIsActive;
|
||||
virStoragePoolObjIsAutostart;
|
||||
virStoragePoolObjIsDuplicate;
|
||||
virStoragePoolObjListExport;
|
||||
virStoragePoolObjListForEach;
|
||||
virStoragePoolObjListFree;
|
||||
virStoragePoolObjLoadAllConfigs;
|
||||
virStoragePoolObjLoadAllState;
|
||||
|
@ -102,7 +102,8 @@ virStoragePoolUpdateInactive(virStoragePoolObjPtr *objptr)
|
||||
|
||||
|
||||
static void
|
||||
storagePoolUpdateState(virStoragePoolObjPtr obj)
|
||||
storagePoolUpdateStateCallback(virStoragePoolObjPtr obj,
|
||||
const void *opaque ATTRIBUTE_UNUSED)
|
||||
{
|
||||
virStoragePoolDefPtr def = virStoragePoolObjGetDef(obj);
|
||||
bool active = false;
|
||||
@ -157,44 +158,27 @@ storagePoolUpdateState(virStoragePoolObjPtr obj)
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
storagePoolUpdateAllState(void)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < driver->pools.count; i++) {
|
||||
virStoragePoolObjPtr obj = driver->pools.objs[i];
|
||||
|
||||
virStoragePoolObjLock(obj);
|
||||
storagePoolUpdateState(obj);
|
||||
virStoragePoolObjEndAPI(&obj);
|
||||
}
|
||||
virStoragePoolObjListForEach(&driver->pools,
|
||||
storagePoolUpdateStateCallback,
|
||||
NULL);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
storageDriverAutostart(void)
|
||||
storageDriverAutostartCallback(virStoragePoolObjPtr obj,
|
||||
const void *opaque)
|
||||
{
|
||||
size_t i;
|
||||
virConnectPtr conn = NULL;
|
||||
|
||||
/* XXX Remove hardcoding of QEMU URI */
|
||||
if (driver->privileged)
|
||||
conn = virConnectOpen("qemu:///system");
|
||||
else
|
||||
conn = virConnectOpen("qemu:///session");
|
||||
/* Ignoring NULL conn - let backends decide */
|
||||
|
||||
for (i = 0; i < driver->pools.count; i++) {
|
||||
virStoragePoolObjPtr obj = driver->pools.objs[i];
|
||||
virStoragePoolDefPtr def = virStoragePoolObjGetDef(obj);
|
||||
virConnectPtr conn = (virConnectPtr) opaque;
|
||||
virStorageBackendPtr backend;
|
||||
bool started = false;
|
||||
|
||||
virStoragePoolObjLock(obj);
|
||||
if ((backend = virStorageBackendForType(def->type)) == NULL) {
|
||||
virStoragePoolObjEndAPI(&obj);
|
||||
continue;
|
||||
}
|
||||
if (!(backend = virStorageBackendForType(def->type)))
|
||||
return;
|
||||
|
||||
if (virStoragePoolObjIsAutostart(obj) &&
|
||||
!virStoragePoolObjIsActive(obj)) {
|
||||
@ -203,8 +187,7 @@ storageDriverAutostart(void)
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||
_("Failed to autostart storage pool '%s': %s"),
|
||||
def->name, virGetLastErrorMessage());
|
||||
virStoragePoolObjEndAPI(&obj);
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
started = true;
|
||||
}
|
||||
@ -229,8 +212,24 @@ storageDriverAutostart(void)
|
||||
}
|
||||
VIR_FREE(stateFile);
|
||||
}
|
||||
virStoragePoolObjEndAPI(&obj);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
storageDriverAutostart(void)
|
||||
{
|
||||
virConnectPtr conn = NULL;
|
||||
|
||||
/* XXX Remove hardcoding of QEMU URI */
|
||||
if (driver->privileged)
|
||||
conn = virConnectOpen("qemu:///system");
|
||||
else
|
||||
conn = virConnectOpen("qemu:///session");
|
||||
/* Ignoring NULL conn - let backends decide */
|
||||
|
||||
virStoragePoolObjListForEach(&driver->pools,
|
||||
storageDriverAutostartCallback,
|
||||
conn);
|
||||
|
||||
virObjectUnref(conn);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user