diff --git a/rust/hg-core/src/vfs.rs b/rust/hg-core/src/vfs.rs
index 09a36de53b6079184b1851f5f233c965d04013d4_cnVzdC9oZy1jb3JlL3NyYy92ZnMucnM=..a48c688d3e809ee7f4cc11319a68f97932a9c5db_cnVzdC9oZy1jb3JlL3NyYy92ZnMucnM= 100644
--- a/rust/hg-core/src/vfs.rs
+++ b/rust/hg-core/src/vfs.rs
@@ -178,7 +178,9 @@
         relative_path: impl AsRef<Path>,
         contents: &[u8],
     ) -> Result<(), HgError> {
-        let mut tmp = tempfile::NamedTempFile::new_in(&self.base)
+        let mut tmp = tempfile::Builder::new()
+            .permissions(std::fs::Permissions::from_mode(0o666))
+            .tempfile_in(&self.base)
             .when_writing_file(&self.base)?;
         tmp.write_all(contents)
             .and_then(|()| tmp.flush())
diff --git a/tests/test-dirstate.t b/tests/test-dirstate.t
index 09a36de53b6079184b1851f5f233c965d04013d4_dGVzdHMvdGVzdC1kaXJzdGF0ZS50..a48c688d3e809ee7f4cc11319a68f97932a9c5db_dGVzdHMvdGVzdC1kaXJzdGF0ZS50 100644
--- a/tests/test-dirstate.t
+++ b/tests/test-dirstate.t
@@ -64,6 +64,13 @@
   $ hg debugstate
   n 644          2 2031-01-01 12:00:00 a
 
+Check that .hg/dirstate permissions are correct
+(there was a bug where rust atomic replace would set permissions 0600,
+which is not what we want)
+
+  $ f --mode .hg/dirstate
+  .hg/dirstate: mode=644
+
 Test modulo storage/comparison of absurd dates:
 
 #if no-aix