Flush before saving

This commit is contained in:
Wildan M 2025-11-03 21:47:30 +07:00
parent c1db92cb22
commit e4403cb3d9
2 changed files with 17 additions and 4 deletions

View File

@ -5,7 +5,7 @@ use cookbook::cook::cook_build::build;
use cookbook::cook::fetch::{fetch, fetch_offline};
use cookbook::cook::fs::{create_target_dir, run_command};
use cookbook::cook::package::package;
use cookbook::cook::pty::{PtyOut, UnixSlavePty, setup_pty};
use cookbook::cook::pty::{PtyOut, UnixSlavePty, flush_pty, setup_pty};
use cookbook::cook::tree::{display_tree_entry, format_size};
use cookbook::log_to_pty;
use cookbook::recipe::{BuildKind, CookRecipe};
@ -850,7 +850,7 @@ fn run_tui_cook(
.send(StatusUpdate::StartCook(name.clone()))
.unwrap();
let (mut stdout_writer, mut stderr_writer) = setup_logger(&cooker_status_tx, &name);
let logger = Some((&mut stdout_writer, &mut stderr_writer));
let mut logger = Some((&mut stdout_writer, &mut stderr_writer));
'again: loop {
let handler = handle_cook(
&recipe,
@ -863,6 +863,7 @@ fn run_tui_cook(
if let Err(err_ctx) = &handler {
log_to_pty!(&logger, "{:?}", err_ctx)
}
flush_pty(&mut logger);
let log_path = log_path.join(format!("{}.log", name.as_str()));
cooker_status_tx
.send(StatusUpdate::FlushLog(name.clone(), log_path))
@ -945,7 +946,7 @@ fn run_tui_cook(
.unwrap();
let (mut stdout_writer, mut stderr_writer) = setup_logger(&fetcher_status_tx, &name);
let logger = Some((&mut stdout_writer, &mut stderr_writer));
let mut logger = Some((&mut stdout_writer, &mut stderr_writer));
'again: loop {
let handler = handle_fetch(&recipe, &fetcher_config, &logger);
@ -956,6 +957,7 @@ fn run_tui_cook(
if let Err(err_ctx) = &handler {
log_to_pty!(&logger, "{:?}", err_ctx)
}
flush_pty(&mut logger);
let log_path = log_path.join(format!("{}.log", name.as_str()));
fetcher_status_tx
.send(StatusUpdate::FlushLog(name.clone(), log_path))

View File

@ -1,7 +1,7 @@
use anyhow::{Error, bail};
use filedescriptor::FileDescriptor;
use libc::{self, winsize};
use std::io::Read;
use std::io::{Read, Write};
use std::os::fd::FromRawFd;
use std::os::unix::io::AsRawFd;
use std::os::unix::process::CommandExt;
@ -56,6 +56,14 @@ pub fn setup_pty() -> (
(pty_reader, log_reader, pipes)
}
pub fn flush_pty(logger: &mut PtyOut) {
let Some((pty, file)) = logger else {
return;
};
let _ = pty.flush();
let _ = file.flush();
}
pub fn spawn_to_pipe(command: &mut Command, stdout_pipe: &PtyOut) -> Result<Child, Error> {
match stdout_pipe {
Some(stdout) => stdout.0.spawn_command(command.into()),
@ -318,6 +326,9 @@ impl UnixSlavePty {
fn spawn_command(&self, builder: &mut Command) -> Result<std::process::Child, Error> {
Ok(self.fd.spawn_command(builder)?)
}
fn flush(&self) -> Result<(), anyhow::Error> {
Ok(self.fd.as_file()?.flush()?)
}
}
impl UnixMasterPty {