forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.cpp
301 lines (253 loc) · 7.64 KB
/
dump.cpp
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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "dump.hpp"
using namespace ckdb;
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <unistd.h>
#include <kdberrors.h>
#include <kdblogger.h>
namespace dump
{
int serialise (std::ostream & os, ckdb::Key *, ckdb::KeySet * ks)
{
ckdb::Key * cur;
os << "kdbOpen 1" << std::endl;
os << "ksNew " << ckdb::ksGetSize (ks) << std::endl;
ckdb::KeySet * metacopies = ckdb::ksNew (0, KS_END);
ksRewind (ks);
while ((cur = ksNext (ks)) != nullptr)
{
size_t namesize = ckdb::keyGetNameSize (cur);
size_t valuesize = ckdb::keyGetValueSize (cur);
os << "keyNew " << namesize << " " << valuesize << std::endl;
os.write (ckdb::keyName (cur), namesize);
os.write (static_cast<const char *> (ckdb::keyValue (cur)), valuesize);
os << std::endl;
const ckdb::Key * meta;
ckdb::keyRewindMeta (cur);
while ((meta = ckdb::keyNextMeta (cur)) != nullptr)
{
std::stringstream ss;
ss << "user/" << meta; // use the address of pointer as name
ckdb::Key * search = ckdb::keyNew (ss.str ().c_str (), KEY_END);
ckdb::Key * ret = ksLookup (metacopies, search, 0);
if (!ret)
{
/* This metakey was not serialised up to now */
size_t metanamesize = ckdb::keyGetNameSize (meta);
size_t metavaluesize = ckdb::keyGetValueSize (meta);
os << "keyMeta " << metanamesize << " " << metavaluesize << std::endl;
os.write (ckdb::keyName (meta), metanamesize);
os.write (static_cast<const char *> (ckdb::keyValue (meta)), metavaluesize);
os << std::endl;
std::stringstream ssv;
ssv << namesize << " " << metanamesize << std::endl;
ssv.write (ckdb::keyName (cur), namesize);
ssv.write (ckdb::keyName (meta), metanamesize);
ckdb::keySetRaw (search, ssv.str ().c_str (), ssv.str ().size ());
ksAppendKey (metacopies, search);
}
else
{
/* Meta key already serialised, write out a reference to it */
keyDel (search);
os << "keyCopyMeta ";
os.write (static_cast<const char *> (ckdb::keyValue (ret)), ckdb::keyGetValueSize (ret));
os << std::endl;
}
}
os << "keyEnd" << std::endl;
}
os << "ksEnd" << std::endl;
ksDel (metacopies);
return 1;
}
int unserialise (std::istream & is, ckdb::Key * errorKey, ckdb::KeySet * ks)
{
ckdb::Key * cur = nullptr;
std::vector<char> namebuffer (4048);
std::vector<char> valuebuffer (4048);
std::string line;
std::string command;
size_t nrKeys;
size_t namesize;
size_t valuesize;
while (std::getline (is, line))
{
std::stringstream ss (line);
ss >> command;
if (command == "kdbOpen")
{
std::string version;
ss >> version;
if (version != "1")
{
ELEKTRA_SET_ERROR (50, errorKey, version.c_str ());
return -1;
}
}
else if (command == "ksNew")
{
ss >> nrKeys;
ksClear (ks);
}
else if (command == "keyNew")
{
cur = ckdb::keyNew (nullptr);
ss >> namesize;
ss >> valuesize;
if (namesize > namebuffer.size ()) namebuffer.resize (namesize + 1);
is.read (&namebuffer[0], namesize);
namebuffer[namesize] = 0;
ckdb::keySetName (cur, &namebuffer[0]);
if (valuesize > valuebuffer.size ()) valuebuffer.resize (valuesize + 1);
is.read (&valuebuffer[0], valuesize);
valuebuffer[valuesize] = 0;
ckdb::keySetRaw (cur, &valuebuffer[0], valuesize);
std::getline (is, line);
}
else if (command == "keyMeta")
{
ss >> namesize;
ss >> valuesize;
if (namesize > namebuffer.size ()) namebuffer.resize (namesize + 1);
is.read (&namebuffer[0], namesize);
namebuffer[namesize] = 0;
if (valuesize > valuebuffer.size ()) valuebuffer.resize (valuesize + 1);
is.read (&valuebuffer[0], valuesize);
valuebuffer[valuesize] = 0;
keySetMeta (cur, &namebuffer[0], &valuebuffer[0]);
std::getline (is, line);
}
else if (command == "keyCopyMeta")
{
ss >> namesize;
ss >> valuesize;
if (namesize > namebuffer.size ()) namebuffer.resize (namesize + 1);
is.read (&namebuffer[0], namesize);
namebuffer[namesize] = 0;
if (valuesize > valuebuffer.size ()) valuebuffer.resize (valuesize + 1);
is.read (&valuebuffer[0], valuesize);
valuebuffer[valuesize] = 0;
ckdb::Key * search = ckdb::ksLookupByName (ks, &namebuffer[0], 0);
ckdb::keyCopyMeta (cur, search, &valuebuffer[0]);
std::getline (is, line);
}
else if (command == "keyEnd")
{
ckdb::ksAppendKey (ks, cur);
cur = nullptr;
}
else if (command == "ksEnd")
{
break;
}
else
{
ELEKTRA_SET_ERROR (49, errorKey, command.c_str ());
return -1;
}
}
return 1;
}
class pipebuf : public std::streambuf
{
char * buffer_;
int fd_;
public:
pipebuf (int fd) : buffer_ (new char[4096]), fd_ (fd)
{
}
~pipebuf ()
{
delete[] this->buffer_;
}
int underflow ()
{
if (this->gptr () == this->egptr ())
{
// read from the pipe directly into the buffer
ssize_t r = read (fd_, buffer_, 4096);
this->setg (this->buffer_, this->buffer_, this->buffer_ + r);
}
return this->gptr () == this->egptr () ? std::char_traits<char>::eof () :
std::char_traits<char>::to_int_type (*this->gptr ());
}
};
} // namespace dump
extern "C" {
int elektraDumpGet (ckdb::Plugin *, ckdb::KeySet * returned, ckdb::Key * parentKey)
{
Key * root = ckdb::keyNew ("system/elektra/modules/dump", KEY_END);
if (keyRel (root, parentKey) >= 0)
{
keyDel (root);
KeySet * n = ksNew (50, keyNew ("system/elektra/modules/dump", KEY_VALUE, "dump plugin waits for your orders", KEY_END),
keyNew ("system/elektra/modules/dump/exports", KEY_END),
keyNew ("system/elektra/modules/dump/exports/get", KEY_FUNC, elektraDumpGet, KEY_END),
keyNew ("system/elektra/modules/dump/exports/set", KEY_FUNC, elektraDumpSet, KEY_END),
keyNew ("system/elektra/modules/dump/exports/serialise", KEY_FUNC, dump::serialise, KEY_END),
keyNew ("system/elektra/modules/dump/exports/unserialise", KEY_FUNC, dump::unserialise, KEY_END),
keyNew ("system/elektra/modules/dump/config/needs/fcrypt/textmode", KEY_VALUE, "0", KEY_END),
#include "readme_dump.c"
keyNew ("system/elektra/modules/dump/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, n);
ksDel (n);
return 1;
}
keyDel (root);
int errnosave = errno;
// We use dump for the pluginprocess library. Unfortunately on macOS reading from /dev/fd/<fd> via
// ifstream fails, thus we read directly from unnamed pipes using a custom buffer and read
const char pipe[] = "/dev/fd/";
if (!strncmp (keyString (parentKey), pipe, strlen (pipe)))
{
int fd = std::stoi (std::string (keyString (parentKey) + strlen (pipe)));
dump::pipebuf pipebuf (fd);
std::istream is (&pipebuf);
return dump::unserialise (is, parentKey, returned);
}
else
{
// ELEKTRA_LOG (ELEKTRA_LOG_MODULE_DUMP, "opening file %s", keyString (parentKey));
std::ifstream is (keyString (parentKey), std::ios::binary);
if (!is.is_open ())
{
ELEKTRA_SET_ERROR_GET (parentKey);
errno = errnosave;
return -1;
}
return dump::unserialise (is, parentKey, returned);
}
}
int elektraDumpSet (ckdb::Plugin *, ckdb::KeySet * returned, ckdb::Key * parentKey)
{
int errnosave = errno;
// ELEKTRA_LOG (ELEKTRA_LOG_MODULE_DUMP, "opening file %s", keyString (parentKey));
std::ofstream ofs (keyString (parentKey), std::ios::binary);
if (!ofs.is_open ())
{
ELEKTRA_SET_ERROR_SET (parentKey);
errno = errnosave;
return -1;
}
return dump::serialise (ofs, parentKey, returned);
}
ckdb::Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("dump",
ELEKTRA_PLUGIN_GET, &elektraDumpGet,
ELEKTRA_PLUGIN_SET, &elektraDumpSet,
ELEKTRA_PLUGIN_END);
}
} // extern C