Skip to content

Commit 6cc7b64

Browse files
author
khachi
committed
feat(property): freeze properties after realize
1 parent 6df5892 commit 6cc7b64

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

src/qom/property.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include <assert.h>
22
#include <ctype.h>
33
#include <errno.h>
4+
#include <stdbool.h>
45
#include <stdlib.h>
56
#include <string.h>
67

78
#include "miniqom/property.h"
9+
#include "miniqom/error.h"
10+
#include "miniqom/object.h"
811
#include "miniqom/type.h"
912

1013
ObjectClass *object_class_new(Type *type)
@@ -116,9 +119,21 @@ static bool parse_u64(const char *text, uint64_t *value)
116119
bool object_property_set_from_string(Object *obj, const char *name,
117120
const char *text, Error *err)
118121
{
119-
const Property *property = object_property_find(obj, name);
122+
const Property *property;
120123
PropertyValue value = {0};
121124

125+
if (!obj || !name || !text) {
126+
error_set(err, "invalid property set arguments");
127+
return false;
128+
}
129+
130+
if (object_is_realized(obj)) {
131+
error_set(err, "object is already realized");
132+
return false;
133+
}
134+
135+
property = object_property_find(obj, name);
136+
122137
if (!property)
123138
{
124139
error_set(err, "property not found");
@@ -178,4 +193,4 @@ bool object_property_get(Object *obj,
178193
return false;
179194
}
180195
return property->get(obj, value, err);
181-
}
196+
}

tests/test_qom.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <assert.h>
2+
#include <stdio.h>
23
#include <string.h>
34

5+
#include "miniqom/error.h"
46
#include "miniqom/memory.h"
57
#include "miniqom/object.h"
68
#include "miniqom/property.h"
@@ -186,9 +188,28 @@ int main(void)
186188
assert(backend->parent_obj.size == 2ULL * 1024 * 1024 * 1024);
187189
assert(!strcmp(backend->parent_obj.swap_storage, "file:///swap"));
188190

189-
// ====== Test property ======
191+
// ====== Freeze properties ======
192+
//
193+
Object *property_object;
194+
property_object = object_new(TYPE_MEMORY_BACKEND_MEMFD);
195+
assert(property_object!=NULL);
196+
190197
PropertyValue property_value;
198+
error_clear(&err);
199+
200+
assert(object_property_set_from_string(property_object, "size", "2G", &err));
201+
202+
assert(object_realize(property_object, &err));
203+
assert(object_is_realized(property_object));
204+
205+
assert(!object_property_set_from_string(property_object, "size", "4G", &err));
191206

207+
assert(!strcmp(err.message, "object is already realized"));
208+
209+
assert(object_property_get(property_object, "size", &property_value, &err));
210+
211+
assert(property_value.u64 == 2ULL * 1024 *1024 *1024);
212+
// ====== Test property ======
192213
assert(object_property_get(mem0, "size",
193214
&property_value, &err));
194215
assert(property_value.u64 == 2ULL * 1024 * 1024 * 1024);
@@ -252,4 +273,4 @@ int main(void)
252273
// ======= Release test resources ======
253274
object_free(root);
254275
return 0;
255-
}
276+
}

0 commit comments

Comments
 (0)