xref: /openbmc/qemu/qobject/block-qdict.c (revision 429d3ae2)
1 /*
2  * Special QDict functions used by the block layer
3  *
4  * Copyright (c) 2013-2018 Red Hat, Inc.
5  *
6  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
7  * See the COPYING.LIB file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "block/qdict.h"
12 #include "qapi/qmp/qbool.h"
13 #include "qapi/qmp/qlist.h"
14 #include "qapi/qmp/qnum.h"
15 #include "qapi/qmp/qstring.h"
16 #include "qapi/qobject-input-visitor.h"
17 #include "qemu/cutils.h"
18 #include "qapi/error.h"
19 
20 /**
21  * qdict_copy_default(): If no entry mapped by 'key' exists in 'dst' yet, the
22  * value of 'key' in 'src' is copied there (and the refcount increased
23  * accordingly).
24  */
25 void qdict_copy_default(QDict *dst, QDict *src, const char *key)
26 {
27     QObject *val;
28 
29     if (qdict_haskey(dst, key)) {
30         return;
31     }
32 
33     val = qdict_get(src, key);
34     if (val) {
35         qdict_put_obj(dst, key, qobject_ref(val));
36     }
37 }
38 
39 /**
40  * qdict_set_default_str(): If no entry mapped by 'key' exists in 'dst' yet, a
41  * new QString initialised by 'val' is put there.
42  */
43 void qdict_set_default_str(QDict *dst, const char *key, const char *val)
44 {
45     if (qdict_haskey(dst, key)) {
46         return;
47     }
48 
49     qdict_put_str(dst, key, val);
50 }
51 
52 static void qdict_flatten_qdict(QDict *qdict, QDict *target,
53                                 const char *prefix);
54 
55 static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix)
56 {
57     QObject *value;
58     const QListEntry *entry;
59     QDict *dict_val;
60     QList *list_val;
61     char *new_key;
62     int i;
63 
64     /* This function is never called with prefix == NULL, i.e., it is always
65      * called from within qdict_flatten_q(list|dict)(). Therefore, it does not
66      * need to remove list entries during the iteration (the whole list will be
67      * deleted eventually anyway from qdict_flatten_qdict()). */
68     assert(prefix);
69 
70     entry = qlist_first(qlist);
71 
72     for (i = 0; entry; entry = qlist_next(entry), i++) {
73         value = qlist_entry_obj(entry);
74         dict_val = qobject_to(QDict, value);
75         list_val = qobject_to(QList, value);
76         new_key = g_strdup_printf("%s.%i", prefix, i);
77 
78         /*
79          * Flatten non-empty QDict and QList recursively into @target,
80          * copy other objects to @target
81          */
82         if (dict_val && qdict_size(dict_val)) {
83             qdict_flatten_qdict(dict_val, target, new_key);
84         } else if (list_val && !qlist_empty(list_val)) {
85             qdict_flatten_qlist(list_val, target, new_key);
86         } else {
87             qdict_put_obj(target, new_key, qobject_ref(value));
88         }
89 
90         g_free(new_key);
91     }
92 }
93 
94 static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)
95 {
96     QObject *value;
97     const QDictEntry *entry, *next;
98     QDict *dict_val;
99     QList *list_val;
100     char *key, *new_key;
101 
102     entry = qdict_first(qdict);
103 
104     while (entry != NULL) {
105         next = qdict_next(qdict, entry);
106         value = qdict_entry_value(entry);
107         dict_val = qobject_to(QDict, value);
108         list_val = qobject_to(QList, value);
109 
110         if (prefix) {
111             key = new_key = g_strdup_printf("%s.%s", prefix, entry->key);
112         } else {
113             key = entry->key;
114             new_key = NULL;
115         }
116 
117         /*
118          * Flatten non-empty QDict and QList recursively into @target,
119          * copy other objects to @target.
120          * On the root level (if @qdict == @target), remove flattened
121          * nested QDicts and QLists from @qdict.
122          *
123          * (Note that we do not need to remove entries from nested
124          * dicts or lists.  Their reference count is decremented on
125          * the root level, so there are no leaks.  In fact, if they
126          * have a reference count greater than one, we are probably
127          * well advised not to modify them altogether.)
128          */
129         if (dict_val && qdict_size(dict_val)) {
130             qdict_flatten_qdict(dict_val, target, key);
131             if (target == qdict) {
132                 qdict_del(qdict, entry->key);
133             }
134         } else if (list_val && !qlist_empty(list_val)) {
135             qdict_flatten_qlist(list_val, target, key);
136             if (target == qdict) {
137                 qdict_del(qdict, entry->key);
138             }
139         } else if (target != qdict) {
140             qdict_put_obj(target, key, qobject_ref(value));
141         }
142 
143         g_free(new_key);
144         entry = next;
145     }
146 }
147 
148 /**
149  * qdict_flatten(): For each nested non-empty QDict with key x, all
150  * fields with key y are moved to this QDict and their key is renamed
151  * to "x.y". For each nested non-empty QList with key x, the field at
152  * index y is moved to this QDict with the key "x.y" (i.e., the
153  * reverse of what qdict_array_split() does).
154  * This operation is applied recursively for nested QDicts and QLists.
155  */
156 void qdict_flatten(QDict *qdict)
157 {
158     qdict_flatten_qdict(qdict, qdict, NULL);
159 }
160 
161 /* extract all the src QDict entries starting by start into dst */
162 void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start)
163 
164 {
165     const QDictEntry *entry, *next;
166     const char *p;
167 
168     *dst = qdict_new();
169     entry = qdict_first(src);
170 
171     while (entry != NULL) {
172         next = qdict_next(src, entry);
173         if (strstart(entry->key, start, &p)) {
174             qdict_put_obj(*dst, p, qobject_ref(entry->value));
175             qdict_del(src, entry->key);
176         }
177         entry = next;
178     }
179 }
180 
181 static int qdict_count_prefixed_entries(const QDict *src, const char *start)
182 {
183     const QDictEntry *entry;
184     int count = 0;
185 
186     for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
187         if (strstart(entry->key, start, NULL)) {
188             if (count == INT_MAX) {
189                 return -ERANGE;
190             }
191             count++;
192         }
193     }
194 
195     return count;
196 }
197 
198 /**
199  * qdict_array_split(): This function moves array-like elements of a QDict into
200  * a new QList. Every entry in the original QDict with a key "%u" or one
201  * prefixed "%u.", where %u designates an unsigned integer starting at 0 and
202  * incrementally counting up, will be moved to a new QDict at index %u in the
203  * output QList with the key prefix removed, if that prefix is "%u.". If the
204  * whole key is just "%u", the whole QObject will be moved unchanged without
205  * creating a new QDict. The function terminates when there is no entry in the
206  * QDict with a prefix directly (incrementally) following the last one; it also
207  * returns if there are both entries with "%u" and "%u." for the same index %u.
208  * Example: {"0.a": 42, "0.b": 23, "1.x": 0, "4.y": 1, "o.o": 7, "2": 66}
209  *      (or {"1.x": 0, "4.y": 1, "0.a": 42, "o.o": 7, "0.b": 23, "2": 66})
210  *       => [{"a": 42, "b": 23}, {"x": 0}, 66]
211  *      and {"4.y": 1, "o.o": 7} (remainder of the old QDict)
212  */
213 void qdict_array_split(QDict *src, QList **dst)
214 {
215     unsigned i;
216 
217     *dst = qlist_new();
218 
219     for (i = 0; i < UINT_MAX; i++) {
220         QObject *subqobj;
221         bool is_subqdict;
222         QDict *subqdict;
223         char indexstr[32], prefix[32];
224         size_t snprintf_ret;
225 
226         snprintf_ret = snprintf(indexstr, 32, "%u", i);
227         assert(snprintf_ret < 32);
228 
229         subqobj = qdict_get(src, indexstr);
230 
231         snprintf_ret = snprintf(prefix, 32, "%u.", i);
232         assert(snprintf_ret < 32);
233 
234         /* Overflow is the same as positive non-zero results */
235         is_subqdict = qdict_count_prefixed_entries(src, prefix);
236 
237         /*
238          * There may be either a single subordinate object (named
239          * "%u") or multiple objects (each with a key prefixed "%u."),
240          * but not both.
241          */
242         if (!subqobj == !is_subqdict) {
243             break;
244         }
245 
246         if (is_subqdict) {
247             qdict_extract_subqdict(src, &subqdict, prefix);
248             assert(qdict_size(subqdict) > 0);
249         } else {
250             qobject_ref(subqobj);
251             qdict_del(src, indexstr);
252         }
253 
254         qlist_append_obj(*dst, subqobj ?: QOBJECT(subqdict));
255     }
256 }
257 
258 /**
259  * qdict_split_flat_key:
260  * @key: the key string to split
261  * @prefix: non-NULL pointer to hold extracted prefix
262  * @suffix: non-NULL pointer to remaining suffix
263  *
264  * Given a flattened key such as 'foo.0.bar', split it into two parts
265  * at the first '.' separator. Allows double dot ('..') to escape the
266  * normal separator.
267  *
268  * e.g.
269  *    'foo.0.bar' -> prefix='foo' and suffix='0.bar'
270  *    'foo..0.bar' -> prefix='foo.0' and suffix='bar'
271  *
272  * The '..' sequence will be unescaped in the returned 'prefix'
273  * string. The 'suffix' string will be left in escaped format, so it
274  * can be fed back into the qdict_split_flat_key() key as the input
275  * later.
276  *
277  * The caller is responsible for freeing the string returned in @prefix
278  * using g_free().
279  */
280 static void qdict_split_flat_key(const char *key, char **prefix,
281                                  const char **suffix)
282 {
283     const char *separator;
284     size_t i, j;
285 
286     /* Find first '.' separator, but if there is a pair '..'
287      * that acts as an escape, so skip over '..' */
288     separator = NULL;
289     do {
290         if (separator) {
291             separator += 2;
292         } else {
293             separator = key;
294         }
295         separator = strchr(separator, '.');
296     } while (separator && separator[1] == '.');
297 
298     if (separator) {
299         *prefix = g_strndup(key, separator - key);
300         *suffix = separator + 1;
301     } else {
302         *prefix = g_strdup(key);
303         *suffix = NULL;
304     }
305 
306     /* Unescape the '..' sequence into '.' */
307     for (i = 0, j = 0; (*prefix)[i] != '\0'; i++, j++) {
308         if ((*prefix)[i] == '.') {
309             assert((*prefix)[i + 1] == '.');
310             i++;
311         }
312         (*prefix)[j] = (*prefix)[i];
313     }
314     (*prefix)[j] = '\0';
315 }
316 
317 /**
318  * qdict_is_list:
319  * @maybe_list: dict to check if keys represent list elements.
320  *
321  * Determine whether all keys in @maybe_list are valid list elements.
322  * If @maybe_list is non-zero in length and all the keys look like
323  * valid list indexes, this will return 1. If @maybe_list is zero
324  * length or all keys are non-numeric then it will return 0 to indicate
325  * it is a normal qdict. If there is a mix of numeric and non-numeric
326  * keys, or the list indexes are non-contiguous, an error is reported.
327  *
328  * Returns: 1 if a valid list, 0 if a dict, -1 on error
329  */
330 static int qdict_is_list(QDict *maybe_list, Error **errp)
331 {
332     const QDictEntry *ent;
333     ssize_t len = 0;
334     ssize_t max = -1;
335     int is_list = -1;
336     int64_t val;
337 
338     for (ent = qdict_first(maybe_list); ent != NULL;
339          ent = qdict_next(maybe_list, ent)) {
340         int is_index = !qemu_strtoi64(ent->key, NULL, 10, &val);
341 
342         if (is_list == -1) {
343             is_list = is_index;
344         }
345 
346         if (is_index != is_list) {
347             error_setg(errp, "Cannot mix list and non-list keys");
348             return -1;
349         }
350 
351         if (is_index) {
352             len++;
353             if (val > max) {
354                 max = val;
355             }
356         }
357     }
358 
359     if (is_list == -1) {
360         assert(!qdict_size(maybe_list));
361         is_list = 0;
362     }
363 
364     /* NB this isn't a perfect check - e.g. it won't catch
365      * a list containing '1', '+1', '01', '3', but that
366      * does not matter - we've still proved that the
367      * input is a list. It is up the caller to do a
368      * stricter check if desired */
369     if (len != (max + 1)) {
370         error_setg(errp, "List indices are not contiguous, "
371                    "saw %zd elements but %zd largest index",
372                    len, max);
373         return -1;
374     }
375 
376     return is_list;
377 }
378 
379 /**
380  * qdict_crumple:
381  * @src: the original flat dictionary (only scalar values) to crumple
382  *
383  * Takes a flat dictionary whose keys use '.' separator to indicate
384  * nesting, and values are scalars, empty dictionaries or empty lists,
385  * and crumples it into a nested structure.
386  *
387  * To include a literal '.' in a key name, it must be escaped as '..'
388  *
389  * For example, an input of:
390  *
391  * { 'foo.0.bar': 'one', 'foo.0.wizz': '1',
392  *   'foo.1.bar': 'two', 'foo.1.wizz': '2' }
393  *
394  * will result in an output of:
395  *
396  * {
397  *   'foo': [
398  *      { 'bar': 'one', 'wizz': '1' },
399  *      { 'bar': 'two', 'wizz': '2' }
400  *   ],
401  * }
402  *
403  * The following scenarios in the input dict will result in an
404  * error being returned:
405  *
406  *  - Any values in @src are non-scalar types
407  *  - If keys in @src imply that a particular level is both a
408  *    list and a dict. e.g., "foo.0.bar" and "foo.eek.bar".
409  *  - If keys in @src imply that a particular level is a list,
410  *    but the indices are non-contiguous. e.g. "foo.0.bar" and
411  *    "foo.2.bar" without any "foo.1.bar" present.
412  *  - If keys in @src represent list indexes, but are not in
413  *    the "%zu" format. e.g. "foo.+0.bar"
414  *
415  * Returns: either a QDict or QList for the nested data structure, or NULL
416  * on error
417  */
418 QObject *qdict_crumple(const QDict *src, Error **errp)
419 {
420     const QDictEntry *ent;
421     QDict *two_level, *multi_level = NULL, *child_dict;
422     QDict *dict_val;
423     QList *list_val;
424     QObject *dst = NULL, *child;
425     size_t i;
426     char *prefix = NULL;
427     const char *suffix = NULL;
428     int is_list;
429 
430     two_level = qdict_new();
431 
432     /* Step 1: split our totally flat dict into a two level dict */
433     for (ent = qdict_first(src); ent != NULL; ent = qdict_next(src, ent)) {
434         dict_val = qobject_to(QDict, ent->value);
435         list_val = qobject_to(QList, ent->value);
436         if ((dict_val && qdict_size(dict_val))
437             || (list_val && !qlist_empty(list_val))) {
438             error_setg(errp, "Value %s is not flat", ent->key);
439             goto error;
440         }
441 
442         qdict_split_flat_key(ent->key, &prefix, &suffix);
443         child = qdict_get(two_level, prefix);
444         child_dict = qobject_to(QDict, child);
445 
446         if (child) {
447             /*
448              * If @child_dict, then all previous keys with this prefix
449              * had a suffix.  If @suffix, this one has one as well,
450              * and we're good, else there's a clash.
451              */
452             if (!child_dict || !suffix) {
453                 error_setg(errp, "Cannot mix scalar and non-scalar keys");
454                 goto error;
455             }
456         }
457 
458         if (suffix) {
459             if (!child_dict) {
460                 child_dict = qdict_new();
461                 qdict_put(two_level, prefix, child_dict);
462             }
463             qdict_put_obj(child_dict, suffix, qobject_ref(ent->value));
464         } else {
465             qdict_put_obj(two_level, prefix, qobject_ref(ent->value));
466         }
467 
468         g_free(prefix);
469         prefix = NULL;
470     }
471 
472     /* Step 2: optionally process the two level dict recursively
473      * into a multi-level dict */
474     multi_level = qdict_new();
475     for (ent = qdict_first(two_level); ent != NULL;
476          ent = qdict_next(two_level, ent)) {
477         dict_val = qobject_to(QDict, ent->value);
478         if (dict_val && qdict_size(dict_val)) {
479             child = qdict_crumple(dict_val, errp);
480             if (!child) {
481                 goto error;
482             }
483 
484             qdict_put_obj(multi_level, ent->key, child);
485         } else {
486             qdict_put_obj(multi_level, ent->key, qobject_ref(ent->value));
487         }
488     }
489     qobject_unref(two_level);
490     two_level = NULL;
491 
492     /* Step 3: detect if we need to turn our dict into list */
493     is_list = qdict_is_list(multi_level, errp);
494     if (is_list < 0) {
495         goto error;
496     }
497 
498     if (is_list) {
499         dst = QOBJECT(qlist_new());
500 
501         for (i = 0; i < qdict_size(multi_level); i++) {
502             char *key = g_strdup_printf("%zu", i);
503 
504             child = qdict_get(multi_level, key);
505             g_free(key);
506 
507             if (!child) {
508                 error_setg(errp, "Missing list index %zu", i);
509                 goto error;
510             }
511 
512             qlist_append_obj(qobject_to(QList, dst), qobject_ref(child));
513         }
514         qobject_unref(multi_level);
515         multi_level = NULL;
516     } else {
517         dst = QOBJECT(multi_level);
518     }
519 
520     return dst;
521 
522  error:
523     g_free(prefix);
524     qobject_unref(multi_level);
525     qobject_unref(two_level);
526     qobject_unref(dst);
527     return NULL;
528 }
529 
530 /**
531  * qdict_crumple_for_keyval_qiv:
532  * @src: the flat dictionary (only scalar values) to crumple
533  * @errp: location to store error
534  *
535  * Like qdict_crumple(), but additionally transforms scalar values so
536  * the result can be passed to qobject_input_visitor_new_keyval().
537  *
538  * The block subsystem uses this function to prepare its flat QDict
539  * with possibly confused scalar types for a visit.  It should not be
540  * used for anything else, and it should go away once the block
541  * subsystem has been cleaned up.
542  */
543 static QObject *qdict_crumple_for_keyval_qiv(QDict *src, Error **errp)
544 {
545     QDict *tmp = NULL;
546     char *buf;
547     const char *s;
548     const QDictEntry *ent;
549     QObject *dst;
550 
551     for (ent = qdict_first(src); ent; ent = qdict_next(src, ent)) {
552         buf = NULL;
553         switch (qobject_type(ent->value)) {
554         case QTYPE_QNULL:
555         case QTYPE_QSTRING:
556             continue;
557         case QTYPE_QNUM:
558             s = buf = qnum_to_string(qobject_to(QNum, ent->value));
559             break;
560         case QTYPE_QDICT:
561         case QTYPE_QLIST:
562             /* @src isn't flat; qdict_crumple() will fail */
563             continue;
564         case QTYPE_QBOOL:
565             s = qbool_get_bool(qobject_to(QBool, ent->value))
566                 ? "on" : "off";
567             break;
568         default:
569             abort();
570         }
571 
572         if (!tmp) {
573             tmp = qdict_clone_shallow(src);
574         }
575         qdict_put(tmp, ent->key, qstring_from_str(s));
576         g_free(buf);
577     }
578 
579     dst = qdict_crumple(tmp ?: src, errp);
580     qobject_unref(tmp);
581     return dst;
582 }
583 
584 /**
585  * qdict_array_entries(): Returns the number of direct array entries if the
586  * sub-QDict of src specified by the prefix in subqdict (or src itself for
587  * prefix == "") is valid as an array, i.e. the length of the created list if
588  * the sub-QDict would become empty after calling qdict_array_split() on it. If
589  * the array is not valid, -EINVAL is returned.
590  */
591 int qdict_array_entries(QDict *src, const char *subqdict)
592 {
593     const QDictEntry *entry;
594     unsigned i;
595     unsigned entries = 0;
596     size_t subqdict_len = strlen(subqdict);
597 
598     assert(!subqdict_len || subqdict[subqdict_len - 1] == '.');
599 
600     /* qdict_array_split() loops until UINT_MAX, but as we want to return
601      * negative errors, we only have a signed return value here. Any additional
602      * entries will lead to -EINVAL. */
603     for (i = 0; i < INT_MAX; i++) {
604         QObject *subqobj;
605         int subqdict_entries;
606         char *prefix = g_strdup_printf("%s%u.", subqdict, i);
607 
608         subqdict_entries = qdict_count_prefixed_entries(src, prefix);
609 
610         /* Remove ending "." */
611         prefix[strlen(prefix) - 1] = 0;
612         subqobj = qdict_get(src, prefix);
613 
614         g_free(prefix);
615 
616         if (subqdict_entries < 0) {
617             return subqdict_entries;
618         }
619 
620         /* There may be either a single subordinate object (named "%u") or
621          * multiple objects (each with a key prefixed "%u."), but not both. */
622         if (subqobj && subqdict_entries) {
623             return -EINVAL;
624         } else if (!subqobj && !subqdict_entries) {
625             break;
626         }
627 
628         entries += subqdict_entries ? subqdict_entries : 1;
629     }
630 
631     /* Consider everything handled that isn't part of the given sub-QDict */
632     for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
633         if (!strstart(qdict_entry_key(entry), subqdict, NULL)) {
634             entries++;
635         }
636     }
637 
638     /* Anything left in the sub-QDict that wasn't handled? */
639     if (qdict_size(src) != entries) {
640         return -EINVAL;
641     }
642 
643     return i;
644 }
645 
646 /**
647  * qdict_join(): Absorb the src QDict into the dest QDict, that is, move all
648  * elements from src to dest.
649  *
650  * If an element from src has a key already present in dest, it will not be
651  * moved unless overwrite is true.
652  *
653  * If overwrite is true, the conflicting values in dest will be discarded and
654  * replaced by the corresponding values from src.
655  *
656  * Therefore, with overwrite being true, the src QDict will always be empty when
657  * this function returns. If overwrite is false, the src QDict will be empty
658  * iff there were no conflicts.
659  */
660 void qdict_join(QDict *dest, QDict *src, bool overwrite)
661 {
662     const QDictEntry *entry, *next;
663 
664     entry = qdict_first(src);
665     while (entry) {
666         next = qdict_next(src, entry);
667 
668         if (overwrite || !qdict_haskey(dest, entry->key)) {
669             qdict_put_obj(dest, entry->key, qobject_ref(entry->value));
670             qdict_del(src, entry->key);
671         }
672 
673         entry = next;
674     }
675 }
676 
677 /**
678  * qdict_rename_keys(): Rename keys in qdict according to the replacements
679  * specified in the array renames. The array must be terminated by an entry
680  * with from = NULL.
681  *
682  * The renames are performed individually in the order of the array, so entries
683  * may be renamed multiple times and may or may not conflict depending on the
684  * order of the renames array.
685  *
686  * Returns true for success, false in error cases.
687  */
688 bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **errp)
689 {
690     QObject *qobj;
691 
692     while (renames->from) {
693         if (qdict_haskey(qdict, renames->from)) {
694             if (qdict_haskey(qdict, renames->to)) {
695                 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
696                            "same time", renames->to, renames->from);
697                 return false;
698             }
699 
700             qobj = qdict_get(qdict, renames->from);
701             qdict_put_obj(qdict, renames->to, qobject_ref(qobj));
702             qdict_del(qdict, renames->from);
703         }
704 
705         renames++;
706     }
707     return true;
708 }
709 
710 /*
711  * Create a QObject input visitor for flat @qdict with possibly
712  * confused scalar types.
713  *
714  * The block subsystem uses this function to visit its flat QDict with
715  * possibly confused scalar types.  It should not be used for anything
716  * else, and it should go away once the block subsystem has been
717  * cleaned up.
718  */
719 Visitor *qobject_input_visitor_new_flat_confused(QDict *qdict,
720                                                  Error **errp)
721 {
722     QObject *crumpled;
723     Visitor *v;
724 
725     crumpled = qdict_crumple_for_keyval_qiv(qdict, errp);
726     if (!crumpled) {
727         return NULL;
728     }
729 
730     v = qobject_input_visitor_new_keyval(crumpled);
731     qobject_unref(crumpled);
732     return v;
733 }
734