diff rust/hg-core/src/vfs.rs @ 52294:645d247d4c75

rust-vfs: rename `open` to `open_write` and `open_read` to `open` `open` being read *and* write is surprising because it differs from the Rust stdlib where `std::fs::File::open` is read-only by default. More importantly, writing is more dangerous than reading, so let's make it more explicit.
author Rapha?l Gom?s <rgomes@octobus.net>
date Tue, 29 Oct 2024 12:03:55 +0100
parents 85bff84f0ad5
children a876ab6c3fd5
line wrap: on
line diff
--- a/rust/hg-core/src/vfs.rs	Tue Oct 29 11:41:27 2024 +0100
+++ b/rust/hg-core/src/vfs.rs	Tue Oct 29 12:03:55 2024 +0100
@@ -518,13 +518,12 @@
 /// Abstracts over the VFS to allow for different implementations of the
 /// filesystem layer (like passing one from Python).
 pub trait Vfs: Sync + Send + DynClone {
-    // TODO make `open` readonly and make `open_read` an `open_write`
+    /// Open a [`VfsFile::Normal`] for reading the file at `filename`,
+    /// relative to this VFS's root.
+    fn open(&self, filename: &Path) -> Result<VfsFile, HgError>;
     /// Open a [`VfsFile::Normal`] for writing and reading the file at
     /// `filename`, relative to this VFS's root.
-    fn open(&self, filename: &Path) -> Result<VfsFile, HgError>;
-    /// Open a [`VfsFile::Normal`] for reading the file at `filename`,
-    /// relative to this VFS's root.
-    fn open_read(&self, filename: &Path) -> Result<VfsFile, HgError>;
+    fn open_write(&self, filename: &Path) -> Result<VfsFile, HgError>;
     /// Open a [`VfsFile::Normal`] for reading and writing the file at
     /// `filename`, relative to this VFS's root. This file will be checked
     /// for an ambiguous mtime on [`drop`]. See [`is_filetime_ambiguous`].
@@ -580,6 +579,15 @@
 /// users of `hg-core` start doing more on their own, like writing to files.
 impl Vfs for VfsImpl {
     fn open(&self, filename: &Path) -> Result<VfsFile, HgError> {
+        // TODO auditpath
+        let path = self.base.join(filename);
+        Ok(VfsFile::normal(
+            std::fs::File::open(&path).when_reading_file(&path)?,
+            filename.to_owned(),
+        ))
+    }
+
+    fn open_write(&self, filename: &Path) -> Result<VfsFile, HgError> {
         if self.readonly {
             return Err(HgError::abort(
                 "write access in a readonly vfs",
@@ -603,15 +611,6 @@
         ))
     }
 
-    fn open_read(&self, filename: &Path) -> Result<VfsFile, HgError> {
-        // TODO auditpath
-        let path = self.base.join(filename);
-        Ok(VfsFile::normal(
-            std::fs::File::open(&path).when_reading_file(&path)?,
-            filename.to_owned(),
-        ))
-    }
-
     fn open_check_ambig(&self, filename: &Path) -> Result<VfsFile, HgError> {
         if self.readonly {
             return Err(HgError::abort(
@@ -843,15 +842,15 @@
 impl Vfs for FnCacheVfs {
     fn open(&self, filename: &Path) -> Result<VfsFile, HgError> {
         let encoded = path_encode(&get_bytes_from_path(filename));
-        let encoded_path = get_path_from_bytes(&encoded);
-        self.maybe_add_to_fncache(filename, encoded_path)?;
-        self.inner.open(encoded_path)
+        let filename = get_path_from_bytes(&encoded);
+        self.inner.open(filename)
     }
 
-    fn open_read(&self, filename: &Path) -> Result<VfsFile, HgError> {
+    fn open_write(&self, filename: &Path) -> Result<VfsFile, HgError> {
         let encoded = path_encode(&get_bytes_from_path(filename));
-        let filename = get_path_from_bytes(&encoded);
-        self.inner.open_read(filename)
+        let encoded_path = get_path_from_bytes(&encoded);
+        self.maybe_add_to_fncache(filename, encoded_path)?;
+        self.inner.open_write(encoded_path)
     }
 
     fn open_check_ambig(&self, filename: &Path) -> Result<VfsFile, HgError> {