Look at the line you mentioned yourself: "std::memcpy(buf, reply.getBuf().begin(), res);". You probably meant "size" instead of "res". What's the output of "res = reply.getResult();" exactly? Unless it's not the size, you've got logic error and "memcpy" may go out-of-range when copying data. Does the destination buffer have enough space to hold all the copied data (you might need to guarantee it explicitly - it won't resize on it's own)? What's the type of "reply.getBuf().begin()"? Remember that "memcpy" operates on raw pointers.
-Paweł On Sun, 3 Nov 2019 at 06:18, Diogo Leitão <[email protected]> wrote: > > Hi > > I'm using FUSE + Cap'n Proto to build a distributed filesystem. Everything > seems to be fine, but when i try get the data field, on the client side, > using the getBuf method, i get a segmentation fault error. > > This is the code that i made. What am i doing wrong? > > > /* > > * Capnp source file > > */ > > interface FilesystemOps { > > ... > read @16 (request :ReadRequest) -> (reply :ReadReply); > > ... > } > > > struct ReadRequest { > path @0 :Text; > size @1 :UInt64; > offset @2 :Int64; > } > > struct ReadReply { > result @0 :Int32; > buf @1 :Data; > } > > > /* > > * Client side > > */ > > int FilesystemOpsClient::rpc_read(const char *path, char *buf, size_t size, > off_t offset, struct fuse_file_info *fi) { > std::cout << "rpc_read begin " << size << std::endl; > > int res; > auto clientData = getClient(); > auto request = clientData->cap_.readRequest(); > auto builder = request.getRequest(); > > builder.setPath(path); > builder.setSize(size); > builder.setOffset(offset); > > auto reply = request.send().wait(clientData->waitScope_).getReply(); > > res = reply.getResult(); > if (res != -1) { > std::memcpy(buf, reply.getBuf().begin(), res); > > } > > return res; > } > > > /* > > * Server side > > */ > > kj::Promise<void> FilesystemOpsImpl::read(FilesystemOps::Server::ReadContext > context) { > int res; > auto request = context.getParams().getRequest(); > auto reply = context.getResults().getReply(); > > std::string path = root_path_ + request.getPath().cStr(); > size_t size = request.getSize(); > off_t offset = request.getOffset(); > > int fd = ::open(path.c_str(), O_RDONLY); > if (fd == -1) { > res = -1; > } else { > res = pread(fd, reply.initBuf(size).begin(), size, offset); > close(fd); > } > > reply.setResult(res); > > return kj::READY_NOW; > } > > > Appreciate your help. Thank you. > > > -- > You received this message because you are subscribed to the Google Groups > "Cap'n Proto" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/capnproto/ee51e0ec-819c-4962-9245-41cafcd03862%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "Cap'n Proto" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/CAPqF3Br3LQBF_%2BmbCpcDQBOi-890Z_N7snBWDXtoWxbnkestpA%40mail.gmail.com.
