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 extern char _isam_version[];
46 static char *_version_ = _isam_version;
48 static int _ambuild();
51 * isfd = isbuild(isfname, recordlength, primkey, mode)
53 * Isbuild() determines on which machine the ISAM file is to be built,
54 * checks the permissions for creating a file by this user running on
55 * this client machine by using the access(2) UNIX call.
56 * If the file is remote, it is created by the netisamd daemon running
57 * on the machine hosting the ISAM file. The chown(2) is then used to
58 * change the ownership of the file to the client.
59 * All UNIX files created will have their permissions set to 0666, allowing
60 * thus both read and write access to anybody.
62 * Isbuild() returns an ISAM file descriptor (isfd) is the call was successful,
63 * or a value of -1 if the call failed.
66 * EBADARG Improper mode parameter
67 * EBADARG isreclen >= recordlength and ISVARLEN specified
68 * E2BIG recordlength greater than system imposed limit (8196)
69 * EBADKEY Invalid key descriptor
70 * EBADFILE ISAM file is corrupted or it is not an NetISAM file
71 * EFNAME Invalid ISAM file name
72 * ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
74 * The following error numbers are "borrowed" from UNIX.
75 * EACCES UNIX file system protection denies creation of the file
76 * EEXIST - ISAM file already exists
77 * EEXIST - A UNIX file with the same name exists
81 isbuild(char *isfname, int recordlength, struct keydesc *primkey, int mode)
85 enum openmode openmode;
86 int minreclen; /* Minimum record length */
89 /* Get file open mode part of the mode parameter. */
90 if ((openmode = _getopenmode(mode)) == OM_BADMODE) {
91 _setiserrno2(EBADARG, '9', '0');
96 * Minimum record length.
99 minreclen = ((mode & ISLENMODE) == ISVARLEN) ? isreclen : recordlength;
101 /* Check recordlength against system imposed limit. */
102 if (recordlength > ISMAXRECLEN) {
103 _setiserrno2(E2BIG, '9', '0');
107 /* Check that ssminreclen >= ISMINRECLEN. */
108 if (minreclen < ISMINRECLEN) {
109 _setiserrno2(EBADARG, '9', '0');
113 /* Check that minreclen <= recordlength */
114 if (minreclen > recordlength) {
115 _setiserrno2(EBADARG, '9', '0');
119 /* Create a Fab object. */
120 fab = _fab_new(isfname,
122 (Bool)((mode & ISLENMODE) == ISVARLEN),
126 return (NOISFD); /* iserrno is set by fab_new */
129 /* Get an ISAM file descriptor for this fab */
130 if ((isfd = _isfd_insert(fab)) == NOISFD) {
131 /* Table of ISAM file descriptors would overflow. */
133 _setiserrno2(ETOOMANY, '9', '0');
136 FAB_ISFDSET(fab, isfd);
139 * Extract umask. It is send to the Acces Layer (which may reside
140 * on a remote machine).
142 origumask = umask(0);
143 (void)umask(origumask);
149 if (_ambuild(fab->isfname, fab->openmode, fab->varlength,
150 fab->minreclen, fab->maxreclen, primkey, getuid(),
151 getgid(), origumask, &fab->isfhandle, &fab->curpos,
153 _seterr_errcode(&fab->errcode);
158 return ((int)isfd); /* Successful isopen() */
162 * _ambuild(isfname, openmode, varflag, minlen, maxlen,
163 * primkey, owner, group, umask, isfhandle, curpos, errcode)
165 * _ambuild() creates a new ISAM file with the name isfname.
168 * isfname ISAM file name
169 * varflag is 0/1 flag set to 1 if the file is for variable lengths records
170 * minlen minimum length of record in bytes
171 * maxlen maximum length of record in bytes
172 * primkey definition of the primary key
173 * owner, group set the ownership of the file to this user and group
174 * umask application's value of umask
177 * isfhandle a file handle to be used in subsequent operations on the file
178 * curpos initial current record position
179 * errcode {iserrno, isstat1-4}
181 * _ambuild() returns 0 if successful, or -1 to indicate an error.
183 #define FDNEEDED 3 /* Needs at most 3 UNIX fds to open ISAM file */
187 _ambuild(char *isfname, enum openmode openmode, Bool varflag,
188 int minlen, int maxlen, struct keydesc *primkey,
189 int owner, int group, int umask,
190 Bytearray *isfhandle, Bytearray *curpos,
191 struct errcode *errcode)
195 Bytearray *isfhandle2;
196 Bytearray isfhandle0 = _bytearr_getempty();
204 * Validate the primary key descriptor.
206 if (!USE_PHYS_ORDER(primkey) &&
207 _validate_keydesc(primkey, minlen) == ISERROR) {
208 _amseterrcode(errcode, EBADKEY);
215 isfhandle0 = _makeisfhandle(isfname);
218 * Check that there is not entry with the same name in FCB cache.
220 if ((fcb = _mngfcb_find(&isfhandle0)) != NULL) {
221 fcb = _mngfcb_find(&isfhandle0);
222 (void) _watchfd_decr(_isfcb_nfds(fcb));
224 _mngfcb_delete(&isfhandle0);
228 * Check that there are UNIX file descriptors available.
230 while (_watchfd_check() < FDNEEDED) {
232 * Find victim (LRU FCB) and close it.
234 if((isfhandle2 = _mngfcb_victim()) == NULL)
235 _isfatal_error ("_openfcb() cannot find LRU victim");
237 fcb = _mngfcb_find(isfhandle2);
238 (void) _watchfd_decr(_isfcb_nfds(fcb));
240 _mngfcb_delete(isfhandle2);
244 * Create UNIX files, return isfhandle and File Control Block (fcb).
246 if ((fcb = _isfcb_create(isfname, 1, (primkey->k_nparts != 0), (int)varflag,
247 owner, group, umask, errcode)) == NULL) {
252 * Add length info to the FCB.
254 _isfcb_setreclength(fcb, varflag, minlen, maxlen);
256 if (!USE_PHYS_ORDER(primkey)) {
258 * Convert key descriptor to internal form.
260 _iskey_xtoi (&keydesc2, primkey);
263 * Create index structure.
265 if ((err = _create_index(fcb , &keydesc2)) != ISOK) {
266 _amseterrcode(errcode, err);
271 * Add primary key descriptor to FCB.
273 if (_isfcb_primkeyadd(fcb, &keydesc2) == ISERROR) {
274 _amseterrcode(errcode, ETOOMANY);
280 * Initial current record position.
282 if (FCB_NOPRIMARY_KEY(fcb)) {
283 /* Use physical order. */
284 crp = (Crp *) _ismalloc(sizeof(*crp));
285 memset ((char *) crp, 0, sizeof(*crp));
287 crp->keyid = PHYS_ORDER;
288 crp->flag = CRP_BEFOREANY;
290 curpos->length = sizeof(*crp);
291 curpos->data = (char *) crp;
295 * Use primary key order.
298 crp = (Crp *) _ismalloc((unsigned)(sizeof(*crp) + fcb->keys[0].k2_len));
299 memset((char *) crp, 0, (sizeof(*crp) + fcb->keys[0].k2_len));
301 crp->keyid = fcb->keys[0].k2_keyid;
302 crp->flag = CRP_BEFOREANY;
304 _iskey_fillmin(&fcb->keys[0], crp->key);
306 curpos->length = sizeof(*crp) + fcb->keys[0].k2_len;
307 curpos->data = (char *) crp;
310 * Set full key length as the number of bytes to match in key comparison
312 crp->matchkeylen = fcb->keys[0].k2_len - RECNOSIZE;
314 if (ALLOWS_DUPS2(&fcb->keys[0]))
315 crp->matchkeylen -= DUPIDSIZE;
318 _amseterrcode(errcode, ISOK);
321 * Register the number of UNIX fd consumed.
323 (void) _watchfd_incr(_isfcb_nfds(fcb));
326 * Insert new entry into FCB cache.
328 _mngfcb_insert(fcb, &isfhandle0);
329 *isfhandle = isfhandle0;
331 /* Commit all work in disk cache. */
338 * Create Control Page (CNTLPAGE).
340 if (_isfcb_cntlpg_w(fcb) == ISERROR) {
355 _bytearr_free(&isfhandle0);