Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / mini_isam / iserase.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 librararies 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: iserase.c /main/3 1995/10/23 11:38:08 rswiston $                                                     */
28 #ifndef lint
29 static char sccsid[] = "@(#)iserase.c 1.8 89/07/17 Copyr 1988 Sun Micro";
30 #endif
31 /*
32  * Copyright (c) 1988 by Sun Microsystems, Inc.
33  */
34
35 /*
36  * iserase.c
37  *
38  * Description:
39  *      Erase an ISAM file. 
40  */
41
42
43 #include "isam_impl.h"
44 #include <sys/time.h>
45
46 static void _unlink_datfile(), _unlink_indfile(), _unlink_varfile();
47 static int _amerase();
48
49 /*
50  * isfd = iserase(isfname, mode)
51  *
52  *
53  * Errors:
54  *      EBADFILE ISAM file is corrupted or it is not an NetISAM file
55  *      EFLOCKED The file is exclusively locked by other process.
56  *      EFNAME  Invalid ISAM file name 
57  *      EFNAME  ISAM file does not exist
58  *      ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
59  *
60  * The following error code is "borrowed" from UNIX:
61  *      EACCES  UNIX file system protection denies access to the file:
62  *               - mode is INOUT or OUTPUT and ISAM file is on 
63  *                 a Read-Only mounted file system
64  *               - UNIX file permissions don't allow access to the file
65  */
66
67 int 
68 iserase(isfname)
69     char                *isfname;
70 {
71     Isfd                isfd, isfd_nfs;
72     Fab                 *fab, *fab_nfs;
73
74     /*
75      * Open the file
76      */
77     if ((isfd = isopen(isfname, ISINOUT)) == -1)
78         return (ISERROR);                    /* iserrno is set */
79
80     /*
81      * Get File Access Block.
82      */
83     if ((fab = _isfd_find(isfd)) == NULL) {
84         _isfatal_error("iserase() cannot find FAB");
85         _setiserrno2(EFATAL, '9', '0');
86         return ISERROR;
87     }
88
89     if (_amerase(&fab->isfhandle, &fab->errcode)
90                 && fab->errcode.iserrno != ENOENT) {
91         _seterr_errcode(&fab->errcode); 
92         (void)isclose(isfd);
93         return (ISERROR);
94     }
95
96     _fab_destroy(fab);                       /* Deallocate Fab object */
97     _isfd_delete(isfd);
98     
99     return (ISOK);                           /* Successful iserase() */
100 }
101
102 /*
103  * _amerase(isfhandle)
104  *
105  * _amerase() erases ISAM file
106  *
107  * Input params:
108  *      isfhandle       Handle of ISAM file
109  *
110  * Output params:
111  *      errcode         Error code
112  *
113  */
114
115 static int
116 _amerase(isfhandle, errcode)
117     Bytearray           *isfhandle;
118     struct errcode      *errcode;
119 {
120     Fcb                 *fcb;
121     char                *isfname = _getisfname(isfhandle);
122
123     _isam_entryhook();
124
125     /*
126      * Get FCB corresponding to the isfhandle handle.
127      */
128     if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
129         goto ERROR;
130     }
131
132     /*
133      * Delete FCB and remove it from FCB cache.
134      */
135     (void) _watchfd_decr(_isfcb_nfds(fcb));
136     _isfcb_close(fcb);
137     _mngfcb_delete(isfhandle);
138
139     /*
140      * Unlink all UNIX files.
141      */
142     _unlink_datfile(isfname);
143     _unlink_indfile(isfname);
144     _unlink_varfile(isfname);
145
146     _isam_exithook();
147     return (ISOK);
148
149  ERROR:
150
151     _isam_exithook();
152     return (ISERROR);
153 }
154
155
156 Static void
157 _unlink_datfile(isfname)
158     char        *isfname;
159 {
160     char        namebuf[MAXPATHLEN];
161
162     (void) strcpy(namebuf, isfname);
163     _makedat_isfname(namebuf);
164
165     (void)unlink(namebuf);
166 }
167
168
169 Static void
170 _unlink_indfile(isfname)
171     char        *isfname;
172 {
173     char        namebuf[MAXPATHLEN];
174
175     (void) strcpy(namebuf, isfname);
176     _makeind_isfname(namebuf);
177
178     (void)unlink(namebuf);
179 }
180
181
182 Static void
183 _unlink_varfile(isfname)
184     char        *isfname;
185 {
186     char        namebuf[MAXPATHLEN];
187
188     (void) strcpy(namebuf, isfname);
189     _makevar_isfname(namebuf);
190
191     (void)unlink(namebuf);
192 }