src/dbfs/link.c
changeset 38 1fd4da071575
parent 37 c3880f3b4de8
child 40 03017f5f0087
--- 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");   
+}