rework/hack parent to be a FOREIGN KEY (add a dir_ino col), implement dbfs_link
authorTero Marttila <terom@fixme.fi>
Wed, 22 Oct 2008 18:14:24 +0300
changeset 38 1fd4da071575
parent 37 c3880f3b4de8
child 39 7e655be2189a
rework/hack parent to be a FOREIGN KEY (add a dir_ino col), implement dbfs_link
Makefile
doc/evfuse_db.txt
doc/fuse_db.sql
src/dbfs/core.c
src/dbfs/dbfs.c
src/dbfs/link.c
src/dbfs/mk.c
src/dbfs/ops.h
src/evsql.h
src/evsql/util.c
--- a/Makefile	Tue Oct 21 21:54:20 2008 +0300
+++ b/Makefile	Wed Oct 22 18:14:24 2008 +0300
@@ -20,7 +20,7 @@
 
 # complex modules
 EVSQL_OBJS = obj/evsql/evsql.o obj/evsql/util.o obj/evpq.o
-DBFS_OBJS = obj/dbfs/dbfs.o obj/dbfs/common.o obj/dbfs/core.o obj/dbfs/op_base.o obj/dbfs/trans.o obj/dbfs/dirop.o obj/dirbuf.o obj/dbfs/fileop.o obj/dbfs/attr.o obj/dbfs/link.o obj/dbfs/tree.o obj/dbfs/mk.o
+DBFS_OBJS = obj/dbfs/dbfs.o obj/dbfs/common.o obj/dbfs/op_base.o obj/dbfs/trans.o obj/dbfs/dirop.o obj/dirbuf.o obj/dbfs/fileop.o obj/dbfs/attr.o obj/dbfs/link.o obj/dbfs/tree.o obj/dbfs/mk.o
 
 # first target
 all: ${BIN_PATHS}
--- a/doc/evfuse_db.txt	Tue Oct 21 21:54:20 2008 +0300
+++ b/doc/evfuse_db.txt	Wed Oct 22 18:14:24 2008 +0300
@@ -4,16 +4,22 @@
 
 
 file_tree:
-    offset              serial4                 ephemeral counter used to tell one file entry from another
-    name                varchar(256)            the filename
-    parent              int4 -> inodes.ino      file entry's parent
-    inode               int4 -> inodes.ino      the file's data
+    offset              serial4                     ephemeral counter used to tell one file entry from another
+    ino                 int4 -> inodes.ino          the file inode
+    ino_dir             int4 -> inodes.ino          the file's inode if it's a dir, NULL otherwise
+    name                varchar(256)                the filename
+    parent              int4 -> file_tree.ino_dir   file entry's parent dir
+    
+    unique(ino_dir)
+    unique(name, parent)
 
 inodes:
     ino                 serial4                 inode number
     type                char(3)
             REG                         normal file
             DIR                         directory
+            LNK                         symlink
     mode                int2                    file access modes 
-    size                int8                    file content size (?)
+    data                oid                     type=REG -> file data, else NULL
+    link_path           varchar(512)            type=LNK -> symlink target, else NULL
 
--- a/doc/fuse_db.sql	Tue Oct 21 21:54:20 2008 +0300
+++ b/doc/fuse_db.sql	Wed Oct 22 18:14:24 2008 +0300
@@ -16,17 +16,22 @@
 CREATE TABLE file_tree (
     "offset" serial4 primary key, 
     name varchar(256), 
-    ino int4 references inodes(ino) NOT NULL UNIQUE,
-    parent int4 references file_tree(ino)
+    ino int4 references inodes(ino) NOT NULL,
+    ino_dir int4 references inodes(ino),
+    parent int4,
+
+    CONSTRAINT file_tree_uniq_direntry UNIQUE (parent, name),
+    CONSTRAINT file_tree_uniq_dir_ino UNIQUE (ino_dir),
+    CONSTRAINT file_tree_exist_parent FOREIGN KEY (parent) REFERENCES file_tree(ino_dir)
 );
 
 INSERT INTO inodes (ino, type, mode, data) VALUES 
     (1, 'DIR', 365, NULL),
     (2, 'REG', 292, lo_create(0));
 
