configure: create some convenience AC_SUBST's for the global includes
[oweals/cde.git] / cde / lib / tt / mini_isam / isrename.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: isrename.c /main/3 1995/10/23 11:43:48 rswiston $                                                    */
28 /*
29  * Copyright (c) 1988 by Sun Microsystems, Inc.
30  */
31
32 /*
33  * isrename.c
34  *
35  * Description:
36  *      Rename an ISAM file. 
37  */
38
39
40 #include "isam_impl.h"
41 #include <unistd.h>
42 #include <sys/time.h>
43
44 void _removelast();
45 static void _removelast2();
46 char *_lastelement();
47 static void _rename_datfile(), _rename_indfile(), _rename_varfile();
48 static int _amrename();
49
50 /*
51  * isfd = isrename(oldname, newname)
52  *
53  *
54  * Errors:
55  *      EBADFILE ISAM file is corrupted or it is not an NetISAM file
56  *      EFLOCKED The file is exclusively locked by other process.
57  *      EFNAME  Invalid ISAM file name 
58  *      EFNAME  ISAM file does not exist
59  *      ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
60  *
61  * The following error code is "borrowed" from UNIX:
62  *      EACCES  UNIX file system protection denies access to the file:
63  *               - mode is INOUT or OUTPUT and ISAM file is on 
64  *                 a Read-Only mounted file system
65  *               - UNIX file permissions don't allow access to the file
66  */
67
68 int 
69 isrename(char *oldname, char *newname)
70 {
71     Isfd                isfd, isfd2;
72     Fab                 *fab;
73     char                olddir [MAXPATHLEN];
74     char                newdir [MAXPATHLEN];
75     char                datfname[MAXPATHLEN];
76
77     /*
78      * Check that the odl and new filename are in the same directory.
79      */
80     snprintf(olddir, sizeof(olddir), "%s", oldname);
81     _removelast2(olddir);
82     snprintf(newdir, sizeof(newdir), "%s", newname);
83     _removelast2(newdir);
84
85     if (strcmp(newdir, olddir) != 0) {
86         _setiserrno2(EFNAME, 9, 0);
87         return (ISERROR);
88     }
89
90     /*
91      * Open the file
92      */
93     if ((isfd = isopen(oldname, ISINOUT)) == -1)
94         return (ISERROR);                    /* iserrno is set */
95
96     /*
97      * Reject rename if 'newfile' exists.
98      */
99     if ((isfd2 = isopen(newname, ISINOUT)) != -1 || iserrno != ENOENT) {
100
101         _setiserrno2(EEXIST, 9, 0);
102         if (isfd2 >= 0) (void)isclose(isfd2);
103         return (ISERROR);                    /* iserrno is set */
104     }
105
106     /*
107      * Get File Access Block.
108      */
109     if ((fab = _isfd_find(isfd)) == NULL) {
110         _isfatal_error("isrename() cannot find FAB");
111         _setiserrno2(EFATAL,'9','0');
112         return (ISERROR);
113     }
114
115     if (_amrename(&fab->isfhandle, _lastelement(newname), &fab->errcode)) {
116         _seterr_errcode(&fab->errcode);
117         (void)isclose(isfd);
118         return (ISERROR);
119     }
120
121     _fab_destroy(fab);                       /* Deallocate Fab object */
122     _isfd_delete(isfd);
123
124     /*
125      * We must unlink() the .rec file, or isbuild() with the same 
126      * following immediatly would fail because for NFS files the client
127      * still thinks that the file exists for a few seconds.
128      */
129
130     snprintf(datfname, sizeof(datfname), "%s", oldname);
131     _makedat_isfname(datfname);
132     (void)unlink(datfname);
133
134     return (ISOK);                           /* Successful isrename() */
135 }
136
137 /*
138  * _removelast2(path)
139  *
140  * Remove last element of path. E.g. /usr/db/part yields /usr/db.
141  * Unlike _removelast() that path does not have to start with '/'.
142  */
143
144 Static void
145 _removelast2(char *path)
146 {
147     char        *p;
148
149     for (p = path + strlen(path); *--p != '/' && p >= path; ) 
150         *p = '\0';
151 }
152
153 /*
154  * _amrename(isfhandle, newname)
155  *
156  * _amrename() renames ISAM file
157  *
158  * Input params:
159  *      isfhandle       Handle of ISAM file
160  *
161  * Output params:
162  *      errcode         Error code
163  *
164  */
165
166
167 static int
168 _amrename(Bytearray *isfhandle, char *newname, struct errcode *errcode)
169 {
170     Fcb                 *fcb;
171     char                *isfname = _getisfname(isfhandle);
172
173     _isam_entryhook();
174
175     /*
176      * Get FCB corresponding to the isfhandle handle.
177      */
178     if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
179         goto ERROR;
180     }
181
182     /*
183      * Delete FCB and remove it from FCB cache.
184      */
185     (void) _watchfd_decr(_isfcb_nfds(fcb));
186     _isfcb_close(fcb);
187     _mngfcb_delete(isfhandle);
188
189     /*
190      * Rename all UNIX files.
191      */
192     _rename_datfile(isfname, newname);
193     _rename_indfile(isfname, newname);
194     _rename_varfile(isfname, newname);
195
196     _isam_exithook();
197     return (ISOK);
198
199  ERROR:
200
201     _isam_exithook();
202     return (ISERROR);
203 }
204
205
206 /* newname, with no prefix */
207 Static void
208 _rename_datfile(char *isfname, char *newname)     
209 {
210     char        namebuf[MAXPATHLEN];
211     char        newbuf[MAXPATHLEN];
212     char        newbuftemp[sizeof(newbuf)];
213
214     snprintf(namebuf, sizeof(namebuf), "%s", isfname);
215     snprintf(newbuf, sizeof(newbuf), "%s", isfname);
216
217     /*
218      * Replace the last element of the old path with newname.
219      */
220     _removelast(newbuf);
221     if (strcmp(newbuf, "/") != 0) {
222         snprintf(newbuftemp, sizeof(newbuftemp),  "%s/", newbuf);
223         strcpy(newbuf, newbuftemp);
224     }
225     snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
226     strcpy(newbuf, newbuftemp);
227
228     _makedat_isfname(namebuf);
229     _makedat_isfname(newbuf);
230
231     (void)rename(namebuf, newbuf);
232 }
233
234 /* newname, with no prefix */
235 Static void
236 _rename_indfile(char *isfname, char *newname)     
237 {
238     char        namebuf[MAXPATHLEN];
239     char        newbuf[MAXPATHLEN];
240     char        newbuftemp[MAXPATHLEN];
241
242     snprintf(namebuf, sizeof(namebuf), "%s", isfname);
243     snprintf(newbuf, sizeof(newbuf), "%s", isfname);
244
245     /*
246      * Replace the last element of the old path with newname.
247      */
248     _removelast(newbuf);
249     if (strcmp(newbuf, "/") != 0) {
250         snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
251         strcpy(newbuf, newbuftemp);
252     }
253     snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
254     strcpy(newbuf, newbuftemp);
255
256     _makeind_isfname(namebuf);
257     _makeind_isfname(newbuf);
258
259     (void)rename(namebuf, newbuf);
260 }
261
262 /* newname, with no prefix */
263 Static void
264 _rename_varfile(char *isfname, char *newname) 
265 {
266     char        namebuf[MAXPATHLEN];
267     char        newbuf[MAXPATHLEN];
268     char        newbuftemp[MAXPATHLEN];
269
270     snprintf(namebuf, sizeof(namebuf), "%s", isfname);
271     snprintf(newbuf, sizeof(newbuf), "%s", isfname);
272
273     /*
274      * Replace the last element of the old path with newname.
275      */
276     _removelast(newbuf);
277     if (strcmp(newbuf, "/") != 0) {
278         snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
279         strcpy(newbuf, newbuftemp);
280     }
281     snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
282     strcpy(newbuf, newbuftemp);
283
284     _makevar_isfname(namebuf);
285     _makevar_isfname(newbuf);
286
287     (void)rename(namebuf, newbuf);
288 }
289