tt/mini_isam: remove all ancient sccsid blocks
[oweals/cde.git] / cde / lib / tt / mini_isam / isrewcurr.c
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 /*
33  * isrewcurr.c
34  *
35  * Description:
36  *      Rewrite current record in ISAM file. 
37  */
38
39
40 #include "isam_impl.h"
41 #include <sys/time.h>
42
43 static int _amrewcurr(), _changekeys2();
44
45 /*
46  * err = isrewcurr(isfd, record)
47  *
48  * Isrewcurr() modifies the current record in ISAM file. 
49  * All indexes of the ISAM file are updated.
50  *
51  * Current record position is changed in relation to the new key value.
52  * isrecnum is set to the changed record.
53  *
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().
57  *
58  * Returns 0 if successful, or -1 of any error.
59  *
60  * Errors:
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.
68  */
69
70 int 
71 isrewcurr(int isfd, char *record)
72 {
73     Fab *fab;
74     int                 reclen;
75     int                 ret;
76     int                 recnum;
77
78     /*
79      * Get File Access Block.
80      */
81     if ((fab = _isfd_find(isfd)) == NULL) {
82         _setiserrno2(ENOTOPEN, '9', '0');
83         return (ISERROR);
84     }
85
86     /*
87      * Check that the open mode was  ISINOUT.
88      */
89     if (fab->openmode != OM_INOUT) {
90         _setiserrno2(ENOTOPEN, '9', '0');
91         return (ISERROR);
92     }
93
94     /*
95      * Determine record length. Check it against min and max record length.
96      */
97     reclen = (fab->varlength == TRUE) ? isreclen : fab->minreclen;
98     if (reclen < fab->minreclen || reclen > fab->maxreclen) {
99         _setiserrno2(EBADARG, '9', '0');
100         return (ISERROR);
101     }
102
103     if ((ret = _amrewcurr(&fab->isfhandle, record, reclen, &fab->curpos,
104                           &recnum, &fab->errcode)) == ISOK) {
105         isrecnum = recnum;                   /* Set isrecnum */
106     }
107
108     _seterr_errcode(&fab->errcode);
109
110     return (ret);                            /* Successful write */
111 }
112
113 /*
114  * _amrewcurr(isfhandle, record, reclen, curpos, recnum, errcode)
115  *
116  * _amrewcurr() rewrites the current record in ISAM file.
117  *
118  * Input params:
119  *      isfhandle       Handle of ISAM file
120  *      record          record
121  *      reclen          length of the record
122  *      curpos          curent record position
123  *
124  * Output params:
125  *      errcode         error status of the operation
126  *      curpos          new current position
127  *      recnum          record number fo current record
128  *
129  */
130
131 static int
132 _amrewcurr(Bytearray *isfhandle, char *record, int reclen,
133            Bytearray *curpos, Recno *recnum, struct errcode *errcode)
134 {
135     Fcb                 *fcb;
136     Crp                 *crp;
137     Bytearray           newcurpos;
138     int                 err;
139     char                oldrecord[ISMAXRECLEN];
140     int                 reclen2;
141     int                 (*rec_read)();
142     int                 (*rec_rewrite)();
143
144     _isam_entryhook();
145
146     /*
147      * Get FCB corresponding to the isfhandle handle.
148      */
149     if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
150         _isam_exithook();
151         return (ISERROR);
152     }
153
154     rec_read = (fcb->varflag?_vlrec_read:_flrec_read);
155     rec_rewrite = (fcb->varflag?_vlrec_rewrite:_flrec_rewrite);
156
157     /*
158      * Get info from current record position structure.
159      */
160     crp = (Crp *) curpos->data;
161
162
163     if (crp->flag != CRP_ON) {
164         _amseterrcode(errcode, ENOCURR);
165         goto ERROR;
166     }
167
168     /*
169      * Update information in FCB from CNTL page on the disk
170      */
171     (void)_isfcb_cntlpg_r2(fcb);
172
173     /*
174      * We must read the record first to be able to delete keys.
175      */
176     if (rec_read(fcb, oldrecord, crp->recno, &reclen2) != ISOK) {
177         _amseterrcode(errcode, ENOCURR);
178         goto ERROR;
179     }
180
181     if (rec_rewrite(fcb, record, crp->recno, reclen) != ISOK) {
182         _amseterrcode(errcode, ENOCURR);
183         goto ERROR;
184     }
185              
186     /*
187      * Update keys, set new key position.
188      */
189     newcurpos = _bytearr_dup(curpos);
190     if ((err = _changekeys2 (fcb, record, oldrecord, crp->recno, &newcurpos)) 
191         != ISOK) {
192         _bytearr_free(&newcurpos);
193         _amseterrcode(errcode, err);    
194         goto ERROR;
195     }
196
197     /* 
198      * This takes care of new record position if the physical order is in use.
199      */
200     *recnum = crp->recno;       
201     
202     _bytearr_free(curpos);
203     crp = NULL;                 /* was aliased to freed curpos->data */
204     *curpos = newcurpos;
205
206     _amseterrcode(errcode, ISOK);
207     _issignals_mask();
208     _isdisk_commit();
209     _isdisk_sync();
210     _isdisk_inval();
211
212     /*
213      * Update control page.
214      */
215     (void)_isfcb_cntlpg_w2(fcb);
216     _issignals_unmask();
217
218     _isam_exithook();
219     return (ISOK);
220
221  ERROR:
222     _isdisk_rollback();
223     _isdisk_inval();
224
225     /*
226      * Restore FCB from CNTL page.
227      */
228     (void)_isfcb_cntlpg_r2(fcb);
229
230     _isam_exithook();
231     return (ISERROR);
232 }
233
234 Static int
235 _changekeys2(Fcb *fcb, char *record, char *oldrecord,
236              Recno recnum, Bytearray *curpos)
237 {
238     int                 nkeys = fcb->nkeys;
239     int        i;
240     int                 err;
241     Crp                 *crp;
242     int                 keyid;
243     Keydesc2            *keydesc2;
244
245     crp = (Crp *)curpos->data;
246     keyid = crp->keyid;
247
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)
253             return (err);
254     }
255
256     return (ISOK);
257 }