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