r23798: updated old Temple Place FSF addresses to new URL
[samba.git] / source4 / lib / tdb / common / io.c
index a276a8c291af695ca97f65966e24650ff2d1808c..cc66b85b3384d3620f3523e201e9d0b6818a44ac 100644 (file)
@@ -14,7 +14,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 
 #include "tdb_private.h"
 
-#ifndef HAVE_PREAD
- static ssize_t pread(int fd, void *buf, size_t count, off_t offset)
-{
-       if (lseek(fd, offset, SEEK_SET) != offset) {
-               errno = EIO;
-               return -1;
-       }
-       return read(fd, buf, count);
-}
-#endif
-
-#ifndef HAVE_PWRITE
- static ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset)
-{
-       if (lseek(fd, offset, SEEK_SET) != offset) {
-               errno = EIO;
-               return -1;
-       }
-       return write(fd, buf, count);
-}
-#endif
-
 /* check for an out of bounds access - if it is out of bounds then
    see if the database has been expanded by someone else and expand
    if necessary 
@@ -65,7 +42,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
                if (!probe) {
                        /* Ensure ecode is set for log fn. */
                        tdb->ecode = TDB_ERR_IO;
-                       TDB_LOG((tdb, 0,"tdb_oob len %d beyond internal malloc size %d\n",
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond internal malloc size %d\n",
                                 (int)len, (int)tdb->map_size));
                }
                return TDB_ERRCODE(TDB_ERR_IO, -1);
@@ -79,7 +56,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
                if (!probe) {
                        /* Ensure ecode is set for log fn. */
                        tdb->ecode = TDB_ERR_IO;
-                       TDB_LOG((tdb, 0,"tdb_oob len %d beyond eof at %d\n",
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond eof at %d\n",
                                 (int)len, (int)st.st_size));
                }
                return TDB_ERRCODE(TDB_ERR_IO, -1);
@@ -97,6 +74,10 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
 static int tdb_write(struct tdb_context *tdb, tdb_off_t off, 
                     const void *buf, tdb_len_t len)
 {
+       if (len == 0) {
+               return 0;
+       }
+
        if (tdb->read_only || tdb->traverse_read) {
                tdb->ecode = TDB_ERR_RDONLY;
                return -1;
@@ -110,7 +91,7 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
        } else if (pwrite(tdb->fd, buf, len, off) != (ssize_t)len) {
                /* Ensure ecode is set for log fn. */
                tdb->ecode = TDB_ERR_IO;
-               TDB_LOG((tdb, 0,"tdb_write failed at %d len=%d (%s)\n",
+               TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_write failed at %d len=%d (%s)\n",
                           off, len, strerror(errno)));
                return TDB_ERRCODE(TDB_ERR_IO, -1);
        }
@@ -120,7 +101,7 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
 /* Endian conversion: we only ever deal with 4 byte quantities */
 void *tdb_convert(void *buf, u32 size)
 {
-       u32 i, *p = buf;
+       u32 i, *p = (u32 *)buf;
        for (i = 0; i < size / 4; i++)
                p[i] = TDB_BYTEREV(p[i]);
        return buf;
@@ -142,8 +123,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, 0,"tdb_read failed at %d len=%d ret=%d (%s) map_size=%d\n",
-                                off, len, ret, strerror(errno), 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);
                }
        }
@@ -169,7 +152,7 @@ static void tdb_next_hash_chain(struct tdb_context *tdb, u32 *chain)
                        }
                }
        } else {
-               u32 off;
+               u32 off=0;
                for (;h < tdb->header.hash_size;h++) {
                        if (tdb_ofs_read(tdb, TDB_HASH_TOP(h), &off) != 0 || off != 0) {
                                break;
@@ -213,7 +196,7 @@ void tdb_mmap(struct tdb_context *tdb)
 
                if (tdb->map_ptr == MAP_FAILED) {
                        tdb->map_ptr = NULL;
-                       TDB_LOG((tdb, 2, "tdb_mmap failed for size %d (%s)\n", 
+                       TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_mmap failed for size %d (%s)\n", 
                                 tdb->map_size, strerror(errno)));
                }
        } else {
@@ -238,7 +221,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_off_t size, tdb_off_t ad
        if (ftruncate(tdb->fd, size+addition) == -1) {
                char b = 0;
                if (pwrite(tdb->fd,  &b, 1, (size+addition) - 1) != 1) {
-                       TDB_LOG((tdb, 0, "expand_file to %d failed (%s)\n", 
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file to %d failed (%s)\n", 
                                 size+addition, strerror(errno)));
                        return -1;
                }
@@ -252,7 +235,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_off_t size, tdb_off_t ad
                int n = addition>sizeof(buf)?sizeof(buf):addition;
                int ret = pwrite(tdb->fd, buf, n, size);
                if (ret != n) {
-                       TDB_LOG((tdb, 0, "expand_file write of %d failed (%s)\n", 
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file write of %d failed (%s)\n", 
                                   n, strerror(errno)));
                        return -1;
                }
@@ -271,7 +254,7 @@ int tdb_expand(struct tdb_context *tdb, tdb_off_t size)
        tdb_off_t offset;
 
        if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
-               TDB_LOG((tdb, 0, "lock failed in tdb_expand\n"));
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "lock failed in tdb_expand\n"));
                return -1;
        }
 
@@ -300,7 +283,8 @@ int tdb_expand(struct tdb_context *tdb, tdb_off_t size)
        tdb->map_size += size;
 
        if (tdb->flags & TDB_INTERNAL) {
-               char *new_map_ptr = realloc(tdb->map_ptr, tdb->map_size);
+               char *new_map_ptr = (char *)realloc(tdb->map_ptr,
+                                                   tdb->map_size);
                if (!new_map_ptr) {
                        tdb->map_size -= size;
                        goto fail;
@@ -351,10 +335,15 @@ unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len
 {
        unsigned char *buf;
 
-       if (!(buf = malloc(len))) {
+       /* some systems don't like zero length malloc */
+       if (len == 0) {
+               len = 1;
+       }
+
+       if (!(buf = (unsigned char *)malloc(len))) {
                /* Ensure ecode is set for log fn. */
                tdb->ecode = TDB_ERR_OOM;
-               TDB_LOG((tdb, 0,"tdb_alloc_read malloc failed len=%d (%s)\n",
+               TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_alloc_read malloc failed len=%d (%s)\n",
                           len, strerror(errno)));
                return TDB_ERRCODE(TDB_ERR_OOM, buf);
        }
@@ -365,6 +354,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)
 {
@@ -373,7 +396,7 @@ int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *
        if (TDB_BAD_MAGIC(rec)) {
                /* Ensure ecode is set for log fn. */
                tdb->ecode = TDB_ERR_CORRUPT;
-               TDB_LOG((tdb, 0,"tdb_rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
                return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
        }
        return tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0);
@@ -386,12 +409,12 @@ int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct list_struct
 }
 
 static const struct tdb_methods io_methods = {
-       .tdb_read        = tdb_read,
-       .tdb_write       = tdb_write,
-       .next_hash_chain = tdb_next_hash_chain,
-       .tdb_oob         = tdb_oob,
-       .tdb_expand_file = tdb_expand_file,
-       .tdb_brlock      = tdb_brlock
+       tdb_read,
+       tdb_write,
+       tdb_next_hash_chain,
+       tdb_oob,
+       tdb_expand_file,
+       tdb_brlock
 };
 
 /*