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*55312Sbostic static char sccsid[] = "@(#)rec_close.c 5.4 (Berkeley) 07/17/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 * XXX 5950995Sbostic * Currently don't handle a key marked for deletion when the tree is synced. 6050995Sbostic * Should copy the page and write it out instead of the real page. 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; 70*55312Sbostic recno_t scursor, trec; 7150995Sbostic int status; 7250995Sbostic 7350995Sbostic t = dbp->internal; 7450995Sbostic 7550995Sbostic if (ISSET(t, BTF_INMEM) || NOTSET(t, BTF_MODIFIED)) 7650995Sbostic return (RET_SUCCESS); 7750995Sbostic 7850995Sbostic if (ISSET(t, BTF_RDONLY)) { 7950995Sbostic errno = EPERM; 8050995Sbostic return (RET_ERROR); 8150995Sbostic } 8250995Sbostic 8350995Sbostic /* Suck any remaining records into the tree. */ 8450995Sbostic if (t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 8550995Sbostic return (RET_ERROR); 8650995Sbostic 8750995Sbostic /* Rewind the file descriptor. */ 88*55312Sbostic if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0L) 8950995Sbostic return (RET_ERROR); 9050995Sbostic 9150995Sbostic iov[1].iov_base = "\n"; 9250995Sbostic iov[1].iov_len = 1; 9350995Sbostic scursor = t->bt_rcursor; 9450995Sbostic 95*55312Sbostic key.size = sizeof(recno_t); 96*55312Sbostic key.data = &trec; 97*55312Sbostic 9850995Sbostic status = (dbp->seq)(dbp, &key, &data, R_FIRST); 9950995Sbostic while (status == RET_SUCCESS) { 10050995Sbostic iov[0].iov_base = data.data; 10150995Sbostic iov[0].iov_len = data.size; 10250995Sbostic if (writev(t->bt_rfd, iov, 2) != data.size + 1) 10350995Sbostic return (RET_ERROR); 10450995Sbostic status = (dbp->seq)(dbp, &key, &data, R_NEXT); 10550995Sbostic } 10650995Sbostic t->bt_rcursor = scursor; 10751087Sbostic if (status == RET_ERROR) 10851087Sbostic return (RET_ERROR); 109*55312Sbostic if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) == -1) 11051087Sbostic return (RET_ERROR); 11151087Sbostic if (ftruncate(t->bt_rfd, off)) 11251087Sbostic return (RET_ERROR); 11351087Sbostic UNSET(t, BTF_MODIFIED); 11451087Sbostic return (RET_SUCCESS); 11550995Sbostic } 116