-INSERT INTO file_tree (name, parent, ino) VALUES 
-    (NULL,  NULL,   1   ),
-    ('foo', 1,      2   );
+INSERT INTO file_tree (name, parent, ino, ino_dir) VALUES 
+    (NULL,  NULL,   1,  1       ),
+    ('foo', 1,      2,  NULL    );
 
 CREATE OR REPLACE FUNCTION lo_size (oid) RETURNS int4 LANGUAGE SQL STABLE RETURNS NULL ON NULL INPUT AS 'select lo_lseek(lo_open($1, 262144), 0, 2);';
 CREATE OR REPLACE FUNCTION lo_pread (IN fd int4, IN len int4, IN "off" int4) RETURNS bytea LANGUAGE SQL STRICT AS 'select lo_lseek($1, $3, 0); select loread($1, $2);';
@@ -40,3 +45,12 @@
         ELSE 0 
     END;
 $$;
+
+CREATE OR REPLACE FUNCTION dbfs_link (
+    IN ino int4, IN new_parent int4, IN new_name varchar, 
+    OUT ino int4, OUT type char(3), OUT mode int2, OUT size int4, OUT nlink int8
+) LANGUAGE SQL VOLATILE AS $$
+    INSERT INTO file_tree (name, ino, parent) VALUES ($3, $1, $2);
+    SELECT ino, type, mode, dbfs_size(type, data, link_path) AS size, (SELECT COUNT(*) FROM inodes i LEFT JOIN file_tree ft ON (i.ino = ft.ino) WHERE i.ino = inodes.ino) AS nlink
+     FROM inodes WHERE ino = $1;
+$$;
--- a/src/dbfs/core.c	Tue Oct 21 21:54:20 2008 +0300
+++ b/src/dbfs/core.c	Wed Oct 22 18:14:24 2008 +0300
@@ -7,84 +7,4 @@
  * Core fs functionality like lookup, getattr
  */
 
-void _dbfs_lookup_result (const struct evsql_result_info *res, void *arg) {
-    struct fuse_req *req = arg;
-    struct fuse_entry_param e; ZINIT(e);
-    int err = 0;
-    
-    uint32_t ino;
-    
-    // check the results
-    if ((err = _dbfs_check_res(res, 1, 5)))
-        SERROR(err = (err ==  1 ? ENOENT : EIO));
-    
-    // get the data
-    if (0
-        ||  evsql_result_uint32(res, 0, 0, &ino,        0 ) // inodes.ino
-    )
-        EERROR(err = EIO, "invalid db data");
-        
-    INFO("\t[dbfs.lookup] -> ino=%u", ino);
-    
-    // stat attrs
-    if ((err = _dbfs_stat_info(&e.attr, res, 0, 1)))
-        goto error;
-
-    // other attrs
-    e.ino = e.attr.st_ino = ino;
-    e.attr_timeout = CACHE_TIMEOUT;
-    e.entry_timeout = CACHE_TIMEOUT;
-        
-    // reply
-    if ((err = fuse_reply_entry(req, &e)))
-        EERROR(err, "fuse_reply_entry");
-
-error:
-    if (err && (err = fuse_reply_err(req, err)))
-        EWARNING(err, "fuse_reply_err");
-
-    // free
-    evsql_result_free(res);
-}
 
