]> git.saurik.com Git - apple/libc.git/blobdiff - db/recno/rec_open.c
Libc-320.tar.gz
[apple/libc.git] / db / recno / rec_open.c
index 7a74b274910b65d1ad57dd91047c71f5206a10a4..680f9beb22f9716f20ef02d1beb790f45552ec79 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
@@ -22,8 +22,8 @@
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
-/*
- * Copyright (c) 1990, 1993
+/*-
+ * Copyright (c) 1990, 1993, 1994
  *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * SUCH DAMAGE.
  */
 
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)rec_open.c 8.10 (Berkeley) 9/1/94";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
 
 #include <sys/types.h>
+#include <sys/mman.h>
 #include <sys/stat.h>
 
-#include <mach/mach.h>
-#include <mach/mach_traps.h>
-
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
@@ -121,7 +123,7 @@ __rec_open(fname, flags, mode, openinfo, dflags)
        t = dbp->internal;
        if (openinfo) {
                if (openinfo->flags & R_FIXEDLEN) {
-                       SET(t, R_FIXLEN);
+                       F_SET(t, R_FIXLEN);
                        t->bt_reclen = openinfo->reclen;
                        if (t->bt_reclen == 0)
                                goto einval;
@@ -130,12 +132,11 @@ __rec_open(fname, flags, mode, openinfo, dflags)
        } else
                t->bt_bval = '\n';
 
-       SET(t, R_RECNO);
+       F_SET(t, R_RECNO);
        if (fname == NULL)
-               SET(t, R_EOF | R_INMEM);
+               F_SET(t, R_EOF | R_INMEM);
        else
                t->bt_rfd = rfd;
-       t->bt_rcursor = 0;
 
        if (fname != NULL) {
                /*
@@ -147,20 +148,20 @@ __rec_open(fname, flags, mode, openinfo, dflags)
                if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
                        switch (flags & O_ACCMODE) {
                        case O_RDONLY:
-                               SET(t, R_RDONLY);
+                               F_SET(t, R_RDONLY);
                                break;
                        default:
                                goto einval;
                        }
 slow:                  if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
                                goto err;
-                       SET(t, R_CLOSEFP);
+                       F_SET(t, R_CLOSEFP);
                        t->bt_irec =
-                           ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
+                           F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
                } else {
                        switch (flags & O_ACCMODE) {
                        case O_RDONLY:
-                               SET(t, R_RDONLY);
+                               F_SET(t, R_RDONLY);
                                break;
                        case O_RDWR:
                                break;
@@ -180,21 +181,29 @@ slow:                     if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
                         * fails if the file is too large.
                         */
                        if (sb.st_size == 0)
-                               SET(t, R_EOF);
+                               F_SET(t, R_EOF);
                        else {
+#ifdef MMAP_NOT_AVAILABLE
+                               /*
+                                * XXX
+                                * Mmap doesn't work correctly on many current
+                                * systems.  In particular, it can fail subtly,
+                                * with cache coherency problems.  Don't use it
+                                * for now.
+                                */
                                t->bt_msize = sb.st_size;
-                               if ( (map_fd(rfd, 0, (vm_offset_t *)
-                                   &t->bt_smap, TRUE, t->bt_msize)
-                                    != KERN_SUCCESS)
-                               ||   (vm_protect(mach_task_self(), (vm_offset_t)
-                                   t->bt_smap,  t->bt_msize, TRUE,
-                                   VM_PROT_READ) != KERN_SUCCESS) )
+                               if ((t->bt_smap = mmap(NULL, t->bt_msize,
+                                   PROT_READ, MAP_PRIVATE, rfd,
+                                   (off_t)0)) == MAP_FAILED)
                                        goto slow;
                                t->bt_cmap = t->bt_smap;
                                t->bt_emap = t->bt_smap + sb.st_size;
-                               t->bt_irec = ISSET(t, R_FIXLEN) ?
+                               t->bt_irec = F_ISSET(t, R_FIXLEN) ?
                                    __rec_fmap : __rec_vmap;
-                               SET(t, R_MEMMAPPED);
+                               F_SET(t, R_MEMMAPPED);
+#else
+                               goto slow;
+#endif
                        }
                }
        }
@@ -212,13 +221,14 @@ slow:                     if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
        if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
                goto err;
        if ((h->flags & P_TYPE) == P_BLEAF) {
-               h->flags = h->flags & ~P_TYPE | P_RLEAF;
+               F_CLR(h, P_TYPE);
+               F_SET(h, P_RLEAF);
                mpool_put(t->bt_mp, h, MPOOL_DIRTY);
        } else
                mpool_put(t->bt_mp, h, 0);
 
        if (openinfo && openinfo->flags & R_SNAPSHOT &&
-           !ISSET(t, R_EOF | R_INMEM) &&
+           !F_ISSET(t, R_EOF | R_INMEM) &&
            t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
                 goto err;
        return (dbp);
@@ -248,7 +258,7 @@ __rec_fd(dbp)
        }
 
        /* In-memory database can't have a file descriptor. */
-       if (ISSET(t, R_INMEM)) {
+       if (F_ISSET(t, R_INMEM)) {
                errno = ENOENT;
                return (-1);
        }