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 : #ifndef HAVE_KDBCONFIG
10 : #include "kdbconfig.h"
11 : #endif
12 :
13 : #include "sync.h"
14 :
15 : #include <kdberrors.h>
16 :
17 : #include <errno.h>
18 : #include <fcntl.h>
19 : #include <sys/stat.h>
20 : #include <sys/types.h>
21 : #include <unistd.h>
22 :
23 : #define ERROR_SIZE 1024
24 :
25 :
26 1536 : int elektraSyncGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
27 : {
28 1536 : if (!elektraStrCmp (keyName (parentKey), "system/elektra/modules/sync"))
29 : {
30 1536 : KeySet * contract =
31 1536 : ksNew (30, keyNew ("system/elektra/modules/sync", KEY_VALUE, "sync plugin waits for your orders", KEY_END),
32 : keyNew ("system/elektra/modules/sync/exports", KEY_END),
33 : keyNew ("system/elektra/modules/sync/exports/get", KEY_FUNC, elektraSyncGet, KEY_END),
34 : keyNew ("system/elektra/modules/sync/exports/set", KEY_FUNC, elektraSyncSet, KEY_END),
35 : #include ELEKTRA_README
36 : keyNew ("system/elektra/modules/sync/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
37 1536 : ksAppend (returned, contract);
38 1536 : ksDel (contract);
39 :
40 1536 : return 1; /* success */
41 : }
42 : /* get all keys */
43 :
44 : return 1; /* success */
45 : }
46 :
47 418 : int elektraSyncSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey)
48 : {
49 : /* set all keys */
50 418 : const char * configFile = keyString (parentKey);
51 418 : if (!strcmp (configFile, "")) return 0; // no underlying config file
52 417 : int fd = open (configFile, O_RDWR);
53 417 : if (fd == -1)
54 : {
55 0 : ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Could not open config file %s. Reason: %s", configFile, strerror (errno));
56 0 : return -1;
57 : }
58 417 : if (fsync (fd) == -1)
59 : {
60 0 : ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Could not fsync config file %s. Reason: %s", configFile, strerror (errno));
61 0 : close (fd);
62 0 : return -1;
63 : }
64 417 : close (fd);
65 :
66 417 : return 1; /* success */
67 : }
68 :
69 9232 : Plugin * ELEKTRA_PLUGIN_EXPORT
70 : {
71 : // clang-format off
72 9232 : return elektraPluginExport("sync",
73 : ELEKTRA_PLUGIN_GET, &elektraSyncGet,
74 : ELEKTRA_PLUGIN_SET, &elektraSyncSet,
75 : ELEKTRA_PLUGIN_END);
76 : }
77 :
|