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*58812Sbostic static char sccsid[] = "@(#)rec_close.c 5.10 (Berkeley) 03/25/93"; 1050995Sbostic #endif /* LIBC_SCCS and not lint */ 1150995Sbostic 1257462Sbostic #include <sys/types.h> 1350995Sbostic #include <sys/uio.h> 1458748Sbostic #include <sys/mman.h> 1556754Sbostic 1650995Sbostic #include <errno.h> 1757462Sbostic #include <limits.h> 1856754Sbostic #include <stdio.h> 1950995Sbostic #include <unistd.h> 2056754Sbostic 2157933Sbostic #include <db.h> 2251087Sbostic #include "recno.h" 2350995Sbostic 2450995Sbostic /* 2550995Sbostic * __REC_CLOSE -- Close a recno tree. 2650995Sbostic * 2750995Sbostic * Parameters: 2850995Sbostic * dbp: pointer to access method 2950995Sbostic * 3050995Sbostic * Returns: 3150995Sbostic * RET_ERROR, RET_SUCCESS 3250995Sbostic */ 3350995Sbostic int 3450995Sbostic __rec_close(dbp) 3550995Sbostic DB *dbp; 3650995Sbostic { 3754280Sbostic BTREE *t; 3854280Sbostic int rval; 3954280Sbostic 4050995Sbostic if (__rec_sync(dbp) == RET_ERROR) 4150995Sbostic return (RET_ERROR); 4254280Sbostic 4354280Sbostic /* Committed to closing. */ 4454280Sbostic t = dbp->internal; 4554280Sbostic 4658748Sbostic rval = RET_SUCCESS; 4758748Sbostic if (ISSET(t, BTF_MEMMAPPED) && munmap(t->bt_smap, t->bt_msize)) 4858748Sbostic rval = RET_ERROR; 4958748Sbostic 50*58812Sbostic if (!ISSET(t, BTF_RINMEM)) 51*58812Sbostic if (ISSET(t, BTF_CLOSEFP)) { 52*58812Sbostic if (fclose(t->bt_rfp)) 53*58812Sbostic rval = RET_ERROR; 54*58812Sbostic } else 55*58812Sbostic if (close(t->bt_rfd)) 56*58812Sbostic rval = RET_ERROR; 5758748Sbostic 5854280Sbostic if (__bt_close(dbp) == RET_ERROR) 5958748Sbostic rval = RET_ERROR; 6054280Sbostic 6158748Sbostic return (rval); 6250995Sbostic } 6350995Sbostic 6450995Sbostic /* 6550995Sbostic * __REC_SYNC -- sync the recno tree to disk. 6650995Sbostic * 6750995Sbostic * Parameters: 6850995Sbostic * dbp: pointer to access method 6950995Sbostic * 7050995Sbostic * Returns: 7150995Sbostic * RET_SUCCESS, RET_ERROR. 7250995Sbostic */ 7350995Sbostic int 7450995Sbostic __rec_sync(dbp) 7550995Sbostic const DB *dbp; 7650995Sbostic { 7750995Sbostic struct iovec iov[2]; 7850995Sbostic BTREE *t; 7950995Sbostic DBT data, key; 8051087Sbostic off_t off; 8155312Sbostic recno_t scursor, trec; 8250995Sbostic int status; 8350995Sbostic 8450995Sbostic t = dbp->internal; 8550995Sbostic 8656754Sbostic if (ISSET(t, BTF_RDONLY | BTF_RINMEM) || !ISSET(t, BTF_MODIFIED)) 8750995Sbostic return (RET_SUCCESS); 8850995Sbostic 8956702Sbostic /* Read any remaining records into the tree. */ 9058748Sbostic if (!ISSET(t, BTF_EOF) && t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 9150995Sbostic return (RET_ERROR); 9250995Sbostic 9350995Sbostic /* Rewind the file descriptor. */ 9456702Sbostic if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0) 9550995Sbostic return (RET_ERROR); 9650995Sbostic 9750995Sbostic iov[1].iov_base = "\n"; 9850995Sbostic iov[1].iov_len = 1; 9950995Sbostic scursor = t->bt_rcursor; 10050995Sbostic 10155312Sbostic key.size = sizeof(recno_t); 10255312Sbostic key.data = &trec; 10355312Sbostic 10450995Sbostic status = (dbp->seq)(dbp, &key, &data, R_FIRST); 10550995Sbostic while (status == RET_SUCCESS) { 10650995Sbostic iov[0].iov_base = data.data; 10750995Sbostic iov[0].iov_len = data.size; 10850995Sbostic if (writev(t->bt_rfd, iov, 2) != data.size + 1) 10950995Sbostic return (RET_ERROR); 11050995Sbostic status = (dbp->seq)(dbp, &key, &data, R_NEXT); 11150995Sbostic } 11250995Sbostic t->bt_rcursor = scursor; 11351087Sbostic if (status == RET_ERROR) 11451087Sbostic return (RET_ERROR); 11558748Sbostic if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) == -1) 11651087Sbostic return (RET_ERROR); 11751087Sbostic if (ftruncate(t->bt_rfd, off)) 11851087Sbostic return (RET_ERROR); 11956754Sbostic CLR(t, BTF_MODIFIED); 12051087Sbostic return (RET_SUCCESS); 12150995Sbostic } 122