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