src/dbfs/core.c
changeset 31 7804cd7b5cd5
parent 30 d8fabd347a8e
child 33 c71f3053c714
equal deleted inserted replaced
30:d8fabd347a8e 31:7804cd7b5cd5
     1 
     1 
       
     2 #include "dbfs.h"
     2 #include "../lib/log.h"
     3 #include "../lib/log.h"
     3 #include "../lib/misc.h"
     4 #include "../lib/misc.h"
     4 
       
     5 #include "dbfs.h"
       
     6 
     5 
     7 /*
     6 /*
     8  * Core fs functionality like lookup, getattr
     7  * Core fs functionality like lookup, getattr
     9  */
     8  */
    10 
     9 
    26         EERROR(err = EIO, "invalid db data");
    25         EERROR(err = EIO, "invalid db data");
    27         
    26         
    28     INFO("[dbfs.lookup] -> ino=%u", ino);
    27     INFO("[dbfs.lookup] -> ino=%u", ino);
    29     
    28     
    30     // stat attrs
    29     // stat attrs
    31     if (_dbfs_stat_info(&e.attr, res, 0, 1))
    30     if ((err = _dbfs_stat_info(&e.attr, res, 0, 1)))
    32         goto error;
    31         goto error;
    33 
    32 
    34     // other attrs
    33     // other attrs
    35     e.ino = e.attr.st_ino = ino;
    34     e.ino = e.attr.st_ino = ino;
    36     e.attr_timeout = CACHE_TIMEOUT;
    35     e.attr_timeout = CACHE_TIMEOUT;
    55     INFO("[dbfs.lookup] parent=%lu name=%s", parent, name);
    54     INFO("[dbfs.lookup] parent=%lu name=%s", parent, name);
    56     
    55     
    57     // query and params
    56     // query and params
    58     const char *sql = 
    57     const char *sql = 
    59         "SELECT"
    58         "SELECT"
    60         " inodes.ino, inodes.type, inodes.mode, dbfs_lo_size(data), count(*)"
    59         " inodes.ino, " DBFS_STAT_COLS
    61         " FROM file_tree INNER JOIN inodes ON (file_tree.inode = inodes.ino)"
    60         " FROM file_tree INNER JOIN inodes ON (file_tree.inode = inodes.ino)"
    62         " WHERE file_tree.parent = $1::int4 AND file_tree.name = $2::varchar"
    61         " WHERE file_tree.parent = $1::int4 AND file_tree.name = $2::varchar"
    63         " GROUP BY inodes.ino, inodes.type, inodes.mode, data";
    62         " GROUP BY inodes.ino, inodes.type, inodes.mode, data";
    64     
    63     
    65     static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) {
    64     static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) {
    88 error:
    87 error:
    89     if ((err = fuse_reply_err(req, err)))
    88     if ((err = fuse_reply_err(req, err)))
    90         EWARNING(err, "fuse_reply_err");
    89         EWARNING(err, "fuse_reply_err");
    91 }
    90 }
    92 
    91 
    93 void _dbfs_getattr_result (const struct evsql_result_info *res, void *arg) {
       
    94     struct fuse_req *req = arg;
       
    95     struct stat st; ZINIT(st);
       
    96     int err = 0;
       
    97     
       
    98     // check the results
       
    99     if ((err = _dbfs_check_res(res, 1, 4)))
       
   100         SERROR(err = (err ==  1 ? ENOENT : EIO));
       
   101         
       
   102     INFO("[dbfs.getattr %p] -> (stat follows)", req);
       
   103     
       
   104     // stat attrs
       
   105     if (_dbfs_stat_info(&st, res, 0, 0))
       
   106         goto error;
       
   107 
       
   108     // XXX: we don't have the ino
       
   109     st.st_ino = 0;
       
   110 
       
   111     // reply
       
   112     if ((err = fuse_reply_attr(req, &st, CACHE_TIMEOUT)))
       
   113         EERROR(err, "fuse_reply_entry");
       
   114 
       
   115 error:
       
   116     if (err && (err = fuse_reply_err(req, err)))
       
   117         EWARNING(err, "fuse_reply_err");
       
   118 
       
   119     // free
       
   120     evsql_result_free(res);
       
   121 }
       
   122 
       
   123 void dbfs_getattr (struct fuse_req *req, fuse_ino_t ino, struct fuse_file_info *fi) {
       
   124     struct dbfs *ctx = fuse_req_userdata(req);
       
   125     int err;
       
   126     
       
   127     (void) fi;
       
   128 
       
   129     INFO("[dbfs.getattr %p] ino=%lu", req, ino);
       
   130 
       
   131     const char *sql =
       
   132         "SELECT"
       
   133         " inodes.type, inodes.mode, dbfs_lo_size(data), count(*)"
       
   134         " FROM inodes"
       
   135         " WHERE inodes.ino = $1::int4"
       
   136         " GROUP BY inodes.type, inodes.mode, data";
       
   137 
       
   138     static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) {
       
   139         EVSQL_PARAM ( UINT32 ),
       
   140 
       
   141         EVSQL_PARAMS_END
       
   142     };
       
   143 
       
   144     // build params
       
   145     if (0
       
   146         ||  evsql_param_uint32(&params, 0, ino)
       
   147     )
       
   148         SERROR(err = EIO);
       
   149         
       
   150     // query
       
   151     if (evsql_query_params(ctx->db, NULL, sql, &params, _dbfs_getattr_result, req) == NULL)
       
   152         SERROR(err = EIO);
       
   153 
       
   154     // XXX: handle interrupts
       
   155     
       
   156     // wait
       
   157     return;
       
   158 
       
   159 error:
       
   160     if ((err = fuse_reply_err(req, err)))
       
   161         EWARNING(err, "fuse_reply_err");
       
   162 }
       
   163