-void dbfs_lookup (struct fuse_req *req, fuse_ino_t parent, const char *name) {
-    struct dbfs *ctx = fuse_req_userdata(req);
-    int err;
-
-    INFO("[dbfs.lookup] parent=%lu name=%s", parent, name);
-    
-    // query and params
-    const char *sql = 
-        "SELECT"
-        " inodes.ino, " DBFS_STAT_COLS
-        " FROM file_tree INNER JOIN inodes ON (file_tree.ino = inodes.ino)"
-        " WHERE file_tree.parent = $1::int4 AND file_tree.name = $2::varchar";
-    
-    static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) {
-        EVSQL_PARAM ( UINT32 ),
-        EVSQL_PARAM ( STRING ),
-
-        EVSQL_PARAMS_END
-    };
-    
-    // build params
-    if (0
-        ||  evsql_param_uint32(&params, 0, parent)
-        ||  evsql_param_string(&params, 1, name)
-    )
-        EERROR(err = EIO, "evsql_param_*");
-
-    // query
-    if (evsql_query_params(ctx->db, NULL, sql, &params, _dbfs_lookup_result, req) == NULL)
-        EERROR(err = EIO, "evsql_query_params");
-
-    // XXX: handle interrupts
-    
-    // wait
-    return;
-
-error:
-    if ((err = fuse_reply_err(req, err)))
-        EWARNING(err, "fuse_reply_err");
-}
-
--- a/src/dbfs/dbfs.c	Tue Oct 21 21:54:20 2008 +0300
+++ b/src/dbfs/dbfs.c	Wed Oct 22 18:14:24 2008 +0300
@@ -21,7 +21,7 @@
     .rmdir          = dbfs_unlink,  // this behaves just the same
     .symlink        = dbfs_symlink,
     .rename         = dbfs_rename,
-
+    .link           = dbfs_link,
     .open           = dbfs_open,
     .read           = dbfs_read,
     .write          = dbfs_write,
--- a/src/dbfs/link.c	Tue Oct 21 21:54:20 2008 +0300
+++ b/src/dbfs/link.c	Wed Oct 22 18:14:24 2008 +0300
@@ -1,6 +1,95 @@
 #include "dbfs.h"
 
+/*
+ * Handling simple ino-related ops, like lookup, readlink, unlink and link
+ */
+
 #include "../lib/log.h"
+#include "../lib/misc.h"
+
+/*
+ * Used for lookup and link
+ */
+void dbfs_entry_res (const struct evsql_result_info *res, void *arg) {
+    struct fuse_req *req = arg;
+    struct fuse_entry_param e; ZINIT(e);
+    int err = 0;
+    
+    uint32_t ino;
+    
+    // check the results
+    if ((err = _dbfs_check_res(res, 1, 5)))
+        SERROR(err = (err ==  1 ? ENOENT : EIO));
+    
+    // get the data
+    if (0
+        ||  evsql_result_uint32(res, 0, 0, &ino,        0 ) // inodes.ino
+    )
+        EERROR(err = EIO, "invalid db data");
+        
+    INFO("\t[dbfs.lookup] -> ino=%u", ino);
+    
+    // stat attrs
+    if ((err = _dbfs_stat_info(&e.attr, res, 0, 1)))
+        goto error;
+
+    // other attrs
+    e.ino = e.attr.st_ino = ino;
+    e.attr_timeout = CACHE_TIMEOUT;
+    e.entry_timeout = CACHE_TIMEOUT;
+        
+    // reply
+    if ((err = fuse_reply_entry(req, &e)))
+        EERROR(err, "fuse_reply_entry");
+
+error:
+    if (err && (err = fuse_reply_err(req, err)))
+        EWARNING(err, "fuse_reply_err");
+
+    // free
+    evsql_result_free(res);
+}
+
+void dbfs_lookup (struct fuse_req *req, fuse_ino_t parent, const char *name) {
+    struct dbfs *ctx = fuse_req_userdata(req);
+    int err;
+
+    INFO("[dbfs.lookup] parent=%lu name=%s", parent, name);
+    
+    // query and params
+    const char *sql = 
+        "SELECT"
+        " inodes.ino, " DBFS_STAT_COLS
+        " FROM file_tree INNER JOIN inodes ON (file_tree.ino = inodes.ino)"
+        " WHERE file_tree.parent = $1::int4 AND file_tree.name = $2::varchar";
+    
+    static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) {
+        EVSQL_PARAM ( UINT32 ),
+        EVSQL_PARAM ( STRING ),
+
+        EVSQL_PARAMS_END
+    };
+    
+    // build params
+    if (0
+        ||  evsql_param_uint32(&params, 0, parent)
+        ||  evsql_param_string(&params, 1, name)
+    )
+        EERROR(err = EIO, "evsql_param_*");
+
+    // query
+    if (evsql_query_params(ctx->db, NULL, sql, &params, dbfs_entry_res, req) == NULL)
+        EERROR(err = EIO, "evsql_query_params");
+
+    // XXX: handle interrupts
+    
+    // wait
+    return;
+
+error:
+    if ((err = fuse_reply_err(req, err)))
+        EWARNING(err, "fuse_reply_err");
+}
 
 void _dbfs_readlink_res (const struct evsql_result_info *res, void *arg) {
     struct fuse_req *req = arg;
@@ -142,3 +231,41 @@
         EWARNING(err, "fuse_reply_err");
 }
 
