mirror of
https://github.com/ankitects/anki.git
synced 2025-09-23 16:26:40 -04:00
22 lines
718 B
Rust
22 lines
718 B
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
use std::path::Path;
|
|
|
|
use tempfile::NamedTempFile;
|
|
|
|
use crate::prelude::*;
|
|
|
|
pub(crate) fn atomic_rename(file: NamedTempFile, target: &Path) -> Result<()> {
|
|
file.as_file().sync_all()?;
|
|
file.persist(&target)
|
|
.map_err(|err| AnkiError::IoError(format!("write {target:?} failed: {err}")))?;
|
|
if !cfg!(windows) {
|
|
if let Some(parent) = target.parent() {
|
|
std::fs::File::open(parent)
|
|
.and_then(|file| file.sync_all())
|
|
.map_err(|err| AnkiError::IoError(format!("sync {parent:?} failed: {err}")))?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|