xref: /csrg-svn/lib/libc/db/recno/rec_close.c (revision 57462)
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*57462Sbostic static char sccsid[] = "@(#)rec_close.c	5.7 (Berkeley) 01/10/93";
1050995Sbostic #endif /* LIBC_SCCS and not lint */
1150995Sbostic 
12*57462Sbostic #include <sys/types.h>
1350995Sbostic #include <sys/uio.h>
1456754Sbostic 
1556754Sbostic #include <db.h>
1650995Sbostic #include <errno.h>
17*57462Sbostic #include <limits.h>
1856754Sbostic #include <stdio.h>
1950995Sbostic #include <unistd.h>
2056754Sbostic 
2151087Sbostic #include "recno.h"
2250995Sbostic 
2350995Sbostic /*
2450995Sbostic  * __REC_CLOSE -- Close a recno tree.
2550995Sbostic  *
2650995Sbostic  * Parameters:
2750995Sbostic  *	dbp:	pointer to access method
2850995Sbostic  *
2950995Sbostic  * Returns:
3050995Sbostic  *	RET_ERROR, RET_SUCCESS
3150995Sbostic  */
3250995Sbostic int
3350995Sbostic __rec_close(dbp)
3450995Sbostic 	DB *dbp;
3550995Sbostic {
3654280Sbostic 	BTREE *t;
3754280Sbostic 	int rval;
3854280Sbostic 
3950995Sbostic 	if (__rec_sync(dbp) == RET_ERROR)
4050995Sbostic 		return (RET_ERROR);
4154280Sbostic 
4254280Sbostic 	/* Committed to closing. */
4354280Sbostic 	t = dbp->internal;
4456754Sbostic 	rval = ISSET(t, BTF_RINMEM) ? 0 :
4556754Sbostic 	    t->bt_rfp == NULL ? close(t->bt_rfd) : fclose(t->bt_rfp);
4654280Sbostic 
4754280Sbostic 	if (__bt_close(dbp) == RET_ERROR)
4854280Sbostic 		return (RET_ERROR);
4954280Sbostic 
5054280Sbostic 	return (rval ? RET_ERROR : RET_SUCCESS);
5150995Sbostic }
5250995Sbostic 
5350995Sbostic /*
5450995Sbostic  * __REC_SYNC -- sync the recno tree to disk.
5550995Sbostic  *
5650995Sbostic  * Parameters:
5750995Sbostic  *	dbp:	pointer to access method
5850995Sbostic  *
5950995Sbostic  * Returns:
6050995Sbostic  *	RET_SUCCESS, RET_ERROR.
6150995Sbostic  */
6250995Sbostic int
6350995Sbostic __rec_sync(dbp)
6450995Sbostic 	const DB *dbp;
6550995Sbostic {
6650995Sbostic 	struct iovec iov[2];
6750995Sbostic 	BTREE *t;
6850995Sbostic 	DBT data, key;
6951087Sbostic 	off_t off;
7055312Sbostic 	recno_t scursor, trec;
7150995Sbostic 	int status;
7250995Sbostic 
7350995Sbostic 	t = dbp->internal;
7450995Sbostic 
7556754Sbostic 	if (ISSET(t, BTF_RDONLY | BTF_RINMEM) || !ISSET(t, BTF_MODIFIED))
7650995Sbostic 		return (RET_SUCCESS);
7750995Sbostic 
7856702Sbostic 	/* Read any remaining records into the tree. */
7950995Sbostic 	if (t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
8050995Sbostic 		return (RET_ERROR);
8150995Sbostic 
8250995Sbostic 	/* Rewind the file descriptor. */
8356702Sbostic 	if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0)
8450995Sbostic 		return (RET_ERROR);
8550995Sbostic 
8650995Sbostic 	iov[1].iov_base = "\n";
8750995Sbostic 	iov[1].iov_len = 1;
8850995Sbostic 	scursor = t->bt_rcursor;
8950995Sbostic 
9055312Sbostic 	key.size = sizeof(recno_t);
9155312Sbostic 	key.data = &trec;
9255312Sbostic 
9350995Sbostic 	status = (dbp->seq)(dbp, &key, &data, R_FIRST);
9450995Sbostic         while (status == RET_SUCCESS) {
9550995Sbostic 		iov[0].iov_base = data.data;
9650995Sbostic 		iov[0].iov_len = data.size;
9750995Sbostic 		if (writev(t->bt_rfd, iov, 2) != data.size + 1)
9850995Sbostic 			return (RET_ERROR);
9950995Sbostic                 status = (dbp->seq)(dbp, &key, &data, R_NEXT);
10050995Sbostic         }
10150995Sbostic 	t->bt_rcursor = scursor;
10251087Sbostic 	if (status == RET_ERROR)
10351087Sbostic 		return (RET_ERROR);
10456702Sbostic 	if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) != 0)
10551087Sbostic 		return (RET_ERROR);
10651087Sbostic 	if (ftruncate(t->bt_rfd, off))
10751087Sbostic 		return (RET_ERROR);
10856754Sbostic 	CLR(t, BTF_MODIFIED);
10951087Sbostic 	return (RET_SUCCESS);
11050995Sbostic }
111