+void dbfs_link (struct fuse_req *req, fuse_ino_t ino, fuse_ino_t newparent, const char *newname) {
+    struct dbfs *ctx = fuse_req_userdata(req);
+    int err;
+    
+    INFO("[dbfs.link %p] ino=%lu, newparent=%lu, newname=%s", req, ino, newparent, newname);
+
+    const char *sql =
+        "SELECT ino, type, mode, size, nlink FROM dbfs_link($1::int4, $2::int4, $3::varchar)";
+
+    static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) {
+        EVSQL_PARAM ( UINT32 ),
+        EVSQL_PARAM ( UINT32 ),
+        EVSQL_PARAM ( STRING ),
+
+        EVSQL_PARAMS_END
+    };
+
+    // build params
+    if (0
+        ||  evsql_param_uint32(&params, 0, ino)
+        ||  evsql_param_uint32(&params, 1, newparent)
+        ||  evsql_param_string(&params, 2, newname)
+    )
+        SERROR(err = EIO);
+        
+    // query
+    if (evsql_query_params(ctx->db, NULL, sql, &params, dbfs_entry_res, req) == NULL)
+        SERROR(err = EIO);
+
+    // XXX: handle interrupts
+    
+    // wait
+    return;
+
+error:
+    if ((err = fuse_reply_err(req, err)))
+        EWARNING(err, "fuse_reply_err");   
+}
--- a/src/dbfs/mk.c	Tue Oct 21 21:54:20 2008 +0300
+++ b/src/dbfs/mk.c	Wed Oct 22 18:14:24 2008 +0300
@@ -12,6 +12,8 @@
     char *link, *name;
     uint16_t mode;
     uint32_t ino, parent;
+
+    unsigned char is_dir : 1;
 };
 
 // default mode for symlinks
@@ -93,13 +95,14 @@
     // insert file_tree entry
     const char *sql = 
         "INSERT"
-        " INTO file_tree (name, parent, ino)"
-        " VALUES ($1::varchar, $2::int4, $3::int4)";
+        " INTO file_tree (name, parent, ino, ino_dir)"
+        " VALUES ($1::varchar, $2::int4, $3::int4, $4::int4)";
     
     static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) {
         EVSQL_PARAM ( STRING ),
         EVSQL_PARAM ( UINT32 ),
         EVSQL_PARAM ( UINT32 ),
+        EVSQL_PARAM ( UINT32 ),
 
         EVSQL_PARAMS_END
     };
@@ -108,6 +111,7 @@
         ||  evsql_param_string(&params, 0, ctx->name)
         ||  evsql_param_uint32(&params, 1, ctx->parent)
         ||  evsql_param_uint32(&params, 2, ctx->ino)
+        ||  ctx->is_dir ? evsql_param_uint32(&params, 3, ctx->ino) : evsql_param_null(&params, 3)
     )
         goto error;
     
@@ -168,7 +172,7 @@
 /*
  * It is assumed that name and link_path must be copied, but type remains useable
  */ 
