Backup old map.
This commit is contained in:
24
src/io.rs
24
src/io.rs
@@ -1,7 +1,7 @@
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{self, Seek, SeekFrom};
|
||||
use std::ops::{Index, Range, RangeFrom};
|
||||
use std::slice::SliceIndex;
|
||||
use std::ops::Index;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::cli::CONFIG;
|
||||
|
||||
@@ -66,10 +66,21 @@ pub fn load_map_read() -> std::io::Result<File> {
|
||||
}
|
||||
|
||||
pub fn load_map_write() -> anyhow::Result<File> {
|
||||
// Attempt to check if a map exists on the disk.
|
||||
// If so, make a backup of it.
|
||||
//
|
||||
// This should be recoverable by just skipping over this error and logging a warning,
|
||||
// but for now it will be an error condition.
|
||||
if std::fs::exists(crate::path::MAP_PATH.clone())
|
||||
.context("Could not check if map exists in fs to make a backup.")?
|
||||
{
|
||||
backup(crate::path::MAP_PATH.clone())?;
|
||||
}
|
||||
|
||||
OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true) // Wipe old map. Should really make a backup first.
|
||||
.truncate(true) // Wipe old map, in case we skip over backing up the old one.
|
||||
.open(crate::path::MAP_PATH.clone())
|
||||
.with_context(|| {
|
||||
format!(
|
||||
@@ -79,6 +90,13 @@ pub fn load_map_write() -> anyhow::Result<File> {
|
||||
})
|
||||
}
|
||||
|
||||
fn backup<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
|
||||
std::fs::rename(
|
||||
&path,
|
||||
format!("{}.bak", path.as_ref().to_path_buf().display()),
|
||||
)
|
||||
}
|
||||
|
||||
#[repr(C, align(512))]
|
||||
pub struct DirectIOBuffer(pub [u8; crate::MAX_BUFFER_SIZE]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user