src/dbfs/dbfs.c
changeset 28 e944453ca924
child 29 5de62ca9a5aa
equal deleted inserted replaced
27:461be4cd34a3 28:e944453ca924
       
     1 
       
     2 #include <stdlib.h>
       
     3 
       
     4 #include "../dbfs.h"
       
     5 #include "common.h"
       
     6 #include "ops.h"
       
     7 
       
     8 static struct fuse_lowlevel_ops dbfs_llops = {
       
     9 
       
    10     .init           = dbfs_init,
       
    11     .destroy        = dbfs_destroy,
       
    12     
       
    13     .lookup         = dbfs_lookup,
       
    14 
       
    15     .getattr        = dbfs_getattr,
       
    16 
       
    17     .opendir        = dbfs_opendir,
       
    18     .readdir        = dbfs_readdir,
       
    19     .releasedir     = dbfs_releasedir,
       
    20 };
       
    21 
       
    22 void dbfs_init (void *userdata, struct fuse_conn_info *conn) {
       
    23     INFO("[dbfs.init] userdata=%p, conn=%p", userdata, conn);
       
    24 
       
    25 }
       
    26 
       
    27 void dbfs_destroy (void *arg) {
       
    28     struct dbfs *ctx = arg;
       
    29     INFO("[dbfs.destroy %p]", ctx);
       
    30 
       
    31     // exit libevent
       
    32     event_base_loopexit(ctx->ev_base, NULL);
       
    33 }
       
    34 
       
    35 
       
    36 void dbfs_sql_error (struct evsql *evsql, void *arg) {
       
    37     struct dbfs *ctx = arg;
       
    38 
       
    39     // AAAAAAAAAA.... panic
       
    40     WARNING("[dbfs] SQL error: BREAKING MAIN LOOP LIEK NAO");
       
    41 
       
    42     event_base_loopbreak(ctx->ev_base);
       
    43 }
       
    44 
       
    45 struct dbfs *dbfs_open (struct event_base *ev_base, struct fuse_args *args, const char *db_conninfo) {
       
    46     struct dbfs *ctx = NULL;
       
    47 
       
    48     // alloc ctx
       
    49     if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
       
    50         ERROR("calloc");
       
    51     
       
    52     ctx->ev_base = ev_base;
       
    53     ctx->db_conninfo = db_conninfo;
       
    54 
       
    55     // open sql
       
    56     if ((ctx->db = evsql_new_pq(ctx->ev_base, ctx->db_conninfo, dbfs_sql_error, ctx)) == NULL)
       
    57         ERROR("evsql_new_pq");
       
    58 
       
    59     // open fuse
       
    60     if ((ctx->ev_fuse = evfuse_new(ctx->ev_base, args, &dbfs_llops, ctx)) == NULL)
       
    61         ERROR("evfuse_new");
       
    62 
       
    63     // success
       
    64     return ctx;
       
    65 
       
    66 error:
       
    67     if (ctx)
       
    68         dbfs_release(ctx);
       
    69 
       
    70     return NULL;
       
    71 }    
       
    72 
       
    73 void dbfs_release (struct dbfs *ctx) {
       
    74     // cleanup
       
    75     if (ctx->ev_fuse) {
       
    76         evfuse_free(ctx->ev_fuse);
       
    77     
       
    78         ctx->ev_fuse = NULL;
       
    79     }
       
    80 
       
    81     if (ctx->db) {
       
    82         // XXX: not yet implemented 
       
    83         // evsql_close(ctx->db);
       
    84         // ctx->db = NULL;
       
    85     }
       
    86     
       
    87     free(ctx);
       
    88 }