[or-cvs] Add replace_file to util.[ch] to survive stupidity of windo...

Nick Mathewson nickm at seul.org
Mon Aug 9 04:28:27 UTC 2004


Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv8889/src/common

Modified Files:
	util.c util.h 
Log Message:
Add replace_file to util.[ch] to survive stupidity of windows rename call

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.122
retrieving revision 1.123
diff -u -d -r1.122 -r1.123
--- util.c	7 Aug 2004 02:46:15 -0000	1.122
+++ util.c	9 Aug 2004 04:28:25 -0000	1.123
@@ -1559,6 +1559,33 @@
   }
 }
 
+/**
+ * Rename the file 'from' to the file 'to'.  On unix, this is the same as
+ * rename(2).  On windows, this removes 'to' first if it already exists.
+ * Returns 0 on success.  Returns -1 and sets errno on failure.
+ */
+int replace_file(const char *from, const char *to)
+{
+#ifndef MS_WINDOWS
+  return rename(from,to);
+#else
+  switch(file_status(to)) 
+    {
+    case FN_NOENT:
+      break;
+    case FN_FILE:
+      if (unlink(to)) return -1;
+      break;
+    case FN_ERROR:
+      return -1;
+    case FN_DIR:
+      errno = EISDIR;
+      return -1;
+    }
+  return rename(from,to);
+#endif
+}
+
 /** Return true iff <b>ip</b> (in host order) is an IP reserved to localhost,
  * or reserved for local networks by RFC 1918.
  */

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.79
retrieving revision 1.80
diff -u -d -r1.79 -r1.80
--- util.h	7 Aug 2004 02:46:15 -0000	1.79
+++ util.h	9 Aug 2004 04:28:25 -0000	1.80
@@ -220,6 +220,7 @@
 char *read_file_to_str(const char *filename);
 int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
 char *expand_filename(const char *filename);
+int replace_file(const char *from, const char *to);
 
 int spawn_func(int (*func)(void *), void *data);
 void spawn_exit();



More information about the tor-commits mailing list