fix: don't remove file in get_writer; fail if target is symlink

This commit is contained in:
Gabriele Musco 2024-09-28 17:47:14 +02:00
commit 91972e0300
No known key found for this signature in database
GPG key ID: 1068D795C80E51DE

View file

@ -1,10 +1,11 @@
use crate::{async_process::async_process, profile::Profile}; use crate::{async_process::async_process, profile::Profile};
use anyhow::bail;
use nix::{ use nix::{
errno::Errno, errno::Errno,
sys::statvfs::{statvfs, FsFlags}, sys::statvfs::{statvfs, FsFlags},
}; };
use std::{ use std::{
fs::{self, copy, create_dir_all, remove_dir_all, remove_file, File, OpenOptions}, fs::{self, copy, create_dir_all, remove_dir_all, File, OpenOptions},
io::{BufReader, BufWriter}, io::{BufReader, BufWriter},
path::Path, path::Path,
}; };
@ -15,8 +16,11 @@ pub fn get_writer(path: &Path) -> anyhow::Result<BufWriter<std::fs::File>> {
create_dir_all(parent)?; create_dir_all(parent)?;
} }
}; };
if path.is_file() || path.is_symlink() { if path.is_symlink() {
remove_file(path)?; bail!(
"Provided path '{}' exists already, but it is a symlink and not a file",
path.to_string_lossy()
);
} }
let file = OpenOptions::new() let file = OpenOptions::new()
.write(true) .write(true)