-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkvs_base.c
76 lines (68 loc) · 1.6 KB
/
kvs_base.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
#include "kvs_base.h"
#include <linux/limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
kvs_base_t* kvs_base_new(const char* directory) {
kvs_base_t* kvs_base = malloc(sizeof(kvs_base_t));
if (kvs_base == NULL) {
return NULL;
}
// create directory if not exist
struct stat st;
if (stat(directory, &st) == -1) {
int rc = mkdir(directory, S_IRWXU | S_IRWXG | S_IRWXO);
if (rc != 0) {
return NULL;
}
}
strcpy(kvs_base->directory, directory);
kvs_base->get_count = 0;
kvs_base->set_count = 0;
return kvs_base;
}
void kvs_base_free(kvs_base_t** ptr) {
free(*ptr);
*ptr = NULL;
}
int kvs_base_set(kvs_base_t* kvs, const char* key, const char* value) {
int rc;
char filename[PATH_MAX];
strcpy(filename, kvs->directory);
strcat(filename, "/");
strcat(filename, key);
FILE* fp = fopen(filename, "w");
if (fp == NULL) {
return -1;
}
fwrite(value, sizeof(char), strlen(value), fp);
rc = fclose(fp);
if (rc != 0) {
return rc;
}
kvs->set_count += 1;
return 0;
}
int kvs_base_get(kvs_base_t* kvs, const char* key, char* value) {
int rc;
char filename[PATH_MAX];
strcpy(filename, kvs->directory);
strcat(filename, "/");
strcat(filename, key);
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
// if the file doesn't exist, return the empty string
kvs->get_count += 1;
strcpy(value, "");
return 0;
}
size_t num_read = fread(value, sizeof(char), KVS_VALUE_MAX, fp);
value[num_read] = '\0';
rc = fclose(fp);
if (rc != 0) {
return rc;
}
kvs->get_count += 1;
return 0;
}