static int apipe_write( const char* path, const char* buf, size_t count, off_t offset, struct fuse_file_info* info) { if (count == 0) return 0; struct PP_Var req_var = nspawn_dict_create(); nspawn_dict_setstring(req_var, "command", "nacl_apipe_write"); nspawn_dict_setint(req_var, "pipe_id", info->fh); struct PP_Var data = PSInterfaceVarArrayBuffer()->Create(count); if (data.type == PP_VARTYPE_NULL) return -EIO; void *p = PSInterfaceVarArrayBuffer()->Map(data); if (count > 0 && !p) { nspawn_var_release(data); nspawn_var_release(req_var); return -EIO; } memcpy(p, buf, count); PSInterfaceVarArrayBuffer()->Unmap(data); nspawn_dict_set(req_var, "data", data); struct PP_Var result_var = nspawn_send_request(req_var); int ret = nspawn_dict_getint(result_var, "count"); nspawn_var_release(result_var); return ret; }
static int apipe_release(const char* path, struct fuse_file_info* info) { struct PP_Var req_var = nspawn_dict_create(); nspawn_dict_setstring(req_var, "command", "nacl_apipe_close"); nspawn_dict_setint(req_var, "pipe_id", info->fh); nspawn_dict_setint(req_var, "writer", (info->flags & O_WRONLY) == O_WRONLY); struct PP_Var result_var = nspawn_send_request(req_var); int ret = nspawn_dict_getint(result_var, "result"); nspawn_var_release(result_var); return ret; }
static int apipe_read( const char* path, char* buf, size_t count, off_t offset, struct fuse_file_info* info) { struct PP_Var req_var = nspawn_dict_create(); nspawn_dict_setstring(req_var, "command", "nacl_apipe_read"); nspawn_dict_setint(req_var, "pipe_id", info->fh); nspawn_dict_setint(req_var, "count", count); nspawn_dict_setint(req_var, "nonblock", (info->flags & O_NONBLOCK) == O_NONBLOCK); struct PP_Var result_var = nspawn_send_request(req_var); int err = nspawn_dict_getint(result_var, "error"); if (err != 0) { nspawn_var_release(result_var); return -err; } struct PP_Var data = nspawn_dict_get(result_var, "data"); assert(data.type == PP_VARTYPE_ARRAY_BUFFER); uint32_t len; if (!PSInterfaceVarArrayBuffer()->ByteLength(data, &len)) { nspawn_var_release(data); nspawn_var_release(result_var); return -EIO; } void *p = PSInterfaceVarArrayBuffer()->Map(data); if (len > 0 && !p) { nspawn_var_release(data); nspawn_var_release(result_var); return -EIO; } assert(len <= count); memcpy(buf, p, len); PSInterfaceVarArrayBuffer()->Unmap(data); nspawn_var_release(data); nspawn_var_release(result_var); return len; }
int nspawn_dict_getint_release(struct PP_Var dict_var, const char* key) { int ret = nspawn_dict_getint(dict_var, key); nspawn_var_release(dict_var); return ret; }