Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libc/db - centralize opening of regular and temp files t...
details: https://anonhg.NetBSD.org/src/rev/784d1448c8f9
branches: trunk
changeset: 325027:784d1448c8f9
user: christos <christos%NetBSD.org@localhost>
date: Sun Dec 01 00:22:48 2013 +0000
description:
- centralize opening of regular and temp files to avoid code duplication
- don't cast malloc
- use malloc sizeof(*var) instead of sizeof(type)
diffstat:
lib/libc/db/btree/bt_open.c | 51 ++---------------
lib/libc/db/btree/bt_overflow.c | 6 +-
lib/libc/db/btree/bt_utils.c | 12 ++--
lib/libc/db/db/Makefile.inc | 4 +-
lib/libc/db/db/dbfile.c | 118 ++++++++++++++++++++++++++++++++++++++++
lib/libc/db/hash/hash.c | 12 +--
lib/libc/db/hash/hash_page.c | 44 +-------------
lib/libc/db/recno/rec_open.c | 13 +---
lib/libc/db/recno/rec_put.c | 7 +-
lib/libc/db/recno/rec_utils.c | 22 +++---
10 files changed, 159 insertions(+), 130 deletions(-)
diffs (truncated from 556 to 300 lines):
diff -r c38ab9842ec2 -r 784d1448c8f9 lib/libc/db/btree/bt_open.c
--- a/lib/libc/db/btree/bt_open.c Sun Dec 01 00:17:14 2013 +0000
+++ b/lib/libc/db/btree/bt_open.c Sun Dec 01 00:22:48 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: bt_open.c,v 1.26 2012/03/13 21:13:32 christos Exp $ */
+/* $NetBSD: bt_open.c,v 1.27 2013/12/01 00:22:48 christos Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@@ -37,7 +37,7 @@
#endif
#include <sys/cdefs.h>
-__RCSID("$NetBSD: bt_open.c,v 1.26 2012/03/13 21:13:32 christos Exp $");
+__RCSID("$NetBSD: bt_open.c,v 1.27 2013/12/01 00:22:48 christos Exp $");
/*
* Implementation of btree access method for 4.4BSD.
@@ -71,7 +71,6 @@
static int byteorder(void);
static int nroot(BTREE *);
-static int tmp(void);
/*
* __BT_OPEN -- Open a btree.
@@ -161,7 +160,7 @@
goto einval;
/* Allocate and initialize DB and BTREE structures. */
- if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL)
+ if ((t = malloc(sizeof(*t))) == NULL)
goto err;
memset(t, 0, sizeof(BTREE));
t->bt_fd = -1; /* Don't close unopened fd on error. */
@@ -171,7 +170,7 @@
t->bt_pfx = b.prefix;
t->bt_rfd = -1;
- if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL)
+ if ((t->bt_dbp = dbp = malloc(sizeof(*dbp))) == NULL)
goto err;
memset(t->bt_dbp, 0, sizeof(DB));
if (t->bt_lorder != machine_lorder)
@@ -202,24 +201,17 @@
default:
goto einval;
}
-
- if ((t->bt_fd = open(fname, flags, mode)) == -1)
- goto err;
- if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1)
+ if ((t->bt_fd = __dbopen(fname, flags, mode, &sb)) == -1)
goto err;
} else {
if ((flags & O_ACCMODE) != O_RDWR)
goto einval;
- if ((t->bt_fd = tmp()) == -1)
+ if ((t->bt_fd = __dbtemp("bt.", &sb)) == -1)
goto err;
F_SET(t, B_INMEM);
}
- if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1)
- goto err;
- if (fstat(t->bt_fd, &sb))
- goto err;
if (sb.st_size) {
if ((nr = read(t->bt_fd, &m, sizeof(BTMETA))) < 0)
goto err;
@@ -390,37 +382,6 @@
}
static int
-tmp(void)
-{
- sigset_t set, oset;
- int len;
- int fd;
- char *envtmp;
- char path[PATH_MAX];
-
- if (issetugid())
- envtmp = NULL;
- else
- envtmp = getenv("TMPDIR");
-
- len = snprintf(path,
- sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : _PATH_TMP);
- if (len < 0 || (size_t)len >= sizeof(path)) {
- errno = ENAMETOOLONG;
- return -1;
- }
-
- (void)sigfillset(&set);
- (void)sigprocmask(SIG_BLOCK, &set, &oset);
- if ((fd = mkstemp(path)) != -1) {
- (void)unlink(path);
- (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
- }
- (void)sigprocmask(SIG_SETMASK, &oset, NULL);
- return(fd);
-}
-
-static int
byteorder(void)
{
uint32_t x;
diff -r c38ab9842ec2 -r 784d1448c8f9 lib/libc/db/btree/bt_overflow.c
--- a/lib/libc/db/btree/bt_overflow.c Sun Dec 01 00:17:14 2013 +0000
+++ b/lib/libc/db/btree/bt_overflow.c Sun Dec 01 00:22:48 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: bt_overflow.c,v 1.18 2012/03/13 21:13:32 christos Exp $ */
+/* $NetBSD: bt_overflow.c,v 1.19 2013/12/01 00:22:48 christos Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@@ -37,7 +37,7 @@
#endif
#include <sys/cdefs.h>
-__RCSID("$NetBSD: bt_overflow.c,v 1.18 2012/03/13 21:13:32 christos Exp $");
+__RCSID("$NetBSD: bt_overflow.c,v 1.19 2013/12/01 00:22:48 christos Exp $");
#include "namespace.h"
#include <sys/param.h>
@@ -97,7 +97,7 @@
#endif
/* Make the buffer bigger as necessary. */
if (*bufsz < sz) {
- *buf = (*buf == NULL ? malloc(sz) : realloc(*buf, sz));
+ *buf = *buf == NULL ? malloc(sz) : realloc(*buf, sz);
if (*buf == NULL)
return (RET_ERROR);
*bufsz = sz;
diff -r c38ab9842ec2 -r 784d1448c8f9 lib/libc/db/btree/bt_utils.c
--- a/lib/libc/db/btree/bt_utils.c Sun Dec 01 00:17:14 2013 +0000
+++ b/lib/libc/db/btree/bt_utils.c Sun Dec 01 00:22:48 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: bt_utils.c,v 1.14 2013/09/04 13:03:22 ryoon Exp $ */
+/* $NetBSD: bt_utils.c,v 1.15 2013/12/01 00:22:48 christos Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@@ -37,7 +37,7 @@
#endif
#include <sys/cdefs.h>
-__RCSID("$NetBSD: bt_utils.c,v 1.14 2013/09/04 13:03:22 ryoon Exp $");
+__RCSID("$NetBSD: bt_utils.c,v 1.15 2013/12/01 00:22:48 christos Exp $");
#include <sys/param.h>
@@ -88,8 +88,8 @@
key->data = rkey->data;
} else if (copy || F_ISSET(t, B_DB_LOCK)) {
if (bl->ksize > rkey->size) {
- p = (void *)(rkey->data == NULL ?
- malloc(bl->ksize) : realloc(rkey->data, bl->ksize));
+ p = rkey->data == NULL ?
+ malloc(bl->ksize) : realloc(rkey->data, bl->ksize);
if (p == NULL)
return (RET_ERROR);
rkey->data = p;
@@ -115,9 +115,9 @@
} else if (copy || F_ISSET(t, B_DB_LOCK)) {
/* Use +1 in case the first record retrieved is 0 length. */
if (bl->dsize + 1 > rdata->size) {
- p = (void *)(rdata->data == NULL ?
+ p = rdata->data == NULL ?
malloc(bl->dsize + 1) :
- realloc(rdata->data, bl->dsize + 1));
+ realloc(rdata->data, bl->dsize + 1);
if (p == NULL)
return (RET_ERROR);
rdata->data = p;
diff -r c38ab9842ec2 -r 784d1448c8f9 lib/libc/db/db/Makefile.inc
--- a/lib/libc/db/db/Makefile.inc Sun Dec 01 00:17:14 2013 +0000
+++ b/lib/libc/db/db/Makefile.inc Sun Dec 01 00:22:48 2013 +0000
@@ -1,6 +1,6 @@
-# $NetBSD: Makefile.inc,v 1.4 1995/02/27 13:21:22 cgd Exp $
+# $NetBSD: Makefile.inc,v 1.5 2013/12/01 00:22:48 christos Exp $
# @(#)Makefile.inc 8.1 (Berkeley) 6/4/93
.PATH: ${.CURDIR}/db/db
-SRCS+= db.c
+SRCS+= db.c dbfile.c
diff -r c38ab9842ec2 -r 784d1448c8f9 lib/libc/db/db/dbfile.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/db/db/dbfile.c Sun Dec 01 00:22:48 2013 +0000
@@ -0,0 +1,118 @@
+/* $NetBSD: dbfile.c,v 1.1 2013/12/01 00:22:48 christos Exp $ */
+
+/*-
+ * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: dbfile.c,v 1.1 2013/12/01 00:22:48 christos Exp $");
+
+#include <sys/stat.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <paths.h>
+#include <db.h>
+
+int
+__dbopen(const char *file, int flags, mode_t mode, struct stat *sb)
+{
+ int fd;
+ int serrno;
+
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
+ if ((fd = open(file, flags | O_CLOEXEC, mode)) == -1)
+ return -1;
+
+#if O_CLOEXEC == 0
+ if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
+ goto out;
+#endif
+
+ if (sb && fstat(fd, sb) == -1)
+ goto out;
+
+ return fd;
+out:
+ serrno = errno;
+ close(fd);
+ errno = serrno;
+ return -1;
+
+}
+
+int
+__dbtemp(const char *prefix, struct stat *sb)
+{
+ sigset_t set, oset;
+ int len;
+ int fd, serrno;
+ char *envtmp;
+ char path[PATH_MAX];
+
+ if (issetugid())
+ envtmp = NULL;
+ else
+ envtmp = getenv("TMPDIR");
+
+ len = snprintf(path, sizeof(path), "%s/%sXXXXXX",
+ envtmp ? envtmp : _PATH_TMP, prefix);
+ if ((size_t)len >= sizeof(path)) {
+ errno = ENAMETOOLONG;
+ return -1;
+ }
+
+ (void)sigfillset(&set);
+ (void)sigprocmask(SIG_BLOCK, &set, &oset);
+
+ if ((fd = mkstemp(path)) != -1) {
+ if (unlink(path) == -1)
+ goto out;
+
+ if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
+ goto out;
+
+ if (sb && fstat(fd, sb) == -1)
Home |
Main Index |
Thread Index |
Old Index