Add a comment to search_rbtree().
[osl.git] / osl.c
diff --git a/osl.c b/osl.c
index f0506a50821f1d095ec543a655022d46ece212bf..c92412fbce765444c7a0d713cdeeffca35d824c1 100644 (file)
--- a/osl.c
+++ b/osl.c
@@ -11,8 +11,7 @@
 #include "log.h"
 #include "osl.h"
 #include "error.h"
-#include "fd.h"
-#include "list.h"
+#include "util.h"
 #include "osl_core.h"
 
 /* Taken from Drepper: How to write shared libraries, Appendix B. */
 #define MSGSTRFIELD1(line) str##line
 static const union msgstr_t {
        struct {
-#define _S(n, s) char MSGSTRFIELD(__LINE__)[sizeof(s)];
+#define OSL_ERROR(n, s) char MSGSTRFIELD(__LINE__)[sizeof(s)];
 #include "errtab.h"
-#undef _S
+#undef OSL_ERROR
        };
        char str[0];
 } msgstr = { {
-#define _S(n, s) s,
+#define OSL_ERROR(n, s) s,
 #include "errtab.h"
-#undef _S
+#undef OSL_ERROR
 } };
 static const unsigned int errmsgidx[] = {
-#define _S(n, s) [n] = offsetof(union msgstr_t, MSGSTRFIELD(__LINE__)),
+#define OSL_ERROR(n, s) [n] = offsetof(union msgstr_t, MSGSTRFIELD(__LINE__)),
 #include "errtab.h"
-#undef _S
+#undef OSL_ERROR
 };
 
 __export const char *osl_strerror(int num)
@@ -44,7 +43,7 @@ __export const char *osl_strerror(int num)
        return msgstr.str + errmsgidx[num];
 }
 
-static int loglevel;
+int loglevel = 0;
 
 static void __attribute ((constructor)) init_loglevel(void)
 {
@@ -107,8 +106,8 @@ static int __lseek(int fd, off_t *offset, int whence)
        return 1;
 }
 
