123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these libraries and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
- /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
- /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
- /*%% (c) Copyright 1993, 1994 Novell, Inc. */
- /*%% $XConsortium: isread.c /main/3 1995/10/23 11:43:35 rswiston $ */
- /*
- * Copyright (c) 1988 by Sun Microsystems, Inc.
- */
- /*
- * isread.c
- *
- * Description:
- * Read a record from ISAM file.
- */
- #include "isam_impl.h"
- #include <sys/time.h>
- static int _amread();
- /*
- * err = isread(isfd, record, mode)
- *
- * Isread() reads a new record from an ISAM file.
- *
- * Current record position is set.
- * isrecnum is set to indicate the read record.
- *
- * If the ISAM file is for variable length records, the isreclen variable
- * is set to indicate the actual length of the record.
- *
- * Returns 0 if successful, or -1 of any error.
- *
- * Errors:
- * ELOCKED The record or the entire file is locked by another process.
- * ENOTOPEN isfd does not correspond to an open ISAM file, or the
- * ISAM file was opened with ISOUTPUT mode.
- * EBADARG Bad mode parameter.
- * ENOCURR Mode is ISCURR and the current record position is not set.
- * ENOREC Specified record cannot be found (random access read)
- * EENDFILE The end file of is reached (sequential read).
- * EBADKEY Index was deleted by other process (can happen only
- * when lock file is purged).
- */
- int
- isread(int isfd, char *record, int mode)
- {
- Fab *fab;
- int reclen;
- Recno recnum;
- int ret;
- enum readmode readmode;
- /*
- * Get File Access Block.
- */
- if ((fab = _isfd_find(isfd)) == NULL) {
- _setiserrno2(ENOTOPEN, '9', '0');
- return (ISERROR);
- }
- /*
- * Check that the open mode was ISINPUT, or ISINOUT.
- */
- if (fab->openmode != OM_INPUT && fab->openmode != OM_INOUT) {
- _setiserrno2(ENOTOPEN, '9', '0');
- return (ISERROR);
- }
- /*
- * Extract read mode.
- */
- if ((readmode = _getreadmode(mode)) == RM_BADMODE) {
- _setiserrno2(EBADARG, '9', '0');
- return (ISERROR);
- }
- /*
- * All keys must be in the minimum record length.
- * So send just the minimum length part of the record.
- */
- reclen = fab->minreclen;
- /*
- * Call the Access Method
- */
- recnum = isrecnum;
- if ((ret = _amread(&fab->isfhandle, record, &reclen,
- readmode, &fab->curpos, &recnum,
- &fab->errcode)) == ISOK) {
- isrecnum = recnum; /* Set isrecnum */
- }
- isreclen = reclen;
- _seterr_errcode(&fab->errcode);
- return (ret); /* Successful read */
- }
- /*
- * _amread(isfhandle, record, reclen, readmode, curpos, recnum, errcode)
- *
- * _amread() reads a record from ISAM file
- *
- * Input params:
- * isfhandle Handle of ISAM file
- * readmode Specifies access mode (random or sequential)
- * curpos current record position
- * recnum copy if isrecnum
- *
- * Output params:
- * curpos new current position
- * recnum record number
- * errcode error status of the operation
- * reclen actual length of the record
- * record filled with data
- *
- */
- static int
- _amread(Bytearray *isfhandle, char *record, int *reclen,
- enum readmode readmode, Bytearray *curpos, Recno *recnum,
- struct errcode *errcode)
- {
- Fcb *fcb = NULL;
- Recno recnum2 = 0;
- int err;
- Crp *crp;
- Btree *btree = NULL;
- Keydesc2 *pkeydesc2;
- char keybuf1[MAXKEYSIZE], keybuf2[MAXKEYSIZE];
- char *pkey = NULL, *pkeynext;
- int skipbytes;
- int ret;
- Bytearray oldcurpos;
- int (*rec_read)();
- _isam_entryhook();
- /*
- * Get FCB corresponding to the isfhandle handle.
- */
- if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
- _isam_exithook();
- return (ISERROR);
- }
- rec_read = (fcb->varflag?_vlrec_read:_flrec_read);
- /*
- * Update information in FCB from CNTL page on the disk
- */
- (void)_isfcb_cntlpg_r2(fcb);
- /*
- * Save the old record position.
- */
- oldcurpos = _bytearr_dup(curpos);
- /*
- * Get info from current record position structure.
- */
- crp = (Crp *) curpos->data;
- if (crp->keyid == PHYS_ORDER) {
- /*
- * Physical order in use.
- */
-
- switch (readmode) {
- case RM_EQUAL:
- recnum2 = *recnum; /* passed from isrecnum */
- if ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK) {
- _amseterrcode(errcode, ENOREC);
- goto ERROR;
- }
- break;
- case RM_GREAT:
- recnum2 = *recnum + 1;
- if (recnum2 < 1) recnum2 = 1;
- /*
- * Skip deleted records.
- */
- while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
- err == ENOREC)
- recnum2++;
-
- if (err != ISOK) {
- _amseterrcode(errcode, ENOREC);
- goto ERROR;
- }
- break;
- case RM_GTEQ:
- recnum2 = *recnum;
- if (recnum2 < 1) recnum2 = 1;
- /*
- * Skip deleted records.
- */
- while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
- err == ENOREC)
- recnum2++;
-
- if (err != ISOK) {
- _amseterrcode(errcode, ENOREC);
- goto ERROR;
- }
- break;
- case RM_LESS:
- recnum2 = *recnum - 1;
- if (recnum2 > fcb->lastrecno) recnum2 = fcb->lastrecno;
- /*
- * Skip deleted records.
- */
- while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
- err == ENOREC)
- recnum2--;
-
- if (err != ISOK) {
- _amseterrcode(errcode, ENOREC);
- goto ERROR;
- }
- break;
- case RM_LTEQ:
- recnum2 = *recnum;
- if (recnum2 > fcb->lastrecno) recnum2 = fcb->lastrecno;
- /*
- * Skip deleted records.
- */
- while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
- err == ENOREC)
- recnum2--;
-
- if (err != ISOK) {
- _amseterrcode(errcode, ENOREC);
- goto ERROR;
- }
- break;
- case RM_FIRST:
- recnum2 = 1;
- /*
- * Skip deleted records.
- */
- while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
- err == ENOREC)
- recnum2++;
-
- if (err != ISOK) {
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- }
- break;
- case RM_CURR:
- switch (crp->flag) {
- case CRP_ON:
- case CRP_BEFORE:
- case CRP_AFTER:
- recnum2 = crp->recno;
- break;
- case CRP_BEFOREANY:
- recnum2 = 1;
- break;
- default:
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
-
- if( rec_read(fcb, record, recnum2, reclen) != ISOK) {
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
- break;
- case RM_NEXT:
- switch (crp->flag) {
- case CRP_ON:
- case CRP_AFTER:
- recnum2 = crp->recno + 1;
- break;
- case CRP_BEFOREANY:
- recnum2 = 1;
- break;
- case CRP_BEFORE:
- recnum2 = crp->recno;
- break;
- case CRP_AFTERANY:
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- default:
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
-
- /*
- * Skip deleted records.
- */
- while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
- err == ENOREC)
- recnum2++;
-
- if (err != ISOK) {
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- }
- break;
- case RM_PREV:
- switch (crp->flag) {
- case CRP_ON:
- case CRP_BEFORE:
- recnum2 = crp->recno - 1;
- break;
- case CRP_AFTER:
- recnum2 = crp->recno;
- break;
- case CRP_BEFOREANY:
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- default:
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
-
- /*
- * Skip deleted records.
- */
- while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
- err == ENOREC)
- recnum2--;
-
- if (err != ISOK) {
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- }
- break;
- case RM_LAST:
- recnum2 = fcb->lastrecno;
- /*
- * Skip deleted records.
- */
- while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
- err == ENOREC)
- recnum2--;
- if (err != ISOK) {
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- }
- break;
- default:
- _isfatal_error("Invalid readmode");
- }
- *recnum = recnum2;
- /*
- * Set new current record position.
- */
- crp->recno = recnum2;
- crp->flag = CRP_ON;
- } /* physical order */
- else {
- /*
- * Find key descriptor in FCB
- */
- if ((pkeydesc2 = _isfcb_indfindkey(fcb, crp->keyid)) == NULL) {
- _amseterrcode(errcode, EBADKEY);
- goto ERROR;
- }
- /*
- * skipkeybytes is set to the number of bytes in the beginning
- * of the key:
- * RECNOSIZE for ISNODUPS keys to skip recno part
- * RECNOSIZE + DUPIDSIZE to skip recno and duplicate serial number
- */
- skipbytes = RECNOSIZE;
- if (ALLOWS_DUPS2(pkeydesc2))
- skipbytes += DUPIDSIZE;
-
- /*
- * Create B tree object.
- */
- btree = _isbtree_create(fcb, pkeydesc2);
- switch (readmode) {
- case RM_EQUAL:
- case RM_GTEQ:
- /*
- * Make sure that you will read the first duplicate.
- */
- _iskey_fillmin(pkeydesc2, keybuf1);
-
- /*
- * Extract key fields from record.
- */
- _iskey_extract(pkeydesc2, record, keybuf2);
- memcpy((void *)(keybuf1 + skipbytes),
- (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
- /*
- * Position pointer in the B-tree in before the searched value.
- */
- _isbtree_search(btree, keybuf1);
-
- if ((pkey = _isbtree_next(btree)) == NULL) {
- _amseterrcode(errcode, ENOREC);
- goto ERROR;
- }
-
- if (readmode == RM_EQUAL &&
- memcmp(keybuf1 + skipbytes, pkey + skipbytes,
- crp->matchkeylen) != 0) {
- _amseterrcode(errcode, ENOREC);
- goto ERROR;
- }
- crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
- memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
-
- if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
- recnum2 = crp->recno;
- break;
- case RM_GREAT:
- /*
- * Make sure that you will read past all matching records.
- */
- _iskey_fillmax(pkeydesc2, keybuf1);
-
- /*
- * Extract key fields from record.
- */
- _iskey_extract(pkeydesc2, record, keybuf2);
- memcpy((void *)(keybuf1 + skipbytes),
- (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
- /*
- * Position pointer in the B-tree in before the searched value.
- */
- _isbtree_search(btree, keybuf1);
-
- if ((pkey = _isbtree_next(btree)) == NULL) {
- _amseterrcode(errcode, ENOREC);
- goto ERROR;
- }
-
- crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
- memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
-
- if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
- recnum2 = crp->recno;
- break;
- case RM_LESS:
- /*
- * Make sure that you will read before all matching records.
- */
- _iskey_fillmin(pkeydesc2, keybuf1);
-
- /*
- * Extract key fields from record.
- */
- _iskey_extract(pkeydesc2, record, keybuf2);
- memcpy((void *)(keybuf1 + skipbytes),
- (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
- /*
- * Position pointer in the B-tree in before the searched value.
- */
- _isbtree_search(btree, keybuf1);
-
- if ((pkey = _isbtree_current(btree)) == NULL) {
- _amseterrcode(errcode, ENOREC);
- goto ERROR;
- }
-
- crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
- memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
-
- if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
- recnum2 = crp->recno;
- break;
- case RM_LTEQ:
- /*
- * Make sure that you will read the last duplicate.
- */
- _iskey_fillmax(pkeydesc2, keybuf1);
-
- /*
- * Extract key fields from record.
- */
- _iskey_extract(pkeydesc2, record, keybuf2);
- memcpy((void *)(keybuf1 + skipbytes),
- (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
- /*
- * Position pointer in the B-tree in before the searched value.
- */
- _isbtree_search(btree, keybuf1);
-
- if ((pkey = _isbtree_current(btree)) == NULL) {
- _amseterrcode(errcode, ENOREC);
- goto ERROR;
- }
-
- crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
- memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
-
- if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
- recnum2 = crp->recno;
- break;
- case RM_CURR:
- switch (crp->flag) {
- case CRP_ON:
- case CRP_BEFORE:
- case CRP_AFTER:
- /*
- * We have check if the record has not been deleted
- * since the current record position was set up.
- */
- _isbtree_search(btree, crp->key);
- pkey = _isbtree_current(btree);
- if (pkey == NULL ||
- ldrecno(pkey + KEY_RECNO_OFF) != crp->recno) {
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
- break;
- case CRP_BEFOREANY:
- _isbtree_search(btree, crp->key);
- pkey = _isbtree_next(btree);
- if (pkey == NULL) {
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- }
-
- crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
- memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
- break;
- default:
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
-
- if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
- recnum2 = crp->recno;
- break;
- case RM_NEXT:
- /*
- * Validate that current position has been set.
- */
- switch (crp->flag) {
- case CRP_ON:
- case CRP_BEFORE:
- case CRP_BEFOREANY:
- break;
- case CRP_AFTERANY:
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- default:
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
-
- /*
- * Position pointer to current position.
- */
- _isbtree_search(btree, crp->key);
-
- if (crp->flag == CRP_BEFORE)
- pkey = _isbtree_current(btree);
- else
- /* crp->flag == CRP_ON || crp->flag == CRP_BEFOREANY */
- pkey = _isbtree_next(btree);
- if (pkey == NULL) {
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- }
-
- crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
- memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
-
- if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
- recnum2 = crp->recno;
- break;
- case RM_PREV:
- /*
- * Validate that current position has been set.
- */
- switch (crp->flag) {
- case CRP_ON:
- case CRP_BEFORE:
- /*
- * To get to the previous record, we must decrement
- * the TID part for unique keys, or duplicate serial number
- * for non-unique keys.
- */
- memcpy((void *)keybuf1,
- (const void *)crp->key, pkeydesc2->k2_len);
- if (ALLOWS_DUPS2(pkeydesc2)) {
- stlong(ldlong(keybuf1 + KEY_DUPS_OFF) - 1,
- keybuf1 + KEY_DUPS_OFF);
- }
- else {
- strecno(ldrecno(keybuf1 + KEY_RECNO_OFF) - 1,
- keybuf1 + KEY_RECNO_OFF);
- }
- break;
- case CRP_AFTER:
- memcpy((void *)keybuf1, (const void *)crp->key, pkeydesc2->k2_len);
- break;
- case CRP_BEFOREANY:
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- default:
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
-
- /*
- * Position pointer to current position.
- */
- _isbtree_search(btree, keybuf1);
-
- pkey = _isbtree_current(btree);
- if (pkey == NULL) {
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- }
-
- crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
- memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
-
- if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
- _amseterrcode(errcode, ENOCURR);
- goto ERROR;
- }
- recnum2 = crp->recno;
- break;
- case RM_FIRST:
- /*
- * Fill key buffer with -infinity.
- */
- _iskey_fillmin(pkeydesc2, keybuf1);
-
- /*
- * Position pointer in the B-tree before any key entry.
- */
- _isbtree_search(btree, keybuf1);
-
- if ((pkey = _isbtree_next(btree)) == NULL) {
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- }
-
- crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
- memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
-
- if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- }
- recnum2 = crp->recno;
- break;
- case RM_LAST:
- /*
- * Fill key buffer with +infinity.
- */
- _iskey_fillmax(pkeydesc2, keybuf1);
-
- /*
- * Position pointer in the B-tree before any key entry.
- */
- _isbtree_search(btree, keybuf1);
-
- if ((pkey = _isbtree_current(btree)) == NULL) {
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- }
-
- crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
- memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
-
- if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
- _amseterrcode(errcode, EENDFILE);
- goto ERROR;
- }
- recnum2 = crp->recno;
- break;
- default:
- _isfatal_error("Invalid readmode");
- }
- *recnum = recnum2;
- crp->flag = CRP_ON;
- /*
- * Set up isdupl to handle isstat2 value for keys that allow
- * duplicate values.
- */
- if (ALLOWS_DUPS2(pkeydesc2) && (pkeynext = _isbtree_next(btree)) &&
- memcmp(pkey + skipbytes, pkeynext + skipbytes,
- crp->matchkeylen) == 0) {
- isdupl = 1;
- }
- _isbtree_destroy(btree);
- }
- _amseterrcode(errcode, ISOK);
- ret = ISOK;
- /* Clean-up work. */
- _isdisk_commit(); /* This will only check
- * that we unfixed all fixed
- * buffers */
- _isdisk_inval();
- _bytearr_free(&oldcurpos);
- _isam_exithook();
- return (ret);
- ERROR:
- *reclen = 0;
- _isdisk_inval();
- _bytearr_free(&oldcurpos);
- /*
- * If error is ENOREC, set the current record position undefined.
- */
- if (errcode->iserrno == ENOREC || errcode->iserrno == EENDFILE) {
- ((Crp *)curpos->data)->flag = CRP_UNDEF;
- }
- if (btree != NULL)
- _isbtree_destroy(btree);
- _isam_exithook();
- return (ISERROR);
- }
|