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: isbuild.c /main/3 1995/10/23 11:36:21 rswiston $ */
29 * Copyright (c) 1988 by Sun Microsystems, Inc.
36 * Create an ISAM file.
40 #include "isam_impl.h"
45 #include <sys/types.h>
49 _ambuild(char *isfname, enum openmode openmode, Bool varflag,
50 int minlen, int maxlen, struct keydesc *primkey,
51 int owner, int group, int umask,
52 Bytearray *isfhandle, Bytearray *curpos,
53 struct errcode *errcode);
56 * isfd = isbuild(isfname, recordlength, primkey, mode)
58 * Isbuild() determines on which machine the ISAM file is to be built,
59 * checks the permissions for creating a file by this user running on
60 * this client machine by using the access(2) UNIX call.
61 * If the file is remote, it is created by the netisamd daemon running
62 * on the machine hosting the ISAM file. The chown(2) is then used to
63 * change the ownership of the file to the client.
64 * All UNIX files created will have their permissions set to 0666, allowing
65 * thus both read and write access to anybody.
67 * Isbuild() returns an ISAM file descriptor (isfd) is the call was successful,
68 * or a value of -1 if the call failed.
71 * EBADARG Improper mode parameter
72 * EBADARG isreclen >= recordlength and ISVARLEN specified
73 * E2BIG recordlength greater than system imposed limit (8196)
74 * EBADKEY Invalid key descriptor
75 * EBADFILE ISAM file is corrupted or it is not an NetISAM file
76 * EFNAME Invalid ISAM file name
77 * ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
79 * The following error numbers are "borrowed" from UNIX.
80 * EACCES UNIX file system protection denies creation of the file
81 * EEXIST - ISAM file already exists
82 * EEXIST - A UNIX file with the same name exists
86 isbuild(char *isfname, int recordlength, struct keydesc *primkey, int mode)
90 enum openmode openmode;
91 int minreclen; /* Minimum record length */
94 /* Get file open mode part of the mode parameter. */
95 if ((openmode = _getopenmode(mode)) == OM_BADMODE) {
96 _setiserrno2(EBADARG, '9', '0');
101 * Minimum record length.
104 minreclen = ((mode & ISLENMODE) == ISVARLEN) ? isreclen : recordlength;
106 /* Check recordlength against system imposed limit. */
107 if (recordlength > ISMAXRECLEN) {
108 _setiserrno2(E2BIG, '9', '0');
112 /* Check that ssminreclen >= ISMINRECLEN. */
113 if (minreclen < ISMINRECLEN) {
114 _setiserrno2(EBADARG, '9', '0');
118 /* Check that minreclen <= recordlength */
119 if (minreclen > recordlength) {
120 _setiserrno2(EBADARG, '9', '0');
124 /* Create a Fab object. */
125 fab = _fab_new(isfname,
127 (Bool)((mode & ISLENMODE) == ISVARLEN),
131 return (NOISFD); /* iserrno is set by fab_new */
134 /* Get an ISAM file descriptor for this fab */
135 if ((isfd = _isfd_insert(fab)) == NOISFD) {
136 /* Table of ISAM file descriptors would overflow. */
138 _setiserrno2(ETOOMANY, '9', '0');
141 FAB_ISFDSET(fab, isfd);
144 * Extract umask. It is send to the Acces Layer (which may reside
145 * on a remote machine).
147 origumask = umask(0);
148 (void)umask(origumask);
154 if (_ambuild(fab->isfname, fab->openmode, fab->varlength,
155 fab->minreclen, fab->maxreclen, primkey, getuid(),
156 getgid(), origumask, &fab->isfhandle, &fab->curpos,
158 _seterr_errcode(&fab->errcode);
163 return ((int)isfd); /* Successful isopen() */
167 * _ambuild(isfname, openmode, varflag, minlen, maxlen,
168 * primkey, owner, group, umask, isfhandle, curpos, errcode)
170 * _ambuild() creates a new ISAM file with the name isfname.
173 * isfname ISAM file name
174 * varflag is 0/1 flag set to 1 if the file is for variable lengths records
175 * minlen minimum length of record in bytes
176 * maxlen maximum length of record in bytes
177 * primkey definition of the primary key
178 * owner, group set the ownership of the file to this user and group
179 * umask application's value of umask
182 * isfhandle a file handle to be used in subsequent operations on the file
183 * curpos initial current record position
184 * errcode {iserrno, isstat1-4}
186 * _ambuild() returns 0 if successful, or -1 to indicate an error.
188 #define FDNEEDED 3 /* Needs at most 3 UNIX fds to open ISAM file */
192 _ambuild(char *isfname, enum openmode openmode, Bool varflag,
193 int minlen, int maxlen, struct keydesc *primkey,
194 int owner, int group, int umask,
195 Bytearray *isfhandle, Bytearray *curpos,
196 struct errcode *errcode)
200 Bytearray *isfhandle2;
201 Bytearray isfhandle0 = _bytearr_getempty();
209 * Validate the primary key descriptor.
211 if (!USE_PHYS_ORDER(primkey) &&
212 _validate_keydesc(primkey, minlen) == ISERROR) {
213 _amseterrcode(errcode, EBADKEY);
220 isfhandle0 = _makeisfhandle(isfname);
223 * Check that there is not entry with the same name in FCB cache.
225 if ((fcb = _mngfcb_find(&isfhandle0)) != NULL) {
226 fcb = _mngfcb_find(&isfhandle0);
227 (void) _watchfd_decr(_isfcb_nfds(fcb));
229 _mngfcb_delete(&isfhandle0);
233 * Check that there are UNIX file descriptors available.
235 while (_watchfd_check() < FDNEEDED) {
237 * Find victim (LRU FCB) and close it.
239 if((isfhandle2 = _mngfcb_victim()) == NULL)
240 _isfatal_error ("_openfcb() cannot find LRU victim");
242 fcb = _mngfcb_find(isfhandle2);
243 (void) _watchfd_decr(_isfcb_nfds(fcb));
245 _mngfcb_delete(isfhandle2);
249 * Create UNIX files, return isfhandle and File Control Block (fcb).
251 if ((fcb = _isfcb_create(isfname, 1, (primkey->k_nparts != 0), (int)varflag,
252 owner, group, umask, errcode)) == NULL) {
257 * Add length info to the FCB.
259 _isfcb_setreclength(fcb, varflag, minlen, maxlen);
261 if (!USE_PHYS_ORDER(primkey)) {
263 * Convert key descriptor to internal form.
265 _iskey_xtoi (&keydesc2, primkey);
268 * Create index structure.
270 if ((err = _create_index(fcb , &keydesc2)) != ISOK) {
271 _amseterrcode(errcode, err);
276 * Add primary key descriptor to FCB.
278 if (_isfcb_primkeyadd(fcb, &keydesc2) == ISERROR) {
279 _amseterrcode(errcode, ETOOMANY);
285 * Initial current record position.
287 if (FCB_NOPRIMARY_KEY(fcb)) {
288 /* Use physical order. */
289 crp = (Crp *) _ismalloc(sizeof(*crp));
290 memset ((char *) crp, 0, sizeof(*crp));
292 crp->keyid = PHYS_ORDER;
293 crp->flag = CRP_BEFOREANY;
295 curpos->length = sizeof(*crp);
296 curpos->data = (char *) crp;
300 * Use primary key order.
303 crp = (Crp *) _ismalloc((unsigned)(sizeof(*crp) + fcb->keys[0].k2_len));
304 memset((char *) crp, 0, (sizeof(*crp) + fcb->keys[0].k2_len));
306 crp->keyid = fcb->keys[0].k2_keyid;
307 crp->flag = CRP_BEFOREANY;
309 _iskey_fillmin(&fcb->keys[0], crp->key);
311 curpos->length = sizeof(*crp) + fcb->keys[0].k2_len;
312 curpos->data = (char *) crp;
315 * Set full key length as the number of bytes to match in key comparison
317 crp->matchkeylen = fcb->keys[0].k2_len - RECNOSIZE;
319 if (ALLOWS_DUPS2(&fcb->keys[0]))
320 crp->matchkeylen -= DUPIDSIZE;
323 _amseterrcode(errcode, ISOK);
326 * Register the number of UNIX fd consumed.
328 (void) _watchfd_incr(_isfcb_nfds(fcb));
331 * Insert new entry into FCB cache.
333 _mngfcb_insert(fcb, &isfhandle0);
334 *isfhandle = isfhandle0;
336 /* Commit all work in disk cache. */
343 * Create Control Page (CNTLPAGE).
345 if (_isfcb_cntlpg_w(fcb) == ISERROR) {
360 _bytearr_free(&isfhandle0);