-
Notifications
You must be signed in to change notification settings - Fork 0
/
nullcached.c
318 lines (266 loc) · 5.92 KB
/
nullcached.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <stdarg.h>
#include <syslog.h>
#include <stdbool.h>
static void die(const char *err, ...)
{
va_list params;
va_start(params, err);
vsyslog(LOG_USER | LOG_ERR, err, params);
va_end(params);
exit(EXIT_FAILURE);
}
static void output_error(FILE *output)
{
fprintf(output, "ERROR\r\n");
fflush(output);
}
#if 0
static void output_client_error(FILE *output, const char *error)
{
fprintf(output, "CLIENT_ERROR %s\r\n", error);
fflush(output);
}
static void output_server_error(FILE *output, const char *error)
{
fprintf(output, "SERVER_ERROR %s\r\n", error);
fflush(output);
}
#endif
static char *db_pathname = "/dev/null";
static void parse_options(int argc, char **argv)
{
if (argc >= 2)
db_pathname = argv[1];
}
static void swallow(FILE *input, unsigned long bytes)
{
char buf[BUFSIZ];
while (bytes > 0) {
size_t ret = fread(buf, 1, BUFSIZ, input);
if (!ret)
return;
bytes -= ret;
}
}
static void process_quit_command(FILE *input, FILE *output, FILE *db)
{
fclose(db);
fclose(output);
exit(0);
}
static void *value_buffer(size_t size)
{
static void *value_buffer;
static size_t value_buffer_size;
if (value_buffer_size < size) {
free(value_buffer);
value_buffer = NULL;
value_buffer_size = 0;
value_buffer = malloc(size);
if (!value_buffer)
return NULL;
value_buffer_size = size;
}
return value_buffer;
}
static ssize_t db_get(FILE *db, void *value, size_t bytes)
{
return -1;
}
static ssize_t db_put(FILE *db, void *value, size_t bytes)
{
return bytes;
}
static void process_get_command(FILE *input, FILE *output, FILE *db, char *key)
{
char buf[BUFSIZ];
size_t len;
len = db_get(db, buf, BUFSIZ);
if (len >= 0 && len < BUFSIZ) {
fwrite(buf, 1, len, output);
fprintf(output, "\r\n");
}
}
static void process_set_command(FILE *input, FILE *output, FILE *db,
char *key, unsigned long flags,
unsigned long exptime, unsigned long bytes)
{
unsigned long total_size = bytes + strlen("\r\n");
void *value;
size_t ret;
value = value_buffer(total_size);
if (!value) {
swallow(input, total_size);
goto error;
}
ret = fread(value, 1, total_size, input);
if (ret != total_size)
goto error;
if (strcmp(value + bytes, "\r\n"))
goto error;
db_put(db, value, bytes);
fprintf(output, "STORED\r\n");
fflush(output);
return;
error:
output_error(output);
}
enum {
/* Storage commands */
SET_OP,
ADD_OP,
REPLACE_OP,
APPEND_OP,
PREPEND_OP,
CAS_OP,
/* Retrieval commands */
GET_OP,
GETS_OP,
/* Deletion commands */
DELETE_OP,
/* Increment/Decrement commands */
INCR_OP,
DECR_OP,
/* Statistics commands */
STATS_OP,
/* Other commands */
FLUSH_ALL_OP,
VERSION_OP,
QUIT_OP,
};
enum {
INVALID_COMMAND = -1,
STORAGE_COMMAND,
RETRIEVAL_COMMAND,
DELETION_COMMAND,
INCR_DECR_COMMAND,
STATS_COMMAND,
OTHER_COMMAND,
};
static int command_type(const char *command)
{
if (!strcmp(command, "set") || !strcmp(command, "add") ||
!strcmp(command, "replace") || !strcmp(command, "append") ||
!strcmp(command, "prepend") || !strcmp(command, "cas"))
return STORAGE_COMMAND;
else if (!strcmp(command, "get") || !strcmp(command, "gets"))
return RETRIEVAL_COMMAND;
else if (!strcmp(command, "delete"))
return DELETION_COMMAND;
else if (!strcmp(command, "incr") || !strcmp(command, "decr"))
return INCR_DECR_COMMAND;
else if (!strcmp(command, "stats"))
return STATS_COMMAND;
else if (!strcmp(command, "flush_all") || !strcmp(command, "version") ||
!strcmp(command, "quit"))
return OTHER_COMMAND;
else
return INVALID_COMMAND;
}
static bool check_line(const char *line, size_t n)
{
return (n >= 2) && (line[n - 2] == '\r') && (line[n - 1] == '\n');
}
static void split_line(char *line, char **command, char **arguments)
{
*arguments = line;
*command = strsep(arguments, " \r");
}
static void process_storage_command(FILE *input, FILE *output, FILE *db,
const char *command, const char *arguments)
{
int ret;
char key[BUFSIZ];
unsigned long flags;
unsigned long exptime;
unsigned long bytes;
ret = sscanf(arguments, "%250s %ld %ld %ld\r\n",
key, &flags, &exptime, &bytes);
if (!strcmp(command, "set") && ret == 4) {
process_set_command(input, output, db,
key, flags, exptime, bytes);
} else {
output_error(output);
}
}
static void process_retrieval_command(FILE *input, FILE *output, FILE *db,
const char *command, char *arguments)
{
if (!strcmp(command, "get")) {
char *key;
while ((key = strsep(&arguments, " \r")) != NULL) {
process_get_command(input, output, db, key);
}
fprintf(output, "END\r\n");
fflush(output);
} else {
output_error(output);
}
}
static void process_other_command(FILE *input, FILE *output, FILE *db,
const char *command, const char *arguments)
{
if (!strcmp(command, "quit"))
process_quit_command(input, output, db);
else
output_error(output);
}
static void memcachedb(FILE *input, FILE *output, FILE *db)
{
char *line = NULL;
size_t len = 0;
while (1) {
ssize_t ret;
char *command;
char *arguments;
ret = getline(&line, &len, input);
if (ret < 0)
break;
if (!check_line(line, ret)) {
output_error(output);
continue;
}
split_line(line, &command, &arguments);
switch (command_type(command)) {
case STORAGE_COMMAND:
process_storage_command(input, output, db,
command, arguments);
break;
case RETRIEVAL_COMMAND:
process_retrieval_command(input, output, db,
command, arguments);
break;
case DELETION_COMMAND:
break;
case INCR_DECR_COMMAND:
break;
case STATS_COMMAND:
break;
case OTHER_COMMAND:
process_other_command(input, output, db,
command, arguments);
break;
default:
output_error(output);
break;
}
}
if (line)
free(line);
}
int main(int argc, char **argv)
{
FILE *db;
parse_options(argc, argv);
db = fopen(db_pathname, "w");
if (!db)
die("fopen %s", db_pathname);
memcachedb(stdin, stdout, db);
fclose(db);
return 0;
}