2
0

isrewrec.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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: isrewrec.c /main/3 1995/10/23 11:44:28 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isrewrec.c
  33. *
  34. * Description:
  35. * Rewrite a record in ISAM file.
  36. */
  37. #include "isam_impl.h"
  38. #include <sys/time.h>
  39. static int _amrewrec(), _changekeys();
  40. /*
  41. * err = isrewrec(isfd, recnum, record)
  42. *
  43. * Isrewrec() modifies a record in ISAM file. The record is identified
  44. * by its record number. All indexes of the ISAM file are updated.
  45. *
  46. * Current record position is not changed.
  47. * isrecnum is set to recnum.
  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. * ENOREC Record with record number recnum does not exist.
  62. */
  63. int
  64. isrewrec(int isfd, long recnum, char *record)
  65. {
  66. Fab *fab;
  67. int reclen;
  68. int ret;
  69. /*
  70. * Get File Access Block.
  71. */
  72. if ((fab = _isfd_find(isfd)) == NULL) {
  73. _setiserrno2(ENOTOPEN, '9', '0');
  74. return (ISERROR);
  75. }
  76. /*
  77. * Check that the open mode was ISINOUT.
  78. */
  79. if (fab->openmode != OM_INOUT) {
  80. _setiserrno2(ENOTOPEN, '9', '0');
  81. return (ISERROR);
  82. }
  83. /*
  84. * Determine record length. Check it against min and max record length.
  85. */
  86. reclen = (fab->varlength == TRUE) ? isreclen : fab->minreclen;
  87. if (reclen < fab->minreclen || reclen > fab->maxreclen) {
  88. _setiserrno2(EBADARG, '9', '0');
  89. return (ISERROR);
  90. }
  91. if ((ret = _amrewrec(&fab->isfhandle, record,
  92. reclen, recnum, &fab->errcode)) == ISOK) {
  93. isrecnum = recnum; /* Set isrecnum */
  94. }
  95. _seterr_errcode(&fab->errcode);
  96. return (ret); /* Successful write */
  97. }
  98. /*
  99. * _amrewrec(isfhandle, record, reclen, recnum, errcode)
  100. *
  101. * _amrewrec() rewrites a record in ISAM file.
  102. *
  103. * Input params:
  104. * isfhandle Handle of ISAM file
  105. * record record
  106. * reclen length of the record
  107. * recnum record number of record to be deleted
  108. *
  109. * Output params:
  110. * errcode error status of the operation
  111. *
  112. */
  113. static int
  114. _amrewrec(Bytearray *isfhandle, char *record, int reclen, Recno recnum,
  115. struct errcode *errcode)
  116. {
  117. Fcb *fcb;
  118. char oldrecbuf[ISMAXRECLEN];
  119. int err;
  120. int reclen2;
  121. int (*rec_read)();
  122. int (*rec_rewrite)();
  123. _isam_entryhook();
  124. /*
  125. * Get FCB corresponding to the isfhandle handle.
  126. */
  127. if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
  128. _isam_exithook();
  129. return (ISERROR);
  130. }
  131. rec_read = (fcb->varflag?_vlrec_read:_flrec_read);
  132. rec_rewrite = (fcb->varflag?_vlrec_rewrite:_flrec_rewrite);
  133. /*
  134. * Update information in FCB from CNTL page on the disk
  135. */
  136. (void)_isfcb_cntlpg_r2(fcb);
  137. /*
  138. * We must read the record first to be able to delete keys.
  139. */
  140. if (rec_read(fcb, oldrecbuf, recnum, &reclen2) != ISOK) {
  141. _amseterrcode(errcode, ENOREC);
  142. goto ERROR;
  143. }
  144. if (rec_rewrite(fcb, record, recnum, reclen) != ISOK) {
  145. _amseterrcode(errcode, ENOREC);
  146. goto ERROR;
  147. }
  148. /*
  149. * Update all keys.
  150. */
  151. if ((err = _changekeys(fcb, record, oldrecbuf, recnum)) != ISOK) {
  152. _amseterrcode(errcode, err);
  153. goto ERROR;
  154. }
  155. _amseterrcode(errcode, ISOK);
  156. _issignals_mask();
  157. _isdisk_commit();
  158. _isdisk_sync();
  159. _isdisk_inval();
  160. /*
  161. * Update control page.
  162. */
  163. (void)_isfcb_cntlpg_w2(fcb);
  164. _issignals_unmask();
  165. _isam_exithook();
  166. return (ISOK);
  167. ERROR:
  168. _isdisk_rollback();
  169. _isdisk_inval();
  170. /*
  171. * Restore FCB from CNTL page.
  172. */
  173. (void)_isfcb_cntlpg_r2(fcb);
  174. _isam_exithook();
  175. return (ISERROR);
  176. }
  177. static int
  178. _changekeys (Fcb *fcb, char *record, char *oldrecord, Recno recnum)
  179. {
  180. int nkeys = fcb->nkeys;
  181. int i;
  182. int err;
  183. for (i = 0; i < nkeys; i++) {
  184. if ((err =_change1key(fcb, fcb->keys + i, record, oldrecord, recnum,
  185. (char *)NULL))
  186. != ISOK)
  187. return (err);
  188. }
  189. return (ISOK);
  190. }