2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
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 $ */
29 static char sccsid[] = "@(#)isrename.c 1.10 89/07/17 Copyr 1988 Sun Micro";
32 * Copyright (c) 1988 by Sun Microsystems, Inc.
39 * Rename an ISAM file.
43 #include "isam_impl.h"
47 static void _removelast2();
49 static void _rename_datfile(), _rename_indfile(), _rename_varfile();
50 static int _amrename();
53 * isfd = isrename(oldname, newname)
57 * EBADFILE ISAM file is corrupted or it is not an NetISAM file
58 * EFLOCKED The file is exclusively locked by other process.
59 * EFNAME Invalid ISAM file name
60 * EFNAME ISAM file does not exist
61 * ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
63 * The following error code is "borrowed" from UNIX:
64 * EACCES UNIX file system protection denies access to the file:
65 * - mode is INOUT or OUTPUT and ISAM file is on
66 * a Read-Only mounted file system
67 * - UNIX file permissions don't allow access to the file
71 isrename(char *oldname, char *newname)
75 char olddir [MAXPATHLEN];
76 char newdir [MAXPATHLEN];
77 char datfname[MAXPATHLEN];
80 * Check that the odl and new filename are in the same directory.
82 snprintf(olddir, sizeof(olddir), "%s", oldname);
84 snprintf(newdir, sizeof(newdir), "%s", newname);
87 if (strcmp(newdir, olddir) != 0) {
88 _setiserrno2(EFNAME, 9, 0);
95 if ((isfd = isopen(oldname, ISINOUT)) == -1)
96 return (ISERROR); /* iserrno is set */
99 * Reject rename if 'newfile' exists.
101 if ((isfd2 = isopen(newname, ISINOUT)) != -1 || iserrno != ENOENT) {
103 _setiserrno2(EEXIST, 9, 0);
104 if (isfd2 >= 0) (void)isclose(isfd2);
105 return (ISERROR); /* iserrno is set */
109 * Get File Access Block.
111 if ((fab = _isfd_find(isfd)) == NULL) {
112 _isfatal_error("isrename() cannot find FAB");
113 _setiserrno2(EFATAL,'9','0');
117 if (_amrename(&fab->isfhandle, _lastelement(newname), &fab->errcode)) {
118 _seterr_errcode(&fab->errcode);
123 _fab_destroy(fab); /* Deallocate Fab object */
127 * We must unlink() the .rec file, or isbuild() with the same
128 * following immediatly would fail because for NFS files the client
129 * still thinks that the file exists for a few seconds.
132 snprintf(datfname, sizeof(datfname), "%s", oldname);
133 _makedat_isfname(datfname);
134 (void)unlink(datfname);
136 return (ISOK); /* Successful isrename() */
142 * Remove last element of path. E.g. /usr/db/part yields /usr/db.
143 * Unlike _removelast() that path does not have to start with '/'.
147 _removelast2(char *path)
151 for (p = path + strlen(path); *--p != '/' && p >= path; )
156 * _amrename(isfhandle, newname)
158 * _amrename() renames ISAM file
161 * isfhandle Handle of ISAM file
170 _amrename(Bytearray *isfhandle, char *newname, struct errcode *errcode)
173 char *isfname = _getisfname(isfhandle);
178 * Get FCB corresponding to the isfhandle handle.
180 if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
185 * Delete FCB and remove it from FCB cache.
187 (void) _watchfd_decr(_isfcb_nfds(fcb));
189 _mngfcb_delete(isfhandle);
192 * Rename all UNIX files.
194 _rename_datfile(isfname, newname);
195 _rename_indfile(isfname, newname);
196 _rename_varfile(isfname, newname);
208 /* newname, with no prefix */
210 _rename_datfile(char *isfname, char *newname)
212 char namebuf[MAXPATHLEN];
213 char newbuf[MAXPATHLEN];
214 char newbuftemp[sizeof(newbuf)];
216 snprintf(namebuf, sizeof(namebuf), "%s", isfname);
217 snprintf(newbuf, sizeof(newbuf), "%s", isfname);
220 * Replace the last element of the old path with newname.
223 if (strcmp(newbuf, "/") != 0) {
224 snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
225 strcpy(newbuf, newbuftemp);
227 snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
228 strcpy(newbuf, newbuftemp);
230 _makedat_isfname(namebuf);
231 _makedat_isfname(newbuf);
233 (void)rename(namebuf, newbuf);
236 /* newname, with no prefix */
238 _rename_indfile(char *isfname, char *newname)
240 char namebuf[MAXPATHLEN];
241 char newbuf[MAXPATHLEN];
242 char newbuftemp[MAXPATHLEN];
244 snprintf(namebuf, sizeof(namebuf), "%s", isfname);
245 snprintf(newbuf, sizeof(newbuf), "%s", isfname);
248 * Replace the last element of the old path with newname.
251 if (strcmp(newbuf, "/") != 0) {
252 snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
253 strcpy(newbuf, newbuftemp);
255 snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
256 strcpy(newbuf, newbuftemp);
258 _makeind_isfname(namebuf);
259 _makeind_isfname(newbuf);
261 (void)rename(namebuf, newbuf);
264 /* newname, with no prefix */
266 _rename_varfile(char *isfname, char *newname)
268 char namebuf[MAXPATHLEN];
269 char newbuf[MAXPATHLEN];
270 char newbuftemp[MAXPATHLEN];
272 snprintf(namebuf, sizeof(namebuf), "%s", isfname);
273 snprintf(newbuf, sizeof(newbuf), "%s", isfname);
276 * Replace the last element of the old path with newname.
279 if (strcmp(newbuf, "/") != 0) {
280 snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
281 strcpy(newbuf, newbuftemp);
283 snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
284 strcpy(newbuf, newbuftemp);
286 _makevar_isfname(namebuf);
287 _makevar_isfname(newbuf);
289 (void)rename(namebuf, newbuf);