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(). */
| 287 | * if there is some pending buffer, so this function is also used in order |
| 288 | * to implement rioFdFlush(). */ |
| 289 | static size_t rioFdWrite(rio *r, const void *buf, size_t len) { |
| 290 | ssize_t retval; |
| 291 | unsigned char *p = (unsigned char*) buf; |
| 292 | int doflush = (buf == NULL && len == 0); |
| 293 | |
| 294 | /* For small writes, we rather keep the data in user-space buffer, and flush |
| 295 | * it only when it grows. however for larger writes, we prefer to flush |
| 296 | * any pre-existing buffer, and write the new one directly without reallocs |
| 297 | * and memory copying. */ |
| 298 | if (len > PROTO_IOBUF_LEN) { |
| 299 | /* First, flush any pre-existing buffered data. */ |
| 300 | if (sdslen(r->io.fd.buf)) { |
| 301 | if (rioFdWrite(r, NULL, 0) == 0) |
| 302 | return 0; |
| 303 | } |
| 304 | /* Write the new data, keeping 'p' and 'len' from the input. */ |
| 305 | } else { |
| 306 | if (len) { |
| 307 | r->io.fd.buf = sdscatlen(r->io.fd.buf,buf,len); |
| 308 | if (sdslen(r->io.fd.buf) > PROTO_IOBUF_LEN) |
| 309 | doflush = 1; |
| 310 | if (!doflush) |
| 311 | return 1; |
| 312 | } |
| 313 | /* Flusing the buffered data. set 'p' and 'len' accordintly. */ |
| 314 | p = (unsigned char*) r->io.fd.buf; |
| 315 | len = sdslen(r->io.fd.buf); |
| 316 | } |
| 317 | |
| 318 | size_t nwritten = 0; |
| 319 | while(nwritten != len) { |
| 320 | retval = write(r->io.fd.fd,p+nwritten,len-nwritten); |
| 321 | if (retval <= 0) { |
| 322 | /* With blocking io, which is the sole user of this |
| 323 | * rio target, EWOULDBLOCK is returned only because of |
| 324 | * the SO_SNDTIMEO socket option, so we translate the error |
| 325 | * into one more recognizable by the user. */ |
| 326 | if (retval == -1 && errno == EWOULDBLOCK) errno = ETIMEDOUT; |
| 327 | return 0; /* error. */ |
| 328 | } |
| 329 | nwritten += retval; |
| 330 | } |
| 331 | |
| 332 | r->io.fd.pos += len; |
| 333 | sdsclear(r->io.fd.buf); |
| 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | /* Returns 1 or 0 for success/failure. */ |
| 338 | static size_t rioFdRead(rio *r, void *buf, size_t len) { |
no test coverage detected