ep->cache = NULL;
}
if (close(ep->fd) < 0) {
- logerror("Error closing %s (%s)", ep->name, strerror(errno));
+ logerror("Error closing %s (%s)", ep->file, strerror(errno));
retval = 0;
}
ep->fd = -1;
{
struct empfile *ep;
char *p;
- int i, id;
+ int need_sentinel, i, id;
if (ef_check(type) < 0)
return 0;
id = ep->fids;
if (ep->flags & EFF_MEM) {
- if (id + count > ep->csize) {
+ need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
+ if (id + count + need_sentinel > ep->csize) {
if (ep->flags & EFF_STATIC) {
logerror("Can't extend %s beyond %d elements",
- ep->file, ep->csize);
+ ep->name, ep->csize - need_sentinel);
return 0;
}
- if (!ef_realloc_cache(ep, id + count)) {
+ if (!ef_realloc_cache(ep, id + count + need_sentinel)) {
logerror("Can't extend %s to %d elements (%s)",
- ep->file, id + count, strerror(errno));
+ ep->name, id + count, strerror(errno));
return 0;
}
}
if (do_write(ep, p, id, count) < 0)
return 0;
}
- ep->cids += count;
+ if (need_sentinel)
+ memset(ep->cache + (id + count) * ep->size, 0, ep->size);
+ ep->cids = id + count;
} else {
/* need a buffer, steal last cache slot */
if (ep->cids == ep->csize)
return 0;
}
}
- ep->fids += count;
+ ep->fids = id + count;
return 1;
}
ef_truncate(int type, int count)
{
struct empfile *ep;
+ int need_sentinel;
if (ef_check(type) < 0)
return 0;
ep->fids = count;
if (ep->flags & EFF_MEM) {
+ need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
if (!(ep->flags & EFF_STATIC)) {
- if (!ef_realloc_cache(ep, count)) {
- logerror("Can't shrink cache after truncate");
+ if (!ef_realloc_cache(ep, count + need_sentinel)) {
+ logerror("Can't shrink %s cache after truncate (%s)",
+ ep->name, strerror(errno));
/* continue with unshrunk cache */
}
}
+ if (need_sentinel)
+ memset(ep->cache + count * ep->size, 0, ep->size);
ep->cids = count;
} else {
if (ep->baseid >= count)
}
/*
- * Ensure file-backed table contains ID.
+ * Ensure table contains element ID.
* If necessary, extend it in steps of COUNT elements.
* Return non-zero on success, zero on failure.
*/