summaryrefslogtreecommitdiffstats
path: root/ctdb/lib/tdb/common/io.c
diff options
context:
space:
mode:
authorRonnie sahlberg <ronniesahlberg@gmail.com>2007-04-17 09:37:13 +1000
committerRonnie sahlberg <ronniesahlberg@gmail.com>2007-04-17 09:37:13 +1000
commitc6fe6d592b31096e3334116e7c36aed1ea7eb921 (patch)
tree8f04e127da9546f130a77026ec7424b141697002 /ctdb/lib/tdb/common/io.c
parent995e017b3f6b061ecba240731b3d82915b3e1f86 (diff)
parent71bf837a1939f765419359a6bca5996d54fc662a (diff)
downloadsamba-c6fe6d592b31096e3334116e7c36aed1ea7eb921.tar.gz
samba-c6fe6d592b31096e3334116e7c36aed1ea7eb921.tar.xz
samba-c6fe6d592b31096e3334116e7c36aed1ea7eb921.zip
merge from tridge
(This used to be ctdb commit 1000ea33025b625e23e7065f95efad447c380c8c)
Diffstat (limited to 'ctdb/lib/tdb/common/io.c')
-rw-r--r--ctdb/lib/tdb/common/io.c42
1 files changed, 39 insertions, 3 deletions
diff --git a/ctdb/lib/tdb/common/io.c b/ctdb/lib/tdb/common/io.c
index 5d2c5c8e2e..cd06dbb1e3 100644
--- a/ctdb/lib/tdb/common/io.c
+++ b/ctdb/lib/tdb/common/io.c
@@ -124,8 +124,10 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
if (ret != (ssize_t)len) {
/* Ensure ecode is set for log fn. */
tdb->ecode = TDB_ERR_IO;
- TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_read failed at %d len=%d ret=%d (%s) map_size=%d\n",
- off, len, ret, strerror(errno), (int)tdb->map_size));
+ TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_read failed at %d "
+ "len=%d ret=%d (%s) map_size=%d\n",
+ (int)off, (int)len, (int)ret, strerror(errno),
+ (int)tdb->map_size));
return TDB_ERRCODE(TDB_ERR_IO, -1);
}
}
@@ -339,7 +341,7 @@ unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len
len = 1;
}
- if (!(buf = malloc(len))) {
+ if (!(buf = (unsigned char *)malloc(len))) {
/* Ensure ecode is set for log fn. */
tdb->ecode = TDB_ERR_OOM;
TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_alloc_read malloc failed len=%d (%s)\n",
@@ -353,6 +355,40 @@ unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len
return buf;
}
+/* Give a piece of tdb data to a parser */
+
+int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
+ tdb_off_t offset, tdb_len_t len,
+ int (*parser)(TDB_DATA key, TDB_DATA data,
+ void *private_data),
+ void *private_data)
+{
+ TDB_DATA data;
+ int result;
+
+ data.dsize = len;
+
+ if ((tdb->transaction == NULL) && (tdb->map_ptr != NULL)) {
+ /*
+ * Optimize by avoiding the malloc/memcpy/free, point the
+ * parser directly at the mmap area.
+ */
+ if (tdb->methods->tdb_oob(tdb, offset+len, 0) != 0) {
+ return -1;
+ }
+ data.dptr = offset + (unsigned char *)tdb->map_ptr;
+ return parser(key, data, private_data);
+ }
+
+ if (!(data.dptr = tdb_alloc_read(tdb, offset, len))) {
+ return -1;
+ }
+
+ result = parser(key, data, private_data);
+ free(data.dptr);
+ return result;
+}
+
/* read/write a record */
int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
{