Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_core/tests/file_save_atomic_mode.rs

1.7 KiB, 1 run

created by r4207234560:77739, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! `save_atomic_with` in `Keep` mode, through the public API: the writer's temporary is never
2//! wider than the file it replaces while the bytes go in, and the saved file keeps that file's
3//! mode. The temporary's mode at creation is tested beside `create_tmp` in `src/file.rs`.
4#![cfg(unix)]
5
6use oxedyne_fe2o3_core::{
7 prelude::*,
8 file::{
9 SaveMode,
10 save_atomic_with,
11 },
12};
13
14use std::{
15 fs,
16 io::Write,
17 os::unix::fs::PermissionsExt,
18 path::PathBuf,
19};
20
21
22fn scratch(label: &str) -> PathBuf {
23 std::env::temp_dir().join(fmt!("fe2o3_core_save_atomic_mode_{}_{}", std::process::id(), label))
24}
25
26fn bits_of(m: std::io::Result<fs::Metadata>) -> Outcome<u32> {
27 Ok(res!(m).permissions().mode() & 0o777)
28}
29
30#[test]
31fn keep_save_holds_the_old_mode_and_writes_into_no_wider_temporary() -> Outcome<()> {
32 for bits in [0o600u32, 0o640, 0o644] {
33 let path = scratch(&fmt!("{:o}", bits));
34 res!(fs::write(&path, b"old"));
35 res!(fs::set_permissions(&path, fs::Permissions::from_mode(bits)));
36 let mut seen = 0o777;
37 let saved = save_atomic_with(&path, SaveMode::Keep, |f| {
38 seen = res!(bits_of(f.metadata()));
39 res!(f.write_all(b"new bytes"));
40 Ok(())
41 });
42 let after = bits_of(fs::metadata(&path));
43 let text = fs::read(&path);
44 let _ = fs::remove_file(&path);
45 res!(saved);
46 // req! evaluates its arguments twice, so each is bound first.
47 let (after, text) = (res!(after), res!(text));
48 req!(seen, bits, "temporary mode while written, file at {:04o}", bits);
49 req!(after, bits, "saved file's mode");
50 req!(text, b"new bytes".to_vec());
51 }
52 Ok(())
53}