Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <kdb.h>
10 :
11 : #include <stdio.h>
12 : #include <stdlib.h>
13 :
14 : typedef enum
15 : {
16 : INPUT_USE_OURS,
17 : INPUT_DO_MERGE,
18 : INPUT_USE_THEIRS
19 : } input;
20 :
21 0 : int showElektraErrorDialog (Key * parentKey, Key * problemKey)
22 : {
23 0 : printf ("dialog for %s and %s\n", keyName (parentKey), keyName (problemKey));
24 : int a;
25 0 : if (scanf ("%d", &a) != 1)
26 : {
27 0 : fprintf (stderr, "Unable to convert input to integer number");
28 0 : return EXIT_FAILURE;
29 : }
30 0 : return a;
31 : }
32 :
33 0 : KeySet * doElektraMerge (KeySet * ours, KeySet * theirs, KeySet * base)
34 : {
35 0 : printf ("see libelektra-tools for merging"
36 : " sizes are: %d %d %d\n",
37 0 : (int) ksGetSize (ours), (int) ksGetSize (theirs), (int) ksGetSize (base));
38 0 : return ksNew (0, KS_END);
39 : }
40 :
41 :
42 0 : int main (void)
43 : {
44 : // clang-format off
45 : //! [set]
46 0 : KeySet * myConfig = ksNew (0, KS_END);
47 0 : Key * parentKey = keyNew ("system/sw/MyApp", KEY_END);
48 0 : KDB * handle = kdbOpen (parentKey);
49 :
50 0 : kdbGet (handle, myConfig, parentKey); // kdbGet needs to be called first!
51 0 : KeySet * base = ksDup (myConfig); // save a copy of original keyset
52 :
53 : // change the keys within myConfig
54 :
55 0 : KeySet * ours = ksDup (myConfig); // save a copy of our keyset
56 : KeySet * theirs; // needed for 3-way merging
57 0 : int ret = kdbSet (handle, myConfig, parentKey);
58 0 : while (ret == -1) // as long as we have an error
59 : {
60 : // We got an error. Warn user.
61 0 : Key * problemKey = ksCurrent (myConfig);
62 : // parentKey has the errorInformation
63 : // problemKey is the faulty key (may be null)
64 0 : int userInput = showElektraErrorDialog (parentKey, problemKey);
65 0 : switch (userInput)
66 : {
67 : case INPUT_USE_OURS:
68 0 : kdbGet (handle, myConfig, parentKey); // refresh key database
69 0 : ksDel (myConfig);
70 0 : myConfig = ours;
71 0 : break;
72 : case INPUT_DO_MERGE:
73 0 : theirs = ksDup (ours);
74 0 : kdbGet (handle, theirs, parentKey); // refresh key database
75 0 : KeySet * res = doElektraMerge (ours, theirs, base);
76 0 : ksDel (theirs);
77 0 : myConfig = res;
78 0 : break;
79 : case INPUT_USE_THEIRS:
80 : // should always work, we just write what we got
81 : // but to be sure always give the user another way
82 : // to exit the loop
83 0 : kdbGet (handle, myConfig, parentKey); // refresh key database
84 0 : break;
85 : // other cases ...
86 : }
87 0 : ret = kdbSet (handle, myConfig, parentKey);
88 : }
89 :
90 0 : ksDel (ours);
91 0 : ksDel (base);
92 0 : ksDel (myConfig); // delete the in-memory configuration
93 :
94 0 : kdbClose (handle, parentKey); // no more affairs with the key database.
95 0 : keyDel (parentKey);
96 : //! [set]
97 : }
|