1use std::fs::{File, OpenOptions};
2use std::path::{Path, PathBuf};
3use std::process::Command;
4use std::thread;
5use std::time::Duration;
6
7use anyhow::{Context, Result};
8use thiserror::Error;
9
10use duct::cmd;
11use tempfile::{Builder, NamedTempFile};
12
13#[cfg(not(windows))]
14pub fn command_exists(cmd: &str) -> bool {
15 let output = cmd!("which", cmd).read().unwrap_or_else(|_| String::new());
16
17 !output.trim().is_empty()
18}
19
20pub fn do_sync() {
21 if let Err(e) = Command::new("sync").status() {
22 eprintln!("o-o: warning: failed to execute sync command: {}", e);
23 }
24}
25
26pub fn open_file_with_mode(path: &str) -> Result<File> {
27 let mut options = OpenOptions::new();
28 options.write(true).create(true);
29
30 let (mode, clean_path) = if let Some(s) = path.strip_prefix('+') {
31 (true, s)
32 } else {
33 (false, path)
34 };
35
36 if mode {
37 options.append(true);
38 } else {
39 options.truncate(true);
40 }
41
42 let file = options
43 .open(clean_path)
44 .with_context(|| format!("Failed to open file: {}", clean_path))?;
45
46 Ok(file)
47}
48
49pub fn create_temp_file(tempdir_placeholder: &Option<&str>) -> Result<PathBuf> {
50 let temp_file = if let Some(dir) = tempdir_placeholder {
51 Builder::new().prefix("tempfile").tempfile_in(dir)?
52 } else {
53 NamedTempFile::new()?
54 };
55
56 Ok(temp_file.path().to_path_buf())
57}
58
59#[derive(Error, Debug)]
60pub enum FileIOError {
61 #[error("Write failed for file '{0}' after {1} miliseconds")]
62 WriteFailedError(String, u64),
63}
64
65pub fn wait_for_file_existence_with_mode(file_path: &str, timeout: u64) -> Result<(), FileIOError> {
66 let (_mode, clean_path) = if let Some(s) = file_path.strip_prefix('+') {
67 (true, s)
68 } else {
69 (false, file_path)
70 };
71
72 let start = std::time::Instant::now();
73 let path = Path::new(clean_path);
74
75 while !path.exists() {
76 let elapsed = start.elapsed().as_millis() as u64;
77 if elapsed >= timeout {
78 return Err(FileIOError::WriteFailedError(
79 path.to_string_lossy().to_string(),
80 timeout,
81 ));
82 }
83 thread::sleep(Duration::from_millis(100));
84 }
85
86 Ok(())
87}