LCOV - code coverage report
Current view: top level - libs/utils/src - crashsafe.rs (source / functions) Coverage Total Hit
Test: 32f4a56327bc9da697706839ed4836b2a00a408f.info Lines: 91.9 % 161 148
Test Date: 2024-02-07 07:37:29 Functions: 68.3 % 41 28

            Line data    Source code
       1              : use std::{
       2              :     borrow::Cow,
       3              :     fs::{self, File},
       4              :     io,
       5              : };
       6              : 
       7              : use camino::{Utf8Path, Utf8PathBuf};
       8              : 
       9              : /// Similar to [`std::fs::create_dir`], except we fsync the
      10              : /// created directory and its parent.
      11         1150 : pub fn create_dir(path: impl AsRef<Utf8Path>) -> io::Result<()> {
      12         1150 :     let path = path.as_ref();
      13         1150 : 
      14         1150 :     fs::create_dir(path)?;
      15         1146 :     fsync_file_and_parent(path)?;
      16         1146 :     Ok(())
      17         1150 : }
      18              : 
      19              : /// Similar to [`std::fs::create_dir_all`], except we fsync all
      20              : /// newly created directories and the pre-existing parent.
      21          397 : pub fn create_dir_all(path: impl AsRef<Utf8Path>) -> io::Result<()> {
      22          397 :     let mut path = path.as_ref();
      23          397 : 
      24          397 :     let mut dirs_to_create = Vec::new();
      25              : 
      26              :     // Figure out which directories we need to create.
      27              :     loop {
      28          790 :         match path.metadata() {
      29          395 :             Ok(metadata) if metadata.is_dir() => break,
      30              :             Ok(_) => {
      31            2 :                 return Err(io::Error::new(
      32            2 :                     io::ErrorKind::AlreadyExists,
      33            2 :                     format!("non-directory found in path: {path}"),
      34            2 :                 ));
      35              :             }
      36          395 :             Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
      37            2 :             Err(e) => return Err(e),
      38              :         }
      39              : 
      40          393 :         dirs_to_create.push(path);
      41          393 : 
      42          393 :         match path.parent() {
      43          393 :             Some(parent) => path = parent,
      44              :             None => {
      45            0 :                 return Err(io::Error::new(
      46            0 :                     io::ErrorKind::InvalidInput,
      47            0 :                     format!("can't find parent of path '{path}'"),
      48            0 :                 ));
      49              :             }
      50              :         }
      51              :     }
      52              : 
      53              :     // Create directories from parent to child.
      54          393 :     for &path in dirs_to_create.iter().rev() {
      55          393 :         fs::create_dir(path)?;
      56              :     }
      57              : 
      58              :     // Fsync the created directories from child to parent.
      59          393 :     for &path in dirs_to_create.iter() {
      60          393 :         fsync(path)?;
      61              :     }
      62              : 
      63              :     // If we created any new directories, fsync the parent.
      64          393 :     if !dirs_to_create.is_empty() {
      65          391 :         fsync(path)?;
      66            2 :     }
      67              : 
      68          393 :     Ok(())
      69          397 : }
      70              : 
      71              : /// Adds a suffix to the file(directory) name, either appending the suffix to the end of its extension,
      72              : /// or if there's no extension, creates one and puts a suffix there.
      73        35261 : pub fn path_with_suffix_extension(
      74        35261 :     original_path: impl AsRef<Utf8Path>,
      75        35261 :     suffix: &str,
      76        35261 : ) -> Utf8PathBuf {
      77        35261 :     let new_extension = match original_path.as_ref().extension() {
      78         4742 :         Some(extension) => Cow::Owned(format!("{extension}.{suffix}")),
      79        30519 :         None => Cow::Borrowed(suffix),
      80              :     };
      81        35261 :     original_path.as_ref().with_extension(new_extension)
      82        35261 : }
      83              : 
      84         3520 : pub fn fsync_file_and_parent(file_path: &Utf8Path) -> io::Result<()> {
      85         3520 :     let parent = file_path.parent().ok_or_else(|| {
      86            0 :         io::Error::new(
      87            0 :             io::ErrorKind::Other,
      88            0 :             format!("File {file_path:?} has no parent"),
      89            0 :         )
      90         3520 :     })?;
      91              : 
      92         3520 :     fsync(file_path)?;
      93         3520 :     fsync(parent)?;
      94         3520 :     Ok(())
      95         3520 : }
      96              : 
      97         8985 : pub fn fsync(path: &Utf8Path) -> io::Result<()> {
      98         8985 :     File::open(path)
      99         8985 :         .map_err(|e| io::Error::new(e.kind(), format!("Failed to open the file {path:?}: {e}")))
     100         8985 :         .and_then(|file| {
     101         8985 :             file.sync_all().map_err(|e| {
     102            0 :                 io::Error::new(
     103            0 :                     e.kind(),
     104            0 :                     format!("Failed to sync file {path:?} data and metadata: {e}"),
     105            0 :                 )
     106         8985 :             })
     107         8985 :         })
     108         8985 :         .map_err(|e| io::Error::new(e.kind(), format!("Failed to fsync file {path:?}: {e}")))
     109         8985 : }
     110              : 
     111        11290 : pub async fn fsync_async(path: impl AsRef<Utf8Path>) -> Result<(), std::io::Error> {
     112        11290 :     tokio::fs::File::open(path.as_ref()).await?.sync_all().await
     113        11289 : }
     114              : 
     115        18759 : pub async fn fsync_async_opt(
     116        18759 :     path: impl AsRef<Utf8Path>,
     117        18759 :     do_fsync: bool,
     118        18759 : ) -> Result<(), std::io::Error> {
     119        18759 :     if do_fsync {
     120           23 :         fsync_async(path.as_ref()).await?;
     121        18747 :     }
     122        18759 :     Ok(())
     123        18759 : }
     124              : 
     125              : /// Like postgres' durable_rename, renames file issuing fsyncs do make it
     126              : /// durable. After return, file and rename are guaranteed to be persisted.
     127              : ///
     128              : /// Unlike postgres, it only does fsyncs to 1) file to be renamed to make
     129              : /// contents durable; 2) its directory entry to make rename durable 3) again to
     130              : /// already renamed file, which is not required by standards but postgres does
     131              : /// it, let's stick to that. Postgres additionally fsyncs newpath *before*
     132              : /// rename if it exists to ensure that at least one of the files survives, but
     133              : /// current callers don't need that.
     134              : ///
     135              : /// virtual_file.rs has similar code, but it doesn't use vfs.
     136              : ///
     137              : /// Useful links: <https://lwn.net/Articles/457667/>
     138              : /// <https://www.postgresql.org/message-id/flat/56583BDD.9060302%402ndquadrant.com>
     139              : /// <https://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/>
     140         6253 : pub async fn durable_rename(
     141         6253 :     old_path: impl AsRef<Utf8Path>,
     142         6253 :     new_path: impl AsRef<Utf8Path>,
     143         6253 :     do_fsync: bool,
     144         6253 : ) -> io::Result<()> {
     145         6253 :     // first fsync the file
     146         6253 :     fsync_async_opt(old_path.as_ref(), do_fsync).await?;
     147              : 
     148              :     // Time to do the real deal.
     149         6253 :     tokio::fs::rename(old_path.as_ref(), new_path.as_ref()).await?;
     150              : 
     151              :     // Postgres'ish fsync of renamed file.
     152         6253 :     fsync_async_opt(new_path.as_ref(), do_fsync).await?;
     153              : 
     154              :     // Now fsync the parent
     155         6253 :     let parent = match new_path.as_ref().parent() {
     156         6253 :         Some(p) => p,
     157            0 :         None => Utf8Path::new("./"), // assume current dir if there is no parent
     158              :     };
     159         6253 :     fsync_async_opt(parent, do_fsync).await?;
     160              : 
     161         6253 :     Ok(())
     162         6253 : }
     163              : 
     164              : #[cfg(test)]
     165              : mod tests {
     166              : 
     167              :     use super::*;
     168              : 
     169            2 :     #[test]
     170            2 :     fn test_create_dir_fsyncd() {
     171            2 :         let dir = camino_tempfile::tempdir().unwrap();
     172            2 : 
     173            2 :         let existing_dir_path = dir.path();
     174            2 :         let err = create_dir(existing_dir_path).unwrap_err();
     175            2 :         assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);
     176              : 
     177            2 :         let child_dir = existing_dir_path.join("child");
     178            2 :         create_dir(child_dir).unwrap();
     179            2 : 
     180            2 :         let nested_child_dir = existing_dir_path.join("child1").join("child2");
     181            2 :         let err = create_dir(nested_child_dir).unwrap_err();
     182            2 :         assert_eq!(err.kind(), io::ErrorKind::NotFound);
     183            2 :     }
     184              : 
     185            2 :     #[test]
     186            2 :     fn test_create_dir_all_fsyncd() {
     187            2 :         let dir = camino_tempfile::tempdir().unwrap();
     188            2 : 
     189            2 :         let existing_dir_path = dir.path();
     190            2 :         create_dir_all(existing_dir_path).unwrap();
     191            2 : 
     192            2 :         let child_dir = existing_dir_path.join("child");
     193            2 :         assert!(!child_dir.exists());
     194            2 :         create_dir_all(&child_dir).unwrap();
     195            2 :         assert!(child_dir.exists());
     196              : 
     197            2 :         let nested_child_dir = existing_dir_path.join("child1").join("child2");
     198            2 :         assert!(!nested_child_dir.exists());
     199            2 :         create_dir_all(&nested_child_dir).unwrap();
     200            2 :         assert!(nested_child_dir.exists());
     201              : 
     202            2 :         let file_path = existing_dir_path.join("file");
     203            2 :         std::fs::write(&file_path, b"").unwrap();
     204            2 : 
     205            2 :         let err = create_dir_all(&file_path).unwrap_err();
     206            2 :         assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);
     207              : 
     208            2 :         let invalid_dir_path = file_path.join("folder");
     209            2 :         create_dir_all(invalid_dir_path).unwrap_err();
     210            2 :     }
     211              : 
     212            2 :     #[test]
     213            2 :     fn test_path_with_suffix_extension() {
     214            2 :         let p = Utf8PathBuf::from("/foo/bar");
     215            2 :         assert_eq!(
     216            2 :             &path_with_suffix_extension(p, "temp").to_string(),
     217            2 :             "/foo/bar.temp"
     218            2 :         );
     219            2 :         let p = Utf8PathBuf::from("/foo/bar");
     220            2 :         assert_eq!(
     221            2 :             &path_with_suffix_extension(p, "temp.temp").to_string(),
     222            2 :             "/foo/bar.temp.temp"
     223            2 :         );
     224            2 :         let p = Utf8PathBuf::from("/foo/bar.baz");
     225            2 :         assert_eq!(
     226            2 :             &path_with_suffix_extension(p, "temp.temp").to_string(),
     227            2 :             "/foo/bar.baz.temp.temp"
     228            2 :         );
     229            2 :         let p = Utf8PathBuf::from("/foo/bar.baz");
     230            2 :         assert_eq!(
     231            2 :             &path_with_suffix_extension(p, ".temp").to_string(),
     232            2 :             "/foo/bar.baz..temp"
     233            2 :         );
     234            2 :         let p = Utf8PathBuf::from("/foo/bar/dir/");
     235            2 :         assert_eq!(
     236            2 :             &path_with_suffix_extension(p, ".temp").to_string(),
     237            2 :             "/foo/bar/dir..temp"
     238            2 :         );
     239            2 :     }
     240              : }
        

Generated by: LCOV version 2.1-beta