-static int append_file(const char *filename, char *header, size_t header_size,
-       char *data, size_t data_size, uint32_t *new_pos)
+static int append_file(const char *filename, char *data, size_t data_size,
+               uint32_t *new_pos)
 {
        int ret, fd;
 
@@ -117,11 +116,6 @@ static int append_file(const char *filename, char *header, size_t header_size,
        if (ret < 0)
                return ret;
        fd = ret;
-       if (header && header_size) {
-               ret = write_all(fd, header, &header_size);
-               if (ret < 0)
-                       goto out;
-       }
        ret = write_all(fd, data, &data_size);
        if (ret < 0)
                goto out;
@@ -658,11 +652,12 @@ static int map_column(struct osl_table *t, unsigned col_num)
 {
        struct stat statbuf;
        char *filename = column_filename(t, col_num);
-       int ret = -E_OSL_STAT;
+       int ret;
 
        if (!filename)
                return -ERRNO_TO_ERROR(ENOMEM);
-       if (stat(filename, &statbuf) < 0) {
+       ret = osl_stat(filename, &statbuf);
+       if (ret < 0) {
                free(filename);
                return ret;
        }
@@ -762,7 +757,6 @@ int get_mapped_object(const struct osl_table *t, unsigned col_num,
 {
        struct osl_column *col = &t->columns[col_num];
        uint32_t offset;
-       char *header;
        char *cell_index;
        int ret;
 
@@ -772,17 +766,15 @@ int get_mapped_object(const struct osl_table *t, unsigned col_num,
        if (ret < 0)
                return ret;
        offset = read_u32(cell_index);
-       obj->size = read_u32(cell_index + 4) - 1;
-       header = col->data_map.data + offset;
-       obj->data = header + 1;
-       if (read_u8(header) == 0xff) {
-               ERROR_LOG("col %u, size %zu, offset %u\n", col_num,
-                       obj->size, offset);
-               return -E_OSL_INVALID_OBJECT;
-       }
+       obj->size = read_u32(cell_index + 4);
+       obj->data = col->data_map.data + offset;
        return 1;
 }
 
+/*
+ * It's OK to call this with result = rb_node = NULL.  If result is not NULL,
+ * and rb key was not found, result points to the parent node.
+ */
 static int search_rbtree(const struct osl_object *obj,
                const struct osl_table *t, unsigned col_num,
                struct rb_node **result, struct rb_node ***rb_link)
@@ -1152,12 +1144,10 @@ static int append_map_file(const struct osl_table *t, unsigned col_num,
 {
        char *filename = column_filename(t, col_num);
        int ret;
-       char header = 0; /* zero means valid object */
 
        if (!filename)
                return -ERRNO_TO_ERROR(ENOMEM);
-       ret = append_file(filename, &header, 1, obj->data, obj->size,
-               new_size);
+       ret = append_file(filename, obj->data, obj->size, new_size);
        free(filename);
        return ret;
 }
@@ -1172,43 +1162,11 @@ static int append_row_index(const struct osl_table *t, char *row_index)
        filename = index_filename(t->desc);
        if (!filename)
                return -ERRNO_TO_ERROR(ENOMEM);
-       ret = append_file(filename, NULL, 0, row_index,
-               t->row_index_size, NULL);
+       ret = append_file(filename, row_index, t->row_index_size, NULL);
        free(filename);
        return ret;
 }
 
-/**
- * A wrapper for truncate(2)
- *
- * \param path Name of the regular file to truncate
- * \param size Number of bytes to \b shave \b off
- *
- * Truncate the regular file named by \a path by \a size bytes.
- *
- * \return Standard.
- *
- * \sa truncate(2)
- */
-int para_truncate(const char *path, off_t size)
-{
-       int ret;
-       struct stat statbuf;
-
-       ret = -E_OSL_STAT;
-       if (stat(path, &statbuf) < 0)
-               goto out;
-       ret = -E_OSL_BAD_SIZE;
-       if (statbuf.st_size < size)
-               goto out;
-       ret = -E_OSL_TRUNC;
-       if (truncate(path, statbuf.st_size - size) < 0)
-               goto out;
-       ret = 1;
-out:
-       return ret;
-}
-
 static int truncate_mapped_file(const struct osl_table *t, unsigned col_num,
                off_t size)
 {
@@ -1217,7 +1175,7 @@ static int truncate_mapped_file(const struct osl_table *t, unsigned col_num,
 
        if (!filename)
                return -ERRNO_TO_ERROR(ENOMEM);
-       ret = para_truncate(filename, size);
+       ret = truncate_file(filename, size);
        free(filename);
        return ret;
 }
@@ -1343,7 +1301,7 @@ __export int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects
        if (ret < 0) { /* truncate index and rollback changes */
                char *filename = index_filename(t->desc);
                if (filename)
-                       para_truncate(filename, t->row_index_size);
+                       truncate_file(filename, t->row_index_size);
                free(filename);
                goto rollback;
        }
@@ -1410,21 +1368,6 @@ __export int osl_get_object(const struct osl_table *t, const struct osl_row *r,
        return 1;
 }
 
-static int mark_mapped_object_invalid(const struct osl_table *t,
-               uint32_t row_num, unsigned col_num)
-{
-       struct osl_object obj;
-       char *p;
-       int ret = get_mapped_object(t, col_num, row_num, &obj);
-
-       if (ret < 0)
-               return ret;
-       p = obj.data;
-       p--;
-       *p = 0xff;
-       return 1;
-}
-
 __export int osl_del_row(struct osl_table *t, struct osl_row *row)
 {
        struct osl_row *r = row;
@@ -1448,10 +1391,8 @@ __export int osl_del_row(struct osl_table *t, struct osl_row *row)
                struct osl_column *col = t->columns + i;
                enum osl_storage_type st = cd->storage_type;
                remove_rb_node(t, i, r);
-               if (st == OSL_MAPPED_STORAGE) {
-                       mark_mapped_object_invalid(t, r->num, i);
+               if (st == OSL_MAPPED_STORAGE)
                        continue;
-               }
                if (st == OSL_NO_STORAGE && !(cd->storage_flags & OSL_DONT_FREE))
                        free(r->volatile_objects[col->volatile_num].data);
        }
@@ -1657,9 +1598,6 @@ __export int osl_update_object(struct osl_table *t, const struct osl_row *r,
                        uint32_t new_data_map_size;
                        char *row_index;
                        ret = get_row_index(t, r->num, &row_index);
-                       if (ret < 0)
-                               return ret;
-                       ret = mark_mapped_object_invalid(t, r->num, col_num);
                        if (ret < 0)
                                return ret;
                        unmap_column(t, col_num);