Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

os_rename.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: os_rename.c,v 12.1 2005/06/16 20:23:30 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #include "db_int.h"
00013 
00014 /*
00015  * __os_rename --
00016  *      Rename a file.
00017  */
00018 int
00019 __os_rename(dbenv, oldname, newname, flags)
00020         DB_ENV *dbenv;
00021         const char *oldname, *newname;
00022         u_int32_t flags;
00023 {
00024         int ret;
00025         _TCHAR *toldname, *tnewname;
00026 
00027         ret = 0;
00028         if (DB_GLOBAL(j_rename) != NULL) {
00029                 if (DB_GLOBAL(j_rename)(oldname, newname) == -1)
00030                         ret = __os_get_errno();
00031                 goto done;
00032         }
00033 
00034         TO_TSTRING(dbenv, oldname, toldname, ret);
00035         if (ret != 0)
00036                 goto done;
00037         TO_TSTRING(dbenv, newname, tnewname, ret);
00038         if (ret != 0)
00039                 goto err;
00040 
00041         if (!MoveFile(toldname, tnewname))
00042                 ret = __os_get_errno();
00043 
00044         if (ret == EEXIST) {
00045                 ret = 0;
00046                 if (__os_is_winnt()) {
00047                         if (!MoveFileEx(
00048                             toldname, tnewname, MOVEFILE_REPLACE_EXISTING))
00049                                 ret = __os_get_errno();
00050                 } else {
00051                         /*
00052                          * There is no MoveFileEx for Win9x/Me, so we have to
00053                          * do the best we can.  Note that the MoveFile call
00054                          * above would have succeeded if oldname and newname
00055                          * refer to the same file, so we don't need to check
00056                          * that here.
00057                          */
00058                         (void)DeleteFile(tnewname);
00059                         if (!MoveFile(toldname, tnewname))
00060                                 ret = __os_get_errno();
00061                 }
00062         }
00063 
00064         FREE_STRING(dbenv, tnewname);
00065 err:    FREE_STRING(dbenv, toldname);
00066 
00067 done:   if (ret != 0 && flags == 0)
00068                 __db_err(dbenv,
00069                     "Rename %s %s: %s", oldname, newname, strerror(ret));
00070 
00071         return (ret);
00072 }

Generated on Sun Dec 25 12:14:42 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2