]> mj.ucw.cz Git - libucw.git/commitdiff
Implemented bfix_tmp_file(), which turns a temporary file fastbuf
authorMartin Mares <mj@ucw.cz>
Sat, 25 Aug 2007 11:36:22 +0000 (13:36 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 25 Aug 2007 11:36:22 +0000 (13:36 +0200)
to a permanent one.

lib/fastbuf.h
lib/fb-temp.c

index 4e3bc5ca2afb721ed19635ed603dbf0d1796ac16..9878beeb1593580664f27b5be370e8b088bba60c 100644 (file)
@@ -113,6 +113,7 @@ void bfilesync(struct fastbuf *b);
 
 #define TEMP_FILE_NAME_LEN 256
 void temp_file_name(char *name);
+void bfix_tmp_file(struct fastbuf *fb, const char *name);
 
 /* Internal functions of some file back-ends */
 
index 65398c154eb7ef447d675f746f6c8a738126d9c5..54daad1b51270e9e447be81079b448fe14e0defb 100644 (file)
@@ -48,12 +48,22 @@ bopen_tmp(uns buflen)
   char buf[TEMP_FILE_NAME_LEN];
   struct fastbuf *f;
 
+  // FIXME: This needs cleanup and merging with other bopen functions.
   temp_file_name(buf);
   f = bopen(buf, O_RDWR | O_CREAT | O_TRUNC, buflen);
   bconfig(f, BCONFIG_IS_TEMP_FILE, 1);
   return f;
 }
 
+void bfix_tmp_file(struct fastbuf *fb, const char *name)
+{
+  int was_temp = bconfig(fb, BCONFIG_IS_TEMP_FILE, 0);
+  ASSERT(was_temp == 1);
+  if (rename(fb->name, name))
+    die("Cannot rename %s to %s: %m", fb->name, name);
+  bclose(fb);
+}
+
 #ifdef TEST
 
 #include "lib/getopt.h"