Fix output to special file (pipes etc.)

This commit is contained in:
ByteDream 2022-12-27 22:59:35 +01:00
parent c37e2495e1
commit 14f42833cb
3 changed files with 38 additions and 11 deletions

View file

@ -1,6 +1,6 @@
use log::debug;
use std::io::ErrorKind;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::{env, io};
use tempfile::{Builder, NamedTempFile};
@ -37,9 +37,8 @@ pub fn tempfile<S: AsRef<str>>(suffix: S) -> io::Result<NamedTempFile> {
/// Check if the given path exists and rename it until the new (renamed) file does not exist.
pub fn free_file(mut path: PathBuf) -> PathBuf {
// if path is not a file and not a dir it's probably a pipe on linux which reguarly is intended
// and thus does not need to be renamed. what it is on windows ¯\_(ツ)_/¯
if !path.is_file() && !path.is_dir() {
// if it's a special file does not rename it
if is_special_file(&path) {
return path;
}
@ -58,3 +57,9 @@ pub fn free_file(mut path: PathBuf) -> PathBuf {
}
path
}
/// Check if the given path is a special file. On Linux this is probably a pipe and on Windows
/// ¯\_(ツ)_/¯
pub fn is_special_file<P: AsRef<Path>>(path: P) -> bool {
path.as_ref().exists() && !path.as_ref().is_file() && !path.as_ref().is_dir()
}