Returns 1 or 0 for success/failure. * * When buf is NULL and len is 0, the function performs a flush operation * if there is some pending buffer, so this function is also used in order * to implement rioFdFlush(). */
| 328 | * if there is some pending buffer, so this function is also used in order |
| 329 | * to implement rioFdFlush(). */ |
| 330 | static size_t rioFdWrite(rio *r, const void *buf, size_t len) { |
| 331 | ssize_t retval; |
| 332 | unsigned char *p = (unsigned char*) buf; |
| 333 | int doflush = (buf == NULL && len == 0); |
| 334 | |
| 335 | /* For small writes, we rather keep the data in user-space buffer, and flush |
| 336 | * it only when it grows. however for larger writes, we prefer to flush |
| 337 | * any pre-existing buffer, and write the new one directly without reallocs |
| 338 | * and memory copying. */ |
| 339 | if (len > PROTO_IOBUF_LEN) { |
| 340 | /* First, flush any pre-existing buffered data. */ |
| 341 | if (sdslen(r->io.fd.buf)) { |
| 342 | if (rioFdWrite(r, NULL, 0) == 0) |
| 343 | return 0; |
| 344 | } |
| 345 | /* Write the new data, keeping 'p' and 'len' from the input. */ |
| 346 | } else { |
| 347 | if (len) { |
| 348 | r->io.fd.buf = sdscatlen(r->io.fd.buf,buf,len); |
| 349 | if (sdslen(r->io.fd.buf) > PROTO_IOBUF_LEN) |
| 350 | doflush = 1; |
| 351 | if (!doflush) |
| 352 | return 1; |
| 353 | } |
| 354 | /* Flusing the buffered data. set 'p' and 'len' accordintly. */ |
| 355 | p = (unsigned char*) r->io.fd.buf; |
| 356 | len = sdslen(r->io.fd.buf); |
| 357 | } |
| 358 | |
| 359 | size_t nwritten = 0; |
| 360 | while(nwritten != len) { |
| 361 | retval = write(r->io.fd.fd,p+nwritten,len-nwritten); |
| 362 | if (retval <= 0) { |
| 363 | /* With blocking io, which is the sole user of this |
| 364 | * rio target, EWOULDBLOCK is returned only because of |
| 365 | * the SO_SNDTIMEO socket option, so we translate the error |
| 366 | * into one more recognizable by the user. */ |
| 367 | if (retval == -1 && errno == EWOULDBLOCK) errno = ETIMEDOUT; |
| 368 | return 0; /* error. */ |
| 369 | } |
| 370 | nwritten += retval; |
| 371 | } |
| 372 | |
| 373 | r->io.fd.pos += len; |
| 374 | sdsclear(r->io.fd.buf); |
| 375 | return 1; |
| 376 | } |
| 377 | |
| 378 | /* Returns 1 or 0 for success/failure. */ |
| 379 | static size_t rioFdRead(rio *r, void *buf, size_t len) { |
no test coverage detected