isread.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
  24. /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
  25. /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
  26. /*%% (c) Copyright 1993, 1994 Novell, Inc. */
  27. /*%% $XConsortium: isread.c /main/3 1995/10/23 11:43:35 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isread.c
  33. *
  34. * Description:
  35. * Read a record from ISAM file.
  36. */
  37. #include "isam_impl.h"
  38. #include <sys/time.h>
  39. static int _amread();
  40. /*
  41. * err = isread(isfd, record, mode)
  42. *
  43. * Isread() reads a new record from an ISAM file.
  44. *
  45. * Current record position is set.
  46. * isrecnum is set to indicate the read record.
  47. *
  48. * If the ISAM file is for variable length records, the isreclen variable
  49. * is set to indicate the actual length of the record.
  50. *
  51. * Returns 0 if successful, or -1 of any error.
  52. *
  53. * Errors:
  54. * ELOCKED The record or the entire file is locked by another process.
  55. * ENOTOPEN isfd does not correspond to an open ISAM file, or the
  56. * ISAM file was opened with ISOUTPUT mode.
  57. * EBADARG Bad mode parameter.
  58. * ENOCURR Mode is ISCURR and the current record position is not set.
  59. * ENOREC Specified record cannot be found (random access read)
  60. * EENDFILE The end file of is reached (sequential read).
  61. * EBADKEY Index was deleted by other process (can happen only
  62. * when lock file is purged).
  63. */
  64. int
  65. isread(int isfd, char *record, int mode)
  66. {
  67. Fab *fab;
  68. int reclen;
  69. Recno recnum;
  70. int ret;
  71. enum readmode readmode;
  72. /*
  73. * Get File Access Block.
  74. */
  75. if ((fab = _isfd_find(isfd)) == NULL) {
  76. _setiserrno2(ENOTOPEN, '9', '0');
  77. return (ISERROR);
  78. }
  79. /*
  80. * Check that the open mode was ISINPUT, or ISINOUT.
  81. */
  82. if (fab->openmode != OM_INPUT && fab->openmode != OM_INOUT) {
  83. _setiserrno2(ENOTOPEN, '9', '0');
  84. return (ISERROR);
  85. }
  86. /*
  87. * Extract read mode.
  88. */
  89. if ((readmode = _getreadmode(mode)) == RM_BADMODE) {
  90. _setiserrno2(EBADARG, '9', '0');
  91. return (ISERROR);
  92. }
  93. /*
  94. * All keys must be in the minimum record length.
  95. * So send just the minimum length part of the record.
  96. */
  97. reclen = fab->minreclen;
  98. /*
  99. * Call the Access Method
  100. */
  101. recnum = isrecnum;
  102. if ((ret = _amread(&fab->isfhandle, record, &reclen,
  103. readmode, &fab->curpos, &recnum,
  104. &fab->errcode)) == ISOK) {
  105. isrecnum = recnum; /* Set isrecnum */
  106. }
  107. isreclen = reclen;
  108. _seterr_errcode(&fab->errcode);
  109. return (ret); /* Successful read */
  110. }
  111. /*
  112. * _amread(isfhandle, record, reclen, readmode, curpos, recnum, errcode)
  113. *
  114. * _amread() reads a record from ISAM file
  115. *
  116. * Input params:
  117. * isfhandle Handle of ISAM file
  118. * readmode Specifies access mode (random or sequential)
  119. * curpos current record position
  120. * recnum copy if isrecnum
  121. *
  122. * Output params:
  123. * curpos new current position
  124. * recnum record number
  125. * errcode error status of the operation
  126. * reclen actual length of the record
  127. * record filled with data
  128. *
  129. */
  130. static int
  131. _amread(Bytearray *isfhandle, char *record, int *reclen,
  132. enum readmode readmode, Bytearray *curpos, Recno *recnum,
  133. struct errcode *errcode)
  134. {
  135. Fcb *fcb = NULL;
  136. Recno recnum2 = 0;
  137. int err;
  138. Crp *crp;
  139. Btree *btree = NULL;
  140. Keydesc2 *pkeydesc2;
  141. char keybuf1[MAXKEYSIZE], keybuf2[MAXKEYSIZE];
  142. char *pkey = NULL, *pkeynext;
  143. int skipbytes;
  144. int ret;
  145. Bytearray oldcurpos;
  146. int (*rec_read)();
  147. _isam_entryhook();
  148. /*
  149. * Get FCB corresponding to the isfhandle handle.
  150. */
  151. if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
  152. _isam_exithook();
  153. return (ISERROR);
  154. }
  155. rec_read = (fcb->varflag?_vlrec_read:_flrec_read);
  156. /*
  157. * Update information in FCB from CNTL page on the disk
  158. */
  159. (void)_isfcb_cntlpg_r2(fcb);
  160. /*
  161. * Save the old record position.
  162. */
  163. oldcurpos = _bytearr_dup(curpos);
  164. /*
  165. * Get info from current record position structure.
  166. */
  167. crp = (Crp *) curpos->data;
  168. if (crp->keyid == PHYS_ORDER) {
  169. /*
  170. * Physical order in use.
  171. */
  172. switch (readmode) {
  173. case RM_EQUAL:
  174. recnum2 = *recnum; /* passed from isrecnum */
  175. if ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK) {
  176. _amseterrcode(errcode, ENOREC);
  177. goto ERROR;
  178. }
  179. break;
  180. case RM_GREAT:
  181. recnum2 = *recnum + 1;
  182. if (recnum2 < 1) recnum2 = 1;
  183. /*
  184. * Skip deleted records.
  185. */
  186. while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
  187. err == ENOREC)
  188. recnum2++;
  189. if (err != ISOK) {
  190. _amseterrcode(errcode, ENOREC);
  191. goto ERROR;
  192. }
  193. break;
  194. case RM_GTEQ:
  195. recnum2 = *recnum;
  196. if (recnum2 < 1) recnum2 = 1;
  197. /*
  198. * Skip deleted records.
  199. */
  200. while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
  201. err == ENOREC)
  202. recnum2++;
  203. if (err != ISOK) {
  204. _amseterrcode(errcode, ENOREC);
  205. goto ERROR;
  206. }
  207. break;
  208. case RM_LESS:
  209. recnum2 = *recnum - 1;
  210. if (recnum2 > fcb->lastrecno) recnum2 = fcb->lastrecno;
  211. /*
  212. * Skip deleted records.
  213. */
  214. while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
  215. err == ENOREC)
  216. recnum2--;
  217. if (err != ISOK) {
  218. _amseterrcode(errcode, ENOREC);
  219. goto ERROR;
  220. }
  221. break;
  222. case RM_LTEQ:
  223. recnum2 = *recnum;
  224. if (recnum2 > fcb->lastrecno) recnum2 = fcb->lastrecno;
  225. /*
  226. * Skip deleted records.
  227. */
  228. while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
  229. err == ENOREC)
  230. recnum2--;
  231. if (err != ISOK) {
  232. _amseterrcode(errcode, ENOREC);
  233. goto ERROR;
  234. }
  235. break;
  236. case RM_FIRST:
  237. recnum2 = 1;
  238. /*
  239. * Skip deleted records.
  240. */
  241. while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
  242. err == ENOREC)
  243. recnum2++;
  244. if (err != ISOK) {
  245. _amseterrcode(errcode, EENDFILE);
  246. goto ERROR;
  247. }
  248. break;
  249. case RM_CURR:
  250. switch (crp->flag) {
  251. case CRP_ON:
  252. case CRP_BEFORE:
  253. case CRP_AFTER:
  254. recnum2 = crp->recno;
  255. break;
  256. case CRP_BEFOREANY:
  257. recnum2 = 1;
  258. break;
  259. default:
  260. _amseterrcode(errcode, ENOCURR);
  261. goto ERROR;
  262. }
  263. if( rec_read(fcb, record, recnum2, reclen) != ISOK) {
  264. _amseterrcode(errcode, ENOCURR);
  265. goto ERROR;
  266. }
  267. break;
  268. case RM_NEXT:
  269. switch (crp->flag) {
  270. case CRP_ON:
  271. case CRP_AFTER:
  272. recnum2 = crp->recno + 1;
  273. break;
  274. case CRP_BEFOREANY:
  275. recnum2 = 1;
  276. break;
  277. case CRP_BEFORE:
  278. recnum2 = crp->recno;
  279. break;
  280. case CRP_AFTERANY:
  281. _amseterrcode(errcode, EENDFILE);
  282. goto ERROR;
  283. default:
  284. _amseterrcode(errcode, ENOCURR);
  285. goto ERROR;
  286. }
  287. /*
  288. * Skip deleted records.
  289. */
  290. while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
  291. err == ENOREC)
  292. recnum2++;
  293. if (err != ISOK) {
  294. _amseterrcode(errcode, EENDFILE);
  295. goto ERROR;
  296. }
  297. break;
  298. case RM_PREV:
  299. switch (crp->flag) {
  300. case CRP_ON:
  301. case CRP_BEFORE:
  302. recnum2 = crp->recno - 1;
  303. break;
  304. case CRP_AFTER:
  305. recnum2 = crp->recno;
  306. break;
  307. case CRP_BEFOREANY:
  308. _amseterrcode(errcode, EENDFILE);
  309. goto ERROR;
  310. default:
  311. _amseterrcode(errcode, ENOCURR);
  312. goto ERROR;
  313. }
  314. /*
  315. * Skip deleted records.
  316. */
  317. while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
  318. err == ENOREC)
  319. recnum2--;
  320. if (err != ISOK) {
  321. _amseterrcode(errcode, EENDFILE);
  322. goto ERROR;
  323. }
  324. break;
  325. case RM_LAST:
  326. recnum2 = fcb->lastrecno;
  327. /*
  328. * Skip deleted records.
  329. */
  330. while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
  331. err == ENOREC)
  332. recnum2--;
  333. if (err != ISOK) {
  334. _amseterrcode(errcode, EENDFILE);
  335. goto ERROR;
  336. }
  337. break;
  338. default:
  339. _isfatal_error("Invalid readmode");
  340. }
  341. *recnum = recnum2;
  342. /*
  343. * Set new current record position.
  344. */
  345. crp->recno = recnum2;
  346. crp->flag = CRP_ON;
  347. } /* physical order */
  348. else {
  349. /*
  350. * Find key descriptor in FCB
  351. */
  352. if ((pkeydesc2 = _isfcb_indfindkey(fcb, crp->keyid)) == NULL) {
  353. _amseterrcode(errcode, EBADKEY);
  354. goto ERROR;
  355. }
  356. /*
  357. * skipkeybytes is set to the number of bytes in the beginning
  358. * of the key:
  359. * RECNOSIZE for ISNODUPS keys to skip recno part
  360. * RECNOSIZE + DUPIDSIZE to skip recno and duplicate serial number
  361. */
  362. skipbytes = RECNOSIZE;
  363. if (ALLOWS_DUPS2(pkeydesc2))
  364. skipbytes += DUPIDSIZE;
  365. /*
  366. * Create B tree object.
  367. */
  368. btree = _isbtree_create(fcb, pkeydesc2);
  369. switch (readmode) {
  370. case RM_EQUAL:
  371. case RM_GTEQ:
  372. /*
  373. * Make sure that you will read the first duplicate.
  374. */
  375. _iskey_fillmin(pkeydesc2, keybuf1);
  376. /*
  377. * Extract key fields from record.
  378. */
  379. _iskey_extract(pkeydesc2, record, keybuf2);
  380. memcpy((void *)(keybuf1 + skipbytes),
  381. (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
  382. /*
  383. * Position pointer in the B-tree in before the searched value.
  384. */
  385. _isbtree_search(btree, keybuf1);
  386. if ((pkey = _isbtree_next(btree)) == NULL) {
  387. _amseterrcode(errcode, ENOREC);
  388. goto ERROR;
  389. }
  390. if (readmode == RM_EQUAL &&
  391. memcmp(keybuf1 + skipbytes, pkey + skipbytes,
  392. crp->matchkeylen) != 0) {
  393. _amseterrcode(errcode, ENOREC);
  394. goto ERROR;
  395. }
  396. crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  397. memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
  398. if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
  399. _amseterrcode(errcode, ENOCURR);
  400. goto ERROR;
  401. }
  402. recnum2 = crp->recno;
  403. break;
  404. case RM_GREAT:
  405. /*
  406. * Make sure that you will read past all matching records.
  407. */
  408. _iskey_fillmax(pkeydesc2, keybuf1);
  409. /*
  410. * Extract key fields from record.
  411. */
  412. _iskey_extract(pkeydesc2, record, keybuf2);
  413. memcpy((void *)(keybuf1 + skipbytes),
  414. (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
  415. /*
  416. * Position pointer in the B-tree in before the searched value.
  417. */
  418. _isbtree_search(btree, keybuf1);
  419. if ((pkey = _isbtree_next(btree)) == NULL) {
  420. _amseterrcode(errcode, ENOREC);
  421. goto ERROR;
  422. }
  423. crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  424. memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
  425. if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
  426. _amseterrcode(errcode, ENOCURR);
  427. goto ERROR;
  428. }
  429. recnum2 = crp->recno;
  430. break;
  431. case RM_LESS:
  432. /*
  433. * Make sure that you will read before all matching records.
  434. */
  435. _iskey_fillmin(pkeydesc2, keybuf1);
  436. /*
  437. * Extract key fields from record.
  438. */
  439. _iskey_extract(pkeydesc2, record, keybuf2);
  440. memcpy((void *)(keybuf1 + skipbytes),
  441. (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
  442. /*
  443. * Position pointer in the B-tree in before the searched value.
  444. */
  445. _isbtree_search(btree, keybuf1);
  446. if ((pkey = _isbtree_current(btree)) == NULL) {
  447. _amseterrcode(errcode, ENOREC);
  448. goto ERROR;
  449. }
  450. crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  451. memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
  452. if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
  453. _amseterrcode(errcode, ENOCURR);
  454. goto ERROR;
  455. }
  456. recnum2 = crp->recno;
  457. break;
  458. case RM_LTEQ:
  459. /*
  460. * Make sure that you will read the last duplicate.
  461. */
  462. _iskey_fillmax(pkeydesc2, keybuf1);
  463. /*
  464. * Extract key fields from record.
  465. */
  466. _iskey_extract(pkeydesc2, record, keybuf2);
  467. memcpy((void *)(keybuf1 + skipbytes),
  468. (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
  469. /*
  470. * Position pointer in the B-tree in before the searched value.
  471. */
  472. _isbtree_search(btree, keybuf1);
  473. if ((pkey = _isbtree_current(btree)) == NULL) {
  474. _amseterrcode(errcode, ENOREC);
  475. goto ERROR;
  476. }
  477. crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  478. memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
  479. if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
  480. _amseterrcode(errcode, ENOCURR);
  481. goto ERROR;
  482. }
  483. recnum2 = crp->recno;
  484. break;
  485. case RM_CURR:
  486. switch (crp->flag) {
  487. case CRP_ON:
  488. case CRP_BEFORE:
  489. case CRP_AFTER:
  490. /*
  491. * We have check if the record has not been deleted
  492. * since the current record position was set up.
  493. */
  494. _isbtree_search(btree, crp->key);
  495. pkey = _isbtree_current(btree);
  496. if (pkey == NULL ||
  497. ldrecno(pkey + KEY_RECNO_OFF) != crp->recno) {
  498. _amseterrcode(errcode, ENOCURR);
  499. goto ERROR;
  500. }
  501. break;
  502. case CRP_BEFOREANY:
  503. _isbtree_search(btree, crp->key);
  504. pkey = _isbtree_next(btree);
  505. if (pkey == NULL) {
  506. _amseterrcode(errcode, EENDFILE);
  507. goto ERROR;
  508. }
  509. crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  510. memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
  511. break;
  512. default:
  513. _amseterrcode(errcode, ENOCURR);
  514. goto ERROR;
  515. }
  516. if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
  517. _amseterrcode(errcode, ENOCURR);
  518. goto ERROR;
  519. }
  520. recnum2 = crp->recno;
  521. break;
  522. case RM_NEXT:
  523. /*
  524. * Validate that current position has been set.
  525. */
  526. switch (crp->flag) {
  527. case CRP_ON:
  528. case CRP_BEFORE:
  529. case CRP_BEFOREANY:
  530. break;
  531. case CRP_AFTERANY:
  532. _amseterrcode(errcode, EENDFILE);
  533. goto ERROR;
  534. default:
  535. _amseterrcode(errcode, ENOCURR);
  536. goto ERROR;
  537. }
  538. /*
  539. * Position pointer to current position.
  540. */
  541. _isbtree_search(btree, crp->key);
  542. if (crp->flag == CRP_BEFORE)
  543. pkey = _isbtree_current(btree);
  544. else
  545. /* crp->flag == CRP_ON || crp->flag == CRP_BEFOREANY */
  546. pkey = _isbtree_next(btree);
  547. if (pkey == NULL) {
  548. _amseterrcode(errcode, EENDFILE);
  549. goto ERROR;
  550. }
  551. crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  552. memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
  553. if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
  554. _amseterrcode(errcode, ENOCURR);
  555. goto ERROR;
  556. }
  557. recnum2 = crp->recno;
  558. break;
  559. case RM_PREV:
  560. /*
  561. * Validate that current position has been set.
  562. */
  563. switch (crp->flag) {
  564. case CRP_ON:
  565. case CRP_BEFORE:
  566. /*
  567. * To get to the previous record, we must decrement
  568. * the TID part for unique keys, or duplicate serial number
  569. * for non-unique keys.
  570. */
  571. memcpy((void *)keybuf1,
  572. (const void *)crp->key, pkeydesc2->k2_len);
  573. if (ALLOWS_DUPS2(pkeydesc2)) {
  574. stlong(ldlong(keybuf1 + KEY_DUPS_OFF) - 1,
  575. keybuf1 + KEY_DUPS_OFF);
  576. }
  577. else {
  578. strecno(ldrecno(keybuf1 + KEY_RECNO_OFF) - 1,
  579. keybuf1 + KEY_RECNO_OFF);
  580. }
  581. break;
  582. case CRP_AFTER:
  583. memcpy((void *)keybuf1, (const void *)crp->key, pkeydesc2->k2_len);
  584. break;
  585. case CRP_BEFOREANY:
  586. _amseterrcode(errcode, EENDFILE);
  587. goto ERROR;
  588. default:
  589. _amseterrcode(errcode, ENOCURR);
  590. goto ERROR;
  591. }
  592. /*
  593. * Position pointer to current position.
  594. */
  595. _isbtree_search(btree, keybuf1);
  596. pkey = _isbtree_current(btree);
  597. if (pkey == NULL) {
  598. _amseterrcode(errcode, EENDFILE);
  599. goto ERROR;
  600. }
  601. crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  602. memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
  603. if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
  604. _amseterrcode(errcode, ENOCURR);
  605. goto ERROR;
  606. }
  607. recnum2 = crp->recno;
  608. break;
  609. case RM_FIRST:
  610. /*
  611. * Fill key buffer with -infinity.
  612. */
  613. _iskey_fillmin(pkeydesc2, keybuf1);
  614. /*
  615. * Position pointer in the B-tree before any key entry.
  616. */
  617. _isbtree_search(btree, keybuf1);
  618. if ((pkey = _isbtree_next(btree)) == NULL) {
  619. _amseterrcode(errcode, EENDFILE);
  620. goto ERROR;
  621. }
  622. crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  623. memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
  624. if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
  625. _amseterrcode(errcode, EENDFILE);
  626. goto ERROR;
  627. }
  628. recnum2 = crp->recno;
  629. break;
  630. case RM_LAST:
  631. /*
  632. * Fill key buffer with +infinity.
  633. */
  634. _iskey_fillmax(pkeydesc2, keybuf1);
  635. /*
  636. * Position pointer in the B-tree before any key entry.
  637. */
  638. _isbtree_search(btree, keybuf1);
  639. if ((pkey = _isbtree_current(btree)) == NULL) {
  640. _amseterrcode(errcode, EENDFILE);
  641. goto ERROR;
  642. }
  643. crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  644. memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
  645. if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
  646. _amseterrcode(errcode, EENDFILE);
  647. goto ERROR;
  648. }
  649. recnum2 = crp->recno;
  650. break;
  651. default:
  652. _isfatal_error("Invalid readmode");
  653. }
  654. *recnum = recnum2;
  655. crp->flag = CRP_ON;
  656. /*
  657. * Set up isdupl to handle isstat2 value for keys that allow
  658. * duplicate values.
  659. */
  660. if (ALLOWS_DUPS2(pkeydesc2) && (pkeynext = _isbtree_next(btree)) &&
  661. memcmp(pkey + skipbytes, pkeynext + skipbytes,
  662. crp->matchkeylen) == 0) {
  663. isdupl = 1;
  664. }
  665. _isbtree_destroy(btree);
  666. }
  667. _amseterrcode(errcode, ISOK);
  668. ret = ISOK;
  669. /* Clean-up work. */
  670. _isdisk_commit(); /* This will only check
  671. * that we unfixed all fixed
  672. * buffers */
  673. _isdisk_inval();
  674. _bytearr_free(&oldcurpos);
  675. _isam_exithook();
  676. return (ret);
  677. ERROR:
  678. *reclen = 0;
  679. _isdisk_inval();
  680. _bytearr_free(&oldcurpos);
  681. /*
  682. * If error is ENOREC, set the current record position undefined.
  683. */
  684. if (errcode->iserrno == ENOREC || errcode->iserrno == EENDFILE) {
  685. ((Crp *)curpos->data)->flag = CRP_UNDEF;
  686. }
  687. if (btree != NULL)
  688. _isbtree_destroy(btree);
  689. _isam_exithook();
  690. return (ISERROR);
  691. }