- doc
[oweals/gnunet.git] / src / util / disk.c
index 8d1fed897e5e8e0c50742b2bd8488fc8f1698797..5cd85b67b3da3cabf98f8dbfb3348a51e3bd7e76 100644 (file)
@@ -436,12 +436,14 @@ mktemp_name (const char *t)
   return fn;
 }
 
+
 #if WINDOWS
 static char *
 mkdtemp (char *fn)
 {
   char *random_fn;
   char *tfn;
+
   while (1)
   {
     tfn = GNUNET_strdup (fn);
@@ -494,6 +496,38 @@ GNUNET_DISK_mkdtemp (const char *t)
 }
 
 
+/**
+ * Move a file out of the way (create a backup) by
+ * renaming it to "orig.NUM~" where NUM is the smallest
+ * number that is not used yet.
+ *
+ * @param fil name of the file to back up
+ */
+void
+GNUNET_DISK_file_backup (const char *fil)
+{
+  size_t slen;
+  char *target;
+  unsigned int num;
+
+  slen = strlen (fil) + 20;
+  target = GNUNET_malloc (slen);
+  num = 0;
+  do
+  {
+    GNUNET_snprintf (target, slen,
+                    "%s.%u~",
+                    fil,
+                    num++);
+  } while (0 == access (target, F_OK));
+  if (0 != rename (fil, target))
+    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
+                             "rename",
+                             fil);
+  GNUNET_free (target);  
+}
+
+
 /**
  * Create an (empty) temporary file on disk.  If the given name is not
  * an absolute path, the current 'TMPDIR' will be prepended.  In any case,
@@ -2129,7 +2163,8 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
   /* Default to error. */
   *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE;
 
-  HANDLE read_pipe = INVALID_HANDLE_VALUE, write_pipe = INVALID_HANDLE_VALUE;
+  HANDLE read_pipe;
+  HANDLE write_pipe;
 
   /* Ensure that there is enough pipe buffer space for atomic writes.  */
   if (psize < PIPE_BUF)