}
while (len) {
unsigned todo = len < sizeof(buf) ? len : sizeof(buf);
- if (tdb->methods->write(tdb, off, buf, todo) == -1)
+ if (tdb->methods->twrite(tdb, off, buf, todo) == -1)
return -1;
len -= todo;
off += todo;
return ret;
}
-/* Even on files, we can get partial writes due to signals. */
-bool tdb_pwrite_all(int fd, const void *buf, size_t len, tdb_off_t off)
-{
- while (len) {
- ssize_t ret;
- ret = pwrite(fd, buf, len, off);
- if (ret < 0)
- return false;
- if (ret == 0) {
- errno = ENOSPC;
- return false;
- }
- buf = (char *)buf + ret;
- off += ret;
- len -= ret;
- }
- return true;
-}
-
-/* Even on files, we can get partial reads due to signals. */
-bool tdb_pread_all(int fd, void *buf, size_t len, tdb_off_t off)
-{
- while (len) {
- ssize_t ret;
- ret = pread(fd, buf, len, off);
- if (ret < 0)
- return false;
- if (ret == 0) {
- /* ETOOSHORT? */
- errno = EWOULDBLOCK;
- return false;
- }
- buf = (char *)buf + ret;
- off += ret;
- len -= ret;
- }
- return true;
-}
-
-bool tdb_read_all(int fd, void *buf, size_t len)
-{
- while (len) {
- ssize_t ret;
- ret = read(fd, buf, len);
- if (ret < 0)
- return false;
- if (ret == 0) {
- /* ETOOSHORT? */
- errno = EWOULDBLOCK;
- return false;
- }
- buf = (char *)buf + ret;
- len -= ret;
- }
- return true;
-}
-
/* write a lump of data at a specified offset */
static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
const void *buf, tdb_len_t len)
if (tdb->map_ptr) {
memcpy(off + (char *)tdb->map_ptr, buf, len);
} else {
- if (!tdb_pwrite_all(tdb->fd, buf, len, off)) {
+ ssize_t ret;
+ ret = pwrite(tdb->fd, buf, len, off);
+ if (ret < len) {
+ /* This shouldn't happen: we avoid sparse files. */
+ if (ret >= 0)
+ errno = ENOSPC;
+
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
- "tdb_write failed at %zu len=%zu (%s)",
- (size_t)off, (size_t)len, strerror(errno));
+ "tdb_write: %zi at %zu len=%zu (%s)",
+ ret, (size_t)off, (size_t)len,
+ strerror(errno));
return -1;
}
}
if (tdb->map_ptr) {
memcpy(buf, off + (char *)tdb->map_ptr, len);
} else {
- if (!tdb_pread_all(tdb->fd, buf, len, off)) {
+ ssize_t r = pread(tdb->fd, buf, len, off);
+ if (r != len) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
- "tdb_read failed at %zu "
+ "tdb_read failed with %zi at %zu "
"len=%zu (%s) map_size=%zu",
- (size_t)off, (size_t)len,
- strerror(errno),
- (size_t)tdb->map_size);
+ r, (size_t)off, (size_t)len,
+ strerror(errno),
+ (size_t)tdb->map_size);
return -1;
}
}
return -1;
}
memcpy(conv, rec, len);
- ret = tdb->methods->write(tdb, off,
- tdb_convert(tdb, conv, len), len);
+ ret = tdb->methods->twrite(tdb, off,
+ tdb_convert(tdb, conv, len), len);
free(conv);
} else
- ret = tdb->methods->write(tdb, off, rec, len);
+ ret = tdb->methods->twrite(tdb, off, rec, len);
return ret;
}
int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
void *rec, size_t len)
{
- int ret = tdb->methods->read(tdb, off, rec, len);
+ int ret = tdb->methods->tread(tdb, off, rec, len);
tdb_convert(tdb, rec, len);
return ret;
}
tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
"tdb_alloc_read malloc failed len=%zu",
(size_t)(prefix + len));
- } else if (unlikely(tdb->methods->read(tdb, offset, buf+prefix,
- len) == -1)) {
+ } else if (unlikely(tdb->methods->tread(tdb, offset, buf+prefix, len)
+ == -1)) {
free(buf);
buf = NULL;
}
{
while (len) {
size_t n = len > size ? size : len;
+ ssize_t ret = pwrite(tdb->fd, buf, n, off);
+ if (ret < n) {
+ if (ret >= 0)
+ errno = ENOSPC;
- if (!tdb_pwrite_all(tdb->fd, buf, n, off)) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
- "fill write failed: giving up!");
+ "fill failed: %zi at %zu len=%zu (%s)",
+ ret, (size_t)off, (size_t)len,
+ strerror(errno));
return -1;
}
len -= n;
}
static void *tdb_direct(struct tdb_context *tdb, tdb_off_t off, size_t len,
- bool write)
+ bool write_mode)
{
if (unlikely(!tdb->map_ptr))
return NULL;
return (char *)tdb->map_ptr + off;
}
-void add_stat_(struct tdb_context *tdb, uint64_t *stat, size_t val)
+void add_stat_(struct tdb_context *tdb, uint64_t *s, size_t val)
{
- if ((uintptr_t)stat < (uintptr_t)tdb->stats + tdb->stats->size)
- *stat += val;
+ if ((uintptr_t)s < (uintptr_t)tdb->stats + tdb->stats->size)
+ *s += val;
}
static const struct tdb_methods io_methods = {