]> Untitled Git - bdk/commitdiff
fix(store): replace `Path.exists` by `OpenOptions.create_new`
authornymius <155548262+nymius@users.noreply.github.com>
Tue, 12 Nov 2024 18:56:34 +0000 (15:56 -0300)
committernymius <155548262+nymius@users.noreply.github.com>
Wed, 5 Mar 2025 23:49:53 +0000 (10:49 +1100)
`Path.exists` is not safe against time-of-creation, time-of-use (TOCTOU)
bugs.

`OpenOptions.create_new` on the other hand is atomic, so not prone to
this kind of problems.

crates/file_store/src/store.rs

index ec86d4e5e6eb15b8c8e4ba511e40636e1cbd062a..27523f9d10433aaa5d781a722c4c57b2e2d01142 100644 (file)
@@ -41,16 +41,8 @@ where
     where
         P: AsRef<Path>,
     {
-        if file_path.as_ref().exists() {
-            // `io::Error` is used instead of a variant on `FileError` because there is already a
-            // nightly-only `File::create_new` method
-            return Err(FileError::Io(io::Error::new(
-                io::ErrorKind::Other,
-                "file already exists",
-            )));
-        }
         let mut f = OpenOptions::new()
-            .create(true)
+            .create_new(true)
             .read(true)
             .write(true)
             .truncate(true)