xref: /csrg-svn/lib/libc/db/recno/rec_close.c (revision 56702)
150995Sbostic /*-
250995Sbostic  * Copyright (c) 1990 The Regents of the University of California.
350995Sbostic  * All rights reserved.
450995Sbostic  *
550995Sbostic  * %sccs.include.redist.c%
650995Sbostic  */
750995Sbostic 
850995Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*56702Sbostic static char sccsid[] = "@(#)rec_close.c	5.5 (Berkeley) 11/07/92";
1050995Sbostic #endif /* LIBC_SCCS and not lint */
1150995Sbostic 
1250995Sbostic #include <sys/param.h>
1350995Sbostic #include <sys/uio.h>
1450995Sbostic #include <errno.h>
1550995Sbostic #include <db.h>
1650995Sbostic #include <unistd.h>
1750995Sbostic #include <stdio.h>
1851087Sbostic #include "recno.h"
1950995Sbostic 
2050995Sbostic /*
2150995Sbostic  * __REC_CLOSE -- Close a recno tree.
2250995Sbostic  *
2350995Sbostic  * Parameters:
2450995Sbostic  *	dbp:	pointer to access method
2550995Sbostic  *
2650995Sbostic  * Returns:
2750995Sbostic  *	RET_ERROR, RET_SUCCESS
2850995Sbostic  */
2950995Sbostic int
3050995Sbostic __rec_close(dbp)
3150995Sbostic 	DB *dbp;
3250995Sbostic {
3354280Sbostic 	BTREE *t;
3454280Sbostic 	int rval;
3554280Sbostic 
3650995Sbostic 	if (__rec_sync(dbp) == RET_ERROR)
3750995Sbostic 		return (RET_ERROR);
3854280Sbostic 
3954280Sbostic 	/* Committed to closing. */
4054280Sbostic 	t = dbp->internal;
4154280Sbostic 	rval = t->bt_rfp == NULL ? close(t->bt_rfd) : fclose(t->bt_rfp);
4254280Sbostic 
4354280Sbostic 	if (__bt_close(dbp) == RET_ERROR)
4454280Sbostic 		return (RET_ERROR);
4554280Sbostic 
4654280Sbostic 	return (rval ? RET_ERROR : RET_SUCCESS);
4750995Sbostic }
4850995Sbostic 
4950995Sbostic /*
5050995Sbostic  * __REC_SYNC -- sync the recno tree to disk.
5150995Sbostic  *
5250995Sbostic  * Parameters:
5350995Sbostic  *	dbp:	pointer to access method
5450995Sbostic  *
5550995Sbostic  * Returns:
5650995Sbostic  *	RET_SUCCESS, RET_ERROR.
5750995Sbostic  */
5850995Sbostic int
5950995Sbostic __rec_sync(dbp)
6050995Sbostic 	const DB *dbp;
6150995Sbostic {
6250995Sbostic 	struct iovec iov[2];
6350995Sbostic 	BTREE *t;
6450995Sbostic 	DBT data, key;
6551087Sbostic 	off_t off;
6655312Sbostic 	recno_t scursor, trec;
6750995Sbostic 	int status;
6850995Sbostic 
6950995Sbostic 	t = dbp->internal;
7050995Sbostic 
71*56702Sbostic 	if (ISSET(t, BTF_INMEM) || ISSET(t, BTF_RDONLY) ||
72*56702Sbostic 	    NOTSET(t, BTF_MODIFIED))
7350995Sbostic 		return (RET_SUCCESS);
7450995Sbostic 
75*56702Sbostic 	/* Read any remaining records into the tree. */
7650995Sbostic 	if (t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
7750995Sbostic 		return (RET_ERROR);
7850995Sbostic 
7950995Sbostic 	/* Rewind the file descriptor. */
80*56702Sbostic 	if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0)
8150995Sbostic 		return (RET_ERROR);
8250995Sbostic 
8350995Sbostic 	iov[1].iov_base = "\n";
8450995Sbostic 	iov[1].iov_len = 1;
8550995Sbostic 	scursor = t->bt_rcursor;
8650995Sbostic 
8755312Sbostic 	key.size = sizeof(recno_t);
8855312Sbostic 	key.data = &trec;
8955312Sbostic 
9050995Sbostic 	status = (dbp->seq)(dbp, &key, &data, R_FIRST);
9150995Sbostic         while (status == RET_SUCCESS) {
9250995Sbostic 		iov[0].iov_base = data.data;
9350995Sbostic 		iov[0].iov_len = data.size;
9450995Sbostic 		if (writev(t->bt_rfd, iov, 2) != data.size + 1)
9550995Sbostic 			return (RET_ERROR);
9650995Sbostic                 status = (dbp->seq)(dbp, &key, &data, R_NEXT);
9750995Sbostic         }
9850995Sbostic 	t->bt_rcursor = scursor;
9951087Sbostic 	if (status == RET_ERROR)
10051087Sbostic 		return (RET_ERROR);
101*56702Sbostic 	if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) != 0)
10251087Sbostic 		return (RET_ERROR);
10351087Sbostic 	if (ftruncate(t->bt_rfd, off))
10451087Sbostic 		return (RET_ERROR);
10551087Sbostic 	UNSET(t, BTF_MODIFIED);
10651087Sbostic 	return (RET_SUCCESS);
10750995Sbostic }
108