forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internalnotification.c
700 lines (612 loc) · 20.9 KB
/
internalnotification.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/**
* @file
*
* @brief Source for internalnotification plugin
*
* @copyright BSD License (see doc/COPYING or http://www.libelektra.org)
*
*/
#include "internalnotification.h"
#include <kdb.h>
#include <kdbassert.h>
#include <kdbchangetracking.h>
#include <kdbhelper.h>
#include <kdblogger.h>
#include <kdbnotificationinternal.h>
#include <ctype.h> // isspace()
#include <errno.h> // errno
#include <stdlib.h> // strto* functions
/**
* Structure for registered key variable pairs
* @internal
*/
struct _KeyRegistration
{
char * name;
int sameOrBelow;
int freeContext;
ElektraNotificationChangeCallback callback;
void * context;
struct _KeyRegistration * next;
};
typedef struct _KeyRegistration KeyRegistration;
/**
* Structure for internal plugin state
* @internal
*/
struct _PluginState
{
KeyRegistration * head;
KeyRegistration * last;
ElektraNotificationConversionErrorCallback conversionErrorCallback;
void * conversionErrorCallbackContext;
};
typedef struct _PluginState PluginState;
/**
* @see kdbnotificationinternal.h ::ElektraNotificationSetConversionErrorCallback
*/
static void elektraInternalnotificationSetConversionErrorCallback (Plugin * handle, ElektraNotificationConversionErrorCallback callback,
void * context)
{
ELEKTRA_NOT_NULL (handle);
ELEKTRA_NOT_NULL (callback);
PluginState * data = elektraPluginGetData (handle);
ELEKTRA_NOT_NULL (data);
data->conversionErrorCallback = callback;
data->conversionErrorCallbackContext = context;
}
/**
* @internal
* Check if two keys have the same name.
* If one of the keys is cascading only the cascading names are compared.
*
* @param key key
* @param check check
* @retval 1 if keys have the same name
* @retval 0 otherwise
*/
static int checkKeyIsSame (Key * key, Key * check)
{
int result = 0;
if (keyGetNamespace (check) == KEY_NS_CASCADING || keyGetNamespace (key) == KEY_NS_CASCADING)
{
const char * cascadingCheck = strrchr (keyName (check), '/');
const char * cascadingKey = strrchr (keyName (key), '/');
if (cascadingCheck != NULL && cascadingKey != NULL)
{
result = elektraStrCmp (cascadingKey, cascadingCheck) == 0;
}
else
{
if (!cascadingCheck || !cascadingKey)
{
ELEKTRA_LOG_WARNING ("invalid key given: 'NULL' is not a valid key");
}
}
}
else
{
result = elektraStrCmp (keyName (check), keyName (key)) == 0;
}
return result;
}
/**
* @internal
* Check if a key has the same name or is below a given key.
*
* @param key key
* @param check check
* @retval 1 if key has the same name or is below
* @retval 0 otherwise
*/
static int checkKeyIsBelowOrSame (Key * key, Key * check)
{
int result = 0;
if (keyIsBelow (key, check))
{
result = 1;
}
else
{
result = checkKeyIsSame (key, check);
}
return result;
}
/**
* @internal
* Call kdbGet if there are registrations below the changed key.
*
* On kdbGet this plugin implicitly updates registered keys.
*
* @see ElektraNotificationChangeCallback (kdbnotificationinternal.h)
* @param key changed key
* @param context callback context
*/
void elektraInternalnotificationDoUpdate (Key * changedKey, ElektraNotificationCallbackContext * context)
{
ELEKTRA_NOT_NULL (changedKey);
ELEKTRA_NOT_NULL (context);
Plugin * plugin = context->notificationPlugin;
PluginState * pluginState = elektraPluginGetData (plugin);
ELEKTRA_NOT_NULL (pluginState);
int kdbChanged = 0;
KeyRegistration * keyRegistration = pluginState->head;
while (keyRegistration != NULL)
{
Key * registeredKey = keyNew (keyRegistration->name, KEY_END);
// check if registered key is same or below changed/commit key
kdbChanged |= checkKeyIsBelowOrSame (changedKey, registeredKey);
if (keyRegistration->sameOrBelow)
{
// check if registered key is also above changed/commit key
kdbChanged |= checkKeyIsBelowOrSame (registeredKey, changedKey);
}
keyRegistration = keyRegistration->next;
keyDel (registeredKey);
}
if (kdbChanged)
{
KeySet * global = elektraPluginGetGlobalKeySet (plugin);
Key * kdbKey = ksLookupByName (global, "system:/elektra/kdb", 0);
const void * kdbPtr = keyValue (kdbKey);
KDB * kdb = kdbPtr == NULL ? NULL : *(KDB **) keyValue (kdbKey);
context->kdbUpdate (kdb, changedKey);
}
keyDel (changedKey);
}
/**
* Creates a new KeyRegistration structure and appends it at the end of the registration list
* @internal
*
* @param pluginState internal plugin data structure
* @param key key
* @param callback callback for changes
* @param context context for callback
* @param freeContext context needs to be freed on close
*
* @return pointer to created KeyRegistration structure or NULL if memory allocation failed
*/
static KeyRegistration * elektraInternalnotificationAddNewRegistration (PluginState * pluginState, Key * key,
ElektraNotificationChangeCallback callback, void * context,
int freeContext)
{
KeyRegistration * item = elektraMalloc (sizeof *item);
if (item == NULL)
{
return NULL;
}
item->next = NULL;
item->name = elektraStrDup (keyName (key));
item->callback = callback;
item->context = context;
item->sameOrBelow = 0;
item->freeContext = freeContext;
if (pluginState->head == NULL)
{
// Initialize list
pluginState->head = pluginState->last = item;
}
else
{
// Make new item end of list
pluginState->last->next = item;
pluginState->last = item;
}
return item;
}
/**
* @internal
* Check if a key set contains a key that is same or below a given key.
*
* @param key key
* @param ks key set
* @retval 1 if the key set contains the key
* @retval 0 otherwise
*/
static int keySetContainsSameOrBelow (Key * check, KeySet * ks)
{
Key * current;
for (elektraCursor it = 0; it < ksGetSize (ks); ++it)
{
current = ksAtCursor (ks, it);
if (checkKeyIsBelowOrSame (check, current))
{
return 1;
}
}
return 0;
}
/**
* Walk through all registered key and determine whether they are contained in the diff.
* If they are, call their notifiaction callback.
* @internal
*
* @param plugin internal plugin handle
* @param diff diff as determined in the hook functions
* e.g. elektraInternalnotificationGet or elektraInternalnotificationCommit)
*
*/
void elektraInternalnotificationNotifyChangedKeys (Plugin * plugin, const ElektraDiff * diff)
{
PluginState * pluginState = elektraPluginGetData (plugin);
ELEKTRA_ASSERT (pluginState != NULL, "plugin state was not initialized properly");
KeySet * modifiedKeys = elektraDiffGetModifiedKeys (diff);
KeySet * addedKeys = elektraDiffGetAddedKeys (diff);
KeySet * removedKeys = elektraDiffGetRemovedKeys (diff);
ksAppend (modifiedKeys, addedKeys);
ksAppend (modifiedKeys, removedKeys);
KeyRegistration * registeredKey = pluginState->head;
while (registeredKey != NULL)
{
int changed = 0;
Key * key;
if (registeredKey->sameOrBelow)
{
Key * checkKey = keyNew (registeredKey->name, KEY_END);
if (keySetContainsSameOrBelow (checkKey, modifiedKeys))
{
changed = 1;
key = checkKey;
}
else
{
keyDel (checkKey);
}
}
else
{
key = ksLookupByName (modifiedKeys, registeredKey->name, 0);
if (key != NULL)
{
changed = 1;
}
}
if (changed)
{
ELEKTRA_LOG_DEBUG ("found changed registeredKey=%s with string value \"%s\". using context or variable=%p",
registeredKey->name, keyString (key), registeredKey->context);
// Invoke callback
ElektraNotificationChangeCallback callback = *(ElektraNotificationChangeCallback) registeredKey->callback;
callback (key, registeredKey->context);
if (registeredKey->sameOrBelow)
{
keyDel (key);
}
}
// proceed with next registered key
registeredKey = registeredKey->next;
}
ksDel (modifiedKeys);
ksDel (addedKeys);
ksDel (removedKeys);
}
// Generate register and conversion functions
// for built-in C types
#define TYPE int
#define VALUE_TYPE long int
#define TYPE_NAME Int
#define TO_VALUE (strtol (string, &end, 10))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= INT_MAX && value >= INT_MIN)
#include "macros/add_type.h"
#define TYPE unsigned int
#define VALUE_TYPE unsigned long int
#define TYPE_NAME UnsignedInt
#define TO_VALUE (strtoul (string, &end, 10))
#define PRE_CHECK_BLOCK ELEKTRA_TYPE_NEGATIVE_PRE_CHECK_BLOCK
#define PRE_CHECK_CONVERSION ELEKTRA_TYPE_NEGATIVE_PRE_CHECK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= UINT_MAX)
#include "macros/add_type.h"
#define TYPE long
#define TYPE_NAME Long
#define TO_VALUE (strtol (string, &end, 10))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#define TYPE unsigned long
#define TYPE_NAME UnsignedLong
#define TO_VALUE (strtoul (string, &end, 10))
#define PRE_CHECK_BLOCK ELEKTRA_TYPE_NEGATIVE_PRE_CHECK_BLOCK
#define PRE_CHECK_CONVERSION ELEKTRA_TYPE_NEGATIVE_PRE_CHECK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#define TYPE long long
#define TYPE_NAME LongLong
#define TO_VALUE (strtoll (string, &end, 10))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#define TYPE unsigned long long
#define TYPE_NAME UnsignedLongLong
#define TO_VALUE (strtoull (string, &end, 10))
#define PRE_CHECK_BLOCK ELEKTRA_TYPE_NEGATIVE_PRE_CHECK_BLOCK
#define PRE_CHECK_CONVERSION ELEKTRA_TYPE_NEGATIVE_PRE_CHECK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#define TYPE float
#define TYPE_NAME Float
#define TO_VALUE (strtof (string, &end))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#define TYPE double
#define TYPE_NAME Double
#define TO_VALUE (strtod (string, &end))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
// for kdb_*_t Types
#define TYPE kdb_boolean_t
#define TYPE_NAME KdbBoolean
#define TO_VALUE (!strcmp (string, "1"))
#include "macros/add_type.h"
#define TYPE kdb_char_t
#define TYPE_NAME KdbChar
#define TO_VALUE (string[0])
#include "macros/add_type.h"
#define TYPE kdb_octet_t
#define VALUE_TYPE unsigned int
#define TYPE_NAME KdbOctet
#define TO_VALUE (strtoul (string, &end, 10))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= 255)
#include "macros/add_type.h"
#define TYPE kdb_short_t
#define VALUE_TYPE int
#define TYPE_NAME KdbShort
#define TO_VALUE (strtol (string, &end, 10))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= SHRT_MAX && value >= SHRT_MIN)
#include "macros/add_type.h"
#define TYPE kdb_unsigned_short_t
#define VALUE_TYPE unsigned int
#define TYPE_NAME KdbUnsignedShort
#define TO_VALUE (strtoul (string, &end, 10))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= USHRT_MAX)
#include "macros/add_type.h"
#define TYPE kdb_long_t
#define TYPE_NAME KdbLong
#define TO_VALUE (strtol (string, &end, 10))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#define TYPE kdb_unsigned_long_t
#define TYPE_NAME KdbUnsignedLong
#define TO_VALUE (strtoul (string, &end, 10))
#define PRE_CHECK_BLOCK ELEKTRA_TYPE_NEGATIVE_PRE_CHECK_BLOCK
#define PRE_CHECK_CONVERSION ELEKTRA_TYPE_NEGATIVE_PRE_CHECK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#define TYPE kdb_long_long_t
#define TYPE_NAME KdbLongLong
#define TO_VALUE (ELEKTRA_LONG_LONG_S (string, &end, 10))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#define TYPE kdb_unsigned_long_long_t
#define TYPE_NAME KdbUnsignedLongLong
#define TO_VALUE (ELEKTRA_UNSIGNED_LONG_LONG_S (string, &end, 10))
#define PRE_CHECK_BLOCK ELEKTRA_TYPE_NEGATIVE_PRE_CHECK_BLOCK
#define PRE_CHECK_CONVERSION ELEKTRA_TYPE_NEGATIVE_PRE_CHECK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#define TYPE kdb_float_t
#define TYPE_NAME KdbFloat
#define TO_VALUE (strtof (string, &end))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#define TYPE kdb_double_t
#define TYPE_NAME KdbDouble
#define TO_VALUE (strtod (string, &end))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#ifdef ELEKTRA_HAVE_KDB_LONG_DOUBLE
#define TYPE kdb_long_double_t
#define TYPE_NAME KdbLongDouble
#define TO_VALUE (strtold (string, &end))
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#include "macros/add_type.h"
#endif // ELEKTRA_HAVE_KDB_LONG_DOUBLE
/**
* @see kdbnotificationinternal.h ::ElektraNotificationPluginRegisterCallback
*/
int elektraInternalnotificationRegisterCallback (Plugin * handle, Key * key, ElektraNotificationChangeCallback callback, void * context)
{
PluginState * pluginState = elektraPluginGetData (handle);
ELEKTRA_ASSERT (pluginState != NULL, "plugin state was not initialized properly");
KeyRegistration * registeredKey = elektraInternalnotificationAddNewRegistration (pluginState, key, callback, context, 0);
if (registeredKey == NULL)
{
return 0;
}
return 1;
}
/**
* @see kdbnotificationinternal.h ::ElektraNotificationPluginRegisterCallbackSameOrBelow
*/
int elektraInternalnotificationRegisterCallbackSameOrBelow (Plugin * handle, Key * key, ElektraNotificationChangeCallback callback,
void * context)
{
PluginState * pluginState = elektraPluginGetData (handle);
ELEKTRA_ASSERT (pluginState != NULL, "plugin state was not initialized properly");
KeyRegistration * registeredKey = elektraInternalnotificationAddNewRegistration (pluginState, key, callback, context, 0);
if (registeredKey == NULL)
{
return 0;
}
registeredKey->sameOrBelow = 1;
return 1;
}
/**
* Updates registrations with current data from storage.
* Part of elektra plugin contract.
*
* @param handle plugin handle
* @param returned key set containing current data from storage
* @param parentKey key for errors
*
* @retval 1 on success
* @retval -1 on failure
*/
int elektraInternalnotificationGet (Plugin * handle, KeySet * returned, Key * parentKey)
{
if (!elektraStrCmp (keyName (parentKey), "system:/elektra/modules/internalnotification"))
{
KeySet * contract = ksNew (
30,
keyNew ("system:/elektra/modules/internalnotification", KEY_VALUE,
"internalnotification plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/internalnotification/exports", KEY_END),
keyNew ("system:/elektra/modules/internalnotification/exports/get", KEY_FUNC, elektraInternalnotificationGet,
KEY_END),
keyNew ("system:/elektra/modules/internalnotification/exports/commit", KEY_FUNC, elektraInternalnotificationCommit,
KEY_END),
keyNew ("system:/elektra/modules/internalnotification/exports/open", KEY_FUNC, elektraInternalnotificationOpen,
KEY_END),
keyNew ("system:/elektra/modules/internalnotification/exports/close", KEY_FUNC, elektraInternalnotificationClose,
KEY_END),
keyNew ("system:/elektra/modules/internalnotification/exports/hook/notification/send/get", KEY_FUNC,
elektraInternalnotificationGet, KEY_END),
keyNew ("system:/elektra/modules/internalnotification/exports/hook/notification/send/set", KEY_FUNC,
elektraInternalnotificationCommit, KEY_END),
// Export register* functions
INTERNALNOTIFICATION_EXPORT_FUNCTION (Int), INTERNALNOTIFICATION_EXPORT_FUNCTION (UnsignedInt),
INTERNALNOTIFICATION_EXPORT_FUNCTION (Long), INTERNALNOTIFICATION_EXPORT_FUNCTION (UnsignedLong),
INTERNALNOTIFICATION_EXPORT_FUNCTION (LongLong), INTERNALNOTIFICATION_EXPORT_FUNCTION (UnsignedLongLong),
INTERNALNOTIFICATION_EXPORT_FUNCTION (Float), INTERNALNOTIFICATION_EXPORT_FUNCTION (Double),
// Export register* functions for kdb_*_t types
INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbBoolean), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbChar),
INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbOctet), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbShort),
INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbUnsignedShort), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbLong),
INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbUnsignedLong), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbLongLong),
INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbUnsignedLongLong), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbFloat),
INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbDouble),
#ifdef ELEKTRA_HAVE_KDB_LONG_DOUBLE
INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbLongDouble),
#endif // ELEKTRA_HAVE_KDB_LONG_DOUBLE
keyNew ("system:/elektra/modules/internalnotification/exports/registerCallback", KEY_FUNC,
elektraInternalnotificationRegisterCallback, KEY_END),
keyNew ("system:/elektra/modules/internalnotification/exports/registerCallbackSameOrBelow", KEY_FUNC,
elektraInternalnotificationRegisterCallbackSameOrBelow, KEY_END),
keyNew ("system:/elektra/modules/internalnotification/exports/setConversionErrorCallback", KEY_FUNC,
elektraInternalnotificationSetConversionErrorCallback, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/internalnotification/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return 1;
}
const ChangeTrackingContext * context = elektraChangeTrackingGetContextFromPlugin (handle);
// As this plugin is a hooks plugin, it will get called before the internal cache for changetracking is updated on kdbGet
// Thus we receive the new keys in "returned" and have the old keys still in "context" and can therefore successfully
// detect changes that have been made externally on disk
ElektraDiff * diff = elektraChangeTrackingCalculateDiff (returned, context, parentKey);
elektraInternalnotificationNotifyChangedKeys (handle, diff);
elektraDiffDel (diff);
return 1;
}
/**
* Updates registrations with data written by the application.
* Part of elektra plugin contract.
*
* @param handle plugin handle
* @param returned key set containing current data from the application
* @param parentKey key for errors
* @retval 1 on success
* @retval -1 on failure
*/
int elektraInternalnotificationCommit (Plugin * handle, KeySet * returned, Key * parentKey)
{
const ChangeTrackingContext * context = elektraChangeTrackingGetContextFromPlugin (handle);
ElektraDiff * diff = elektraChangeTrackingCalculateDiff (returned, context, parentKey);
elektraInternalnotificationNotifyChangedKeys (handle, diff);
elektraDiffDel (diff);
return 1;
}
/**
* Initialize data plugin data structures.
* Part of elektra plugin contract.
*
* @param handle plugin handle
* @param parentKey key for errors
*
* @retval 1 on success
* @retval -1 on failure
*/
int elektraInternalnotificationOpen (Plugin * handle, Key * parentKey ELEKTRA_UNUSED)
{
PluginState * pluginState = elektraPluginGetData (handle);
if (pluginState == NULL)
{
pluginState = elektraMalloc (sizeof *pluginState);
if (pluginState == NULL)
{
return -1;
}
elektraPluginSetData (handle, pluginState);
// Initialize list pointers for registered keys
pluginState->head = NULL;
pluginState->last = NULL;
pluginState->conversionErrorCallback = NULL;
pluginState->conversionErrorCallbackContext = NULL;
}
KeySet * config = elektraPluginGetConfig (handle);
KeySet * global = elektraPluginGetGlobalKeySet (handle);
if (global != NULL)
{
ksAppendKey (global,
keyNew ("system:/elektra/notification/callback", KEY_FUNC, elektraInternalnotificationDoUpdate, KEY_END));
Key * contextKey = ksLookupByName (config, "/context", 0);
if (contextKey != NULL)
{
ElektraNotificationCallbackContext * context = *(ElektraNotificationCallbackContext **) keyValue (contextKey);
ksAppendKey (global, keyNew ("system:/elektra/notification/context", KEY_BINARY, KEY_SIZE, sizeof (context),
KEY_VALUE, &context, KEY_END));
}
}
return 1;
}
/**
* Free used memory.
* Part of elektra plugin contract.
*
* @param handle plugin handle
* @param parentKey key for errors
*
* @retval 1 on success
* @retval -1 on failure
*/
int elektraInternalnotificationClose (Plugin * handle, Key * parentKey ELEKTRA_UNUSED)
{
PluginState * pluginState = elektraPluginGetData (handle);
if (pluginState != NULL)
{
// Free registrations
KeyRegistration * current = pluginState->head;
KeyRegistration * next;
while (current != NULL)
{
next = current->next;
elektraFree (current->name);
if (current->freeContext)
{
elektraFree (current->context);
}
elektraFree (current);
current = next;
}
// Free list pointer
elektraFree (pluginState);
elektraPluginSetData (handle, NULL);
}
KeySet * config = elektraPluginGetConfig (handle);
Key * contextKey = ksLookupByName (config, "/context", KDB_O_POP);
if (contextKey != NULL)
{
ElektraNotificationCallbackContext * context = *(ElektraNotificationCallbackContext **) keyValue (contextKey);
elektraFree (context);
}
keyDel (contextKey);
return 1;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("internalnotification",
ELEKTRA_PLUGIN_GET, &elektraInternalnotificationGet,
ELEKTRA_PLUGIN_COMMIT, &elektraInternalnotificationCommit,
ELEKTRA_PLUGIN_OPEN, &elektraInternalnotificationOpen,
ELEKTRA_PLUGIN_CLOSE, &elektraInternalnotificationClose,
ELEKTRA_PLUGIN_END);
}