terom@29: #ifndef DBFS_DBFS_H terom@29: #define DBFS_DBFS_H terom@29: terom@29: #include terom@29: #include terom@29: terom@29: #include terom@29: terom@29: #include "ops.h" terom@29: #include "../evfuse.h" terom@29: #include "../evsql.h" terom@41: #include "../lib/error.h" terom@29: terom@29: /* terom@29: * Structs and functions shared between all dbfs components terom@29: */ terom@29: terom@29: struct dbfs { terom@29: struct event_base *ev_base; terom@29: terom@29: const char *db_conninfo; terom@29: struct evsql *db; terom@29: terom@29: struct evfuse *ev_fuse; terom@29: }; terom@29: terom@29: // XXX: not sure how this should work terom@29: #define CACHE_TIMEOUT 1.0 terom@29: terom@31: // columns used for stat_info terom@33: #define DBFS_STAT_COLS " inodes.type, inodes.mode, dbfs_size(inodes.type, inodes.data, inodes.link_path), (SELECT COUNT(*) FROM inodes i LEFT JOIN file_tree ft ON (i.ino = ft.ino) WHERE i.ino = inodes.ino) AS nlink" terom@31: terom@29: /* terom@29: * Convert the CHAR(4) inodes.type from SQL into a mode_t. terom@29: * terom@29: * Returns zero for unknown types. terom@29: */ terom@29: mode_t _dbfs_mode (const char *type); terom@29: terom@29: /* terom@29: * Check that the number of rows and columns in the result set matches what we expect. terom@29: * terom@29: * If rows is nonzero, there must be exactly that many rows (mostly useful for rows=1). terom@29: * The number of columns must always be given, and match. terom@29: * terom@29: * Returns; terom@29: * -1 if the query failed, the columns/rows do not match terom@29: * 0 the results match terom@29: * 1 there were no results (zero rows) terom@29: */ terom@29: int _dbfs_check_res (const struct evsql_result_info *res, size_t rows, size_t cols); terom@29: terom@29: /* terom@36: * Same as _dbfs_check_res, but returns ENOENT/EIO directly terom@36: */ terom@36: err_t dbfs_check_result (const struct evsql_result_info *res, size_t rows, size_t cols); terom@36: terom@36: /* terom@29: * Fill a `struct state` with info retrieved from a SQL query. terom@29: * terom@29: * The result must contain four columns, starting at the given offset: terom@29: * inodes.type, inodes.mode, inodes.size, count(*) AS nlink terom@29: * terom@29: * Note that this does not fill the st_ino field terom@29: */ terom@29: int _dbfs_stat_info (struct stat *st, const struct evsql_result_info *res, size_t row, size_t col_offset); terom@29: terom@41: /** interrupt.c terom@41: * terom@41: * Fuse interrupts are handled using fuse_req_interrupt_func. Calling this registers a callback function with the req, terom@41: * which may or may not be called either by fuse_req_interrupt_func, or later on via evfuse's event handler. It is terom@41: * assumed that this will never be called after a call to fuse_reply_*. terom@41: * terom@41: * Hence, to handle an interrupt, we must first ensure that fuse_reply_* will not be called afterwards (it'll return terom@41: * an error), and then we must call fuse_reply_err(req, EINTR). terom@41: * terom@41: * In the simplest case, we can simply submit a query, and then abort it once the req is interrupted (now or later). terom@41: * In the more complicated case, we can check if the request was interrupted, if not, do the query and handle terom@41: * interrupts. terom@41: */ terom@41: terom@41: /* terom@41: * Useable as a callback to fuse_req_interrupt_func, will abort the given query and err the req. terom@42: * terom@42: * Due to a locking bug in libfuse 2.7.4, this will actually delay the fuse_req_err until the next event-loop iteration. terom@41: */ terom@41: void dbfs_interrupt_query (struct fuse_req *req, void *query_ptr); terom@41: terom@41: /* terom@41: * XXX: More complicated state, is this actually needed? terom@41: */ terom@41: struct dbfs_interrupt_ctx { terom@41: struct fuse_req *req; terom@41: struct evsql_query *query; terom@41: terom@41: int interrupted : 1; terom@41: }; terom@41: terom@41: /* terom@41: * Register as a fuse interrupt function for simple requests that only run one query without allocating any resources. terom@41: * terom@41: * This will abort the query if the interrupt is run, causing it's callback to not be called. terom@41: * terom@41: * Returns nonzero if the request was already interrupted, zero otherwise. Be careful that the interrupt does not get terom@41: * fired between you checking for it and setting query. terom@41: */ terom@41: int dbfs_interrupt_register (struct fuse_req *req, struct dbfs_interrupt_ctx *ctx); terom@41: terom@29: #endif /* DBFS_DBFS_H */