Simplify ypdb_open() API and just use it for opening the DB for reading.

Provide a separate ypdb_mktemp() API to open a temporary file read-write.
Use mkstemp() instead of mktemp().

NOTE: makedbm & mkalias tested ok.  I couldn't test ypxfer.
This commit is contained in:
lukem 2008-02-29 03:00:47 +00:00
parent e48401e466
commit cea4b7cdfd
7 changed files with 117 additions and 70 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: ypdb.c,v 1.10 2005/06/20 00:29:42 lukem Exp $ */
/* $NetBSD: ypdb.c,v 1.11 2008/02/29 03:00:47 lukem Exp $ */
/*
* Copyright (c) 1990, 1993
@ -38,7 +38,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: ypdb.c,v 1.10 2005/06/20 00:29:42 lukem Exp $");
__RCSID("$NetBSD: ypdb.c,v 1.11 2008/02/29 03:00:47 lukem Exp $");
#endif
#include <sys/param.h>
@ -47,31 +47,34 @@ __RCSID("$NetBSD: ypdb.c,v 1.10 2005/06/20 00:29:42 lukem Exp $");
#include <db.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <rpcsvc/yp.h>
#include "ypdb.h"
static DBM *_ypdb_dbopen(const char *, int, mode_t);
/*
* ypdb_open --
* dbopen(3) file with the flags & mode.
* dbopen(3) file, read-only.
* First ensure that file has a suffix of YPDB_SUFFIX.
* Try opening as a DB_BTREE first, then DB_HASH.
*
* Returns:
* *DBM on success
* *DBM on success
* NULL on failure
*/
DBM *
ypdb_open(const char *file, int flags, int mode)
ypdb_open(const char *file)
{
char path[MAXPATHLEN];
const char *cp, *suffix;
DBM *db;
BTREEINFO info;
cp = strrchr(file, '.');
if (cp != NULL && strcmp(cp, YPDB_SUFFIX) == 0)
@ -83,6 +86,66 @@ ypdb_open(const char *file, int flags, int mode)
return (NULL);
}
snprintf(path, sizeof(path), "%s%s", file, suffix);
return _ypdb_dbopen(path, O_RDONLY, 0444);
}
/*
* ypdb_mktemp --
* Create a temporary file using mkstemp(3) based on the
* template provided in file.
* dbopen(3) file, read-write, 0644 (modified by umask(2)).
* Try opening as a DB_BTREE first, then DB_HASH.
* file won't have YPDB_SUFFIX.
*
* Returns:
* *DBM on success; file now exists.
* NULL on failure
*/
DBM *
ypdb_mktemp(char *file)
{
int fd = -1;
DBM *db = NULL;
mode_t myumask;
int save_errno;
if ((fd = mkstemp(file)) == -1)
return NULL;
myumask = umask(0);
(void)umask(myumask);
if (fchmod(fd, 0644 & ~myumask) == -1)
goto bad;
(void) close(fd);
fd = -1;
if ((db = _ypdb_dbopen(file, O_RDWR, 0644)) == NULL)
goto bad;
return db;
bad:
save_errno = errno;
if (fd != 1)
(void) close(fd);
(void) unlink(file);
errno = save_errno;
return NULL;
}
/*
* _ypdb_dbopen --
* dbopen(3) path with the flags & mode.
* Try opening as a DB_BTREE first, then DB_HASH.
*/
static DBM *
_ypdb_dbopen(const char *path, int flags, mode_t mode)
{
DBM *db;
BTREEINFO info;
/* try our btree format first */
info.flags = 0;
@ -102,6 +165,11 @@ ypdb_open(const char *file, int flags, int mode)
return (db);
}
/*
* ypdb_close --
* Close the db
*/
void
ypdb_close(DBM *db)
{

View File

@ -1,4 +1,4 @@
/* $NetBSD: ypdb.h,v 1.4 2003/08/07 11:25:51 agc Exp $ */
/* $NetBSD: ypdb.h,v 1.5 2008/02/29 03:00:47 lukem Exp $ */
/*
* Copyright (c) 1990, 1993
@ -66,7 +66,8 @@ datum ypdb_fetch(DBM *, datum);
datum ypdb_firstkey(DBM *);
datum ypdb_nextkey(DBM *);
datum ypdb_setkey(DBM *, datum);
DBM *ypdb_open(const char *, int, int);
DBM *ypdb_open(const char *);
DBM *ypdb_mktemp(char *);
int ypdb_store(DBM *, datum, datum, int);
__END_DECLS

View File

@ -1,4 +1,4 @@
/* $NetBSD: makedbm.c,v 1.21 2005/06/20 00:08:35 lukem Exp $ */
/* $NetBSD: makedbm.c,v 1.22 2008/02/29 03:00:47 lukem Exp $ */
/*
* Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
@ -33,7 +33,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: makedbm.c,v 1.21 2005/06/20 00:08:35 lukem Exp $");
__RCSID("$NetBSD: makedbm.c,v 1.22 2008/02/29 03:00:47 lukem Exp $");
#endif
#include <sys/param.h>
@ -42,7 +42,6 @@ __RCSID("$NetBSD: makedbm.c,v 1.21 2005/06/20 00:08:35 lukem Exp $");
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -202,7 +201,7 @@ list_database(char *database)
DBM *db;
datum key, val;
db = ypdb_open(database, O_RDONLY, 0444);
db = ypdb_open(database);
if (db == NULL)
err(1, "can't open database `%s'", database);
@ -237,14 +236,12 @@ create_database(char *infile, char *database, char *yp_input_file,
size_t len;
char *p, *k, *v, *slash;
DBM *new_db;
static char mapname[] = "ypdbXXXXXX";
static const char template[] = "ypdbXXXXXX";
char db_mapname[MAXPATHLEN + 1], db_outfile[MAXPATHLEN + 1];
char db_tempname[MAXPATHLEN + 1];
char empty_str[] = "";
memset(db_mapname, 0, sizeof(db_mapname));
memset(db_outfile, 0, sizeof(db_outfile));
memset(db_tempname, 0, sizeof(db_tempname));
if (strcmp(infile, "-") == 0)
data_file = stdin;
@ -267,19 +264,16 @@ create_database(char *infile, char *database, char *yp_input_file,
/* NOTE: database is now directory where map goes ! */
if (strlen(database) + strlen(mapname) + strlen(YPDB_SUFFIX) >
if (strlen(database) + strlen(template) + strlen(YPDB_SUFFIX) >
(sizeof(db_mapname) - 1))
errx(1, "directory name `%s' too long", database);
snprintf(db_tempname, sizeof(db_tempname), "%s%s",
database, mapname);
mktemp(db_tempname); /* OK */
snprintf(db_mapname, sizeof(db_mapname), "%s%s",
db_tempname, YPDB_SUFFIX);
database, template);
new_db = ypdb_open(db_tempname, O_RDWR | O_CREAT | O_EXCL, 0644);
new_db = ypdb_mktemp(db_mapname);
if (new_db == NULL)
err(1, "can't create temp database `%s'", db_tempname);
err(1, "can't create temp database `%s'", db_mapname);
for (;
(p = fparseln(data_file, &len, &line_no, NULL, FPARSELN_UNESCALL));

View File

@ -1,4 +1,4 @@
/* $NetBSD: mkalias.c,v 1.14 2006/05/11 08:44:56 mrg Exp $ */
/* $NetBSD: mkalias.c,v 1.15 2008/02/29 03:00:47 lukem Exp $ */
/*
* Copyright (c) 1997 Mats O Jansson <moj@stacken.kth.se>
@ -33,7 +33,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: mkalias.c,v 1.14 2006/05/11 08:44:56 mrg Exp $");
__RCSID("$NetBSD: mkalias.c,v 1.15 2008/02/29 03:00:47 lukem Exp $");
#endif
#include <sys/types.h>
@ -43,7 +43,6 @@ __RCSID("$NetBSD: mkalias.c,v 1.14 2006/05/11 08:44:56 mrg Exp $");
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <netdb.h>
#include <resolv.h>
#include <stdio.h>
@ -164,9 +163,8 @@ main(int argc, char *argv[])
datum key, val;
char *slash;
DBM *new_db = NULL;
static char mapname[] = "ypdbXXXXXXXXXX";
char db_mapname[MAXPATHLEN], db_outfile[MAXPATHLEN],
db_tempname[MAXPATHLEN];
static const char template[] = "ypdbXXXXXX";
char db_mapname[MAXPATHLEN], db_outfile[MAXPATHLEN];
int status;
char user[4096], host[4096]; /* XXX: DB bsize = 4096 in ypdb.c */
char datestr[11];
@ -217,7 +215,7 @@ main(int argc, char *argv[])
if (optind < argc)
usage();
db = ypdb_open(input, O_RDONLY, 0444);
db = ypdb_open(input);
if (db == NULL)
err(1, "Unable to open input database `%s'", input);
@ -236,17 +234,14 @@ main(int argc, char *argv[])
/* note: output is now directory where map goes ! */
if (strlen(output) + strlen(mapname) + strlen(YPDB_SUFFIX) >
if (strlen(output) + strlen(template) + strlen(YPDB_SUFFIX) >
(sizeof(db_mapname) - 1))
errx(1, "Directory name `%s' too long", output);
snprintf(db_tempname, sizeof(db_tempname), "%s%s", output,
mapname);
mktemp(db_tempname); /* OK */
snprintf(db_mapname, sizeof(db_mapname), "%s%s", db_tempname,
YPDB_SUFFIX);
snprintf(db_mapname, sizeof(db_mapname), "%s%s",
output, template);
new_db = ypdb_open(db_tempname, O_RDWR|O_CREAT, 0444);
new_db = ypdb_mktemp(db_mapname);
if (new_db == NULL)
err(1, "Unable to open output database `%s'",
db_outfile);

View File

@ -1,4 +1,4 @@
/* $NetBSD: yppush.c,v 1.20 2008/02/27 01:27:35 lukem Exp $ */
/* $NetBSD: yppush.c,v 1.21 2008/02/29 03:00:47 lukem Exp $ */
/*
*
@ -45,7 +45,6 @@
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@ -175,7 +174,7 @@ main(int argc, char *argv[])
* now open the database so we can extract "order number"
* (i.e. timestamp) of the map.
*/
ypdb = ypdb_open(ypi.map, O_RDONLY, 0444);
ypdb = ypdb_open(ypi.map);
if (ypdb == NULL)
err(1, "ypdb_open %s/%s/%s", YP_DB_PATH, ypi.ourdomain,
ypi.map);

View File

@ -1,4 +1,4 @@
/* $NetBSD: ypserv_db.c,v 1.18 2006/10/15 01:10:00 christos Exp $ */
/* $NetBSD: ypserv_db.c,v 1.19 2008/02/29 03:00:47 lukem Exp $ */
/*
* Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
@ -35,7 +35,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: ypserv_db.c,v 1.18 2006/10/15 01:10:00 christos Exp $");
__RCSID("$NetBSD: ypserv_db.c,v 1.19 2008/02/29 03:00:47 lukem Exp $");
#endif
/*
@ -54,14 +54,12 @@ __RCSID("$NetBSD: ypserv_db.c,v 1.18 2006/10/15 01:10:00 christos Exp $");
#include <arpa/nameser.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <resolv.h>
#include <syslog.h>
#include <unistd.h>
#include <rpc/rpc.h>
#include <rpcsvc/yp_prot.h>
@ -363,7 +361,7 @@ ypdb_open_db(const char *domain, const char *map, u_int *status,
#ifdef OPTIMIZE_DB
retryopen:
#endif /* OPTIMIZE_DB */
db = ypdb_open(map_path, O_RDONLY, 0444);
db = ypdb_open(map_path);
#ifdef OPTIMIZE_DB
if (db == NULL) {
#ifdef DEBUG

View File

@ -1,4 +1,4 @@
/* $NetBSD: ypxfr.c,v 1.14 2006/03/22 19:54:13 bouyer Exp $ */
/* $NetBSD: ypxfr.c,v 1.15 2008/02/29 03:00:47 lukem Exp $ */
/*
* Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
@ -33,9 +33,10 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: ypxfr.c,v 1.14 2006/03/22 19:54:13 bouyer Exp $");
__RCSID("$NetBSD: ypxfr.c,v 1.15 2008/02/29 03:00:47 lukem Exp $");
#endif
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
@ -44,7 +45,6 @@ __RCSID("$NetBSD: ypxfr.c,v 1.14 2006/03/22 19:54:13 bouyer Exp $");
#include <arpa/inet.h>
#include <err.h>
#include <fcntl.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
@ -69,7 +69,7 @@ int main(int, char *[]);
int get_local_ordernum(char *, char *, u_int *);
int get_remote_ordernum(CLIENT *, char *, char *, u_int, u_int *);
void get_map(CLIENT *, char *, char *, struct ypall_callback *);
DBM *create_db(char *, char *, char *);
DBM *create_db(char *, char *, char *, size_t);
int install_db(char *, char *, char *);
int unlink_db(char *, char *, char *);
int add_order(DBM *, u_int);
@ -95,7 +95,7 @@ main(int argc, char **argv)
u_int ordernum, new_ordernum;
struct ypall_callback callback;
CLIENT *client;
char mapname[] = "ypdbXXXXXX";
char temp_map[MAXPATHLEN];
int status, xfr_status;
status = YPPUSH_SUCC;
@ -212,8 +212,7 @@ main(int argc, char **argv)
if (status == YPPUSH_SUCC) {
/* Create temporary db */
mktemp(mapname);
db = create_db(domain, map, mapname);
db = create_db(domain, map, temp_map, sizeof(temp_map));
if (db == NULL)
status = YPPUSH_DBM;
@ -244,9 +243,9 @@ main(int argc, char **argv)
/* Rename db */
if (status > 0)
status = install_db(domain, map, mapname);
status = install_db(domain, map, temp_map);
else
status = unlink_db(domain, map, mapname);
status = unlink_db(domain, map, temp_map);
}
punt:
@ -329,7 +328,7 @@ get_local_ordernum(char *domain, char *map, u_int *lordernum)
/* Open the map file. */
snprintf(map_path, sizeof(map_path), "%s/%s/%s",
YP_DB_PATH, domain, map);
db = ypdb_open(map_path, O_RDONLY, 0444);
db = ypdb_open(map_path);
if (db == NULL) {
status = YPPUSH_DBM;
goto out;
@ -385,30 +384,27 @@ get_map(CLIENT *client, char *domain, char *map,
}
DBM *
create_db(char *domain, char *map, char *temp_map)
create_db(char *domain, char *map, char *db_temp, size_t db_temp_len)
{
char db_temp[255];
static const char template[] = "ypdbXXXXXX";
DBM *db;
snprintf(db_temp, sizeof(db_temp), "%s/%s/%s",
YP_DB_PATH, domain, temp_map);
snprintf(db_temp, db_temp_len, "%s/%s/%s",
YP_DB_PATH, domain, template);
db = ypdb_open(db_temp, O_RDWR|O_CREAT|O_EXCL, 0444);
db = ypdb_mktemp(db_temp);
return db;
}
int
install_db(char *domain, char *map, char *temp_map)
install_db(char *domain, char *map, char *db_temp)
{
char db_name[255], db_temp[255];
char db_name[MAXPATHLEN];
snprintf(db_name, sizeof(db_name), "%s/%s/%s%s",
YP_DB_PATH, domain, map, YPDB_SUFFIX);
snprintf(db_temp, sizeof(db_temp), "%s/%s/%s%s",
YP_DB_PATH, domain, temp_map, YPDB_SUFFIX);
if (rename(db_temp, db_name)) {
warn("can't rename `%s' -> `%s'", db_temp, db_name);
return YPPUSH_YPERR;
@ -418,12 +414,8 @@ install_db(char *domain, char *map, char *temp_map)
}
int
unlink_db(char *domain, char *map, char *temp_map)
unlink_db(char *domain, char *map, char *db_temp)
{
char db_temp[255];
snprintf(db_temp, sizeof(db_temp), "%s/%s/%s%s",
YP_DB_PATH, domain, temp_map, YPDB_SUFFIX);
if (unlink(db_temp)) {
warn("can't unlink `%s'", db_temp);