disk. This must be done with write, not via mmap */
memset(buf, TDB_PAD_BYTE, sizeof(buf));
while (addition) {
- int n = addition>sizeof(buf)?sizeof(buf):addition;
- int ret = pwrite(tdb->fd, buf, n, size);
- if (ret == -1) {
- TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file write of %d failed (%s)\n",
- n, strerror(errno)));
+ size_t n = addition>sizeof(buf)?sizeof(buf):addition;
+ ssize_t written = pwrite(tdb->fd, buf, n, size);
+ if (written == 0) {
+ /* prevent infinite loops: try _once_ more */
+ written = pwrite(tdb->fd, buf, n, size);
+ }
+ if (written == 0) {
+ /* give up, trying to provide a useful errno */
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file write "
+ "returned 0 twice: giving up!\n"));
+ errno = ENOSPC;
+ return -1;
+ } else if (written == -1) {
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file write of "
+ "%d bytes failed (%s)\n", n, strerror(errno)));
return -1;
+ } else if (written != n) {
+ TDB_LOG((tdb, TDB_DEBUG_WARNING, "expand_file: wrote "
+ "only %d of %d bytes - retrying\n", written,n));
}
- addition -= ret;
- size += ret;
+ addition -= written;
+ size += written;
}
return 0;
}