isrewcurr.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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: isrewcurr.c /main/3 1995/10/23 11:44:15 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isrewcurr.c
  33. *
  34. * Description:
  35. * Rewrite current record in ISAM file.
  36. */
  37. #include "isam_impl.h"
  38. #include <sys/time.h>
  39. static int _amrewcurr(), _changekeys2();
  40. /*
  41. * err = isrewcurr(isfd, record)
  42. *
  43. * Isrewcurr() modifies the current record in ISAM file.
  44. * All indexes of the ISAM file are updated.
  45. *
  46. * Current record position is changed in relation to the new key value.
  47. * isrecnum is set to the changed record.
  48. *
  49. * If the ISAM file is for variable length records, the isreclen variable
  50. * must be set to indicate the actual length of the record, which must
  51. * be between the minimum and maximum length, as specified in isbuild().
  52. *
  53. * Returns 0 if successful, or -1 of any error.
  54. *
  55. * Errors:
  56. * EDUPL The write woul result in a duplicate on a key that
  57. * does not allow duplicates.
  58. * ELOCKED The file has been locked by another process.
  59. * ENOTOPEN isfd does not correspond to an open ISAM file, or the
  60. * ISAM file was not opened with ISINOUT mode.
  61. * ENOCURR Record with record number recnum does not exist, or it
  62. * was deleted by another process.
  63. */
  64. int
  65. isrewcurr(int isfd, char *record)
  66. {
  67. Fab *fab;
  68. int reclen;
  69. int ret;
  70. int recnum;
  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 ISINOUT.
  80. */
  81. if (fab->openmode != OM_INOUT) {
  82. _setiserrno2(ENOTOPEN, '9', '0');
  83. return (ISERROR);
  84. }
  85. /*
  86. * Determine record length. Check it against min and max record length.
  87. */
  88. reclen = (fab->varlength == TRUE) ? isreclen : fab->minreclen;
  89. if (reclen < fab->minreclen || reclen > fab->maxreclen) {
  90. _setiserrno2(EBADARG, '9', '0');
  91. return (ISERROR);
  92. }
  93. if ((ret = _amrewcurr(&fab->isfhandle, record, reclen, &fab->curpos,
  94. &recnum, &fab->errcode)) == ISOK) {
  95. isrecnum = recnum; /* Set isrecnum */
  96. }
  97. _seterr_errcode(&fab->errcode);
  98. return (ret); /* Successful write */
  99. }
  100. /*
  101. * _amrewcurr(isfhandle, record, reclen, curpos, recnum, errcode)
  102. *
  103. * _amrewcurr() rewrites the current record in ISAM file.
  104. *
  105. * Input params:
  106. * isfhandle Handle of ISAM file
  107. * record record
  108. * reclen length of the record
  109. * curpos curent record position
  110. *
  111. * Output params:
  112. * errcode error status of the operation
  113. * curpos new current position
  114. * recnum record number fo current record
  115. *
  116. */
  117. static int
  118. _amrewcurr(Bytearray *isfhandle, char *record, int reclen,
  119. Bytearray *curpos, Recno *recnum, struct errcode *errcode)
  120. {
  121. Fcb *fcb;
  122. Crp *crp;
  123. Bytearray newcurpos;
  124. int err;
  125. char oldrecord[ISMAXRECLEN];
  126. int reclen2;
  127. int (*rec_read)();
  128. int (*rec_rewrite)();
  129. _isam_entryhook();
  130. /*
  131. * Get FCB corresponding to the isfhandle handle.
  132. */
  133. if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
  134. _isam_exithook();
  135. return (ISERROR);
  136. }
  137. rec_read = (fcb->varflag?_vlrec_read:_flrec_read);
  138. rec_rewrite = (fcb->varflag?_vlrec_rewrite:_flrec_rewrite);
  139. /*
  140. * Get info from current record position structure.
  141. */
  142. crp = (Crp *) curpos->data;
  143. if (crp->flag != CRP_ON) {
  144. _amseterrcode(errcode, ENOCURR);
  145. goto ERROR;
  146. }
  147. /*
  148. * Update information in FCB from CNTL page on the disk
  149. */
  150. (void)_isfcb_cntlpg_r2(fcb);
  151. /*
  152. * We must read the record first to be able to delete keys.
  153. */
  154. if (rec_read(fcb, oldrecord, crp->recno, &reclen2) != ISOK) {
  155. _amseterrcode(errcode, ENOCURR);
  156. goto ERROR;
  157. }
  158. if (rec_rewrite(fcb, record, crp->recno, reclen) != ISOK) {
  159. _amseterrcode(errcode, ENOCURR);
  160. goto ERROR;
  161. }
  162. /*
  163. * Update keys, set new key position.
  164. */
  165. newcurpos = _bytearr_dup(curpos);
  166. if ((err = _changekeys2 (fcb, record, oldrecord, crp->recno, &newcurpos))
  167. != ISOK) {
  168. _bytearr_free(&newcurpos);
  169. _amseterrcode(errcode, err);
  170. goto ERROR;
  171. }
  172. /*
  173. * This takes care of new record position if the physical order is in use.
  174. */
  175. *recnum = crp->recno;
  176. _bytearr_free(curpos);
  177. crp = NULL; /* was aliased to freed curpos->data */
  178. *curpos = newcurpos;
  179. _amseterrcode(errcode, ISOK);
  180. _issignals_mask();
  181. _isdisk_commit();
  182. _isdisk_sync();
  183. _isdisk_inval();
  184. /*
  185. * Update control page.
  186. */
  187. (void)_isfcb_cntlpg_w2(fcb);
  188. _issignals_unmask();
  189. _isam_exithook();
  190. return (ISOK);
  191. ERROR:
  192. _isdisk_rollback();
  193. _isdisk_inval();
  194. /*
  195. * Restore FCB from CNTL page.
  196. */
  197. (void)_isfcb_cntlpg_r2(fcb);
  198. _isam_exithook();
  199. return (ISERROR);
  200. }
  201. Static int
  202. _changekeys2(Fcb *fcb, char *record, char *oldrecord,
  203. Recno recnum, Bytearray *curpos)
  204. {
  205. int nkeys = fcb->nkeys;
  206. int i;
  207. int err;
  208. Crp *crp;
  209. int keyid;
  210. Keydesc2 *keydesc2;
  211. crp = (Crp *)curpos->data;
  212. keyid = crp->keyid;
  213. for (i = 0; i < nkeys; i++) {
  214. keydesc2 = fcb->keys + i;
  215. if ((err =_change1key(fcb, keydesc2, record, oldrecord, recnum,
  216. (keydesc2->k2_keyid == keyid) ?
  217. crp->key : (char *) NULL)) != ISOK)
  218. return (err);
  219. }
  220. return (ISOK);
  221. }