diff --git a/gvdb/gvdb-builder.c b/gvdb/gvdb-builder.c
index 90ea50b..cf5f1f4 100644
--- a/gvdb/gvdb-builder.c
+++ b/gvdb/gvdb-builder.c
@@ -21,6 +21,7 @@
 #include "gvdb-format.h"
 
 #include <glib.h>
+#include <glib/gstdio.h>
 #include <fcntl.h>
 #if !defined(G_OS_WIN32) || !defined(_MSC_VER)
 #include <unistd.h>
@@ -509,13 +510,22 @@ gvdb_table_write_contents (GHashTable   *table,
   gboolean status;
   FileBuilder *fb;
   GString *str;
+  GStatBuf buf;
+  gint stat_ret;
 
   fb = file_builder_new (byteswap);
   file_builder_add_hash (fb, table, &root);
   str = file_builder_serialise (fb, root);
 
+  stat_ret = g_stat (filename, &buf);
+
   status = g_file_set_contents (filename, str->str, str->len, error);
   g_string_free (str, TRUE);
 
+  if (stat_ret == 0)
+    g_chmod (filename, buf.st_mode);
+  else
+    g_chmod (filename, 0644);
+
   return status;
 }
