aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2004-08-09 04:28:25 +0000
committerNick Mathewson <nickm@torproject.org>2004-08-09 04:28:25 +0000
commit6453a2556722e1b34bc3258891f33c2a95884440 (patch)
treee39a4559827af6da8f99c9229248c5f3946c2a42
parent98b8a89aa85d51a3fc8df7c6facc42438de4d96b (diff)
downloadtor-6453a2556722e1b34bc3258891f33c2a95884440.tar.gz
tor-6453a2556722e1b34bc3258891f33c2a95884440.zip
Add replace_file to util.[ch] to survive stupidity of windows rename call
svn:r2208
-rw-r--r--src/common/util.c27
-rw-r--r--src/common/util.h1
2 files changed, 28 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c
index bc821fa592..50010bcb43 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -1559,6 +1559,33 @@ char *expand_filename(const char *filename)
}
}
+/**
+ * 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.
*/
diff --git a/src/common/util.h b/src/common/util.h
index 792d5cffe2..0bf0a8c6ed 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -220,6 +220,7 @@ int write_str_to_file(const char *fname, const char *str);
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();