]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
CodeWriter: Do not replace .vapi if unchanged
authorRyan Lortie <desrt@desrt.ca>
Sat, 28 Aug 2010 14:30:05 +0000 (16:30 +0200)
committerJürg Billeter <j@bitron.ch>
Fri, 17 Sep 2010 23:00:14 +0000 (01:00 +0200)
Steal some logic from the CCodeWriter to avoid replacing .vapi output if
nothing has changed.

vala/valacodewriter.vala

index 056f20d804f0468ebae5a7dd45cc3cefab8a8864..cc8ad049d774264c3a84cb9edf74468b33ba3e00 100644 (file)
@@ -65,9 +65,16 @@ public class Vala.CodeWriter : CodeVisitor {
         * @param filename a relative or absolute filename
         */
        public void write_file (CodeContext context, string filename) {
+               var file_exists = FileUtils.test (filename, FileTest.EXISTS);
+               var temp_filename = filename + ".valatmp";
                this.context = context;
-       
-               stream = FileStream.open (filename, "w");
+
+               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;
@@ -87,6 +94,32 @@ public class Vala.CodeWriter : CodeVisitor {
                current_scope = null;
 
                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);
+                       }
+               }
+
        }
 
        public override void visit_using_directive (UsingDirective ns) {