iswrrec.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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: iswrrec.c /main/3 1995/10/23 11:46:30 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * iswrrec.c
  33. *
  34. * Description:
  35. * Write a record to ISAM file.
  36. */
  37. #include "isam_impl.h"
  38. #include <sys/time.h>
  39. static int _amwrrec();
  40. /*
  41. * isfd = iswrrec(isfd, recnum, record)
  42. *
  43. * Iswrrec() writes a record to ISAM file at specified recno position.
  44. * 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 would 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. * EKEXISTS Record with recnum already exists.
  62. * EBADARG recnum is negative.
  63. */
  64. int
  65. iswrrec(int isfd, long recnum, char *record)
  66. {
  67. Fab *fab;
  68. int reclen;
  69. int ret;
  70. /*
  71. * Get File Access Block.
  72. */
  73. if ((fab = _isfd_find(isfd)) == NULL) {
  74. _setiserrno2(ENOTOPEN, '9', '0');
  75. return (ISERROR);
  76. }
  77. /*
  78. * Check that the open mode was ISINOUT.
  79. */
  80. if (fab->openmode != OM_INOUT) {
  81. _setiserrno2(ENOTOPEN, '9', '0');
  82. return (ISERROR);
  83. }
  84. /*
  85. * Determine record length. Check it against min and max record length.
  86. */
  87. reclen = (fab->varlength == TRUE) ? isreclen : fab->minreclen;
  88. if (reclen < fab->minreclen || reclen > fab->maxreclen) {
  89. _setiserrno2(EBADARG, '9' ,'0');
  90. return (ISERROR);
  91. }
  92. if ((ret = _amwrrec(&fab->isfhandle, record, reclen,
  93. recnum, &fab->errcode)) == ISOK) {
  94. isrecnum = recnum; /* Set isrecnum */
  95. }
  96. _seterr_errcode(&fab->errcode);
  97. return (ret); /* Successful write */
  98. }
  99. /*
  100. * _amwrrec(isfhandle, record, reclen, recnum, errcode)
  101. *
  102. * _amwrrec() rewrites a record in ISAM file.
  103. *
  104. * Input params:
  105. * isfhandle Handle of ISAM file
  106. * record record
  107. * reclen length of the record
  108. * recnum record number of record to be deleted
  109. *
  110. * Output params:
  111. * errcode error status of the operation
  112. *
  113. */
  114. static int
  115. _amwrrec(Bytearray *isfhandle, char *record, int reclen, Recno recnum,
  116. struct errcode *errcode)
  117. {
  118. Fcb *fcb = NULL;
  119. int err;
  120. int (*rec_wrrec)();
  121. _isam_entryhook();
  122. /*
  123. * Get FCB corresponding to the isfhandle handle.
  124. */
  125. if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
  126. _isam_exithook();
  127. return (ISERROR);
  128. }
  129. rec_wrrec = (fcb->varflag?_vlrec_wrrec:_flrec_wrrec);
  130. /*
  131. * Update information in FCB from CNTL page on the disk
  132. */
  133. (void)_isfcb_cntlpg_r2(fcb);
  134. if ((err = rec_wrrec(fcb, record, recnum, reclen)) != ISOK) {
  135. _amseterrcode(errcode, err);
  136. goto ERROR;
  137. }
  138. /*
  139. * Update all keys.
  140. */
  141. if ((err = _addkeys(fcb, record, recnum)) != ISOK) {
  142. _amseterrcode(errcode, err);
  143. goto ERROR;
  144. }
  145. fcb->nrecords++;
  146. _amseterrcode(errcode, ISOK);
  147. _issignals_mask();
  148. _isdisk_commit();
  149. _isdisk_sync();
  150. _isdisk_inval();
  151. /*
  152. * Update CNTL Page from the FCB.
  153. */
  154. (void)_isfcb_cntlpg_w2(fcb);
  155. _issignals_unmask();
  156. _isam_exithook();
  157. return (ISOK);
  158. ERROR:
  159. _isdisk_rollback();
  160. _isdisk_inval();
  161. /*
  162. * Restore FCB from CNTL page.
  163. */
  164. if (fcb) (void)_isfcb_cntlpg_r2(fcb);
  165. _isam_exithook();
  166. return (ISERROR);
  167. }