-void dbfs_mk (struct fuse_req *req, fuse_ino_t parent, const char *name, const char *type, uint16_t mode, const char *data_expr, const char *link) {
+void dbfs_mk (struct fuse_req *req, fuse_ino_t parent, const char *name, const char *type, uint16_t mode, const char *data_expr, const char *link, unsigned char is_dir) {
     struct dbfs_mk_ctx *ctx = NULL;
 
     // alloc
@@ -190,6 +194,7 @@
     ctx->type = type;
     ctx->data_expr = data_expr;
     ctx->mode = mode;
+    ctx->is_dir = is_dir;
 
     // copy volatile strings
     if (
@@ -199,7 +204,7 @@
         ERROR("strdup");
     
     // log
-    INFO("[dbfs.mk %p:%p] parent=%lu, name=%s, type=%s, mode=%#04o data_expr=%s link=%s", ctx, req, parent, name, type, mode, data_expr, link);
+    INFO("[dbfs.mk %p:%p] parent=%lu, name=%s, type=%s, mode=%#04o data_expr=%s link=%s is_dir=%hhd", ctx, req, parent, name, type, mode, data_expr, link, is_dir);
 
     // wait
     return;
@@ -218,7 +223,7 @@
     if ((mode & S_IFMT) != S_IFREG)
         EERROR(err = EINVAL, "mode is not REG: %#08o", mode);
 
-    dbfs_mk(req, parent, name, "REG", mode & 07777, "lo_create(0)", NULL);
+    dbfs_mk(req, parent, name, "REG", mode & 07777, "lo_create(0)", NULL, 0);
 
     return;
 
@@ -228,11 +233,11 @@
 }
 
 void dbfs_mkdir (struct fuse_req *req, fuse_ino_t parent, const char *name, mode_t mode) {
-    dbfs_mk(req, parent, name, "DIR", mode, NULL, NULL);
+    dbfs_mk(req, parent, name, "DIR", mode, NULL, NULL, 1);
 }
 
 
 void dbfs_symlink (struct fuse_req *req, const char *link, fuse_ino_t parent, const char *name) {
-    dbfs_mk(req, parent, name, "LNK", DBFS_SYMLINK_MODE, NULL, link);
+    dbfs_mk(req, parent, name, "LNK", DBFS_SYMLINK_MODE, NULL, link, 0);
 }
 
--- a/src/dbfs/ops.h	Tue Oct 21 21:54:20 2008 +0300
+++ b/src/dbfs/ops.h	Wed Oct 22 18:14:24 2008 +0300
@@ -7,16 +7,15 @@
 void dbfs_init (void *userdata, struct fuse_conn_info *conn);
 void dbfs_destroy (void *arg);
 
-/* core.c */
-void dbfs_lookup (struct fuse_req *req, fuse_ino_t parent, const char *name);
-
 /* attr.c */
 void dbfs_getattr (struct fuse_req *req, fuse_ino_t ino, struct fuse_file_info *fi);
 void dbfs_setattr(struct fuse_req *req, fuse_ino_t ino, struct stat *attr, int to_set, struct fuse_file_info *fi);
 
 /* link.c */
+void dbfs_lookup (struct fuse_req *req, fuse_ino_t parent, const char *name);
 void dbfs_readlink (struct fuse_req *req, fuse_ino_t ino);
 void dbfs_unlink (struct fuse_req *req, fuse_ino_t parent, const char *name);
+void dbfs_link (struct fuse_req *req, fuse_ino_t ino, fuse_ino_t newparent, const char *newname);
 
 /* dirop.c */
 void dbfs_opendir (struct fuse_req *req, fuse_ino_t ino, struct fuse_file_info *fi);
--- a/src/evsql.h	Tue Oct 21 21:54:20 2008 +0300
+++ b/src/evsql.h	Wed Oct 22 18:14:24 2008 +0300
@@ -218,6 +218,7 @@
 int evsql_param_string (struct evsql_query_params *params, size_t param, const char *ptr);
 int evsql_param_uint16 (struct evsql_query_params *params, size_t param, uint16_t uval);
 int evsql_param_uint32 (struct evsql_query_params *params, size_t param, uint32_t uval);
+int evsql_param_null   (struct evsql_query_params *params, size_t param);
 int evsql_params_clear (struct evsql_query_params *params);
 
 /*
--- a/src/evsql/util.c	Tue Oct 21 21:54:20 2008 +0300
+++ b/src/evsql/util.c	Wed Oct 22 18:14:24 2008 +0300
@@ -51,6 +51,14 @@
     return 0;
 }
 
+int evsql_param_null   (struct evsql_query_params *params, size_t param) {
+    struct evsql_query_param *p = &params->list[param];
+
+    p->data_raw = NULL;
+
+    return 0;
+}
+
 int evsql_param_binary (struct evsql_query_params *params, size_t param, const char *ptr, size_t len) {
     struct evsql_query_param *p = &params->list[param];