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*54280Sbostic static char sccsid[] = "@(#)rec_close.c 5.3 (Berkeley) 06/23/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 { 33*54280Sbostic BTREE *t; 34*54280Sbostic int rval; 35*54280Sbostic 3650995Sbostic if (__rec_sync(dbp) == RET_ERROR) 3750995Sbostic return (RET_ERROR); 38*54280Sbostic 39*54280Sbostic /* Committed to closing. */ 40*54280Sbostic t = dbp->internal; 41*54280Sbostic rval = t->bt_rfp == NULL ? close(t->bt_rfd) : fclose(t->bt_rfp); 42*54280Sbostic 43*54280Sbostic if (__bt_close(dbp) == RET_ERROR) 44*54280Sbostic return (RET_ERROR); 45*54280Sbostic 46*54280Sbostic 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; 7050995Sbostic recno_t scursor; 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. */ 8850995Sbostic if (lseek(t->bt_rfd, 0L, 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 9550995Sbostic status = (dbp->seq)(dbp, &key, &data, R_FIRST); 9650995Sbostic while (status == RET_SUCCESS) { 9750995Sbostic iov[0].iov_base = data.data; 9850995Sbostic iov[0].iov_len = data.size; 9950995Sbostic if (writev(t->bt_rfd, iov, 2) != data.size + 1) 10050995Sbostic return (RET_ERROR); 10150995Sbostic status = (dbp->seq)(dbp, &key, &data, R_NEXT); 10250995Sbostic } 10350995Sbostic t->bt_rcursor = scursor; 10451087Sbostic if (status == RET_ERROR) 10551087Sbostic return (RET_ERROR); 10651087Sbostic if ((off = lseek(t->bt_rfd, 0L, SEEK_CUR)) == -1) 10751087Sbostic return (RET_ERROR); 10851087Sbostic if (ftruncate(t->bt_rfd, off)) 10951087Sbostic return (RET_ERROR); 11051087Sbostic UNSET(t, BTF_MODIFIED); 11151087Sbostic return (RET_SUCCESS); 11250995Sbostic } 113