Only rewrite changed files

korrat
Jun 23, 2023, 5:55 PM
2PCWN3KWPCSPLKGKZ553CBIYDEEBI6WDCKVRE2ZH2Z74LRTFD25AC

Dependencies

  • [2] R524JUUE Implement metadata & price directives
  • [3] SJ6AFVZL remove const configuration in favor of runtime config
  • [4] MG46NYAC enable deserialization of configuration for common utilities
  • [5] 2NYDNXH7 Use OS dependent path separator when constructing paths in tree writer
  • [6] D6UTHZA4 add a simple writer for saving a set of directives to a tree of files
  • [*] 2JBFREZG enable additional warnings

Change contents

  • edit in common/beancount-tree-writer/src/lib.rs at line 8
    [8.16504]
    [3.0]
    use std::io::Read;
    use std::io::Write;
  • replacement in common/beancount-tree-writer/src/lib.rs at line 86
    [3.770][2.13916:13941]()
    file: &Utf8Path,
    [3.770]
    [3.2328]
    path: &Utf8Path,
  • replacement in common/beancount-tree-writer/src/lib.rs at line 100
    [3.979][2.13942:13971](),[2.13971][3.2544:2575](),[3.2544][3.2544:2575](),[3.2575][3.1040:1139](),[3.234][3.2667:2682](),[3.1139][3.2667:2682](),[3.2667][3.2667:2682](),[3.2682][2.13972:14023](),[2.14023][3.2740:2876](),[3.2740][3.2740:2876](),[3.2876][3.2876:2877](),[3.2877][3.291:323](),[3.323][3.2928:2934](),[3.2928][3.2928:2934]()
    self.open_file(file)
    .and_then(|file| {
    PrettyPrinter::buffered(pretty_printer_config, file).print_directives(&directives)
    })
    .context(PrettyPrintingSnafu { file })
    }
    fn open_file(&self, file_path: &Utf8Path) -> io::Result<File> {
    self.ensure_directory(file_path.parent().unwrap())?;
    File::create(file_path)
    }
    [3.979]
    [3.2934]
    let mut buffer = Vec::new();
    PrettyPrinter::unbuffered(pretty_printer_config, &mut buffer)
    .print_directives(&directives)
    .and_then(|_| update_file(path, &buffer))
    .context(PrettyPrintingSnafu { file: path })?;
  • replacement in common/beancount-tree-writer/src/lib.rs at line 106
    [3.2935][3.2935:3046]()
    fn ensure_directory(&self, directory: &Utf8Path) -> io::Result<()> {
    fs::create_dir_all(directory)
    [3.2935]
    [3.3046]
    Ok(())
  • edit in common/beancount-tree-writer/src/lib.rs at line 178
    [8.16866]
    fn ensure_directory(directory: &Utf8Path) -> io::Result<()> {
    fs::create_dir_all(directory)
    }
    fn open_file(file_path: &Utf8Path) -> io::Result<File> {
    ensure_directory(file_path.parent().unwrap())?;
    File::options()
    .create(true)
    .read(true)
    .write(true)
    .open(file_path)
    }
    fn update_file(path: &Utf8Path, buffer: &[u8]) -> io::Result<()> {
    let mut file = open_file(path)?;
    let size =
    usize::try_from(file.metadata()?.len()).expect("file should be smaller than usize::MAX");
    if buffer.len() == size {
    let mut current_contents = Vec::with_capacity(size);
    file.read_to_end(&mut current_contents)?;
    if current_contents == buffer {
    tracing::debug!(%path, "skipping write of unchanged file");
    return Ok(());
    }
    }
    // File changed in the import process
    file.write_all(buffer)?;
    file.set_len(u64::try_from(buffer.len()).expect("should always work"))
    }