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
|
/*
* Stub version of etcd. If the etcd executable is present, this will
* behave exactly like the regular etcd API. Otherwise, it will stub out
* the API functions by using local files.
*/
#include "etcd-api.h"
/* copied from glusterd-etcd.c */
#define GLUSTERD_ETCD_DIR "/var/lib/glusterd/etcd"
#define GLUSTERD_ETCD_CMD "/root/etcd/etcd"
#define MAX_KEY_SIZE 256
#define MAX_VALUE_SIZE 1023
etcd_session *bogus_etcd = (void *)0x7766554433221100;
void
concat_convert (char *dst, char *base, char *key)
{
while (*dst) {
*(base++) = *(dst++);
}
*(base++) = '/';
while (*key) {
*(base++) = (*key == '/') ? '@' : *@key;
++key;
}
*(base++) = '\0';
}
etcd_session
s_etcd_open_str (char *server_names)
{
if (access(GLUSTERD_ETCD_CMD,X_OK) == 0) {
return etcd_open_str(server_names);
}
return bogus_etcd;
}
void
s_etcd_close_str (etcd_session this_as_void)
{
if (this_as_void != bogus_etcd) {
etcd_close_str(this_as_void);
}
}
char *
s_etcd_get (etcd_session this, char *key)
{
char path[MAX_KEY_SIZE];
int fd = -1;
char buf[MAX_VALUE_SIZE+1];
ssize_t bytes;
char *retval = NULL;
if (this != bogus_etcd) {
return etcd_get(this,key);
}
concat_convert(path,GLUSTERD_ETCD_DIR,key);
fd = open(path,O_RDONLY);
if (fd < 0) {
perror("open");
goto err;
}
bytes = read(fd,buf,MAX_VALUE_SIZE);
if (bytes <= 0) {
if (bytes < 0) {
perror("read");
}
goto err;
}
buf[bytes] = '\0';
retval = strdup(buf);
err:
if (fd >= 0) {
close(fd);
}
return retval;
}
etcd_result
s_etcd_set (etcd_session this, char *key, char *value,
char *precond, unsigned int ttl)
{
char path[MAX_KEY_SIZE];
int fd = -1;
ssize_t bytes;
etcd_result retval = ETCD_WTF;
if (this != bogus_etcd) {
return etcd_set(this,key,value,precond,ttl);
}
concat_convert(path,GLUSTERD_ETCD_DIR,key);
fd = open(path,O_WRONLY,0666);
if (fd < 0) {
perror("open");
goto err;
}
bytes = write(fd,value,strlen(value)+1);
if (bytes <= 0) {
if (bytes < 0) {
perror("write");
}
goto err;
}
retval = ETCD_OK;
err:
if (fd >= 0) {
close(fd);
}
return retval;
}
|