int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie) { char name[PROP_NAME_MAX]; char value[PROP_VALUE_MAX]; const prop_info *pi; unsigned n; for(n = 0; (pi = __system_property_find_nth(n)); n++) { __system_property_read(pi, name, value); propfn(name, value, cookie); } return 0; }
TEST(properties, find_nth) { #if defined(__BIONIC__) LocalPropertyTestState pa; ASSERT_TRUE(pa.valid); ASSERT_EQ(0, __system_property_add("property", 8, "value1", 6)); ASSERT_EQ(0, __system_property_add("other_property", 14, "value2", 6)); ASSERT_EQ(0, __system_property_add("property_other", 14, "value3", 6)); ASSERT_NE((const prop_info *)NULL, __system_property_find_nth(0)); ASSERT_NE((const prop_info *)NULL, __system_property_find_nth(1)); ASSERT_NE((const prop_info *)NULL, __system_property_find_nth(2)); ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(3)); ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(4)); ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(5)); ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(100)); ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(200)); ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(247)); #else // __BIONIC__ GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif // __BIONIC__ }
static struct property_vector* find_all_properties_raw(void) { for (;;) { SCOPED_RESLIST(rl); struct find_all_properties_context ctx = { .pv = property_vector_new(), .oom = false, }; if (property_foreach(find_all_properties_propfn, &ctx) == 1) continue; if (ctx.oom) die_oom(); reslist_xfer(rl->parent, rl); property_vector_sort(ctx.pv); return ctx.pv; } } static void property_vector_swap(struct property_vector* a, struct property_vector* b) { struct property_vector tmp = *a; *a = *b; *b = tmp; } static bool property_vector_equal(const struct property_vector* a, const struct property_vector* b) { return a->size == b->size && memcmp(a->props, b->props, a->size * sizeof (a->props[0])) == 0; } static struct property_vector* find_all_properties(void) { struct property_vector* pv1 = find_all_properties_raw(); for (;;) { SCOPED_RESLIST(pv); struct property_vector* pv2 = find_all_properties_raw(); if (property_vector_equal(pv1, pv2)) return pv1; property_vector_swap(pv1, pv2); } } static int compat_property_foreach( void (*propfn)(const prop_info *pi, void *cookie), void *cookie) { unsigned propno = 0; for (;;) { const prop_info* pi = __system_property_find_nth(propno++); if (pi == NULL) break; propfn(pi, cookie); } return 0; }