]> git.pond.sub.org Git - empserver/commitdiff
New ef_truncate()
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 9 Mar 2008 12:42:12 +0000 (13:42 +0100)
committerMarkus Armbruster <armbru@pond.sub.org>
Fri, 14 Mar 2008 19:25:10 +0000 (20:25 +0100)
include/file.h
src/lib/common/file.c

index fc57162029be7c7751b4cc4e8cd20be7b149951b..c0eca3ade69b56b56a6fd93e1f547f835af08c22 100644 (file)
@@ -187,6 +187,7 @@ extern void ef_blank(int, int, void *);
 extern int ef_write(int, int, void *);
 extern int ef_extend(int, int);
 extern int ef_ensure_space(int, int, int);
+extern int ef_truncate(int, int);
 extern int ef_nelem(int);
 extern int ef_flags(int);
 extern int ef_byname(char *);
index caf5505292de9c9f581002c5eaf5665ed31e8990..f2e4353a22337cef4091cb21dfff88425d5a23f9 100644 (file)
@@ -514,6 +514,49 @@ do_blank(struct empfile *ep, void *buf, int id, int count)
     }
 }
 
+/*
+ * Truncate table TYPE to COUNT elements.
+ * Any pointers obtained from ef_ptr() become invalid.
+ * Return non-zero on success, zero on failure.
+ */
+int
+ef_truncate(int type, int count)
+{
+    struct empfile *ep;
+
+    if (ef_check(type) < 0)
+       return 0;
+    ep = &empfile[type];
+    if (CANT_HAPPEN(count < 0 || count > ep->fids))
+       return 0;
+
+    if (ep->fd >= 0 && !(ep->flags & EFF_PRIVATE)) {
+       if (ftruncate(ep->fd, count * ep->size) < 0) {
+           logerror("Can't truncate %s to %d elements (%s)",
+                    ep->file, count, strerror(errno));
+           return 0;
+       }
+    }
+    ep->fids = count;
+
+    if (ep->flags & EFF_MEM) {
+       if (!(ep->flags & EFF_STATIC)) {
+           if (!ef_realloc_cache(ep, count)) {
+               logerror("Can't shrink cache after truncate");
+               /* continue with unshrunk cache */
+           }
+       }
+       ep->cids = count;
+    } else {
+       if (ep->baseid >= count)
+           ep->cids = 0;
+       else if (ep->cids > count - ep->baseid)
+           ep->cids = count - ep->baseid;
+    }
+
+    return 1;
+}
+
 struct castr *
 ef_cadef(int type)
 {