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 * Copyright (c) 1988 by Sun Microsystems, Inc.
36 * Rename an ISAM file.
40 #include "isam_impl.h"
45 static void _removelast2();
47 static void _rename_datfile(), _rename_indfile(), _rename_varfile();
48 static int _amrename();
51 * isfd = isrename(oldname, newname)
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)
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
69 isrename(char *oldname, char *newname)
73 char olddir [MAXPATHLEN];
74 char newdir [MAXPATHLEN];
75 char datfname[MAXPATHLEN];
78 * Check that the odl and new filename are in the same directory.
80 snprintf(olddir, sizeof(olddir), "%s", oldname);
82 snprintf(newdir, sizeof(newdir), "%s", newname);
85 if (strcmp(newdir, olddir) != 0) {
86 _setiserrno2(EFNAME, 9, 0);
93 if ((isfd = isopen(oldname, ISINOUT)) == -1)
94 return (ISERROR); /* iserrno is set */
97 * Reject rename if 'newfile' exists.
99 if ((isfd2 = isopen(newname, ISINOUT)) != -1 || iserrno != ENOENT) {
101 _setiserrno2(EEXIST, 9, 0);
102 if (isfd2 >= 0) (void)isclose(isfd2);
103 return (ISERROR); /* iserrno is set */
107 * Get File Access Block.
109 if ((fab = _isfd_find(isfd)) == NULL) {
110 _isfatal_error("isrename() cannot find FAB");
111 _setiserrno2(EFATAL,'9','0');
115 if (_amrename(&fab->isfhandle, _lastelement(newname), &fab->errcode)) {
116 _seterr_errcode(&fab->errcode);
121 _fab_destroy(fab); /* Deallocate Fab object */
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.
130 snprintf(datfname, sizeof(datfname), "%s", oldname);
131 _makedat_isfname(datfname);
132 (void)unlink(datfname);
134 return (ISOK); /* Successful isrename() */
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 '/'.
145 _removelast2(char *path)
149 for (p = path + strlen(path); *--p != '/' && p >= path; )
154 * _amrename(isfhandle, newname)
156 * _amrename() renames ISAM file
159 * isfhandle Handle of ISAM file
168 _amrename(Bytearray *isfhandle, char *newname, struct errcode *errcode)
171 char *isfname = _getisfname(isfhandle);
176 * Get FCB corresponding to the isfhandle handle.
178 if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
183 * Delete FCB and remove it from FCB cache.
185 (void) _watchfd_decr(_isfcb_nfds(fcb));
187 _mngfcb_delete(isfhandle);
190 * Rename all UNIX files.
192 _rename_datfile(isfname, newname);
193 _rename_indfile(isfname, newname);
194 _rename_varfile(isfname, newname);
206 /* newname, with no prefix */
208 _rename_datfile(char *isfname, char *newname)
210 char namebuf[MAXPATHLEN];
211 char newbuf[MAXPATHLEN];
212 char newbuftemp[sizeof(newbuf)];
214 snprintf(namebuf, sizeof(namebuf), "%s", isfname);
215 snprintf(newbuf, sizeof(newbuf), "%s", isfname);
218 * Replace the last element of the old path with newname.
221 if (strcmp(newbuf, "/") != 0) {
222 snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
223 strcpy(newbuf, newbuftemp);
225 snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
226 strcpy(newbuf, newbuftemp);
228 _makedat_isfname(namebuf);
229 _makedat_isfname(newbuf);
231 (void)rename(namebuf, newbuf);
234 /* newname, with no prefix */
236 _rename_indfile(char *isfname, char *newname)
238 char namebuf[MAXPATHLEN];
239 char newbuf[MAXPATHLEN];
240 char newbuftemp[MAXPATHLEN];
242 snprintf(namebuf, sizeof(namebuf), "%s", isfname);
243 snprintf(newbuf, sizeof(newbuf), "%s", isfname);
246 * Replace the last element of the old path with newname.
249 if (strcmp(newbuf, "/") != 0) {
250 snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
251 strcpy(newbuf, newbuftemp);
253 snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
254 strcpy(newbuf, newbuftemp);
256 _makeind_isfname(namebuf);
257 _makeind_isfname(newbuf);
259 (void)rename(namebuf, newbuf);
262 /* newname, with no prefix */
264 _rename_varfile(char *isfname, char *newname)
266 char namebuf[MAXPATHLEN];
267 char newbuf[MAXPATHLEN];
268 char newbuftemp[MAXPATHLEN];
270 snprintf(namebuf, sizeof(namebuf), "%s", isfname);
271 snprintf(newbuf, sizeof(newbuf), "%s", isfname);
274 * Replace the last element of the old path with newname.
277 if (strcmp(newbuf, "/") != 0) {
278 snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
279 strcpy(newbuf, newbuftemp);
281 snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
282 strcpy(newbuf, newbuftemp);
284 _makevar_isfname(namebuf);
285 _makevar_isfname(newbuf);
287 (void)rename(namebuf, newbuf);