tt/mini_isam: remove all ancient sccsid blocks
[oweals/cde.git] / cde / lib / tt / mini_isam / isbuild.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: isbuild.c /main/3 1995/10/23 11:36:21 rswiston $                                                     */
28 /*
29  * Copyright (c) 1988 by Sun Microsystems, Inc.
30  */
31
32 /*
33  * isbuild.c
34  *
35  * Description:
36  *      Create an ISAM file. 
37  */
38
39
40 #include "isam_impl.h"
41 #include <netdb.h>
42 #include <sys/file.h>
43 #include <sys/time.h>
44
45 extern char _isam_version[];
46 static char *_version_ = _isam_version;
47
48 static int _ambuild();
49
50 /*
51  * isfd = isbuild(isfname, recordlength, primkey, mode)
52  *
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.
61  *
62  * Isbuild() returns an ISAM file descriptor (isfd) is the call was successful, 
63  * or a value of -1 if the call failed.
64  *
65  * Errors:
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)
73  *
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
78  */
79
80 int
81 isbuild(char *isfname, int recordlength, struct keydesc *primkey, int mode)
82 {
83     Fab                 *fab;
84     Isfd                isfd;
85     enum openmode       openmode;
86     int                 minreclen;           /* Minimum record length */
87     int                 origumask;
88
89     /* Get file open mode part of the mode parameter. */
90     if ((openmode = _getopenmode(mode)) == OM_BADMODE) {
91         _setiserrno2(EBADARG, '9', '0');
92         return (NOISFD);
93     }
94
95     /*
96      * Minimum record length.
97      */
98
99     minreclen = ((mode & ISLENMODE) == ISVARLEN) ? isreclen : recordlength;
100
101     /* Check recordlength against system imposed limit. */
102     if (recordlength > ISMAXRECLEN) {
103         _setiserrno2(E2BIG, '9', '0');
104         return (NOISFD);
105     }
106
107     /* Check that ssminreclen >= ISMINRECLEN. */
108     if (minreclen < ISMINRECLEN) {
109         _setiserrno2(EBADARG, '9', '0');
110         return (NOISFD);
111     }
112
113     /* Check that minreclen <= recordlength */
114     if (minreclen > recordlength) {
115         _setiserrno2(EBADARG, '9', '0');
116         return (NOISFD);
117     }
118
119     /* Create a Fab object. */
120     fab = _fab_new(isfname,
121                    openmode,
122                    (Bool)((mode & ISLENMODE) == ISVARLEN),
123                    minreclen,
124                    recordlength);
125     if (fab == NULL) {
126         return (NOISFD);                     /* iserrno is set by fab_new */
127     }
128
129     /* Get an ISAM file descriptor for this fab */
130     if ((isfd = _isfd_insert(fab)) == NOISFD) {
131         /* Table of ISAM file descriptors would overflow. */
132         _fab_destroy(fab);
133         _setiserrno2(ETOOMANY, '9', '0');
134         return (NOISFD);
135     }
136     FAB_ISFDSET(fab, isfd);
137
138     /*
139      * Extract umask. It is send to the Acces Layer (which may reside
140      * on a remote machine).
141      */
142     origumask = umask(0);
143     (void)umask(origumask);
144
145     /* 
146      * Call lower layers.
147      */
148
149     if (_ambuild(fab->isfname, fab->openmode, fab->varlength,
150                  fab->minreclen, fab->maxreclen, primkey, getuid(),
151                  getgid(), origumask, &fab->isfhandle, &fab->curpos,
152                  &fab->errcode)) {
153         _seterr_errcode(&fab->errcode);
154         _fab_destroy(fab);
155         return (NOISFD);
156     }
157
158     return ((int)isfd);                      /* Successful isopen() */
159 }
160
161 /*
162  * _ambuild(isfname, openmode, varflag, minlen, maxlen,
163  *          primkey, owner, group, umask, isfhandle, curpos, errcode)
164  *
165  * _ambuild() creates a new ISAM file with the name isfname. 
166  *
167  * Input params:
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
175  * 
176  * Output params:
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} 
180  *
181  * _ambuild() returns 0 if successful, or -1 to indicate an error.
182  */
183 #define FDNEEDED        3                    /* Needs at most 3 UNIX fds to open ISAM file */
184
185 /* ARGSUSED */
186 static int
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)
192     
193 {
194     Fcb                 *fcb = NULL;
195     Bytearray           *isfhandle2;
196     Bytearray           isfhandle0 = _bytearr_getempty();
197     Keydesc2            keydesc2;
198     int                 err;
199     Crp                 *crp;
200
201     _isam_entryhook();
202
203     /*
204      * Validate the primary key descriptor.
205      */
206     if (!USE_PHYS_ORDER(primkey) && 
207         _validate_keydesc(primkey, minlen) == ISERROR) {
208         _amseterrcode(errcode, EBADKEY);
209         goto ERROR;
210     }
211
212     /*
213      * Make isfhandle0.
214      */
215     isfhandle0 = _makeisfhandle(isfname);
216
217     /* 
218      * Check that there is not entry with the same name in FCB cache. 
219      */
220     if ((fcb = _mngfcb_find(&isfhandle0)) != NULL) {
221         fcb = _mngfcb_find(&isfhandle0);
222         (void) _watchfd_decr(_isfcb_nfds(fcb));
223         _isfcb_close(fcb);
224         _mngfcb_delete(&isfhandle0);
225     }
226
227     /*
228      * Check that there are UNIX file descriptors available.    
229      */
230     while (_watchfd_check() < FDNEEDED) {
231         /*
232          * Find victim (LRU FCB) and close it.
233          */
234         if((isfhandle2 = _mngfcb_victim()) == NULL)
235             _isfatal_error ("_openfcb() cannot find LRU victim");
236
237         fcb = _mngfcb_find(isfhandle2);
238         (void) _watchfd_decr(_isfcb_nfds(fcb));
239         _isfcb_close(fcb);
240         _mngfcb_delete(isfhandle2);
241     }
242
243     /*
244      * Create UNIX files, return isfhandle and File Control Block (fcb).
245      */
246     if ((fcb = _isfcb_create(isfname, 1, (primkey->k_nparts != 0), (int)varflag, 
247                              owner, group, umask, errcode)) == NULL) {
248         goto ERROR;
249     }
250     
251     /* 
252      * Add length info to the FCB. 
253      */
254     _isfcb_setreclength(fcb, varflag, minlen, maxlen);
255       
256     if (!USE_PHYS_ORDER(primkey)) { 
257         /*
258          * Convert key descriptor to internal form.
259          */
260         _iskey_xtoi (&keydesc2, primkey);
261         
262         /*
263          * Create index structure.
264          */
265         if ((err = _create_index(fcb , &keydesc2)) != ISOK) { 
266             _amseterrcode(errcode, err);
267             goto ERROR;
268         }
269         
270         /*
271          * Add primary key descriptor to FCB.
272          */
273         if (_isfcb_primkeyadd(fcb, &keydesc2) == ISERROR) {        
274             _amseterrcode(errcode, ETOOMANY);
275             goto ERROR;
276         }
277     }
278
279     /*
280      * Initial current record position.
281      */
282     if (FCB_NOPRIMARY_KEY(fcb)) {
283         /* Use physical order. */
284         crp = (Crp *) _ismalloc(sizeof(*crp));
285         memset ((char *) crp, 0, sizeof(*crp));
286
287         crp->keyid = PHYS_ORDER;
288         crp->flag = CRP_BEFOREANY;
289
290         curpos->length = sizeof(*crp);
291         curpos->data = (char *) crp;
292     }
293     else {
294         /* 
295          * Use primary key order. 
296          */
297
298         crp = (Crp *) _ismalloc((unsigned)(sizeof(*crp) + fcb->keys[0].k2_len));
299         memset((char *) crp, 0, (sizeof(*crp) + fcb->keys[0].k2_len)); 
300
301         crp->keyid = fcb->keys[0].k2_keyid;
302         crp->flag = CRP_BEFOREANY;
303
304         _iskey_fillmin(&fcb->keys[0], crp->key);
305
306         curpos->length = sizeof(*crp) + fcb->keys[0].k2_len;
307         curpos->data = (char *) crp;
308         
309         /*
310          * Set full key length as the number of bytes to match in key comparison
311          */
312         crp->matchkeylen = fcb->keys[0].k2_len - RECNOSIZE;
313         
314         if (ALLOWS_DUPS2(&fcb->keys[0]))
315             crp->matchkeylen -= DUPIDSIZE;
316     }
317
318     _amseterrcode(errcode, ISOK);
319
320     /*
321      * Register the number of UNIX fd consumed.
322      */
323     (void) _watchfd_incr(_isfcb_nfds(fcb));
324
325     /*
326      * Insert new entry into FCB cache.
327      */
328     _mngfcb_insert(fcb, &isfhandle0);
329     *isfhandle = isfhandle0;
330
331     /* Commit all work in disk cache. */
332     _issignals_mask();
333     _isdisk_commit();
334     _isdisk_sync();
335     _isdisk_inval();
336
337     /* 
338      * Create Control Page (CNTLPAGE). 
339      */
340     if (_isfcb_cntlpg_w(fcb) == ISERROR) {
341         _issignals_unmask();
342         goto ERROR;
343     }
344     
345     _issignals_unmask();
346     _isam_exithook();
347     return (ISOK);
348
349  ERROR:
350     if (fcb != NULL) {
351         _isfcb_remove(fcb);
352         _isfcb_close(fcb);
353     }
354
355     _bytearr_free(&isfhandle0);
356
357     _isam_exithook();
358     return (ISERROR);
359 }
360