2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
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 $ */
29 * Copyright (c) 1988 by Sun Microsystems, Inc.
36 * Rewrite current record in ISAM file.
40 #include "isam_impl.h"
43 static int _amrewcurr(), _changekeys2();
46 * err = isrewcurr(isfd, record)
48 * Isrewcurr() modifies the current record in ISAM file.
49 * All indexes of the ISAM file are updated.
51 * Current record position is changed in relation to the new key value.
52 * isrecnum is set to the changed record.
54 * If the ISAM file is for variable length records, the isreclen variable
55 * must be set to indicate the actual length of the record, which must
56 * be between the minimum and maximum length, as specified in isbuild().
58 * Returns 0 if successful, or -1 of any error.
61 * EDUPL The write woul result in a duplicate on a key that
62 * does not allow duplicates.
63 * ELOCKED The file has been locked by another process.
64 * ENOTOPEN isfd does not correspond to an open ISAM file, or the
65 * ISAM file was not opened with ISINOUT mode.
66 * ENOCURR Record with record number recnum does not exist, or it
67 * was deleted by another process.
71 isrewcurr(int isfd, char *record)
79 * Get File Access Block.
81 if ((fab = _isfd_find(isfd)) == NULL) {
82 _setiserrno2(ENOTOPEN, '9', '0');
87 * Check that the open mode was ISINOUT.
89 if (fab->openmode != OM_INOUT) {
90 _setiserrno2(ENOTOPEN, '9', '0');
95 * Determine record length. Check it against min and max record length.
97 reclen = (fab->varlength == TRUE) ? isreclen : fab->minreclen;
98 if (reclen < fab->minreclen || reclen > fab->maxreclen) {
99 _setiserrno2(EBADARG, '9', '0');
103 if ((ret = _amrewcurr(&fab->isfhandle, record, reclen, &fab->curpos,
104 &recnum, &fab->errcode)) == ISOK) {
105 isrecnum = recnum; /* Set isrecnum */
108 _seterr_errcode(&fab->errcode);
110 return (ret); /* Successful write */
114 * _amrewcurr(isfhandle, record, reclen, curpos, recnum, errcode)
116 * _amrewcurr() rewrites the current record in ISAM file.
119 * isfhandle Handle of ISAM file
121 * reclen length of the record
122 * curpos curent record position
125 * errcode error status of the operation
126 * curpos new current position
127 * recnum record number fo current record
132 _amrewcurr(Bytearray *isfhandle, char *record, int reclen,
133 Bytearray *curpos, Recno *recnum, struct errcode *errcode)
139 char oldrecord[ISMAXRECLEN];
142 int (*rec_rewrite)();
147 * Get FCB corresponding to the isfhandle handle.
149 if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
154 rec_read = (fcb->varflag?_vlrec_read:_flrec_read);
155 rec_rewrite = (fcb->varflag?_vlrec_rewrite:_flrec_rewrite);
158 * Get info from current record position structure.
160 crp = (Crp *) curpos->data;
163 if (crp->flag != CRP_ON) {
164 _amseterrcode(errcode, ENOCURR);
169 * Update information in FCB from CNTL page on the disk
171 (void)_isfcb_cntlpg_r2(fcb);
174 * We must read the record first to be able to delete keys.
176 if (rec_read(fcb, oldrecord, crp->recno, &reclen2) != ISOK) {
177 _amseterrcode(errcode, ENOCURR);
181 if (rec_rewrite(fcb, record, crp->recno, reclen) != ISOK) {
182 _amseterrcode(errcode, ENOCURR);
187 * Update keys, set new key position.
189 newcurpos = _bytearr_dup(curpos);
190 if ((err = _changekeys2 (fcb, record, oldrecord, crp->recno, &newcurpos))
192 _bytearr_free(&newcurpos);
193 _amseterrcode(errcode, err);
198 * This takes care of new record position if the physical order is in use.
200 *recnum = crp->recno;
202 _bytearr_free(curpos);
203 crp = NULL; /* was aliased to freed curpos->data */
206 _amseterrcode(errcode, ISOK);
213 * Update control page.
215 (void)_isfcb_cntlpg_w2(fcb);
226 * Restore FCB from CNTL page.
228 (void)_isfcb_cntlpg_r2(fcb);
235 _changekeys2(Fcb *fcb, char *record, char *oldrecord,
236 Recno recnum, Bytearray *curpos)
238 int nkeys = fcb->nkeys;
245 crp = (Crp *)curpos->data;
248 for (i = 0; i < nkeys; i++) {
249 keydesc2 = fcb->keys + i;
250 if ((err =_change1key(fcb, keydesc2, record, oldrecord, recnum,
251 (keydesc2->k2_keyid == keyid) ?
252 crp->key : (char *) NULL)) != ISOK)