src/dbfs/fileop.c
changeset 31 7804cd7b5cd5
parent 30 d8fabd347a8e
child 35 4f10421681d2
equal deleted inserted replaced
30:d8fabd347a8e 31:7804cd7b5cd5
   137     // check the results
   137     // check the results
   138     if ((err = _dbfs_check_res(res, 1, 1)) < 0)
   138     if ((err = _dbfs_check_res(res, 1, 1)) < 0)
   139         SERROR(err = EIO);
   139         SERROR(err = EIO);
   140         
   140         
   141     // get the data
   141     // get the data
   142     if (evsql_result_buf(res, 0, 0, &buf, &size, 0))
   142     if (evsql_result_binary(res, 0, 0, &buf, &size, 0))
   143         SERROR(err = EIO);
   143         SERROR(err = EIO);
   144 
   144 
   145     INFO("[dbfs.read %p:%p] -> size=%zu", fop, fop->base.req, size);
   145     INFO("[dbfs.read %p:%p] -> size=%zu", fop, fop->base.req, size);
   146         
   146         
   147     // send it
   147     // send it
   206 error:
   206 error:
   207     // fail it
   207     // fail it
   208     dbfs_op_fail(&fop->base, err);
   208     dbfs_op_fail(&fop->base, err);
   209 }
   209 }
   210 
   210 
       
   211 void dbfs_write_res (const struct evsql_result_info *res, void *arg) {
       
   212     struct dbfs_fileop *fop = arg;
       
   213     int err;
       
   214     uint32_t size;
       
   215  
       
   216     // check the results
       
   217     if ((err = _dbfs_check_res(res, 1, 1)) < 0)
       
   218         SERROR(err = EIO);
       
   219         
       
   220     // get the size
       
   221     if (evsql_result_uint32(res, 0, 0, &size, 0))
       
   222         SERROR(err = EIO);
       
   223 
       
   224     INFO("[dbfs.write %p:%p] -> size=%lu", fop, fop->base.req, (long unsigned int) size);
       
   225         
       
   226     // send it
       
   227     if ((err = fuse_reply_write(fop->base.req, size)))
       
   228         EERROR(err, "fuse_reply_write");
       
   229     
       
   230     // ok, req handled
       
   231     if ((err = dbfs_op_req_done(&fop->base)))
       
   232         goto error;
       
   233 
       
   234     // good, fallthrough
       
   235     err = 0;
       
   236 
       
   237 error:
       
   238     if (err)
       
   239         dbfs_op_fail(&fop->base, err);
       
   240 
       
   241     // free
       
   242     evsql_result_free(res);
       
   243 }
       
   244 
   211 void dbfs_write (struct fuse_req *req, fuse_ino_t ino, const char *buf, size_t size, off_t off, struct fuse_file_info *fi) {
   245 void dbfs_write (struct fuse_req *req, fuse_ino_t ino, const char *buf, size_t size, off_t off, struct fuse_file_info *fi) {
   212 
   246     struct dbfs *ctx = fuse_req_userdata(req);
       
   247     struct dbfs_fileop *fop;
       
   248     int err;
       
   249     
       
   250     // get the op
       
   251     if ((fop = (struct dbfs_fileop *) dbfs_op_req(req, ino, fi)) == NULL)
       
   252         return;
       
   253 
       
   254     // log
       
   255     INFO("[dbfs.write %p:%p] ino=%lu, size=%zu, off=%lu, fi->flags=%04X", fop, req, ino, size, off, fi->flags);
       
   256 
       
   257     // query
       
   258     const char *sql = 
       
   259         "SELECT"
       
   260         " lo_pwrite($1::int4, $2::bytea, $3::int4)";
       
   261     
       
   262     static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) {
       
   263         EVSQL_PARAM ( UINT32 ), // fd
       
   264         EVSQL_PARAM ( BINARY ), // buf
       
   265         EVSQL_PARAM ( UINT32 ), // off
       
   266 
       
   267         EVSQL_PARAMS_END
       
   268     };
       
   269 
       
   270     // build params
       
   271     if (0
       
   272         ||  evsql_param_uint32(&params, 0, fop->lo_fd)
       
   273         ||  evsql_param_binary(&params, 1, buf, size)
       
   274         ||  evsql_param_uint32(&params, 2, off)
       
   275     )
       
   276         SERROR(err = EIO);
       
   277         
       
   278     // query
       
   279     if (evsql_query_params(ctx->db, fop->base.trans, sql, &params, dbfs_write_res, fop) == NULL)
       
   280         SERROR(err = EIO);
       
   281 
       
   282     // ok, wait for the info results
       
   283     return;
       
   284 
       
   285 error:
       
   286     // fail it
       
   287     dbfs_op_fail(&fop->base, err);
   213 }
   288 }
   214 
   289 
   215 void dbfs_flush (struct fuse_req *req, fuse_ino_t ino, struct fuse_file_info *fi) {
   290 void dbfs_flush (struct fuse_req *req, fuse_ino_t ino, struct fuse_file_info *fi) {
   216     struct dbfs_fileop *fop;
   291     struct dbfs_fileop *fop;
   217     int err;
   292     int err;