[Opendnssec-commits] [svn.opendnssec.org/svn/dnssec] r6219 - trunk/OpenDNSSEC/signer/src/signer

matthijs at nlnetlabs.nl matthijs at nlnetlabs.nl
Tue Mar 20 16:26:10 CET 2012


Author: matthijs
Date: 2012-03-20 16:26:09 +0100 (Tue, 20 Mar 2012)
New Revision: 6219

Modified:
   trunk/OpenDNSSEC/signer/src/signer/zone.c
Log:
atomic backup file



Modified: trunk/OpenDNSSEC/signer/src/signer/zone.c
===================================================================
--- trunk/OpenDNSSEC/signer/src/signer/zone.c	2012-03-20 14:22:00 UTC (rev 6218)
+++ trunk/OpenDNSSEC/signer/src/signer/zone.c	2012-03-20 15:26:09 UTC (rev 6219)
@@ -961,8 +961,11 @@
 zone_backup2(zone_type* zone)
 {
     char* filename = NULL;
+    char* tmpfile = NULL;
     FILE* fd = NULL;
     task_type* task = NULL;
+    int ret = 0;
+    ods_status status = ODS_STATUS_OK;
 
     ods_log_assert(zone);
     ods_log_assert(zone->name);
@@ -970,9 +973,9 @@
     ods_log_assert(zone->signconf);
     ods_log_assert(zone->task);
 
+    tmpfile = ods_build_path(zone->name, ".backup2.tmp", 0);
     filename = ods_build_path(zone->name, ".backup2", 0);
-    fd = ods_fopen(filename, NULL, "w");
-    free((void*)filename);
+    fd = ods_fopen(tmpfile, NULL, "w");
 
     if (fd) {
         fprintf(fd, "%s\n", ODS_SE_FILE_MAGIC_V3);
@@ -1004,8 +1007,17 @@
         /** Done */
         fprintf(fd, "%s\n", ODS_SE_FILE_MAGIC_V3);
         ods_fclose(fd);
+        ret = rename(tmpfile, filename);
+        if (ret != 0) {
+            ods_log_error("[%s] unable to rename zone %s backup %s to %s: %s",
+                zone_str, zone->name, tmpfile, filename, strerror(errno));
+            status = ODS_STATUS_RENAME_ERR;
+        }
     } else {
-        return ODS_STATUS_FOPEN_ERR;
+        status = ODS_STATUS_FOPEN_ERR;
     }
-    return ODS_STATUS_OK;
+
+    free((void*) tmpfile);
+    free((void*) filename);
+    return status;
 }




More information about the Opendnssec-commits mailing list