os_rename.c 1.56 KB
Newer Older
1 2 3
/*-
 * See the file LICENSE for redistribution information.
 *
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
4
 * Copyright (c) 1997-2002
5 6 7 8 9 10
 *	Sleepycat Software.  All rights reserved.
 */

#include "db_config.h"

#ifndef lint
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
11
static const char revid[] = "$Id: os_rename.c,v 1.12 2002/07/12 18:56:55 bostic Exp $";
12 13 14 15 16 17 18 19 20
#endif /* not lint */

#include "db_int.h"

/*
 * __os_rename --
 *	Rename a file.
 */
int
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
21
__os_rename(dbenv, oldname, newname, flags)
22
	DB_ENV *dbenv;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
23 24
	const char *oldname, *newname;
	u_int32_t flags;
25 26
{
	int ret;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
27
	char oldbuf[MAX_PATH], newbuf[MAX_PATH];
28 29

	ret = 0;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
30 31
	if (DB_GLOBAL(j_rename) != NULL) {
		if (DB_GLOBAL(j_rename)(oldname, newname) == -1)
32
			ret = __os_get_errno();
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
33
		goto done;
34
	}
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

	if (!MoveFile(oldname, newname))
		ret = __os_win32_errno();

	if (ret == EEXIST) {
		ret = 0;
		if (__os_is_winnt()) {
			if (!MoveFileEx(
			    oldname, newname, MOVEFILE_REPLACE_EXISTING))
				ret = __os_win32_errno();
		} else {
			/*
			 * There is no MoveFileEx for Win9x/Me, so we have to
			 * do the best we can.
			 */
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
50 51 52
			LPTSTR FilePath;
			if (!GetFullPathName(oldname, sizeof(oldbuf), oldbuf,
					     &FilePath) ||
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
53
			    !GetFullPathName(newname, sizeof(newbuf), newbuf,
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
54
					     &FilePath)) {
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
				ret = __os_win32_errno();
				goto done;
			}

			/*
			 * If the old and new names differ only in case, we're
			 * done.
			 */
			if (strcasecmp(oldbuf, newbuf) == 0)
				goto done;

			(void)DeleteFile(newname);
			if (!MoveFile(oldname, newname))
				ret = __os_win32_errno();
		}
70
	}
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
71 72 73 74

done:	if (ret != 0 && flags == 0)
		__db_err(dbenv,
		    "Rename %s %s: %s", oldname, newname, strerror(ret));
75 76 77

	return (ret);
}