From: Asanka Herath Date: Tue, 24 Aug 2010 04:34:18 +0000 (-0400) Subject: Add roken/rename.c to fix non-standard rename() X-Git-Url: http://git.samba.org/abartlet/lorikeet-heimdal.git/.git/?p=abartlet%2Florikeet-heimdal.git%2F.git;a=commitdiff_plain;h=4b36b36e0be49ef6532e29ef7649c4249d8ef05b Add roken/rename.c to fix non-standard rename() roken/rename.c is for platforms where the native rename() implementation does not replace the target if it already exists. This implementation isn't atomic, but should be close enough for most purposes. For correct behavior, rk_rename() should be used instead of rename(). rk_rename() is #defined to be rename() on platforms where this fix is not necessary. --- diff --git a/lib/krb5/fcache.c b/lib/krb5/fcache.c index 1e6397427..218bd2cdb 100644 --- a/lib/krb5/fcache.c +++ b/lib/krb5/fcache.c @@ -813,15 +813,7 @@ fcc_remove_cred(krb5_context context, return ret; } - ret = rename(&newname[5], FILENAME(id)); -#ifdef RENAME_DOES_NOT_UNLINK - if (ret && (errno == EEXIST || errno == EACCES)) { - ret = unlink(FILENAME(id)); - if (ret == 0) { - ret = rename(&newname[5], FILENAME(id)); - } - } -#endif + ret = rk_rename(&newname[5], FILENAME(id)); if (ret) ret = errno; free(newname); @@ -917,15 +909,7 @@ fcc_move(krb5_context context, krb5_ccache from, krb5_ccache to) { krb5_error_code ret = 0; - ret = rename(FILENAME(from), FILENAME(to)); -#ifdef RENAME_DOES_NOT_UNLINK - if (ret && (errno == EEXIST || errno == EACCES)) { - ret = unlink(FILENAME(to)); - if (ret == 0) { - ret = rename(FILENAME(from), FILENAME(to)); - } - } -#endif + ret = rk_rename(FILENAME(from), FILENAME(to)); if (ret && errno != EXDEV) { char buf[128]; diff --git a/lib/roken/NTMakefile b/lib/roken/NTMakefile index 3c626f79c..41a820897 100644 --- a/lib/roken/NTMakefile +++ b/lib/roken/NTMakefile @@ -75,6 +75,7 @@ libroken_la_OBJS = \ $(OBJ)\parse_time.obj \ $(OBJ)\parse_units.obj \ $(OBJ)\realloc.obj \ + $(OBJ)\rename.obj \ $(OBJ)\resolve.obj \ $(OBJ)\roken_gethostby.obj \ $(OBJ)\rtbl.obj \ diff --git a/lib/roken/rename.c b/lib/roken/rename.c new file mode 100644 index 000000000..e61e27350 --- /dev/null +++ b/lib/roken/rename.c @@ -0,0 +1,49 @@ +/*********************************************************************** + * Copyright (c) 2010, Secure Endpoints Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + **********************************************************************/ + +#include +#include "roken.h" + +/* rename() for platforms where the native implementation doesn't + * unlink newname. */ +int rk_rename(const char * oldname, const char * newname) +{ + int ret; + + ret = rename(oldname, newname); + if (ret != 0 && (errno == EEXIST || errno == EACCES)) { + ret = unlink(newname); + if (ret == 0) + ret = rename(oldname, newname); + } + + return ret; +} diff --git a/lib/roken/roken.h.in b/lib/roken/roken.h.in index 561257fff..275ab1775 100644 --- a/lib/roken/roken.h.in +++ b/lib/roken/roken.h.in @@ -616,6 +616,12 @@ ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL initgroups(const char *, gid_t); ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL fchown(int, uid_t, gid_t); #endif +#ifdef RENAME_DOES_NOT_UNLINK +ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL rk_rename(const char *, const char *); +#else +#define rk_rename rename +#endif + #if !defined(HAVE_DAEMON) || defined(NEED_DAEMON_PROTO) #ifndef HAVE_DAEMON #define daemon rk_daemon