Add roken/rename.c to fix non-standard rename()
authorAsanka Herath <asanka@secure-endpoints.com>
Tue, 24 Aug 2010 04:34:18 +0000 (00:34 -0400)
committerAsanka C. Herath <asanka@secure-endpoints.com>
Tue, 14 Sep 2010 12:03:34 +0000 (08:03 -0400)
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.

lib/krb5/fcache.c
lib/roken/NTMakefile
lib/roken/rename.c [new file with mode: 0644]
lib/roken/roken.h.in

index 1e6397427569df30e5909c90d1e04411bd5b9375..218bd2cdbfed27fdd3017df49c087c170fa7086b 100644 (file)
@@ -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];
index 3c626f79c4d89c39499a217bb0d79117af868117..41a820897b5ec3a0236bba849b08e599a3867ee3 100644 (file)
@@ -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 (file)
index 0000000..e61e273
--- /dev/null
@@ -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 <config.h>
+#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;
+}
index 561257fffcc3858e0efd4ce9df9ca1fd1684ce21..275ab17753bc26e744828b820575abb2294e88a6 100644 (file)
@@ -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