isstart.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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: isstart.c /main/3 1995/10/23 11:45:08 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isstart.c
  33. *
  34. * Description:
  35. * Select index and set record position.
  36. */
  37. #include "isam_impl.h"
  38. #include <sys/time.h>
  39. #include <stdlib.h>
  40. static int _amstart();
  41. /*
  42. * err = isstart(isfd, keydesc, length, record, mode)
  43. *
  44. * Isstart() selects index for subsequent read operations and set new
  45. * current record position.
  46. *
  47. * isrecnum is set to indicate the start record.
  48. *
  49. *
  50. * Returns 0 if successful, or -1 of any error.
  51. *
  52. * Errors:
  53. * ENOTOPEN isfd does not correspond to an open ISAM file, or the
  54. * ISAM file was opened with ISOUTPUT mode.
  55. * EBADARG Bad mode parameter.
  56. * EBADARG keylen is out of range.
  57. * ENOREC Specified record cannot be found (random access read).
  58. * EBADKEY Error in the key descriptor.
  59. */
  60. int
  61. isstart(int isfd, struct keydesc *keydesc, int length, char *record,
  62. int mode)
  63. {
  64. Fab *fab;
  65. int reclen;
  66. Recno recnum;
  67. int ret;
  68. enum readmode readmode;
  69. char dummy_record [1]; /* used for ISFIRST and ISLAST */
  70. char *precord;
  71. /*
  72. * Get File Access Block.
  73. */
  74. if ((fab = _isfd_find(isfd)) == NULL) {
  75. _setiserrno2(ENOTOPEN, '9', '0');
  76. return (ISERROR);
  77. }
  78. /*
  79. * Check that the open mode was ISINPUT, or ISINOUT.
  80. */
  81. if (fab->openmode != OM_INPUT && fab->openmode != OM_INOUT) {
  82. _setiserrno2(ENOTOPEN, '9', '0');
  83. return (ISERROR);
  84. }
  85. /*
  86. * Extract read mode.
  87. */
  88. if ((readmode = _getreadmode(mode)) == RM_BADMODE) {
  89. _setiserrno2(EBADARG, '9', '0');
  90. return (ISERROR);
  91. }
  92. /*
  93. * Some arguments are used only when a particular mode is specified.
  94. */
  95. if (readmode == RM_FIRST || readmode == RM_LAST || USE_PHYS_ORDER(keydesc)) {
  96. precord = dummy_record;
  97. reclen = 0;
  98. }
  99. else {
  100. precord = record;
  101. reclen = fab->minreclen;
  102. }
  103. reclen = fab->minreclen;
  104. recnum = isrecnum;
  105. if ((ret = _amstart(&fab->isfhandle, precord, reclen,
  106. readmode, keydesc, length, &fab->curpos,
  107. &recnum, &fab->errcode)) == ISOK) {
  108. isrecnum = recnum; /* Set isrecnum */
  109. }
  110. _seterr_errcode(&fab->errcode);
  111. return (ret); /* Successful start */
  112. }
  113. /*
  114. * _amstart(isfhandle, record, reclen, readmode,
  115. * keydesc, keylen, curpos, recnum, errcode)
  116. *
  117. * _amstart() reads a record from ISAM file
  118. *
  119. * Input params:
  120. * isfhandle Handle of ISAM file
  121. * readmode Specifies access mode (random or sequential)
  122. * curpos old record position
  123. * recnum copy of isrecnum
  124. * keydesc key descriptor
  125. * keylen # of bytes of key to match
  126. * record extract key from this record buffer
  127. *
  128. * Output params:
  129. * curpos new current position
  130. * recnum record number
  131. * errcode error status of the operation
  132. * reclen actual length of the record
  133. *
  134. * Note:
  135. * Successfull isstart() returns the new curpos and frees the old curpos.
  136. */
  137. /* ARGSUSED */
  138. static int
  139. _amstart(Bytearray *isfhandle, char *record, int *reclen,
  140. enum readmode readmode, struct keydesc *keydesc, int keylen,
  141. Bytearray *curpos, Recno *recnum, struct errcode *errcode)
  142. {
  143. Fcb *fcb;
  144. Recno recnum2 = 0;
  145. int err;
  146. Crp *newcrp = NULL;
  147. char recbuf [ISMAXRECLEN];
  148. Keydesc2 keydesc2;
  149. Keydesc2 *pkeydesc2;
  150. int newcrpsize = 0;
  151. char keybuf1 [MAXKEYSIZE], keybuf2 [MAXKEYSIZE];
  152. int matchkeylen;
  153. int skipbytes;
  154. char *pkey;
  155. Btree *btree = NULL;
  156. int reclen2;
  157. int (*rec_read)();
  158. _isam_entryhook();
  159. /*
  160. * Get FCB corresponding to the isfhandle handle.
  161. */
  162. if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
  163. _isam_exithook();
  164. return (ISERROR);
  165. }
  166. rec_read = (fcb->varflag?_vlrec_read:_flrec_read);
  167. /*
  168. * Update information in FCB from CNTL page on the disk
  169. */
  170. (void)_isfcb_cntlpg_r2(fcb);
  171. if (USE_PHYS_ORDER(keydesc)) {
  172. /*
  173. * Physical order in use.
  174. */
  175. /*
  176. * Allocate new current position structure.
  177. */
  178. newcrpsize = sizeof(*newcrp);
  179. newcrp = (Crp *) _ismalloc(sizeof(*newcrp));
  180. memset ((char *)newcrp, 0, sizeof(*newcrp));
  181. newcrp->keyid = PHYS_ORDER;
  182. switch (readmode) {
  183. case RM_EQUAL:
  184. recnum2 = *recnum; /* passed from isrecnum */
  185. if ((err = rec_read(fcb, recbuf, recnum2, &reclen2)) != ISOK) {
  186. _amseterrcode(errcode, ENOREC);
  187. goto ERROR;
  188. }
  189. newcrp->flag = CRP_BEFORE;
  190. newcrp->recno = recnum2;
  191. break;
  192. case RM_GREAT:
  193. recnum2 = *recnum + 1;
  194. if (recnum2 < 1) recnum2 = 1;
  195. /*
  196. * Skip deleted records.
  197. */
  198. while ((err = rec_read(fcb, recbuf, recnum2, &reclen2)) != ISOK &&
  199. err == ENOREC)
  200. recnum2++;
  201. if (err != ISOK) {
  202. _amseterrcode(errcode, ENOREC);
  203. goto ERROR;
  204. }
  205. newcrp->flag = CRP_BEFORE;
  206. newcrp->recno = recnum2;
  207. break;
  208. case RM_GTEQ:
  209. recnum2 = *recnum;
  210. if (recnum2 < 1) recnum2 = 1;
  211. /*
  212. * Skip deleted records.
  213. */
  214. while ((err = rec_read(fcb, recbuf, recnum2, &reclen2)) != ISOK &&
  215. err == ENOREC)
  216. recnum2++;
  217. if (err != ISOK) {
  218. _amseterrcode(errcode, ENOREC);
  219. goto ERROR;
  220. }
  221. newcrp->flag = CRP_BEFORE;
  222. newcrp->recno = recnum2;
  223. break;
  224. case RM_LESS:
  225. recnum2 = *recnum - 1;
  226. if (recnum2 > fcb->lastrecno) recnum2 = fcb->lastrecno;
  227. /*
  228. * Skip deleted records.
  229. */
  230. while ((err = rec_read(fcb, recbuf, recnum2, &reclen2)) != ISOK &&
  231. err == ENOREC)
  232. recnum2--;
  233. if (err != ISOK) {
  234. _amseterrcode(errcode, ENOREC);
  235. goto ERROR;
  236. }
  237. newcrp->flag = CRP_AFTER;
  238. newcrp->recno = recnum2;
  239. break;
  240. case RM_LTEQ:
  241. recnum2 = *recnum;
  242. if (recnum2 > fcb->lastrecno) recnum2 = fcb->lastrecno;
  243. /*
  244. * Skip deleted records.
  245. */
  246. while ((err = rec_read(fcb, recbuf, recnum2, &reclen2)) != ISOK &&
  247. err == ENOREC)
  248. recnum2--;
  249. if (err != ISOK) {
  250. _amseterrcode(errcode, ENOREC);
  251. goto ERROR;
  252. }
  253. newcrp->flag = CRP_AFTER;
  254. newcrp->recno = recnum2;
  255. break;
  256. case RM_FIRST:
  257. recnum2 = 1;
  258. /*
  259. * Skip deleted records.
  260. */
  261. while ((err = rec_read(fcb, recbuf, recnum2, &reclen2)) != ISOK &&
  262. err == ENOREC)
  263. recnum2++;
  264. if (err == ISOK) {
  265. newcrp->flag = CRP_BEFORE;
  266. newcrp->recno = recnum2;
  267. }
  268. else {
  269. newcrp->flag = CRP_AFTERANY;
  270. }
  271. break;
  272. case RM_LAST:
  273. recnum2 = fcb->lastrecno;
  274. /*
  275. * Skip deleted records.
  276. */
  277. while ((err = rec_read(fcb, recbuf, recnum2, &reclen2)) != ISOK &&
  278. err == ENOREC)
  279. recnum2--;
  280. if (err == ISOK) {
  281. newcrp->flag = CRP_AFTER;
  282. newcrp->recno = recnum2;
  283. }
  284. else {
  285. newcrp->flag = CRP_BEFOREANY;
  286. }
  287. break;
  288. default:
  289. _isfatal_error("Invalid readmode");
  290. }
  291. *recnum = recnum2;
  292. /*
  293. * Build new curpos, deallocate old curpos.
  294. */
  295. _bytearr_free(curpos);
  296. *curpos = _bytearr_new(sizeof(*newcrp), (char *)newcrp);
  297. } /* physical order */
  298. else {
  299. /*
  300. * Use order defined by some key.
  301. */
  302. /*
  303. * Check key descriptor for validity.
  304. */
  305. if (_validate_keydesc(keydesc, fcb->minreclen) != ISOK) {
  306. _amseterrcode(errcode, EBADKEY);
  307. goto ERROR;
  308. }
  309. /*
  310. * Convert key descriptor to internal form.
  311. */
  312. _iskey_xtoi (&keydesc2, keydesc);
  313. /* Find key decriptor in the FCB. */
  314. if ((pkeydesc2 = _isfcb_findkey(fcb ,&keydesc2)) == NULL) {
  315. _amseterrcode(errcode, EBADKEY);
  316. goto ERROR;
  317. }
  318. /*
  319. * skipkeybytes is set to the number of bytes in the beginning
  320. * of the key:
  321. * RECNOSIZE for ISNODUPS keys to skip recno part
  322. * RECNOSIZE + DUPIDSIZE to skip recno and duplicate serial number
  323. */
  324. skipbytes = RECNOSIZE;
  325. if (ALLOWS_DUPS2(pkeydesc2))
  326. skipbytes += DUPIDSIZE;
  327. /*
  328. * Validate keylen.
  329. */
  330. if (keylen < 0 || keylen > pkeydesc2->k2_len - skipbytes) {
  331. _amseterrcode(errcode, EBADARG);
  332. goto ERROR;
  333. }
  334. /*
  335. * Special case if keylen == 0: use the entire key.
  336. */
  337. if (keylen == 0)
  338. matchkeylen = pkeydesc2->k2_len - skipbytes;
  339. else
  340. matchkeylen = keylen;
  341. /*
  342. * Allocate new current record position.
  343. */
  344. newcrpsize = sizeof(Crp) + pkeydesc2->k2_len;
  345. newcrp = (Crp *) _ismalloc((unsigned)newcrpsize);
  346. memset((char *)newcrp, 0, newcrpsize);
  347. newcrp->keyid = pkeydesc2->k2_keyid; /* Key identifier in FCB */
  348. newcrp->matchkeylen = matchkeylen; /* number of bytes to match */
  349. /*
  350. * Create B tree object.
  351. */
  352. btree = _isbtree_create(fcb, pkeydesc2);
  353. switch (readmode) {
  354. case RM_EQUAL:
  355. case RM_GTEQ:
  356. /*
  357. * Make sure that you will read the first duplicate.
  358. */
  359. _iskey_fillmin(pkeydesc2, keybuf1);
  360. /*
  361. * Extract key fields from record.
  362. */
  363. _iskey_extract(pkeydesc2, record, keybuf2);
  364. memcpy( keybuf1 + skipbytes,keybuf2 + skipbytes, matchkeylen);
  365. /*
  366. * Position pointer in the B-tree in before the searched value.
  367. */
  368. _isbtree_search(btree, keybuf1);
  369. if ((pkey = _isbtree_next(btree)) == NULL) {
  370. _amseterrcode(errcode, ENOREC);
  371. goto ERROR;
  372. }
  373. if (readmode == RM_EQUAL &&
  374. memcmp(keybuf1 + skipbytes, pkey + skipbytes,
  375. matchkeylen) != 0) {
  376. _amseterrcode(errcode, ENOREC);
  377. goto ERROR;
  378. }
  379. newcrp->flag = CRP_BEFORE;
  380. newcrp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  381. memcpy( newcrp->key,pkey, pkeydesc2->k2_len);
  382. break;
  383. case RM_GREAT:
  384. /*
  385. * Make sure that you will read past all matching records.
  386. */
  387. _iskey_fillmax(pkeydesc2, keybuf1);
  388. /*
  389. * Extract key fields from record.
  390. */
  391. _iskey_extract(pkeydesc2, record, keybuf2);
  392. memcpy( keybuf1 + skipbytes,keybuf2 + skipbytes, matchkeylen);
  393. /*
  394. * Position pointer in the B-tree after the searched value.
  395. */
  396. _isbtree_search(btree, keybuf1);
  397. if ((pkey = _isbtree_next(btree)) == NULL) {
  398. _amseterrcode(errcode, ENOREC);
  399. goto ERROR;
  400. }
  401. newcrp->flag = CRP_BEFORE;
  402. newcrp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  403. memcpy( newcrp->key,pkey, pkeydesc2->k2_len);
  404. break;
  405. case RM_LESS:
  406. /*
  407. * Make sure that you will read before all matching records.
  408. */
  409. _iskey_fillmin(pkeydesc2, keybuf1);
  410. /*
  411. * Extract key fields from record.
  412. */
  413. _iskey_extract(pkeydesc2, record, keybuf2);
  414. memcpy( keybuf1 + skipbytes,keybuf2 + skipbytes, matchkeylen);
  415. /*
  416. * Position pointer in the B-tree after the searched value.
  417. */
  418. _isbtree_search(btree, keybuf1);
  419. if ((pkey = _isbtree_current(btree)) == NULL) {
  420. _amseterrcode(errcode, ENOREC);
  421. goto ERROR;
  422. }
  423. newcrp->flag = CRP_AFTER;
  424. newcrp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  425. memcpy( newcrp->key,pkey, pkeydesc2->k2_len);
  426. break;
  427. case RM_LTEQ:
  428. /*
  429. * Make sure that you will the last duplicate.
  430. */
  431. _iskey_fillmax(pkeydesc2, keybuf1);
  432. /*
  433. * Extract key fields from record.
  434. */
  435. _iskey_extract(pkeydesc2, record, keybuf2);
  436. memcpy( keybuf1 + skipbytes,keybuf2 + skipbytes, matchkeylen);
  437. /*
  438. * Position pointer in the B-tree in before the searched value.
  439. */
  440. _isbtree_search(btree, keybuf1);
  441. if ((pkey = _isbtree_current(btree)) == NULL) {
  442. _amseterrcode(errcode, ENOREC);
  443. goto ERROR;
  444. }
  445. newcrp->flag = CRP_AFTER;
  446. newcrp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  447. memcpy( newcrp->key,pkey, pkeydesc2->k2_len);
  448. break;
  449. case RM_FIRST:
  450. /*
  451. * Fill key buffer with -infinity.
  452. */
  453. _iskey_fillmin(pkeydesc2, keybuf1);
  454. /*
  455. * Position pointer in the B-tree before any key entry.
  456. */
  457. _isbtree_search(btree, keybuf1);
  458. if ((pkey = _isbtree_next(btree)) == NULL) {
  459. newcrp->flag = CRP_AFTERANY;
  460. }
  461. else {
  462. newcrp->flag = CRP_BEFORE;
  463. newcrp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  464. memcpy( newcrp->key,pkey, pkeydesc2->k2_len);
  465. }
  466. break;
  467. case RM_LAST:
  468. /*
  469. * Fill key buffer with +infinity.
  470. */
  471. _iskey_fillmax(pkeydesc2, keybuf1);
  472. /*
  473. * Position pointer in the B-tree after all entries.
  474. */
  475. _isbtree_search(btree, keybuf1);
  476. if ((pkey = _isbtree_current(btree)) == NULL) {
  477. newcrp->flag = CRP_BEFOREANY;
  478. }
  479. else {
  480. newcrp->flag = CRP_AFTER;
  481. newcrp->recno = ldrecno(pkey + KEY_RECNO_OFF);
  482. memcpy( newcrp->key,pkey, pkeydesc2->k2_len);
  483. }
  484. break;
  485. default:
  486. _isfatal_error("Invalid readmode");
  487. }
  488. *recnum = newcrp->recno;
  489. /*
  490. * Build new curpos, deallocate old curpos data.
  491. */
  492. _bytearr_free(curpos);
  493. *curpos = _bytearr_new((u_short)newcrpsize, (char *)newcrp);
  494. _isbtree_destroy(btree);
  495. }
  496. _amseterrcode(errcode, ISOK);
  497. /* Clean-up work. */
  498. free(newcrp);
  499. _isdisk_commit(); /* This will only check
  500. * that we unfixed all fixed
  501. * buffers */
  502. _isdisk_inval();
  503. _isam_exithook();
  504. return (ISOK);
  505. ERROR:
  506. if (btree != NULL)
  507. _isbtree_destroy(btree);
  508. /*
  509. * If error is EBADKEY, make the current position undefined.
  510. */
  511. if (errcode->iserrno == EBADKEY) {
  512. ((Crp *)curpos->data)->flag = CRP_UNDEF;
  513. }
  514. /*
  515. * If error is ENOREC, switch to the new key, but set the current
  516. * record position undefined.
  517. */
  518. if (errcode->iserrno == ENOREC && newcrp != NULL) {
  519. _bytearr_free(curpos);
  520. *curpos = _bytearr_new((u_short)newcrpsize, (char *)newcrp);
  521. ((Crp *)curpos->data)->flag = CRP_UNDEF;
  522. }
  523. if (newcrp != NULL)
  524. free((char *)newcrp);
  525. _isdisk_inval();
  526. _isam_exithook();
  527. return (ISERROR);
  528. }