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 /*%% $TOG: isamopen.c /main/5 1998/04/10 08:03:57 mgreess $ */
29 * Copyright (c) 1988 by Sun Microsystems, Inc.
35 * Description: _amopen()
42 #include "isam_impl.h"
45 * _amopen(isfname, varflag, minlen, maxlen, primkey,
46 * isfhandle, curpos, errcode)
48 * _amopen() opens an ISAM file.
51 * isfname ISAM file name
54 * isfhandle a file handle to be used in subsequent operations on the file
55 * varflag TRUE if file is for variable length records
56 * minlen, maxlen minimum and maximum record length
57 * curpos initial current record position
58 * errcode {iserrno, isstat1-4}
60 * _amopen() returns 0 if successful, or -1 to indicate an error.
65 _amopen(char *isfname, enum openmode openmode, Bool *varflag,
66 int *minlen, int *maxlen, Bytearray *isfhandle,
67 Bytearray *curpos, struct errcode *errcode)
81 isfhandle0 = _makeisfhandle(isfname);
84 * Invalidate the FCB cache entry to avoid a problem found when using
87 if ((fcb = _mngfcb_find(&isfhandle0)) != NULL) {
88 (void) _watchfd_decr(_isfcb_nfds(fcb));
90 _mngfcb_delete(&isfhandle0);
94 * Get the FCB that corresponds to the isfhandle handle.
96 if ((fcb = _openfcb(&isfhandle0, errcode)) == NULL) {
101 * Check if the FCB allows writes if INOUT or OUTPUT is needed.
103 if (fcb->rdonly==TRUE && (openmode==OM_INOUT || openmode==OM_OUTPUT)) {
104 _amseterrcode(errcode, EACCES);
109 * Fill output parameters.
111 *minlen = fcb->minreclen;
112 *maxlen = fcb->maxreclen;
113 *varflag = fcb->varflag;
114 *isfhandle = isfhandle0;
117 * Initial current record position.
119 if (FCB_NOPRIMARY_KEY(fcb)) {
120 /* Use physical order. */
121 crp = (Crp *) _ismalloc(sizeof(*crp));
122 memset ((char *) crp, 0, sizeof(*crp));
124 crp->keyid = PHYS_ORDER;
125 crp->flag = CRP_BEFOREANY;
127 curpos->length = sizeof(*crp);
128 curpos->data = (char *) crp;
132 * Use primary key order.
135 crp = (Crp *) _ismalloc((unsigned)(sizeof(*crp) + fcb->keys[0].k2_len));
136 memset((char *) crp, 0, (sizeof(*crp) + fcb->keys[0].k2_len));
138 crp->keyid = fcb->keys[0].k2_keyid;
139 crp->flag = CRP_BEFOREANY;
141 _iskey_fillmin(&fcb->keys[0], crp->key);
143 curpos->length = sizeof(*crp) + fcb->keys[0].k2_len;
144 curpos->data = (char *) crp;
147 * Set full key length as the number of bytes to match in key comparison
149 crp->matchkeylen = fcb->keys[0].k2_len - RECNOSIZE;
151 if (ALLOWS_DUPS2(&fcb->keys[0]))
152 crp->matchkeylen -= DUPIDSIZE;
159 _bytearr_free(&isfhandle0);
166 * fcb = _openfcb(isfhandle, errcode)
168 * Try to locate FCB in the FCB cache. If not found, open the FCB and
169 * insert it into the cache.
171 * Return a pointer to the FCB, or NULL in the case of any error.
174 #define FDNEEDED 3 /* Needs 3 UNIX fd to open a file*/
176 _openfcb(Bytearray *isfhandle, struct errcode *errcode)
179 Bytearray *isfhandle2;
183 if ((fcb = _mngfcb_find(isfhandle)) != NULL)
187 * Check that there are UNIX file descriptors available.
189 while (_watchfd_check() < FDNEEDED) {
191 * Find victim (LRU FCB) and close it.
193 if((isfhandle2 = _mngfcb_victim()) == NULL)
194 _isfatal_error ("_openfcb() cannot find LRU victim");
196 fcb = _mngfcb_find(isfhandle2);
197 (void) _watchfd_decr(_isfcb_nfds(fcb));
199 _mngfcb_delete(isfhandle2);
203 * Open files, create FCB block.
205 if ((fcb = _isfcb_open(_getisfname(isfhandle), errcode)) == NULL) {
210 * Check that ISAM file is not corrupted (only check magic number).
212 if (_check_isam_magic(fcb) != ISOK) {
213 _amseterrcode(errcode, EBADFILE);
219 * Read information from CNTL PAGE and store it in the FCB.
221 if (_isfcb_cntlpg_r(fcb) == ISERROR)
222 _isfatal_error("_openfcb() cannot read CNTL PAGE");
225 * Register UNIX file descriptors consumed.
227 (void) _watchfd_incr(_isfcb_nfds(fcb));
230 * Insert new entry into the FCB cache.
232 _mngfcb_insert(fcb, isfhandle);
237 * Check that all file descriptors are open. This is needed to handle
238 * user errors such as removing .ind or .var files.
240 if (fcb->varflag==TRUE && fcb->varfd == -1) {
241 char *fmt = "%s.var has been removed";
242 if (strlen(fmt) + strlen(fcb->isfname) + 1 >= BUFSIZ)
243 errmsg0 = (char*) malloc(strlen(fmt) + strlen(fcb->isfname) + 1);
247 (void)sprintf(errmsg0, fmt, fcb->isfname);
248 _isam_warning(errmsg0);
249 _amseterrcode(errcode, EBADFILE);
250 if (errmsg0 != errbuf) free(errmsg0);
254 if ((fcb->nkeys > 1 || !FCB_NOPRIMARY_KEY(fcb)) && fcb->indfd == -1) {
255 char *fmt = "%s.ind has been removed";
256 if (strlen(fmt) + strlen(fcb->isfname) + 1 >= BUFSIZ)
257 errmsg0 = (char*) malloc(strlen(fmt) + strlen(fcb->isfname) + 1);
261 (void)sprintf(errmsg0, fmt, fcb->isfname);
262 _isam_warning(errmsg0);
263 _amseterrcode(errcode, EBADFILE);
264 if (errmsg0 != errbuf) free(errmsg0);
272 * Delete FCB and remove it from FCB cache. Close UNIX fds.
274 * This is needed to recover netisamd server if users remove .ind or
277 (void) _watchfd_decr(_isfcb_nfds(fcb));
279 _mngfcb_delete(isfhandle);
286 * isfname = _getisfname(isfhandle)
288 * Get ISAM file name from ISAM file handle.
292 _getisfname(Bytearray *isfhandle)
294 return (isfhandle->data);
299 * isfhandle = _makeisfhandle(isfname)
301 * Make ISAM file handle.
305 _makeisfhandle(char *isfname)
307 return (_bytearr_new((u_short)(strlen(isfname) + 1), isfname));