]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
ITS#7992 Tighter utf8_to_utf16(), fix errcodes
[openldap] / libraries / liblmdb / mdb.c
index 3f78d3c4830225a0356aca53997e7c9c630b552d..4e4385c04c547846dd71d779e96bfce435524a59 100644 (file)
@@ -258,6 +258,10 @@ typedef SSIZE_T    ssize_t;
 #  define MDB_USE_ROBUST       0
 # else
 #  define MDB_USE_ROBUST       1
+# endif
+#endif /* !MDB_USE_ROBUST */
+
+#if defined(MDB_USE_POSIX_MUTEX) && (MDB_USE_ROBUST)
 /* glibc < 2.12 only provided _np API */
 #  if (defined(__GLIBC__) && GLIBC_VER < 0x02000c) || \
        (defined(PTHREAD_MUTEX_ROBUST_NP) && !defined(PTHREAD_MUTEX_ROBUST))
@@ -265,10 +269,9 @@ typedef SSIZE_T    ssize_t;
 #   define pthread_mutexattr_setrobust(attr, flag)     pthread_mutexattr_setrobust_np(attr, flag)
 #   define pthread_mutex_consistent(mutex)     pthread_mutex_consistent_np(mutex)
 #  endif
-# endif
-#endif /* MDB_USE_ROBUST */
+#endif /* MDB_USE_POSIX_MUTEX && MDB_USE_ROBUST */
 
-#if defined(MDB_OWNERDEAD) && MDB_USE_ROBUST
+#if defined(MDB_OWNERDEAD) && (MDB_USE_ROBUST)
 #define MDB_ROBUST_SUPPORTED   1
 #endif
 
@@ -768,9 +771,23 @@ typedef struct MDB_txninfo {
          + (((MDB_PIDLOCK) != 0) << 16)))
 /** @} */
 
-/** Common header for all page types.
- * Overflow records occupy a number of contiguous pages with no
- * headers on any page after the first.
+/** Common header for all page types. The page type depends on #mp_flags.
+ *
+ * #P_BRANCH and #P_LEAF pages have unsorted '#MDB_node's at the end, with
+ * sorted #mp_ptrs[] entries referring to them. Exception: #P_LEAF2 pages
+ * omit mp_ptrs and pack sorted #MDB_DUPFIXED values after the page header.
+ *
+ * #P_OVERFLOW records occupy one or more contiguous pages where only the
+ * first has a page header. They hold the real data of #F_BIGDATA nodes.
+ *
+ * #P_SUBP sub-pages are small leaf "pages" with duplicate data.
+ * A node with flag #F_DUPDATA but not #F_SUBDATA contains a sub-page.
+ * (Duplicate data can also go in sub-databases, which use normal pages.)
+ *
+ * #P_META pages contain #MDB_meta, the start point of an LMDB snapshot.
+ *
+ * Each non-metapage up to #MDB_meta.%mm_last_pg is reachable exactly once
+ * in the snapshot: Either used by a database or listed in a freeDB record.
  */
 typedef struct MDB_page {
 #define        mp_pgno mp_p.p_pgno
@@ -779,7 +796,7 @@ typedef struct MDB_page {
                pgno_t          p_pgno; /**< page number */
                struct MDB_page *p_next; /**< for in-memory list of freed pages */
        } mp_p;
-       uint16_t        mp_pad;
+       uint16_t        mp_pad;                 /**< key size if this is a LEAF2 page */
 /**    @defgroup mdb_page      Page Flags
  *     @ingroup internal
  *     Flags for the page headers.
@@ -846,7 +863,9 @@ typedef struct MDB_page {
        /** The number of overflow pages needed to store the given size. */
 #define OVPAGES(size, psize)   ((PAGEHDRSZ-1 + (size)) / (psize) + 1)
 
-       /** Link in #MDB_txn.%mt_loose_pgs list */
+       /** Link in #MDB_txn.%mt_loose_pgs list.
+        *  Kept outside the page header, which is needed when reusing the page.
+        */
 #define NEXT_LOOSE_PAGE(p)             (*(MDB_page **)((p) + 2))
 
        /** Header for a single key/data pair within a page.
@@ -974,9 +993,9 @@ typedef struct MDB_db {
        pgno_t          md_root;                /**< the root page of this tree */
 } MDB_db;
 
-       /** mdb_dbi_open flags */
 #define MDB_VALID      0x8000          /**< DB handle is valid, for me_dbflags */
 #define PERSISTENT_FLAGS       (0xffff & ~(MDB_VALID))
+       /** #mdb_dbi_open() flags */
 #define VALID_FLAGS    (MDB_REVERSEKEY|MDB_DUPSORT|MDB_INTEGERKEY|MDB_DUPFIXED|\
        MDB_INTEGERDUP|MDB_REVERSEDUP|MDB_CREATE)
 
@@ -1007,7 +1026,10 @@ typedef struct MDB_meta {
 #define        mm_psize        mm_dbs[FREE_DBI].md_pad
        /** Any persistent environment flags. @ref mdb_env */
 #define        mm_flags        mm_dbs[FREE_DBI].md_flags
-       pgno_t          mm_last_pg;                     /**< last used page in file */
+       /** Last used page in the datafile.
+        *      Actually the file may be shorter if the freeDB lists the final pages.
+        */
+       pgno_t          mm_last_pg;
        volatile txnid_t        mm_txnid;       /**< txnid that committed this page */
 } MDB_meta;
 
