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(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 0;
}
-/* This is only neded for tdb_access_commit, but used everywhere to simplify. */
-struct tdb_access_hdr {
- tdb_off_t off;
- tdb_len_t len;
- bool convert;
-};
-
const void *tdb_access_read(struct tdb_context *tdb,
tdb_off_t off, tdb_len_t len, bool convert)
{
struct tdb_access_hdr *hdr;
hdr = _tdb_alloc_read(tdb, off, len, sizeof(*hdr));
if (hdr) {
+ hdr->next = tdb->access;
+ tdb->access = hdr;
ret = hdr + 1;
if (convert)
tdb_convert(tdb, (void *)ret, len);
struct tdb_access_hdr *hdr;
hdr = _tdb_alloc_read(tdb, off, len, sizeof(*hdr));
if (hdr) {
+ hdr->next = tdb->access;
+ tdb->access = hdr;
hdr->off = off;
hdr->len = len;
hdr->convert = convert;
return ret;
}
-bool is_direct(const struct tdb_context *tdb, const void *p)
+static struct tdb_access_hdr **find_hdr(struct tdb_context *tdb, const void *p)
{
- return (tdb->map_ptr
- && (char *)p >= (char *)tdb->map_ptr
- && (char *)p < (char *)tdb->map_ptr + tdb->map_size);
+ struct tdb_access_hdr **hp;
+
+ for (hp = &tdb->access; *hp; hp = &(*hp)->next) {
+ if (*hp + 1 == p)
+ return hp;
+ }
+ return NULL;
}
void tdb_access_release(struct tdb_context *tdb, const void *p)
{
- if (is_direct(tdb, p))
+ struct tdb_access_hdr *hdr, **hp = find_hdr(tdb, p);
+
+ if (hp) {
+ hdr = *hp;
+ *hp = hdr->next;
+ free(hdr);
+ } else
tdb->direct_access--;
- else
- free((struct tdb_access_hdr *)p - 1);
}
int tdb_access_commit(struct tdb_context *tdb, void *p)
{
+ struct tdb_access_hdr *hdr, **hp = find_hdr(tdb, p);
int ret = 0;
- if (!tdb->map_ptr
- || (char *)p < (char *)tdb->map_ptr
- || (char *)p >= (char *)tdb->map_ptr + tdb->map_size) {
- struct tdb_access_hdr *hdr;
-
- hdr = (struct tdb_access_hdr *)p - 1;
+ if (hp) {
+ hdr = *hp;
if (hdr->convert)
ret = tdb_write_convert(tdb, hdr->off, p, hdr->len);
else
ret = tdb_write(tdb, hdr->off, p, hdr->len);
+ *hp = hdr->next;
free(hdr);
} else
tdb->direct_access--;
}
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 = {