Git init
[pkgs/e/elektra.git] / src / backends / daemon / kdb_wrapper.c
1 /***************************************************************************
2                    kdb_wrapper.c  -  The server for the daemon backend
3                              -------------------
4     copyright            : (C) 2006 by Yannick Lecaillez
5     email                : sizon5@gmail.com
6  ***************************************************************************/
7
8 /***************************************************************************
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the BSD License (revised).                      *
12  *                                                                         *
13  ***************************************************************************/
14
15
16 /* Subversion stuff
17
18 $Id$
19
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h> /* malloc */
32 #endif
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
35 #endif
36 #ifdef HAVE_PWD_H
37 #include <pwd.h>
38 #endif
39
40 #include "kdbbackend.h"
41
42 #include "datatype.h"
43 #include "message.h"
44
45
46 Message *wrapper_kdbStatKey(KDB *handle, Message *request)
47 {
48         Key             key;
49         int             error, ret;
50         Message         *reply;
51         
52         keyInit(&key);
53         if ( messageExtractArgs(request,
54                         DATATYPE_KEY, &key,
55                         DATATYPE_LAST) ) {
56                 keyClose(&key);
57                 return NULL;
58         }
59         
60         ret = kdbStatKey(handle, &key);
61         error = errno;
62         
63         reply = messageNew(MESSAGE_REPLY, KDB_BE_STATKEY,
64                         DATATYPE_INTEGER, &ret,
65                         DATATYPE_INTEGER, &error,
66                         DATATYPE_KEY, &key,
67                         DATATYPE_LAST);
68         
69         keyClose(&key);
70         
71         return reply;
72 }
73
74 Message *wrapper_kdbGetKey(KDB *handle, Message *request)
75 {
76         Key             key;
77         int             error, ret;
78         Message         *reply;
79
80         keyInit(&key);
81         error = 0;
82         
83         if ( messageExtractArgs(request,
84                                 DATATYPE_KEY, &key,
85                                 DATATYPE_LAST) ) {
86                 keyClose(&key);
87                 return NULL;
88         }
89
90         ret = kdbGetKey(handle, &key);
91         error = errno;
92
93         reply = messageNew(MESSAGE_REPLY, KDB_BE_GETKEY,
94                         DATATYPE_INTEGER, &ret,
95                         DATATYPE_INTEGER, &error,
96                         DATATYPE_KEY, &key,
97                         DATATYPE_LAST);
98
99         keyClose(&key);
100         
101         return reply;
102 }
103
104 Message *wrapper_kdbSetKey(KDB *handle, void *request)
105 {
106         Key             key;
107         int             error, ret;
108         Message         *reply;
109         
110         keyInit(&key);
111         error = 0;
112         
113         if ( messageExtractArgs(request,
114                         DATATYPE_KEY, &key,
115                         DATATYPE_LAST) ) {
116                 keyClose(&key);
117                 return NULL;
118         }
119
120         ret = kdbSetKey(handle, &key);
121         error = errno;
122         
123         reply = messageNew(MESSAGE_REPLY, KDB_BE_SETKEY,
124                         DATATYPE_INTEGER, &ret,
125                         DATATYPE_INTEGER, &error,
126                         DATATYPE_KEY, &key,
127                         DATATYPE_LAST);
128         
129         keyClose(&key);
130         
131         return reply;
132 }
133
134 Message *wrapper_kdbSetKeys(KDB *handle, void *request)
135 {
136         KeySet          *ks;
137         int             error, ret;
138         Message         *reply;
139         
140         if ( (ks = ksNew()) == NULL )
141                 return NULL;
142         
143         if ( messageExtractArgs(request, 
144                         DATATYPE_KEYSET, ks,
145                         DATATYPE_LAST) ) {
146                 ksDel(ks);
147                 return NULL;
148         }
149
150         ret = kdbSetKeys(handle, ks);
151         error = errno;
152         reply = messageNew(MESSAGE_REPLY, KDB_BE_SETKEYS,
153                         DATATYPE_INTEGER, &ret,
154                         DATATYPE_INTEGER, &error,
155                         DATATYPE_KEYSET, ks,
156                         DATATYPE_LAST);
157
158         ksDel(ks);
159
160         return reply;
161 }
162
163 Message *wrapper_kdbGetChild(KDB *handle, Message *request)
164 {
165         Key             parentKey;
166         KeySet          *ks;
167         int             error, ret;
168         unsigned long   options;
169         Message         *reply;
170         
171         keyInit(&parentKey);
172
173         if ( messageExtractArgs(request,
174                         DATATYPE_KEY, &parentKey,
175                         DATATYPE_ULONG, &options,
176                         DATATYPE_LAST) ) {
177                 keyClose(&parentKey);
178                 return NULL;
179         }
180         
181         ks = ksNew();
182         ret = kdbGetKeyChildKeys(handle, &parentKey, ks, options);
183         error = errno;
184         keyClose(&parentKey);
185         
186         reply = messageNew(MESSAGE_REPLY, KDB_BE_GETCHILD,
187                         DATATYPE_INTEGER, &ret,
188                         DATATYPE_INTEGER, &error,
189                         DATATYPE_KEYSET, ks,
190                         DATATYPE_LAST);
191         ksDel(ks);
192
193         return reply;
194 }