forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.c
780 lines (657 loc) · 22.8 KB
/
record.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#include <kdbchangetracking.h>
#include <kdberrors.h>
#include <kdbhelper.h>
#include <kdbprivate.h>
#include <kdbrecord.h>
/**
* Enable session recording.
* This affects both the given @p handle as well as every KDB instance that will be created after this method has been called.
*
* @param handle the KDB instance to use
* @param parentKey recording will be enabled for every key that is same or below this key
* @param errorKey used for reporting errors and warnings.
* As usual, they will be found as meta keys attached to this key.
* @retval true - recording has been enabled successfully
* @retfal false - there was an error enabling recording - see @p errorKey for further details
*/
bool elektraRecordEnableRecording (KDB * handle, const Key * parentKey, Key * errorKey)
{
if (handle == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for KDB handle");
return false;
}
if (parentKey == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for parent key");
return false;
}
if (handle->hooks.record.plugin == NULL)
{
ELEKTRA_ADD_RESOURCE_WARNING (
errorKey, "There is no record plugin present. Session recording will not work for the current KDB instance.");
}
Key * configKey = keyNew (ELEKTRA_RECORD_CONFIG_KEY, KEY_END);
KeySet * config = ksNew (0, KS_END);
if (kdbGet (handle, config, configKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, configKey);
keyDel (configKey);
ksDel (config);
return false;
}
elektraNamespace ns = KEY_NS_SYSTEM;
Key * activeKey = ksLookupByName (config, ELEKTRA_RECORD_CONFIG_ACTIVE_KEY, KDB_O_POP);
if (activeKey != NULL)
{
ELEKTRA_ADD_RESOURCE_WARNINGF (
errorKey, "There is already a session active with parent key \"%s\". Replacing it with new parent key \"%s\".",
keyString (activeKey), keyName (parentKey));
ns = keyGetNamespace (activeKey);
keyDel (activeKey);
}
activeKey = keyNew (ELEKTRA_RECORD_CONFIG_ACTIVE_KEY, KEY_VALUE, keyName (parentKey), KEY_END);
keySetNamespace (activeKey, ns);
ksAppendKey (config, activeKey);
if (kdbSet (handle, config, configKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, configKey);
keyDel (configKey);
// This will also automatically delete activeKey
ksDel (config);
return false;
}
ksAppendKey (handle->global, activeKey);
keyDel (configKey);
ksDel (config);
return true;
}
/**
* Disable session recording.
* This affects both the given @p handle as well as every KDB instance that will be created after this method has been called.
*
* @param handle the KDB instance to use
* @param errorKey used for reporting errors and warnings.
* As usual, they will be found as meta keys attached to this key.
* @retval true - recording has been disabled successfully
* @retfal false - there was an error disabling recording - see @p errorKey for further details
*/
bool elektraRecordDisableRecording (KDB * handle, Key * errorKey)
{
if (handle == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for KDB handle");
return false;
}
Key * configKey = keyNew (ELEKTRA_RECORD_CONFIG_KEY, KEY_END);
KeySet * config = ksNew (0, KS_END);
if (kdbGet (handle, config, configKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, configKey);
keyDel (configKey);
ksDel (config);
return false;
}
Key * activeKey = NULL;
while ((activeKey = ksLookupByName (config, ELEKTRA_RECORD_CONFIG_ACTIVE_KEY, KDB_O_POP)) != NULL)
{
keyDel (activeKey);
}
if (kdbSet (handle, config, configKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, configKey);
keyDel (configKey);
ksDel (config);
keyDel (activeKey);
return false;
}
while ((activeKey = ksLookupByName (handle->global, ELEKTRA_RECORD_CONFIG_ACTIVE_KEY, KDB_O_POP)) != NULL)
{
keyDel (activeKey);
}
keyDel (configKey);
ksDel (config);
return true;
}
/**
* Clears all recorded data.
*
* @param handle the KDB instance to use
* @param errorKey used for reporting errors and warnings.
* As usual, they will be found as meta keys attached to this key.
* @retval true - recording session has been cleared successfully
* @retfal false - there was an error clearing the recording session - see @p errorKey for further details
*/
bool elektraRecordResetSession (KDB * handle, Key * errorKey)
{
if (handle == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for KDB handle");
return false;
}
Key * sessionKey = keyNew (ELEKTRA_RECORD_SESSION_KEY, KEY_END);
KeySet * session = ksNew (0, KS_END);
if (kdbGet (handle, session, sessionKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, sessionKey);
keyDel (sessionKey);
ksDel (session);
return false;
}
ksDel (ksCut (session, sessionKey));
if (kdbSet (handle, session, sessionKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, sessionKey);
keyDel (sessionKey);
ksDel (session);
return false;
}
keyDel (sessionKey);
ksDel (session);
return true;
}
/**
* Check whether session recording is active in the given KDB instance
*
* @param handle the KDB instance to check
* @retval @p true if session recording is active in @p handle
* @retval @p false if session recording is not active in @p handle
* @retval @p false if @p handle is @p NULL
*/
bool elektraRecordIsActive (KDB * handle)
{
if (handle == NULL)
{
return false;
}
const Key * activeKey = ksLookupByName (handle->global, ELEKTRA_RECORD_CONFIG_ACTIVE_KEY, 0);
if (activeKey == NULL)
{
return false;
}
return true;
}
/**
* @internal
*
* Replace the @p oldPrefix in all namespaces with @p newPrefix in a given KeySet
*
* @param oldPrefix the prefix to remove
* @param newPrefix the prefix @p oldPrefix should be replaced with
* @param ks the keyset in which the operation should be carried out
* @retval @p NULL if any of @p oldPrefix, @p newPrefix or @p ks is @p NULL
* @retval @p ks if the operation succeeded
*/
static KeySet * renameKeysInAllNamespaces (const char * oldPrefix, const char * newPrefix, KeySet * ks)
{
if (oldPrefix == NULL || newPrefix == NULL || ks == NULL)
{
return NULL;
}
Key * prefixKey = keyNew (oldPrefix, KEY_END);
Key * newRootKey = keyNew (newPrefix, KEY_END);
for (elektraNamespace ns = KEY_NS_FIRST; ns <= KEY_NS_LAST; ns++)
{
keySetNamespace (prefixKey, ns);
keySetNamespace (newRootKey, ns);
ksRename (ks, prefixKey, newRootKey);
}
keyDel (prefixKey);
keyDel (newRootKey);
return ks;
}
/**
* @internal
*
* Converts a KeySet in session recording format into an @p ElektraDiff.
*
* @param recordStorage the KeySet that contains the session diff
* @param sessionRecordingParentKey the parent key of the recording session
*
* @retval an instance of @p ElektraDiff representing the session diff
* @retval @p NULL if @p recordStorage is @p NULL
*/
static ElektraDiff * getDiffFromSessionStorage (KeySet * recordStorage, Key * sessionRecordingParentKey)
{
if (recordStorage == NULL)
{
return NULL;
}
Key * sessionDiffAddedKey = keyNew (ELEKTRA_RECORD_SESSION_DIFF_ADDED_KEY, KEY_END);
Key * sessionDiffModifiedOldKey = keyNew (ELEKTRA_RECORD_SESSION_DIFF_MODIFIED_OLD_KEY, KEY_END);
Key * sessionDiffModifiedNewKey = keyNew (ELEKTRA_RECORD_SESSION_DIFF_MODIFIED_NEW_KEY, KEY_END);
Key * sessionDiffRemovedKey = keyNew (ELEKTRA_RECORD_SESSION_DIFF_REMOVED_KEY, KEY_END);
ElektraDiff * sessionDiff = elektraDiffNew (
renameKeysInAllNamespaces (ELEKTRA_RECORD_SESSION_DIFF_ADDED_KEY, "/", ksCut (recordStorage, sessionDiffAddedKey)),
renameKeysInAllNamespaces (ELEKTRA_RECORD_SESSION_DIFF_REMOVED_KEY, "/", ksCut (recordStorage, sessionDiffRemovedKey)),
renameKeysInAllNamespaces (ELEKTRA_RECORD_SESSION_DIFF_MODIFIED_OLD_KEY, "/",
ksCut (recordStorage, sessionDiffModifiedOldKey)),
renameKeysInAllNamespaces (ELEKTRA_RECORD_SESSION_DIFF_MODIFIED_NEW_KEY, "/",
ksCut (recordStorage, sessionDiffModifiedNewKey)),
keyDup (sessionRecordingParentKey, KEY_CP_ALL));
keyDel (sessionDiffAddedKey);
keyDel (sessionDiffModifiedOldKey);
keyDel (sessionDiffModifiedNewKey);
keyDel (sessionDiffRemovedKey);
return sessionDiff;
}
/**
* @internal
*
* Adds a session storage format representation of the given session diff to the provided KeySet
*
* @param recordStorage the KeySet where the keys should be added
* @param sessionDiff the diff to add
*/
static void putDiffIntoSessionStorage (KeySet * recordStorage, ElektraDiff * sessionDiff)
{
KeySet * addedKeys = renameKeysInAllNamespaces ("/", ELEKTRA_RECORD_SESSION_DIFF_ADDED_KEY, elektraDiffGetAddedKeys (sessionDiff));
KeySet * modifiedOldKeys =
renameKeysInAllNamespaces ("/", ELEKTRA_RECORD_SESSION_DIFF_MODIFIED_OLD_KEY, elektraDiffGetModifiedKeys (sessionDiff));
KeySet * modifiedNewKeys =
renameKeysInAllNamespaces ("/", ELEKTRA_RECORD_SESSION_DIFF_MODIFIED_NEW_KEY, elektraDiffGetModifiedNewKeys (sessionDiff));
KeySet * removedKeys =
renameKeysInAllNamespaces ("/", ELEKTRA_RECORD_SESSION_DIFF_REMOVED_KEY, elektraDiffGetRemovedKeys (sessionDiff));
ksAppend (recordStorage, addedKeys);
ksAppend (recordStorage, modifiedOldKeys);
ksAppend (recordStorage, modifiedNewKeys);
ksAppend (recordStorage, removedKeys);
ksDel (addedKeys);
ksDel (modifiedOldKeys);
ksDel (modifiedNewKeys);
ksDel (removedKeys);
}
/**
* Diff and record changes to the KDB instance in @p handle.
* This function is mainly intended for use in the recorder plugin.
*
* @param handle the KDB instance that changes occured on
* @param sessionStorageHandle the KDB instances that shall be used to persist the session diff.
* You can use the same as for @p handle.
* @param newKeys the keyset with changed keys
* @param parentKey only changes same or below this key will be determined
* @param errorKey used for reporting errors and warnings.
* As usual, they will be found as meta keys attached to this key.
* @retval true - changes were recorded successfully
* @retfal false - there was an error during recording - see @p errorKey for further details
*/
bool elektraRecordRecord (KDB * handle, KDB * sessionStorageHandle, KeySet * newKeys, Key * parentKey, Key * errorKey)
{
if (handle == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for KDB handle");
return false;
}
if (sessionStorageHandle == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for KDB session storage handle");
return false;
}
if (newKeys == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for new keys");
return false;
}
if (parentKey == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for parent key");
return false;
}
const Key * activeKey = ksLookupByName (handle->global, ELEKTRA_RECORD_CONFIG_ACTIVE_KEY, 0);
if (activeKey == NULL)
{
ELEKTRA_ADD_RESOURCE_WARNINGF (errorKey, "%s called but recording is not enabled.", __func__);
// recording is not activated --> do nothing, but still successful
return true;
}
Key * recordConfigurationKey = keyNew (ELEKTRA_RECORD_CONFIG_KEY, KEY_END);
Key * sessionRecordingKey = keyNew (ELEKTRA_RECORD_SESSION_KEY, KEY_END);
if (keyIsBelowOrSame (sessionRecordingKey, parentKey) || keyIsBelowOrSame (recordConfigurationKey, parentKey))
{
// the parent key is either below our session storage or below our configuration storage
// we do not want to record changes to the recording tool itself
// do nothing, but still successful
keyDel (sessionRecordingKey);
keyDel (recordConfigurationKey);
return true;
}
KeySet * blacklistedKeys = ksNew (4, recordConfigurationKey, keyDup (sessionRecordingKey, KEY_CP_ALL),
keyNew ("system:/elektra/modules", KEY_END), keyNew ("system:/elektra/version", KEY_END), KS_END);
// Remove all keys that belong to the recording tool or are blacklisted
KeySet * toRecord = ksDup (newKeys);
for (elektraCursor it = 0; it < ksGetSize (blacklistedKeys); it++)
{
ksDel (ksCut (toRecord, ksAtCursor (blacklistedKeys, it)));
}
if (ksGetSize (toRecord) == 0 && ksGetSize (newKeys) != 0)
{
// after clearing out all keys that belong to the recording tool there are no more keys
// do nothing, but still successful
keyDel (sessionRecordingKey);
ksDel (blacklistedKeys);
ksDel (toRecord);
return true;
}
const ChangeTrackingContext * changeTrackingContext = elektraChangeTrackingGetContextFromKdb (handle);
if (changeTrackingContext == NULL)
{
ELEKTRA_SET_INTERNAL_ERROR (errorKey, "Could not get changetracking context from KDB");
keyDel (sessionRecordingKey);
ksDel (blacklistedKeys);
ksDel (toRecord);
return false;
}
// Root key for which session recording is enabled
Key * sessionRecordingParentKey = keyNew (keyString (activeKey), KEY_END);
// Parent key for the part diff
Key * parentKeyForDiff = parentKey;
if (keyIsBelow (parentKey, sessionRecordingParentKey))
{
// if the sessionRecordingParentKey is below parentKey we only need to diff changes below sessionRecordingParentKey
parentKeyForDiff = sessionRecordingParentKey;
}
ElektraDiff * partDiff = elektraChangeTrackingCalculateDiff (toRecord, changeTrackingContext, parentKeyForDiff);
for (elektraCursor it = 0; it < ksGetSize (blacklistedKeys); it++)
{
elektraDiffRemoveSameOrBelow (partDiff, ksAtCursor (blacklistedKeys, it));
}
bool successful = true;
if (!elektraDiffIsEmpty (partDiff))
{
KeySet * recordStorage = ksNew (0, KS_END);
// load data for current session diff
if (kdbGet (sessionStorageHandle, recordStorage, sessionRecordingKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, sessionRecordingKey);
successful = false;
ksDel (recordStorage);
goto cleanup;
}
ElektraDiff * sessionDiff = getDiffFromSessionStorage (recordStorage, sessionRecordingParentKey);
// Calculate new session diff
Key * appendKey = keyNew ("/", KEY_END);
elektraDiffAppend (sessionDiff, partDiff, appendKey);
keyDel (appendKey);
putDiffIntoSessionStorage (recordStorage, sessionDiff);
// store data for session diff
if (kdbSet (sessionStorageHandle, recordStorage, sessionRecordingKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, sessionRecordingKey);
successful = false;
elektraDiffDel (sessionDiff);
ksDel (recordStorage);
goto cleanup;
}
elektraDiffDel (sessionDiff);
ksDel (recordStorage);
}
cleanup:
keyDel (sessionRecordingKey);
keyDel (sessionRecordingParentKey);
ksDel (blacklistedKeys);
ksDel (toRecord);
elektraDiffDel (partDiff);
return successful;
}
/**
* Undo changes that were recorded in the current recording session.
* After executing this function, the state of KDB should be the same as it was before starting the recording session.
*
* @param handle the KDB instance to use for accessing configuration data
* @param sessionStorageHandle the KDB instance to use for accessing recording data.
* This can be the same as for @p handle
* @param parentKey only changes same or below this key are undone.
* @param errorKey used for reporting errors and warnings.
* As usual, they will be found as meta keys attached to this key.
* @retval true - changes were undone successfully
* @retfal false - there was an error during the undo operation - see @p errorKey for further details
*/
bool elektraRecordUndo (KDB * handle, KDB * sessionStorageHandle, Key * parentKey, Key * errorKey)
{
if (handle == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for KDB handle");
return false;
}
if (sessionStorageHandle == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for KDB session storage handle");
return false;
}
if (parentKey == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for parent key");
return false;
}
Key * sessionRecordingKey = keyNew (ELEKTRA_RECORD_SESSION_KEY, KEY_END);
KeySet * recordStorage = ksNew (0, KS_END);
// Load data from session diff
if (kdbGet (sessionStorageHandle, recordStorage, sessionRecordingKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, sessionRecordingKey);
keyDel (sessionRecordingKey);
ksDel (recordStorage);
return false;
}
ElektraDiff * sessionDiff = getDiffFromSessionStorage (recordStorage, NULL);
ElektraDiff * undoDiff = elektraDiffCut (sessionDiff, parentKey);
bool successful = true;
if (!elektraDiffIsEmpty (undoDiff))
{
KeySet * ks = ksNew (0, KS_END);
if (kdbGet (handle, ks, parentKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, parentKey);
successful = false;
ksDel (ks);
goto cleanup;
}
elektraDiffUndo (undoDiff, ks);
// Disable session recording for now
Key * activeKey = ksLookupByName (handle->global, ELEKTRA_RECORD_CONFIG_ACTIVE_KEY, KDB_O_POP);
if (kdbSet (handle, ks, parentKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, parentKey);
successful = false;
goto innercleanup;
}
putDiffIntoSessionStorage (recordStorage, sessionDiff);
if (kdbSet (sessionStorageHandle, recordStorage, sessionRecordingKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, parentKey);
successful = false;
goto innercleanup;
}
innercleanup:
if (activeKey != NULL)
{
// Reenable session recording
ksAppendKey (handle->global, activeKey);
}
ksDel (ks);
if (!successful)
{
goto cleanup;
}
}
cleanup:
keyDel (sessionRecordingKey);
ksDel (recordStorage);
elektraDiffDel (sessionDiff);
elektraDiffDel (undoDiff);
return successful;
}
/**
* Removes the provided keys from the recording session.
*
* @param handle the KDB instance to use for accessing recording data.
* @param toRemove the keys in this keyset will be removed.
* @param recursive if @p true, also the keys below every specified key will be removed.
* @param errorKey used for reporting errors and warnings.
* As usual, they will be found as meta keys attached to this key.
* @retval true - removal was successful
* @retfal false - there was an error during the removal operation - see @p errorKey for further details
*/
bool elektraRecordRemoveKeys (KDB * handle, KeySet * toRemove, bool recursive, Key * errorKey)
{
if (handle == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for KDB handle");
return false;
}
if (toRemove == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for keys to remove");
return false;
}
if (ksGetSize (toRemove) == 0)
{
// Nothing to do
return true;
}
Key * sessionRecordingKey = keyNew (ELEKTRA_RECORD_SESSION_KEY, KEY_END);
KeySet * recordStorage = ksNew (0, KS_END);
// Load data from session diff
if (kdbGet (handle, recordStorage, sessionRecordingKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, sessionRecordingKey);
keyDel (sessionRecordingKey);
ksDel (recordStorage);
return false;
}
ElektraDiff * sessionDiff = getDiffFromSessionStorage (recordStorage, NULL);
for (elektraCursor it = 0; it < ksGetSize (toRemove); it++)
{
Key * k = ksAtCursor (toRemove, it);
if (recursive == false)
{
elektraDiffRemoveKey (sessionDiff, k);
}
else
{
elektraDiffRemoveSameOrBelow (sessionDiff, k);
}
}
putDiffIntoSessionStorage (recordStorage, sessionDiff);
bool successful = true;
// store data for session diff
if (kdbSet (handle, recordStorage, sessionRecordingKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, sessionRecordingKey);
successful = false;
}
elektraDiffDel (sessionDiff);
ksDel (recordStorage);
keyDel (sessionRecordingKey);
return successful;
}
bool elektraRecordGetDiff (KDB * handle, ElektraDiff ** diff, Key * errorKey)
{
if (handle == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for KDB handle");
return false;
}
if (diff == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for diff");
return false;
}
Key * sessionRecordingKey = keyNew (ELEKTRA_RECORD_SESSION_KEY, KEY_END);
KeySet * recordStorage = ksNew (0, KS_END);
// Load data from session diff
if (kdbGet (handle, recordStorage, sessionRecordingKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, sessionRecordingKey);
keyDel (sessionRecordingKey);
ksDel (recordStorage);
return false;
}
ElektraDiff * sessionDiff = getDiffFromSessionStorage (recordStorage, NULL);
*diff = sessionDiff;
keyDel (sessionRecordingKey);
ksDel (recordStorage);
return true;
}
static KeySet * buildExportKeySet (ElektraDiff * diff)
{
KeySet * ks = ksNew (0, KS_END);
KeySet * addedKeys = elektraDiffGetAddedKeys (diff);
ksAppend (ks, addedKeys);
ksDel (addedKeys);
KeySet * modifiedNewKeys = elektraDiffGetModifiedNewKeys (diff);
ksAppend (ks, modifiedNewKeys);
ksDel (modifiedNewKeys);
KeySet * removedKeys = elektraDiffGetRemovedKeys (diff);
for (elektraCursor i = 0; i < ksGetSize (removedKeys); i++)
{
Key * k = ksAtCursor (removedKeys, i);
keySetMeta (k, "meta:/elektra/removed", "1");
}
ksAppend (ks, removedKeys);
ksDel (removedKeys);
return ks;
}
bool elektraRecordExportSession (KDB * handle, Plugin * plugin, Key * parentKey, Key * errorKey)
{
if (handle == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for KDB handle");
return false;
}
if (plugin == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL pointer passed for plugin");
return false;
}
Key * sessionKey = keyNew (ELEKTRA_RECORD_SESSION_KEY, KEY_END);
KeySet * sessionStorage = ksNew (0, KS_END);
if (kdbGet (handle, sessionStorage, sessionKey) == -1)
{
elektraCopyErrorAndWarnings (errorKey, sessionKey);
keyDel (sessionKey);
ksDel (sessionStorage);
return false;
}
bool includeRecordingSession = keyGetMeta (parentKey, "meta:/export/includeRecordingSession") != NULL;
KeySet * sessionStorageBackup = NULL;
if (includeRecordingSession)
{
// We need to duplicate the ks, as `getDiffFromSessionStorage` will remove it from sessionStorage
sessionStorageBackup = ksDup (ksBelow (sessionStorage, sessionKey));
// Remove all keys from session storage that are not initiall under parent key
ElektraDiff * d = getDiffFromSessionStorage (sessionStorageBackup, parentKey);
elektraDiffRemoveOther (d, parentKey);
KeySet * tmp = ksNew (0, KS_END);
putDiffIntoSessionStorage (tmp, d);
ksDel (sessionStorageBackup);
sessionStorageBackup = tmp;
}
ElektraDiff * sessionDiff = getDiffFromSessionStorage (sessionStorage, parentKey);
elektraDiffRemoveOther (sessionDiff, parentKey);
KeySet * exportKs = buildExportKeySet (sessionDiff);
if (keyGetMeta (parentKey, "meta:/export/withoutElektra"))
{
Key * cutpoint = keyNew ("system:/elektra", KEY_END);
ksDel (ksCut (exportKs, cutpoint));
keyDel (cutpoint);
}
if (includeRecordingSession)
{
ksAppend (exportKs, sessionStorageBackup);
}
int result = plugin->kdbSet (plugin, exportKs, parentKey);
bool successful = true;
if (result == ELEKTRA_PLUGIN_STATUS_ERROR)
{
elektraCopyErrorAndWarnings (errorKey, parentKey);
successful = false;
}
elektraDiffDel (sessionDiff);
keyDel (parentKey);
keyDel (sessionKey);
ksDel (sessionStorage);
if (sessionStorageBackup != NULL) ksDel (sessionStorageBackup);
ksDel (exportKs);
return successful;
}