1. /* This is a wrapper to the write syscall in order to retry on short writes
    2. * or if the syscall gets interrupted. It could look strange that we retry
    3. * on short writes given that we are writing to a block device: normally if
    4. * the first call is short, there is a end-of-space condition, so the next
    5. * is likely to fail. However apparently in modern systems this is no longer
    6. * true, and in general it looks just more resilient to retry the write. If
    7. * there is an actual error condition we'll get it at the next try. */
    8. ssize_t aofWrite(int fd, const char *buf, size_t len)
    9. {
    10. ssize_t nwritten = 0, totwritten = 0;
    11. while (len)
    12. {
    13. nwritten = write(fd, buf, len);
    14. if (nwritten < 0)
    15. {
    16. if (errno == EINTR)
    17. continue;
    18. return totwritten ? totwritten : -1;
    19. }
    20. len -= nwritten;
    21. buf += nwritten;
    22. totwritten += nwritten;
    23. }
    24. return totwritten;
    25. }