isamwrite.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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: isamwrite.c /main/3 1995/10/23 11:35:14 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isamwrite.c
  33. *
  34. * Description: _amwrite()
  35. * Write record to ISAM file.
  36. *
  37. *
  38. */
  39. #include "isam_impl.h"
  40. static int _addkeys2 ();
  41. extern long *ismaxlong;
  42. /*
  43. * _amwrite(isfhandle, record, reclen, curpos,
  44. * recnum, errcode)
  45. *
  46. * _amwrite() writes a new record to ISAM file.
  47. *
  48. * Input params:
  49. * isfhandle Handle of ISAM file
  50. * record record
  51. * reclen length of the record
  52. * curpos current record position
  53. *
  54. * Output params:
  55. * curpos new current position
  56. * recnum record number
  57. * errcode error status of the operation
  58. *
  59. */
  60. int
  61. _amwrite(Bytearray *isfhandle, char *record, int reclen,
  62. Bytearray *curpos, Recno *recnum, struct errcode *errcode)
  63. {
  64. Fcb *fcb = NULL;
  65. Recno recnum2;
  66. Crp *crp;
  67. int err;
  68. int (*rec_write)();
  69. _isam_entryhook();
  70. /*
  71. * Get FCB corresponding to the isfhandle handle.
  72. */
  73. if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
  74. _isam_exithook();
  75. return (ISERROR);
  76. }
  77. rec_write = (fcb->varflag?_vlrec_write:_flrec_write);
  78. /*
  79. * Update information in FCB from CNTL page on the disk
  80. */
  81. (void)_isfcb_cntlpg_r2(fcb);
  82. if (rec_write(fcb, record, &recnum2, reclen) == ISERROR) {
  83. _isfatal_error("_amwrite() cannot write record");
  84. }
  85. /*
  86. * Update all keys.
  87. */
  88. if ((err = _addkeys2(fcb, record, recnum2, curpos)) != ISOK) {
  89. _amseterrcode(errcode, err);
  90. goto ERROR;
  91. }
  92. fcb->nrecords++;
  93. *recnum = recnum2;
  94. _amseterrcode(errcode, ISOK);
  95. _issignals_mask();
  96. _isdisk_commit();
  97. _isdisk_sync();
  98. _isdisk_inval();
  99. /*
  100. * Return new current record position.
  101. */
  102. crp = (Crp *) curpos->data;
  103. crp->flag = CRP_ON;
  104. crp->recno = recnum2;
  105. /*
  106. * Update CNTL Page from the FCB.
  107. */
  108. (void)_isfcb_cntlpg_w2(fcb);
  109. _issignals_unmask();
  110. _isam_exithook();
  111. return (ISOK);
  112. ERROR:
  113. _isdisk_rollback();
  114. _isdisk_inval();
  115. /*
  116. * If error is not EDUPL position undefined.
  117. */
  118. if (errcode->iserrno != EDUPL) {
  119. ((Crp *)curpos->data)->flag = CRP_UNDEF;
  120. }
  121. /*
  122. * Restore FCB from CNTL page.
  123. */
  124. if (fcb) (void)_isfcb_cntlpg_r2(fcb);
  125. _isam_exithook();
  126. return (ISERROR);
  127. }
  128. /*
  129. * _addkeys()
  130. *
  131. * Insert key entry to all indexes.
  132. *
  133. * Returns ISOK, or EDUPS.
  134. */
  135. int _addkeys (Fcb *fcb, char *record, Recno recnum)
  136. {
  137. int nkeys = fcb->nkeys;
  138. int i;
  139. int err;
  140. for (i = 0; i < nkeys; i++) {
  141. if ((err = _add1key(fcb, fcb->keys + i, record, recnum,
  142. (char*)NULL)) != ISOK)
  143. return (err);
  144. }
  145. return (ISOK);
  146. }
  147. Static int
  148. _addkeys2 (Fcb *fcb, char *record, Recno recnum, Bytearray *curpos)
  149. {
  150. int nkeys = fcb->nkeys;
  151. int i;
  152. int err;
  153. Crp *crp;
  154. int keyid;
  155. Keydesc2 *keydesc2;
  156. crp = (Crp *)curpos->data;
  157. keyid = crp->keyid;
  158. for (i = 0; i < nkeys; i++) {
  159. keydesc2 = fcb->keys + i;
  160. if ((err =_add1key(fcb, keydesc2, record, recnum,
  161. (keydesc2->k2_keyid == keyid) ?
  162. crp->key : (char *) NULL)) != ISOK)
  163. return (err);
  164. }
  165. return (ISOK);
  166. }