ensure that dir of session log file exists

This commit is contained in:
Amnay Mokhtari 2026-01-04 10:02:50 +01:00
commit b8e7fbd4e6

View file

@ -501,14 +501,25 @@ fn executeCmd(global_log_file: *LogFile, allocator: std.mem.Allocator, shell: []
return std.posix.execveZ(shell_z, &args, std.c.environ);
}
fn redirectStandardStreams(global_log_file: *LogFile, session_log: []const u8, create: bool) !std.fs.File {
const log_file = if (create) (std.fs.cwd().createFile(session_log, .{ .mode = 0o666 }) catch |err| {
fn createSessionLogFile(global_log_file: *LogFile, session_log: []const u8) !std.fs.File {
// Create parent directories for the session log file if they don't exist
if (std.fs.path.dirname(session_log)) |session_log_dir| {
try std.fs.cwd().makePath(session_log_dir);
}
return std.fs.cwd().createFile(session_log, .{ .mode = 0o666 }) catch |err| {
try global_log_file.file_writer.interface.print("failed to create new session log file: {s}\n", .{@errorName(err)});
return err;
}) else (std.fs.cwd().openFile(session_log, .{ .mode = .read_write }) catch |err| {
try global_log_file.file_writer.interface.print("failed to open existing session log file: {s}\n", .{@errorName(err)});
return err;
});
};
}
fn redirectStandardStreams(global_log_file: *LogFile, session_log: []const u8, create: bool) !std.fs.File {
const log_file = if (create)
(try createSessionLogFile(global_log_file, session_log))
else
(std.fs.cwd().openFile(session_log, .{ .mode = .read_write }) catch |err| {
try global_log_file.file_writer.interface.print("failed to open existing session log file: {s}\n", .{@errorName(err)});
return err;
});
try std.posix.dup2(std.posix.STDOUT_FILENO, std.posix.STDERR_FILENO);
try std.posix.dup2(log_file.handle, std.posix.STDOUT_FILENO);