-
Notifications
You must be signed in to change notification settings - Fork 6
/
ttskeldir.c
141 lines (108 loc) · 4.24 KB
/
ttskeldir.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
/*************************************************************************************************
* The skeleton database library working as a directory database
* Copyright (C) 2006-2010 FAL Labs
* This file is part of Tokyo Tyrant.
* Tokyo Tyrant is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. Tokyo Tyrant is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Tokyo
* Tyrant; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#include <tcutil.h>
#include <tcadb.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
typedef struct { /* type of structure for a directory database */
char *name; /* name of the directory */
} TCDDB;
/* private funciton prototypes */
static TCDDB *tcddbnew(void);
static void tcddbdel(TCDDB *ddb);
static bool tcddbopen(TCDDB *ddb, const char *name);
static bool tcddbclose(TCDDB *ddb);
static bool tcddbput(TCDDB *ddb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
static bool tcddbout(TCDDB *ddb, const void *kbuf, int ksiz);
static void *tcddbget(TCDDB *ddb, const void *kbuf, int ksiz, int *sp);
static char *makepath(TCDDB *ddb, const void *kbuf, int ksiz);
/*************************************************************************************************
* API
*************************************************************************************************/
bool initialize(ADBSKEL *skel){
skel->opq = tcddbnew();
skel->del = (void (*)(void *))tcddbdel;
skel->open = (bool (*)(void *, const char *))tcddbopen;
skel->close = (bool (*)(void *))tcddbclose;
skel->put = (bool (*)(void *, const void *, int, const void *, int))tcddbput;
skel->out = (bool (*)(void *, const void *, int))tcddbout;
skel->get = (void *(*)(void *, const void *, int, int *))tcddbget;
return true;
}
/*************************************************************************************************
* private features
*************************************************************************************************/
static TCDDB *tcddbnew(void){
TCDDB *ddb = tcmalloc(sizeof(*ddb));
ddb->name = NULL;
return ddb;
}
static void tcddbdel(TCDDB *ddb){
if(ddb->name) tcddbclose(ddb);
tcfree(ddb);
}
static bool tcddbopen(TCDDB *ddb, const char *name){
if(ddb->name) return false;
struct stat sbuf;
if(stat(name, &sbuf) == 0){
if(!S_ISDIR(sbuf.st_mode)) return false;
} else {
if(mkdir(name, 0755) != 0) return false;
}
ddb->name = tcstrdup(name);
return true;
}
static bool tcddbclose(TCDDB *ddb){
if(!ddb->name) return false;
tcfree(ddb->name);
ddb->name = NULL;
return true;
}
static bool tcddbput(TCDDB *ddb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
if(!ddb->name) return false;
bool err = false;
char *path = makepath(ddb, kbuf, ksiz);
if(!tcwritefile(path, vbuf, vsiz)) err = true;
tcfree(path);
return !err;
}
static bool tcddbout(TCDDB *ddb, const void *kbuf, int ksiz){
if(!ddb->name) return false;
bool err = false;
char *path = makepath(ddb, kbuf, ksiz);
if(unlink(path) != 0) err = true;
tcfree(path);
return !err;
}
static void *tcddbget(TCDDB *ddb, const void *kbuf, int ksiz, int *sp){
if(!ddb->name) return false;
void *vbuf;
char *path = makepath(ddb, kbuf, ksiz);
vbuf = tcreadfile(path, -1, sp);
tcfree(path);
return vbuf;
}
static char *makepath(TCDDB *ddb, const void *kbuf, int ksiz){
char *uenc = tcurlencode(kbuf, ksiz);
char *path = tcsprintf("%s/%s", ddb->name, uenc);
tcfree(uenc);
return path;
}
// END OF FILE