]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
girwriter: Only replace existing GIR files if they changed
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 1 May 2019 10:51:45 +0000 (12:51 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 6 Aug 2019 11:39:03 +0000 (13:39 +0200)
Same how it is done in CodeWriter.write_file()

codegen/valagirwriter.vala

index 138d3593b89969b60def087351062c1303a0996a..ebb028b0e3d00c109138ec4c7ab2a3e1d3bdeaea 100644 (file)
@@ -173,7 +173,15 @@ public class Vala.GIRWriter : CodeVisitor {
                buffer.append_printf ("</repository>\n");
 
                string filename = "%s%c%s".printf (directory, Path.DIR_SEPARATOR, gir_filename);
-               stream = FileStream.open (filename, "w");
+               var file_exists = FileUtils.test (filename, FileTest.EXISTS);
+               var temp_filename = "%s.valatmp".printf (filename);
+
+               if (file_exists) {
+                       stream = FileStream.open (temp_filename, "w");
+               } else {
+                       stream = FileStream.open (filename, "w");
+               }
+
                if (stream == null) {
                        Report.error (null, "unable to open `%s' for writing".printf (filename));
                        return;
@@ -199,6 +207,31 @@ public class Vala.GIRWriter : CodeVisitor {
                stream.puts (buffer.str);
                stream = null;
 
+               if (file_exists) {
+                       var changed = true;
+
+                       try {
+                               var old_file = new MappedFile (filename, false);
+                               var new_file = new MappedFile (temp_filename, false);
+                               var len = old_file.get_length ();
+                               if (len == new_file.get_length ()) {
+                                       if (Memory.cmp (old_file.get_contents (), new_file.get_contents (), len) == 0) {
+                                               changed = false;
+                                       }
+                               }
+                               old_file = null;
+                               new_file = null;
+                       } catch (FileError e) {
+                               // assume changed if mmap comparison doesn't work
+                       }
+
+                       if (changed) {
+                               FileUtils.rename (temp_filename, filename);
+                       } else {
+                               FileUtils.unlink (temp_filename);
+                       }
+               }
+
                foreach (var ns in unannotated_namespaces) {
                        if (!our_namespaces.contains(ns)) {
                                Report.warning (ns.source_reference, "Namespace %s does not have a GIR namespace and version annotation".printf (ns.name));