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