Fix panic in atomic-file-install (#1290)

Do not panic if path does not have a parent, instead return an error.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2023-08-13 17:14:46 +10:00 committed by GitHub
parent 2375ba48b6
commit 6c801a97ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,8 +14,17 @@ use std::os::unix::fs::symlink as symlink_file_inner;
#[cfg(windows)] #[cfg(windows)]
use std::os::windows::fs::symlink_file as symlink_file_inner; use std::os::windows::fs::symlink_file as symlink_file_inner;
fn parent(p: &Path) -> io::Result<&Path> {
p.parent().ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("`{}` does not have a parent", p.display()),
)
})
}
fn copy_to_tempfile(src: &Path, dst: &Path) -> io::Result<NamedTempFile> { fn copy_to_tempfile(src: &Path, dst: &Path) -> io::Result<NamedTempFile> {
let parent = dst.parent().unwrap(); let parent = parent(dst)?;
debug!("Creating named tempfile at '{}'", parent.display()); debug!("Creating named tempfile at '{}'", parent.display());
let tempfile = NamedTempFile::new_in(parent)?; let tempfile = NamedTempFile::new_in(parent)?;
@ -125,7 +134,7 @@ pub fn atomic_symlink_file_noclobber(dest: &Path, link: &Path) -> io::Result<()>
/// ///
/// This is a blocking function, must be called in `block_in_place` mode. /// This is a blocking function, must be called in `block_in_place` mode.
pub fn atomic_symlink_file(dest: &Path, link: &Path) -> io::Result<()> { pub fn atomic_symlink_file(dest: &Path, link: &Path) -> io::Result<()> {
let parent = link.parent().unwrap(); let parent = parent(link)?;
debug!("Creating tempPath at '{}'", parent.display()); debug!("Creating tempPath at '{}'", parent.display());
let temp_path = NamedTempFile::new_in(parent)?.into_temp_path(); let temp_path = NamedTempFile::new_in(parent)?.into_temp_path();