forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.c
1442 lines (1284 loc) · 34.2 KB
/
meta.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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
*
* @brief Methods for metadata manipulation.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <kdb.h>
#include <kdbconfig.h>
#include <kdbease.h>
#include <kdbmeta.h>
#include <kdbprivate.h>
#include <kdbproposal.h>
#include <kdbtypes.h>
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
/**
* @defgroup meta Meta Data proposal+compatibility
* @brief Meta data proposal+compatibility methods.
* @ingroup proposal
*
* In versions before Elektra 0.8 only limited metadata was
* available. Now any metadata can be added. These API methods are
* implementations of the 0.7 API using 0.8 metadata.
*
* Additionally, new suggestions can be made here.
*
* It is planned that these methods will be generated from doc/METADATA.ini
* and moved to a separate library.
* Currently, you should better avoid the methods and directly use @link keymeta metainfo @endlink
* instead.
*
* @{
*
*/
/*********************************************
* General comment manipulation methods *
*********************************************/
/**
* Return a pointer to the real internal @p key comment.
*
* This is a much more efficient version of keyGetComment() and you
* should use it if you are responsible enough to not mess up things.
* You are not allowed to change anything in the memory region the
* returned pointer points to.
*
* keyComment() returns "" when there is no keyComment. The reason is
* @code
key=keyNew(0);
keySetComment(key,"");
keyComment(key); // you would expect "" here
keyDel(key);
* @endcode
*
* See keySetComment() for more information on comments.
*
* @note Note that the Key structure keeps its own size field that is calculated
* by library internal calls, so to avoid inconsistencies, you
* must never use the pointer returned by keyComment() method to set a new
* value. Use keySetComment() instead.
*
* @param key the key object to work with
* @return a pointer to the internal managed comment
* @retval "" when there is no comment
* @retval 0 on NULL pointer
* @see keyGetCommentSize() for size and keyGetComment() as alternative
*/
const char * keyComment (const Key * key)
{
const char * comment;
if (!key) return 0;
comment = keyValue (keyGetMeta (key, "comment"));
if (!comment)
{
/*errno=KDB_ERR_NOKEY;*/
return "";
}
return comment;
}
/**
* Calculates number of bytes needed to store a key comment, including
* final NULL.
*
* Use this method to know to size for allocated memory to retrieve
* a key comment.
*
* See keySetComment() for more information on comments.
*
* For an empty key name you need one byte to store the ending NULL.
* For that reason 1 is returned.
*
* @code
char *buffer;
buffer = elektraMalloc (keyGetCommentSize (key));
// use this buffer to store the comment
// pass keyGetCommentSize (key) for maxSize
* @endcode
*
* @param key the key object to work with
* @return number of bytes needed
* @retval 1 if there is no comment
* @retval -1 on NULL pointer
* @see keyGetComment(), keySetComment()
*/
ssize_t keyGetCommentSize (const Key * key)
{
ssize_t size;
if (!key) return -1;
size = keyGetValueSize (keyGetMeta (key, "comment"));
if (!size || size == -1)
{
/*errno=KDB_ERR_NODESC;*/
return 1;
}
return size;
}
/**
* Get the key comment.
*
* @section comment Comments
*
* A Key comment is description for humans what this key is for. It may be a
* textual explanation of valid values, when and why a user or administrator
* changed the key or any other text that helps the user or administrator related
* to that key.
*
* Don't depend on a comment in your program. A user is
* always allowed to remove or change it in any way he wants to. But you are
* allowed or even encouraged to always show the content of the comment
* to the user and allow him to change it.
*
* @param key the key object to work with
* @param returnedComment pre-allocated memory to copy the comments to
* @param maxSize number of bytes that will fit returnedComment
* @return the number of bytes actually copied to @p returnedString, including
* final NULL
* @retval 1 if the string is empty
* @retval -1 on NULL pointer
* @retval -1 if maxSize is 0, not enough to store the comment or when larger then SSIZE_MAX
* @see keyGetCommentSize(), keySetComment()
*/
ssize_t keyGetComment (const Key * key, char * returnedComment, size_t maxSize)
{
const char * comment;
size_t commentSize;
if (!key) return -1;
if (!maxSize) return -1;
if (!returnedComment) return -1;
if (maxSize > SSIZE_MAX) return -1;
comment = keyValue (keyGetMeta (key, "comment"));
commentSize = keyGetValueSize (keyGetMeta (key, "comment"));
if (!comment)
{
/*errno=KDB_ERR_NODESC;*/
returnedComment[0] = 0;
return 1;
}
strncpy (returnedComment, comment, maxSize);
if (maxSize < commentSize)
{
/*errno=KDB_ERR_TRUNC;*/
return -1;
}
return commentSize;
}
/**
* Set a comment for a key.
*
* A key comment is like a configuration file comment.
* See keySetComment() for more information.
*
* @param key the key object to work with
* @param newComment the comment, that can be freed after this call.
* @return the number of bytes actually saved including final NULL
* @retval 0 when the comment was freed (newComment NULL or empty string)
* @retval -1 on NULL pointer or memory problems
* @see keyGetComment()
*/
ssize_t keySetComment (Key * key, const char * newComment)
{
if (!key) return -1;
if (!newComment || *newComment == 0)
{
keySetMeta (key, "comment", 0);
return 1;
}
keySetMeta (key, "comment", newComment);
return keyGetCommentSize (key);
}
#ifndef _WIN32
/*********************************************
* UID, GID access methods *
*********************************************/
/**
* Get the user ID of a key.
*
* @deprecated This API is obsolete.
*
* @section UID UID
*
* The user ID is a unique identification for every user present on a
* system. Keys will belong to root (0) as long as you did not get their
* real UID with kdbGet().
*
* Although usually the same, the UID of a key is not related to its owner.
*
* A fresh key will have no UID.
*
* @param key the key object to work with
* @return the system's UID of the key
* @retval (uid_t)-1 on NULL key
* @see keyGetGID(), keySetUID(), keyGetOwner()
*/
uid_t keyGetUID (const Key * key)
{
const char * uid;
long int val;
char * endptr;
int errorval = errno;
if (!key) return (uid_t) -1;
uid = keyValue (keyGetMeta (key, "uid"));
if (!uid) return (uid_t) -1;
if (*uid == '\0') return (uid_t) -1;
/*From now on we have to leave using cleanup*/
errno = 0;
val = strtol (uid, &endptr, 10);
/*Check for errors*/
if (errno) goto cleanup;
/*Check if nothing was found*/
if (endptr == uid) goto cleanup;
/*Check if the whole string was processed*/
if (*endptr != '\0') goto cleanup;
return val;
cleanup:
/*First restore errno*/
errno = errorval;
return (uid_t) -1;
}
/**
* Set the user ID of a key.
*
* @deprecated This API is obsolete.
*
* See @ref UID for more information about user IDs.
*
* @param key the key object to work with
* @param uid the user ID to set
* @retval 0 on success
* @retval -1 on NULL key or conversion error
* @see keySetGID(), keyGetUID(), keyGetOwner()
*/
int keySetUID (Key * key, uid_t uid)
{
char str[MAX_LEN_INT];
if (!key) return -1;
if (snprintf (str, MAX_LEN_INT - 1, "%d", uid) < 0)
{
return -1;
}
keySetMeta (key, "uid", str);
return 0;
}
/**
* Get the group ID of a key.
*
* @deprecated This API is obsolete.
*
* @section GID GID
*
* The group ID is a unique identification for every group present on
* a system. Keys will belong to root (0) as long as you did not get their
* real GID with kdbGet().
*
* Unlike UID users might change their group. This makes it possible to
* share configuration between some users.
*
* A fresh key will have (gid_t)-1 also known as the group nogroup.
* It means that the key is not related to a group ID at the moment.
*
* @param key the key object to work with
* @return the system's GID of the key
* @retval (gid_t)-1 on NULL key or currently unknown ID
* @see keySetGID(), keyGetUID()
*/
gid_t keyGetGID (const Key * key)
{
const char * gid;
long int val;
char * endptr;
int errorval = errno;
if (!key) return (gid_t) -1;
gid = keyValue (keyGetMeta (key, "gid"));
if (!gid) return (gid_t) -1;
if (*gid == '\0') return (gid_t) -1;
/*From now on we have to leave using cleanup*/
errno = 0;
val = strtol (gid, &endptr, 10);
/*Check for errors*/
if (errno) goto cleanup;
/*Check if nothing was found*/
if (endptr == gid) goto cleanup;
/*Check if the whole string was processed*/
if (*endptr != '\0') goto cleanup;
return val;
cleanup:
/*First restore errno*/
errno = errorval;
return (gid_t) -1;
}
/**
* Set the group ID of a key.
*
* @deprecated This API is obsolete.
*
* See @ref GID for more information about group IDs.
*
* @param key the key object to work with
* @param gid is the group ID
* @retval 0 on success
* @retval -1 on NULL key
* @see keyGetGID(), keySetUID()
*/
int keySetGID (Key * key, gid_t gid)
{
char str[MAX_LEN_INT];
if (!key) return -1;
if (snprintf (str, MAX_LEN_INT - 1, "%d", gid) < 0)
{
return -1;
}
keySetMeta (key, "gid", str);
return 0;
}
/**
* Set mode so that key will be recognized as directory.
*
* @deprecated This API is obsolete.
*
* The function will add all executable bits.
*
* - Mode 0200 will be translated to 0311
* - Mode 0400 will be translated to 0711
* - Mode 0664 will be translated to 0775
*
* The macro KDB_DIR_MODE (defined to 0111) will be used for that.
*
* The executable bits show that child keys are allowed and listable. There
* is no way to have child keys which are not listable for anyone, but it is
* possible to restrict listing the keys to the owner only.
*
* - Mode 0000 means that it is a key not read or writable to anyone.
* - Mode 0111 means that it is a directory not read or writable to anyone.
* But it is recognized as directory to anyone.
*
* For more about mode see keySetMode().
*
* It is not possible to access keys below a not executable key.
* If a key is not writeable and executable kdbSet() will fail to access the
* keys below.
* If a key is not readable and executable kdbGet() will fail to access the
* keys below.
*
* @param key the key to set permissions to be recognized as directory.
* @retval 0 on success
* @retval -1 on NULL pointer
* @see keySetMode()
*/
int keySetDir (Key * key)
{
mode_t mode;
if (!key) return -1;
mode = keyGetMode (key);
mode |= KDB_DIR_MODE;
keySetMode (key, mode);
return 0;
}
/**
* Return the key mode permissions.
*
* @deprecated This API is obsolete.
*
* Default is 0664 (octal) for keys and 0775 for directory keys
* which used keySetDir().
*
* The defaults are defined with the macros KDB_FILE_MODE and KDB_DIR_MODE.
*
* For more information about the mode permissions see @ref mode.
*
* @param key the key object to work with
* @return mode permissions of the key
* @retval KDB_FILE_MODE as defaults
* @retval (mode_t)-1 on NULL pointer
* @see keySetMode()
*/
mode_t keyGetMode (const Key * key)
{
const char * mode;
long int val;
char * endptr;
int errorval = errno;
if (!key) return (mode_t) -1;
mode = keyValue (keyGetMeta (key, "mode"));
if (!mode) return KDB_FILE_MODE;
if (*mode == '\0') return KDB_FILE_MODE;
/*From now on we have to leave using cleanup*/
errno = 0;
val = strtol (mode, &endptr, 8);
/*Check for errors*/
if (errno) goto cleanup;
/*Check if nothing was found*/
if (endptr == mode) goto cleanup;
/*Check if the whole string was processed*/
if (*endptr != '\0') goto cleanup;
return val;
cleanup:
/*First restore errno*/
errno = errorval;
return KDB_FILE_MODE;
}
/**
* Set the key mode permissions.
*
* @deprecated This API is obsolete.
* It is only a mapping
* to keySetMeta(key, "mode", str) which should be preferred.
*
* The mode consists of 9 individual bits for mode permissions.
* In the following explanation the octal notation with leading
* zero will be used.
*
* Default is 0664 (octal) for keys and 0775 for directory keys
* which used keySetDir().
*
* The defaults are defined with the macros KDB_FILE_MODE and KDB_DIR_MODE.
*
* @note libelektra 0.7.0 only allows 0775 (directory keys) and
* 0664 (other keys). More will be added later in a sense of the
* description below.
*
* @section mode Modes
*
* 0000 is the most restrictive mode. No user might read, write
* or execute the key.
*
* Reading the key means to get the value by kdbGet().
*
* Writing the key means to set the value by kdbSet().
*
* Execute the key means to make a step deeper in the hierarchy.
* But you must be able to read the key to be able to list the
* keys below. See also keySetDir() in that context.
* But you must be able to write the key to be able to add or
* remove keys below.
*
* 0777 is the most relaxing mode. Every user is allowed to
* read, write and execute the key, if he is allowed to execute
* and read all keys below.
*
* 0700 allows every action for the current user, identified by
* the uid. See keyGetUID() and keySetUID().
*
* To be more specific for the user the single bits can elect
* the mode for read, write and execute. 0100 only allows
* executing which gives the information that it is a directory
* for that user, but not accessible. 0200 only allows reading.
* This information may be combined to 0300, which allows execute
* and reading of the directory. Last 0400 decides about the
* writing permissions.
*
* The same as above is also valid for the 2 other octal digits.
* 0070 decides about the group permissions, in that case full
* access. Groups are identified by the gid. See keyGetGID() and
* keySetGID(). In that example everyone with a different uid,
* but the gid of the the key, has full access.
*
* 0007 decides about the world permissions. This is taken into
* account when neither the uid nor the gid matches. So that
* example would allow everyone with a different uid and gid
* of that key gains full access.
*
* @param key the key to set mode permissions
* @param mode the mode permissions
* @retval 0 on success
* @retval -1 on NULL key
* @see keyGetMode()
*/
int keySetMode (Key * key, mode_t mode)
{
char str[MAX_LEN_INT];
if (!key) return -1;
if (snprintf (str, MAX_LEN_INT - 1, "%o", mode) < 0)
{
return -1;
}
keySetMeta (key, "mode", str);
return 0;
}
/*********************************************
* Access times methods *
*********************************************/
/**
* Get last time the key data was read from disk.
*
* @deprecated This API is obsolete.
*
* Every kdbGet() might update the access time
* of a key. You get information when the key
* was read the last time from the database.
*
* You will get 0 when the key was not read already.
*
* Beware that multiple copies of keys with keyDup() might have different
* atimes because you kdbGet() one, but not the
* other. You can use this information to decide which
* key is the latest.
*
* @param key Key to get information from.
* @return the time you got the key with kdbGet()
* @retval 0 on key that was never kdbGet()
* @retval (time_t)-1 on NULL pointer
* @see keySetATime()
* @see kdbGet()
*/
time_t keyGetATime (const Key * key)
{
const char * atime;
long int val;
char * endptr;
int errorval = errno;
if (!key) return (time_t) -1;
atime = keyValue (keyGetMeta (key, "atime"));
if (!atime) return 0;
if (*atime == '\0') return (time_t) -1;
/*From now on we have to leave using cleanup*/
errno = 0;
val = strtol (atime, &endptr, 10);
/*Check for errors*/
if (errno) goto cleanup;
/*Check if nothing was found*/
if (endptr == atime) goto cleanup;
/*Check if the whole string was processed*/
if (*endptr != '\0') goto cleanup;
return val;
cleanup:
/*First restore errno*/
errno = errorval;
return (time_t) -1;
}
/**
* Update the atime information for a key.
*
* @deprecated This API is obsolete.
*
* When you do manual sync of keys you might also
* update the atime to make them indistinguishable.
*
* It can also be useful if you work with
* keys not using a keydatabase.
*
* @param key The Key object to work with
* @param atime The new access time for the key
* @retval 0 on success
* @retval -1 on NULL pointer
* @see keyGetATime()
*/
int keySetATime (Key * key, time_t atime)
{
char str[MAX_LEN_INT];
if (!key) return -1;
if (snprintf (str, MAX_LEN_INT - 1, "%lu", atime) < 0)
{
return -1;
}
keySetMeta (key, "atime", str);
return 0;
}
/**
* Get last modification time of the key on disk.
*
* @deprecated This API is obsolete.
*
* You will get 0 when the key was not read already.
*
* Everytime you change value or comment and kdbSet()
* the key the mtime will be updated. When you kdbGet()
* the key, the atime is set appropriate.
*
* Not changed keys may not even passed to kdbSet_backend()
* so it will not update this time, even after kdbSet().
*
* It is possible that other keys written to disc
* influence this time if the backend is not grained
* enough.
*
* If you add or remove a key the key thereunder
* in the hierarchy will update the mtime
* if written with kdbSet() to disc.
*
* @param key Key to get information from.
* @see keySetMTime()
* @return the last modification time
* @retval (time_t)-1 on NULL pointer
*/
time_t keyGetMTime (const Key * key)
{
const char * mtime;
long int val;
char * endptr;
int errorval = errno;
if (!key) return (time_t) -1;
mtime = keyValue (keyGetMeta (key, "mtime"));
if (!mtime) return 0;
if (*mtime == '\0') return (time_t) -1;
/*From now on we have to leave using cleanup*/
errno = 0;
val = strtol (mtime, &endptr, 10);
/*Check for errors*/
if (errno) goto cleanup;
/*Check if nothing was found*/
if (endptr == mtime) goto cleanup;
/*Check if the whole string was processed*/
if (*endptr != '\0') goto cleanup;
return val;
cleanup:
/*First restore errno*/
errno = errorval;
return (time_t) -1;
}
/**
* Update the mtime information for a key.
*
* @deprecated This API is obsolete.
*
* @param key The Key object to work with
* @param mtime The new modification time for the key
* @retval 0 on success
* @see keyGetMTime()
*/
int keySetMTime (Key * key, time_t mtime)
{
char str[MAX_LEN_INT];
if (!key) return -1;
if (snprintf (str, MAX_LEN_INT - 1, "%lu", mtime) < 0)
{
return -1;
}
keySetMeta (key, "mtime", str);
return 0;
}
/**
* Get last time the key metadata was changed from disk.
*
* @deprecated This API is obsolete.
*
* You will get 0 when the key was not read already.
*
* Any changed field in metadata will influence the
* ctime of a key.
*
* This time is not updated if only value
* or comment are changed.
*
* Not changed keys will not update this time,
* even after kdbSet().
*
* It is possible that other keys written to disc
* influence this time if the backend is not grained
* enough.
*
* @param key Key to get information from.
* @see keySetCTime()
* @retval (time_t)-1 on NULL pointer
* @return the metadata change time
*/
time_t keyGetCTime (const Key * key)
{
const char * ctime;
long int val;
char * endptr;
int errorval = errno;
if (!key) return (time_t) -1;
ctime = keyValue (keyGetMeta (key, "ctime"));
if (!ctime) return 0;
if (*ctime == '\0') return (time_t) -1;
/*From now on we have to leave using cleanup*/
errno = 0;
val = strtol (ctime, &endptr, 10);
/*Check for errors*/
if (errno) goto cleanup;
/*Check if nothing was found*/
if (endptr == ctime) goto cleanup;
/*Check if the whole string was processed*/
if (*endptr != '\0') goto cleanup;
return val;
cleanup:
/*First restore errno*/
errno = errorval;
return (time_t) -1;
}
/**
* Update the ctime information for a key.
*
* @deprecated This API is obsolete.
*
* @param key The Key object to work with
* @param ctime The new change metadata time for the key
* @retval 0 on success
* @retval -1 on NULL pointer
* @see keyGetCTime()
*/
int keySetCTime (Key * key, time_t ctime)
{
char str[MAX_LEN_INT];
if (!key) return -1;
if (snprintf (str, MAX_LEN_INT - 1, "%lu", ctime) < 0)
{
return -1;
}
keySetMeta (key, "ctime", str);
return 0;
}
#endif
/**
* Compare the order metadata of two keys.
*
* @return a number less than, equal to or greater than zero if
* the order of k1 is found, respectively, to be less than,
* to match, or be greater than the order of k2. If one key is
* NULL, but the other isn't, the key which is not NULL is considered
* to be greater. If both keys are NULL, they are
* considered to be equal. If one key does have an order
* metadata but the other has not, the key with the metadata
* is considered greater. If no key has metadata,
* they are considered to be equal.
*
* @param ka key to compare with
* @param kb other key to compare with
*/
int elektraKeyCmpOrder (const Key * ka, const Key * kb)
{
if (!ka && !kb) return 0;
if (ka && !kb) return 1;
if (!ka && kb) return -1;
int aorder = -1;
int border = -1;
const Key * kam = keyGetMeta (ka, "order");
const Key * kbm = keyGetMeta (kb, "order");
if (kam) aorder = atoi (keyString (kam));
if (kbm) border = atoi (keyString (kbm));
if (aorder > 0 && border > 0) return aorder - border;
if (aorder < 0 && border < 0) return 0;
if (aorder < 0 && border >= 0) return -1;
if (aorder >= 0 && border < 0) return 1;
/* cannot happen anyway */
return 0;
}
/**
* creates an metadata array or appends another element to an existing metadata array
* e.g.
* Key *key = keyNew("user/test", KEY_END);
* elektraMetaArrayAdd(key, "test", "val0");
* key now has "test/#0" with value "val0" as metadata
* elektraMetaArrayAdd(key, "test", "val1");
* appends "test/#1" with value "val1" to key
*
* @param key the key the metadata should be added to
* @param metaName the name of the metakey array parent
* @param value the value of the newly appended metakey
*/
void elektraMetaArrayAdd (Key * key, const char * metaName, const char * value)
{
const Key * meta = keyGetMeta (key, metaName);
Key * arrayKey;
if (!meta)
{
keySetMeta (key, metaName, "#0");
arrayKey = keyDup (keyGetMeta (key, metaName));
keySetString (arrayKey, 0);
keyAddBaseName (arrayKey, "#");
}
else
{
arrayKey = keyDup (meta);
keyAddBaseName (arrayKey, keyString (meta));
}
elektraArrayIncName (arrayKey);
keySetMeta (key, keyName (arrayKey), value);
keySetMeta (key, metaName, keyBaseName (arrayKey));
keyDel (arrayKey);
}
/**
* Create a `KeySet` from a metakey array.
*
* For example, the following function call
*
* @code
elektraMetaArrayToKS(
keyNew ("/a", KEY_VALUE, "b, c",
KEY_META, "dep", "#1",
KEY_META, "dep/#0", "/b",
KEY_META, "dep/#1", "/c", KEY_END),
"dep");
* @endcode
*
* returns a `KeySet` containing the keys `dep` with value `#1`, `"dep/#0"` with value `"/b"` and
* `"dep/#1"` with value `"/c"`.
*
* If no meta key array is found, null is returned.
* The returned `KeySet` must be freed with `ksDel`
*
* @returns a keyset containing all the metakeys of the metakey array
* @param key the key containing the metakey array
* @param metaName the name of the metakey array parent
*/
KeySet * elektraMetaArrayToKS (const Key * key, const char * metaName)
{
const Key * meta = keyGetMeta (key, metaName);
if (!meta) return NULL;
KeySet * result = ksNew (0, KS_END);
if (keyString (meta)[0] != '#')
{
ksAppendKey (result, (Key *) meta);
ksRewind (result);
return result;
}
ksAppendKey (result, keyDup (meta));
Key * currentKey = keyDup (meta);
keyAddName (currentKey, "#");
elektraArrayIncName (currentKey);
Key * curMeta = NULL;
while ((curMeta = (Key *) keyGetMeta (key, keyName (currentKey))) != NULL)
{
ksAppendKey (result, keyDup (curMeta));
elektraArrayIncName (currentKey);
}
keyDel (currentKey);
ksRewind (result);
return result;
}
/**
* @internal
*
* elektraSortTopology helper
* matrix struct
*/
typedef struct
{