aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Garzik <jeff@garzik.org>2006-04-22 23:52:53 -0400
committerJeff Garzik <jeff@garzik.org>2006-04-22 23:52:53 -0400
commit820cede9cebaacf9ada47e66b7df45a13f6012cf (patch)
tree3191f406c6c1b85ae11b1f37d5c0668a1146d22a
parent184391ee9de19fe74bbbd7e950d22db1d967b4ef (diff)
downloaddbfs-820cede9cebaacf9ada47e66b7df45a13f6012cf.tar.gz
Replace with non-lowlevel implementation. Remove DB usage.
-rw-r--r--dbfs.c247
1 files changed, 89 insertions, 158 deletions
diff --git a/dbfs.c b/dbfs.c
index b56e052..ae88860 100644
--- a/dbfs.c
+++ b/dbfs.c
@@ -6,200 +6,131 @@
See the file COPYING.
*/
-#include <fuse_lowlevel.h>
+#define FUSE_USE_VERSION 25
+
+#include <fuse.h>
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
-#include <unistd.h>
-#include <assert.h>
-#include <db.h>
-
-static const char *hello_str = "Hello World!\n";
-static const char *hello_name = "hello";
+#include <glib.h>
-static int hello_stat(fuse_ino_t ino, struct stat *stbuf)
-{
- stbuf->st_ino = ino;
- switch (ino) {
- case 1:
- stbuf->st_mode = S_IFDIR | 0755;
- stbuf->st_nlink = 2;
- break;
-
- case 2:
- stbuf->st_mode = S_IFREG | 0444;
- stbuf->st_nlink = 1;
- stbuf->st_size = strlen(hello_str);
- break;
-
- default:
- return -1;
- }
- return 0;
-}
+enum {
+ INO_SIZE = 128,
+};
-static void hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
- struct fuse_file_info *fi)
-{
- struct stat stbuf;
+typedef struct {
+ char buf[INO_SIZE];
+} dfs_ino_t;
- (void)fi;
+struct ndb_val {
+ void *data;
+ unsigned int len;
+};
- memset(&stbuf, 0, sizeof(stbuf));
- if (hello_stat(ino, &stbuf) == -1)
- fuse_reply_err(req, ENOENT);
- else
- fuse_reply_attr(req, &stbuf, 1.0);
+static void ndb_free(struct ndb_val *val)
+{
+ /* TODO */
}
-static void hello_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
+static int ndb_lookup(const char *path, struct ndb_val **val)
{
- struct fuse_entry_param e;
-
- if (parent != 1 || strcmp(name, hello_name) != 0)
- fuse_reply_err(req, ENOENT);
- else {
- memset(&e, 0, sizeof(e));
- e.ino = 2;
- e.attr_timeout = 1.0;
- e.entry_timeout = 1.0;
- hello_stat(e.ino, &e.attr);
-
- fuse_reply_entry(req, &e);
- }
+ /* TODO */
+ *val = NULL;
+ return -ENOMEM;
}
-struct dirbuf {
- char *p;
- size_t size;
-};
-
-static void dirbuf_add(struct dirbuf *b, const char *name, fuse_ino_t ino)
+static int ndb_lookup_data(const char *path, size_t size, off_t offset,
+ struct ndb_val **val)
{
- struct stat stbuf;
- size_t oldsize = b->size;
- b->size += fuse_dirent_size(strlen(name));
- b->p = (char *)realloc(b->p, b->size);
- memset(&stbuf, 0, sizeof(stbuf));
- stbuf.st_ino = ino;
- fuse_add_dirent(b->p + oldsize, name, &stbuf, b->size);
+ /* TODO */
+ *val = NULL;
+ return -ENOMEM;
}
-#define min(x, y) ((x) < (y) ? (x) : (y))
-
-static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
- off_t off, size_t maxsize)
+static int dfs_fill_stat(struct stat *stbuf, struct ndb_val *val)
{
- if (off < bufsize)
- return fuse_reply_buf(req, buf + off,
- min(bufsize - off, maxsize));
- else
- return fuse_reply_buf(req, NULL, 0);
+ /* TODO */
+ return -EIO;
}
-static void hello_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
- off_t off, struct fuse_file_info *fi)
+static int dfs_fill_dir(fuse_fill_dir_t filler, struct ndb_val *val)
{
- (void)fi;
-
- if (ino != 1)
- fuse_reply_err(req, ENOTDIR);
- else {
- struct dirbuf b;
-
- memset(&b, 0, sizeof(b));
- dirbuf_add(&b, ".", 1);
- dirbuf_add(&b, "..", 1);
- dirbuf_add(&b, hello_name, 2);
- reply_buf_limited(req, b.p, b.size, off, size);
- free(b.p);
- }
+ /* TODO */
+ return -EIO;
}
-static void hello_ll_open(fuse_req_t req, fuse_ino_t ino,
- struct fuse_file_info *fi)
+static int dfs_getattr(const char *path, struct stat *stbuf)
{
- if (ino != 2)
- fuse_reply_err(req, EISDIR);
- else if ((fi->flags & 3) != O_RDONLY)
- fuse_reply_err(req, EACCES);
- else
- fuse_reply_open(req, fi);
+ int rc = -ENOENT;
+ struct ndb_val *val = NULL;
+ char *nspath;
+
+ memset(stbuf, 0, sizeof(struct stat));
+
+ nspath = g_strdup_printf("/meta/%s", path);
+ rc = ndb_lookup(nspath, &val);
+ if (rc)
+ goto out;
+
+ rc = dfs_fill_stat(stbuf, val);
+
+ ndb_free(val);
+
+out:
+ g_free(nspath);
+ return rc;
}
-static void hello_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
- off_t off, struct fuse_file_info *fi)
+static int dfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
+ off_t offset, struct fuse_file_info *fi)
{
- (void)fi;
+ struct ndb_val *val = NULL;
+ char *nspath;
+ int rc;
- assert(ino == 2);
- reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
-}
+ nspath = g_strdup_printf("/dir/%s", path);
+ rc = ndb_lookup(nspath, &val);
+ if (rc) {
+ if (rc == -ENOENT)
+ rc = -ENOTDIR;
+ goto out;
+ }
-static const struct fuse_lowlevel_ops hello_ll_oper = {
- .lookup = hello_ll_lookup,
- .getattr = hello_ll_getattr,
- .readdir = hello_ll_readdir,
- .open = hello_ll_open,
- .read = hello_ll_read,
-};
+ filler(buf, ".", NULL, 0);
+ filler(buf, "..", NULL, 0);
+
+ rc = dfs_fill_dir(filler, val);
+
+ ndb_free(val);
-static void db_init(void)
+out:
+ g_free(nspath);
+ return rc;
+}
+
+static int dfs_read(const char *path, char *buf, size_t size, off_t offset,
+ struct fuse_file_info *fi)
{
- DB_ENV *env;
- char *dbdir;
+ struct ndb_val *val = NULL;
int rc;
- dbdir = getenv("FUSE_DB_ENV");
- if (!dbdir)
- dbdir = "fs-data";
+ rc = ndb_lookup_data(path, size, offset, &val);
+ if (rc)
+ return rc;
- db_env_create(&env, 0);
+ ndb_free(val);
- rc = env->open(env, dbdir,
- DB_INIT_LOCK |
- DB_INIT_LOG |
- DB_INIT_MPOOL |
- DB_INIT_TXN |
- DB_RECOVER |
- DB_CREATE, 0);
- if (rc)
- abort();
+ return 0;
}
+static const struct fuse_operations dfs_ops = {
+ .getattr = dfs_getattr,
+ .read = dfs_read,
+ .readdir = dfs_readdir,
+};
+
int main(int argc, char *argv[])
{
- struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
- char *mountpoint;
- int err = -1;
- int fd;
-
- db_init();
-
- if (fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) != -1 &&
- (fd = fuse_mount(mountpoint, &args)) != -1) {
- struct fuse_session *se;
-
- se = fuse_lowlevel_new(&args, &hello_ll_oper,
- sizeof(hello_ll_oper), NULL);
- if (se != NULL) {
- if (fuse_set_signal_handlers(se) != -1) {
- struct fuse_chan *ch = fuse_kern_chan_new(fd);
- if (ch != NULL) {
- /* MAIN LOOP STARTS HERE */
- fuse_session_add_chan(se, ch);
- err = fuse_session_loop(se);
- }
- fuse_remove_signal_handlers(se);
- }
- fuse_session_destroy(se);
- }
- close(fd);
- }
- fuse_unmount(mountpoint);
- fuse_opt_free_args(&args);
-
- return err ? 1 : 0;
+ return fuse_main(argc, argv, &dfs_ops);
}