|
24
|
1 |
|
|
|
2 |
/*
|
|
|
3 |
* A simple PostgreSQL-based filesystem.
|
|
|
4 |
*/
|
|
|
5 |
|
|
|
6 |
#include <string.h>
|
|
|
7 |
#include <errno.h>
|
|
|
8 |
|
|
|
9 |
#include <event2/event.h>
|
|
|
10 |
|
|
|
11 |
#include "evfuse.h"
|
|
|
12 |
#include "evsql.h"
|
|
|
13 |
#include "dirbuf.h"
|
|
|
14 |
#include "lib/log.h"
|
|
|
15 |
#include "lib/signals.h"
|
|
|
16 |
#include "lib/misc.h"
|
|
|
17 |
|
|
|
18 |
#define SERROR(val) do { (val); goto error; } while(0)
|
|
|
19 |
|
|
|
20 |
struct dbfs {
|
|
|
21 |
struct event_base *ev_base;
|
|
|
22 |
struct signals *signals;
|
|
|
23 |
|
|
|
24 |
const char *db_conninfo;
|
|
|
25 |
struct evsql *db;
|
|
|
26 |
|
|
|
27 |
struct evfuse *ev_fuse;
|
|
|
28 |
};
|
|
|
29 |
|
|
|
30 |
#define CONNINFO_DEFAULT "dbname=test"
|
|
|
31 |
|
|
|
32 |
// XXX: not sure how this should work
|
|
|
33 |
#define CACHE_TIMEOUT 1.0
|
|
|
34 |
|
|
|
35 |
mode_t _dbfs_mode (const char *type) {
|
|
|
36 |
if (!strcmp(type, "DIR"))
|
|
|
37 |
return S_IFDIR;
|
|
|
38 |
|
|
|
39 |
if (!strcmp(type, "REG"))
|
|
|
40 |
return S_IFREG;
|
|
|
41 |
|
|
|
42 |
else {
|
|
|
43 |
WARNING("[dbfs] weird mode-type: %s", type);
|
|
|
44 |
return 0;
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
void dbfs_init (void *userdata, struct fuse_conn_info *conn) {
|
|
|
49 |
INFO("[dbfs.init] userdata=%p, conn=%p", userdata, conn);
|
|
|
50 |
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
void dbfs_destroy (void *userdata) {
|
|
|
54 |
INFO("[dbfs.destroy] userdata=%p", userdata);
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
void _dbfs_lookup_result (const struct evsql_result_info *res, void *arg) {
|
|
|
60 |
struct fuse_req *req = arg;
|
|
|
61 |
struct fuse_entry_param e; ZINIT(e);
|
|
|
62 |
int err = 0;
|
|
|
63 |
|
|
|
64 |
uint16_t mode;
|
|
|
65 |
uint32_t ino;
|
|
|
66 |
uint64_t size, nlink;
|
|
|
67 |
const char *type;
|
|
|
68 |
|
|
|
69 |
// check if it failed
|
|
|
70 |
if (res->error && (err = EIO))
|
|
|
71 |
NERROR(evsql_result_error(res));
|
|
|
72 |
|
|
|
73 |
// duplicate rows?
|
|
|
74 |
if (evsql_result_rows(res) > 1)
|
|
|
75 |
EERROR(err = EIO, "multiple rows returned");
|
|
|
76 |
|
|
|
77 |
// not found?
|
|
|
78 |
if (evsql_result_rows(res) == 0)
|
|
|
79 |
SERROR(err = ENOENT);
|
|
|
80 |
|
|
|
81 |
// correct number of columns
|
|
|
82 |
if (evsql_result_cols(res) != 5)
|
|
|
83 |
EERROR(err = EIO, "wrong number of columns: %zu", evsql_result_cols(res));
|
|
|
84 |
|
|
|
85 |
// get the data
|
|
|
86 |
if (0
|
|
|
87 |
|| evsql_result_uint32(res, 0, 0, &ino, 0 ) // inodes.ino
|
|
|
88 |
|| evsql_result_string(res, 0, 1, &type, 0 ) // inodes.type
|
|
|
89 |
|| evsql_result_uint16(res, 0, 2, &mode, 0 ) // inodes.mode
|
|
|
90 |
|| evsql_result_uint64(res, 0, 3, &size, 0 ) // inodes.size
|
|
|
91 |
|| evsql_result_uint64(res, 0, 4, &nlink, 0 ) // count(*)
|
|
|
92 |
)
|
|
|
93 |
EERROR(err = EIO, "invalid db data");
|
|
|
94 |
|
|
|
95 |
INFO("[dbfs.look] -> ino=%u, st_mode=S_IF%s | %ho, st_nlink=%llu, st_size=%llu", ino, type, mode, (long long unsigned int) nlink, (long long unsigned int) size);
|
|
|
96 |
|
|
|
97 |
// convert and store
|
|
|
98 |
e.ino = e.attr.st_ino = ino;
|
|
|
99 |
e.attr.st_mode = _dbfs_mode(type) | mode;
|
|
|
100 |
e.attr.st_nlink = nlink;
|
|
|
101 |
e.attr.st_size = size;
|
|
|
102 |
|
|
|
103 |
// XXX: timeouts
|
|
|
104 |
e.attr_timeout = CACHE_TIMEOUT;
|
|
|
105 |
e.entry_timeout = CACHE_TIMEOUT;
|
|
|
106 |
|
|
|
107 |
// reply
|
|
|
108 |
if ((err = fuse_reply_entry(req, &e)))
|
|
|
109 |
EERROR(err, "fuse_reply_entry");
|
|
|
110 |
|
|
|
111 |
error:
|
|
|
112 |
if (err && (err = fuse_reply_err(req, err)))
|
|
|
113 |
EWARNING(err, "fuse_reply_err");
|
|
|
114 |
|
|
|
115 |
// free
|
|
|
116 |
evsql_result_free(res);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
void dbfs_lookup (struct fuse_req *req, fuse_ino_t parent, const char *name) {
|
|
|
120 |
struct dbfs *ctx = fuse_req_userdata(req);
|
|
|
121 |
int err;
|
|
|
122 |
|
|
|
123 |
INFO("[dbfs.lookup] parent=%lu name=%s", parent, name);
|
|
|
124 |
|
|
|
125 |
// query and params
|
|
|
126 |
const char *sql =
|
|
|
127 |
"SELECT"
|
|
|
128 |
" inodes.ino, inodes.type, inodes.mode, inodes.size, count(*)"
|
|
|
129 |
" FROM file_tree INNER JOIN inodes ON (file_tree.inode = inodes.ino)"
|
|
|
130 |
" WHERE file_tree.parent = $1::int AND file_tree.name = $2::varchar"
|
|
|
131 |
" GROUP BY inodes.ino, inodes.type, inodes.mode, inodes.size";
|
|
|
132 |
|
|
|
133 |
static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) {
|
|
|
134 |
EVSQL_PARAM ( UINT32 ),
|
|
|
135 |
EVSQL_PARAM ( STRING ),
|
|
|
136 |
|
|
|
137 |
EVSQL_PARAMS_END
|
|
|
138 |
};
|
|
|
139 |
|
|
|
140 |
// build params
|
|
|
141 |
if (0
|
|
|
142 |
|| evsql_param_uint32(¶ms, 0, parent)
|
|
|
143 |
|| evsql_param_string(¶ms, 1, name)
|
|
|
144 |
)
|
|
|
145 |
EERROR(err = EIO, "evsql_param_*");
|
|
|
146 |
|
|
|
147 |
// query
|
|
|
148 |
if (evsql_query_params(ctx->db, sql, ¶ms, _dbfs_lookup_result, req) == NULL)
|
|
|
149 |
EERROR(err = EIO, "evsql_query_params");
|
|
|
150 |
|
|
|
151 |
// XXX: handle interrupts
|
|
|
152 |
|
|
|
153 |
// wait
|
|
|
154 |
return;
|
|
|
155 |
|
|
|
156 |
error:
|
|
|
157 |
if ((err = fuse_reply_err(req, err)))
|
|
|
158 |
EWARNING(err, "fuse_reply_err");
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
struct fuse_lowlevel_ops dbfs_llops = {
|
|
|
162 |
.init = dbfs_init,
|
|
|
163 |
.destroy = dbfs_destroy,
|
|
|
164 |
|
|
|
165 |
.lookup = dbfs_lookup,
|
|
|
166 |
};
|
|
|
167 |
|
|
|
168 |
void dbfs_sql_error (struct evsql *evsql, void *arg) {
|
|
|
169 |
struct dbfs *ctx = arg;
|
|
|
170 |
|
|
|
171 |
// AAAAAAAAAA.... panic
|
|
|
172 |
WARNING("[dbfs] SQL error: BREAKING MAIN LOOP LIEK NAO");
|
|
|
173 |
|
|
|
174 |
event_base_loopbreak(ctx->ev_base);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
int main (int argc, char **argv) {
|
|
|
178 |
struct fuse_args fuse_args = FUSE_ARGS_INIT(argc, argv);
|
|
|
179 |
struct dbfs ctx; ZINIT(ctx);
|
|
|
180 |
|
|
|
181 |
// parse args, XXX: fuse_args
|
|
|
182 |
ctx.db_conninfo = CONNINFO_DEFAULT;
|
|
|
183 |
|
|
|
184 |
// init libevent
|
|
|
185 |
if ((ctx.ev_base = event_base_new()) == NULL)
|
|
|
186 |
ERROR("event_base_new");
|
|
|
187 |
|
|
|
188 |
// setup signals
|
|
|
189 |
if ((ctx.signals = signals_default(ctx.ev_base)) == NULL)
|
|
|
190 |
ERROR("signals_default");
|
|
|
191 |
|
|
|
192 |
// open sql
|
|
|
193 |
if ((ctx.db = evsql_new_pq(ctx.ev_base, ctx.db_conninfo, dbfs_sql_error, &ctx)) == NULL)
|
|
|
194 |
ERROR("evsql_new_pq");
|
|
|
195 |
|
|
|
196 |
// open fuse
|
|
|
197 |
if ((ctx.ev_fuse = evfuse_new(ctx.ev_base, &fuse_args, &dbfs_llops, &ctx)) == NULL)
|
|
|
198 |
ERROR("evfuse_new");
|
|
|
199 |
|
|
|
200 |
// run libevent
|
|
|
201 |
INFO("running libevent loop");
|
|
|
202 |
|
|
|
203 |
if (event_base_dispatch(ctx.ev_base))
|
|
|
204 |
PERROR("event_base_dispatch");
|
|
|
205 |
|
|
|
206 |
// clean shutdown
|
|
|
207 |
|
|
|
208 |
error :
|
|
|
209 |
// cleanup
|
|
|
210 |
if (ctx.ev_fuse)
|
|
|
211 |
evfuse_close(ctx.ev_fuse);
|
|
|
212 |
|
|
|
213 |
// XXX: ctx.db
|
|
|
214 |
|
|
|
215 |
if (ctx.signals)
|
|
|
216 |
signals_free(ctx.signals);
|
|
|
217 |
|
|
|
218 |
if (ctx.ev_base)
|
|
|
219 |
event_base_free(ctx.ev_base);
|
|
|
220 |
|
|
|
221 |
fuse_opt_free_args(&fuse_args);
|
|
|
222 |
}
|
|
|
223 |
|