@@ -1057,7 +1079,7 @@ struct MDB_txn {
         *      in this transaction, linked through #NEXT_LOOSE_PAGE(page).
         */
        MDB_page        *mt_loose_pgs;
-       /* #Number of loose pages (#mt_loose_pgs) */
+       /*Number of loose pages (#mt_loose_pgs) */
        int                     mt_loose_count;
        /** The sorted list of dirty pages we temporarily wrote to disk
         *      because the dirty list was full. page numbers in here are
@@ -1580,7 +1602,7 @@ mdb_page_list(MDB_page *mp)
                        pgno, ((MDB_meta *)METADATA(mp))->mm_txnid);
                return;
        default:
-               fprintf(stderr, "Bad page %"Z"u flags 0x%u\n", pgno, mp->mp_flags);
+               fprintf(stderr, "Bad page %"Z"u flags 0x%X\n", pgno, mp->mp_flags);
                return;
        }
 
@@ -1696,7 +1718,7 @@ static void mdb_audit(MDB_txn *txn)
                }
        }
        if (freecount + count + NUM_METAS != txn->mt_next_pgno) {
-               fprintf(stderr, "audit: %lu freecount: %lu count: %lu total: %lu next_pgno: %lu\n",
+               fprintf(stderr, "audit: %"Z"u freecount: %"Z"u count: %"Z"u total: %"Z"u next_pgno: %"Z"u\n",
                        txn->mt_txnid, freecount, count+NUM_METAS,
                        freecount+count+NUM_METAS, txn->mt_next_pgno);
        }
@@ -4477,9 +4499,9 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
 #ifdef O_CLOEXEC       /* Linux: Open file and set FD_CLOEXEC atomically */
 #      define MDB_CLOEXEC              O_CLOEXEC
 #else
-       int fdflags;
 #      define MDB_CLOEXEC              0
 #endif
+       int fdflags;
 #endif
        int rc;
        off_t size, rsize;
@@ -4501,12 +4523,12 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
                if (rc == MDB_ERRCODE_ROFS && (env->me_flags & MDB_RDONLY)) {
                        return MDB_SUCCESS;
                }
-               goto fail_errno;
+               goto fail;
        }
-#if ! ((MDB_CLOEXEC) || defined(_WIN32))
+#ifndef _WIN32
        /* Lose record locks when exec*() */
-       if ((fdflags = fcntl(env->me_lfd, F_GETFD) | FD_CLOEXEC) >= 0)
-                       fcntl(env->me_lfd, F_SETFD, fdflags);
+       if (!(MDB_CLOEXEC) && (fdflags = fcntl(env->me_lfd, F_GETFD)) != -1)
+                       fcntl(env->me_lfd, F_SETFD, fdflags | FD_CLOEXEC);
 #endif
 
        if (!(env->me_flags & MDB_NOTLS)) {
@@ -5906,6 +5928,7 @@ mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                                                }
                                        }
                                        rc = 0;
+                                       mc->mc_flags &= ~C_EOF;
                                        goto set2;
                                }
                        }
@@ -9371,17 +9394,12 @@ mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags)
        }
 
        if (env->me_psize >= env->me_os_psize) {
-#ifdef O_DIRECT
+#ifdef F_NOCACHE       /* __APPLE__ */
+       (void) fcntl(newfd, F_NOCACHE, 1);
+#elif defined O_DIRECT
        /* Set O_DIRECT if the file system supports it */
        if ((rc = fcntl(newfd, F_GETFL)) != -1)
                (void) fcntl(newfd, F_SETFL, rc | O_DIRECT);
-#endif
-#ifdef F_NOCACHE       /* __APPLE__ */
-       rc = fcntl(newfd, F_NOCACHE, 1);
-       if (rc) {
-               rc = ErrCode();
-               goto leave;
-       }
 #endif
        }
 
@@ -10086,25 +10104,31 @@ mdb_mutex_failed(MDB_env *env, mdb_mutexref_t mutex, int rc)
        return rc;
 }
 #endif /* MDB_ROBUST_SUPPORTED */
-/** @} */
 
 #if defined(_WIN32)
-static int utf8_to_utf16(const char *src, int srcsize, wchar_t **dst, int *dstsize)
-{
-       int need;
-       wchar_t *result;
-       need = MultiByteToWideChar(CP_UTF8, 0, src, srcsize, NULL, 0);
-       if (need == 0xFFFD)
-               return EILSEQ;
-       if (need == 0)
-               return EINVAL;
-       result = malloc(sizeof(wchar_t) * need);
-       if (!result)
-               return ENOMEM;
-       MultiByteToWideChar(CP_UTF8, 0, src, srcsize, result, need);
-       if (dstsize)
-               *dstsize = need;
-       *dst = result;
-       return 0;
+static int ESECT
+utf8_to_utf16(const char *src, int srcsize, wchar_t **dst, int *dstsize)
+{
+       int rc, need = 0;
+       wchar_t *result = NULL;
+       for (;;) {                                      /* malloc result, then fill it in */
+               need = MultiByteToWideChar(CP_UTF8, 0, src, srcsize, result, need);
+               if (!need) {
+                       rc = ErrCode();
+                       free(result);
+                       return rc;
+               }
+               if (!result) {
+                       result = malloc(sizeof(wchar_t) * need);
+                       if (!result)
+                               return ENOMEM;
+                       continue;
+               }
+               if (dstsize)
+                       *dstsize = need;
+               *dst = result;
+               return MDB_SUCCESS;
+       }
 }
 #endif /* defined(_WIN32) */
+/** @} */