Function atomic_write_bytes

Source
pub fn atomic_write_bytes(path: &Path, bytes: &[u8]) -> Result<()>
Expand description

Writes bytes to <file-name>.tmp, syncs them, then renames over path.

Missing parent directories are created. The temp name is deterministic, so concurrent writers to the same destination race with one another. A failed operation can leave the temp file behind. File contents are synced before rename; opening and syncing the parent directory is best effort and its errors are ignored. Destination replacement and rename atomicity follow the host filesystem and platform; permissions of an older destination are not preserved on the newly created file.

§Errors

Returns AppStorageError::InvalidDocument if the destination has no UTF-8 file name, or AppStorageError::Io if parent creation, temp-file creation/write/sync, or rename fails.

§Examples

use ailloli_ui_app_storage::atomic_write_bytes;
let path = std::env::temp_dir().join(format!("ailloli-doc-bytes-{}.bin", std::process::id()));
atomic_write_bytes(&path, b"saved")?;
assert_eq!(std::fs::read(&path)?, b"saved");
std::fs::remove_file(path)?;