Log more history_file errors, and add more context

See #10300
This commit is contained in:
Johannes Altmanninger 2024-10-09 12:31:18 +02:00
parent 29a01eb3cf
commit 13e5d8097c
2 changed files with 32 additions and 9 deletions

View file

@ -16,7 +16,10 @@
//! 5. The chaos_mode boolean can be set to true to do things like lower buffer sizes which can //! 5. The chaos_mode boolean can be set to true to do things like lower buffer sizes which can
//! trigger race conditions. This is useful for testing. //! trigger race conditions. This is useful for testing.
use crate::{common::cstr2wcstring, env::EnvVar, wcstringutil::trim}; use crate::{
common::cstr2wcstring, env::EnvVar, wcstringutil::trim,
wutil::fileid::file_id_for_path_or_error,
};
use std::{ use std::{
borrow::Cow, borrow::Cow,
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
@ -621,10 +624,10 @@ impl HistoryImpl {
} }
} }
if let Some(err) = err { if let Some(err) = err {
FLOGF!( FLOG!(
history_file, history_file,
"Error %d when writing to temporary history file", "Error writing to temporary history file:",
err.raw_os_error().unwrap_or_default() err
); );
false false
@ -670,6 +673,9 @@ impl HistoryImpl {
OFlag::O_RDONLY | OFlag::O_CREAT, OFlag::O_RDONLY | OFlag::O_CREAT,
HISTORY_FILE_MODE, HISTORY_FILE_MODE,
); );
if let Err(err) = target_file_before {
FLOG!(history_file, "Error opening history file:", err);
}
let orig_file_id = target_file_before let orig_file_id = target_file_before
.as_ref() .as_ref()
@ -697,7 +703,15 @@ impl HistoryImpl {
unsafe { unsafe {
Self::maybe_lock_file(target_fd_after, LOCK_EX); Self::maybe_lock_file(target_fd_after, LOCK_EX);
} }
new_file_id = file_id_for_path(&target_name); new_file_id = match file_id_for_path_or_error(&target_name) {
Ok(file_id) => file_id,
Err(err) => {
if err.kind() != std::io::ErrorKind::NotFound {
FLOG!(history_file, "Error re-opening history file:", err);
}
INVALID_FILE_ID
}
}
} }
let can_replace_file = new_file_id == orig_file_id || new_file_id == INVALID_FILE_ID; let can_replace_file = new_file_id == orig_file_id || new_file_id == INVALID_FILE_ID;
@ -1108,13 +1122,16 @@ impl HistoryImpl {
// destination file descriptor, since it destroys the name and the file. // destination file descriptor, since it destroys the name and the file.
self.clear(); self.clear();
let Ok(mut dst_file) = wopen_cloexec( let mut dst_file = match wopen_cloexec(
&new_file, &new_file,
OFlag::O_WRONLY | OFlag::O_CREAT, OFlag::O_WRONLY | OFlag::O_CREAT,
HISTORY_FILE_MODE, HISTORY_FILE_MODE,
) else { ) {
FLOG!(history_file, "Error when writing history file"); Ok(file) => file,
Err(err) => {
FLOG!(history_file, "Error when writing history file:", err);
return; return;
}
}; };
let mut buf = [0; libc::BUFSIZ as usize]; let mut buf = [0; libc::BUFSIZ as usize];

View file

@ -85,3 +85,9 @@ pub fn file_id_for_path_narrow(path: &CStr) -> FileId {
.map(FileId::from_md) .map(FileId::from_md)
.unwrap_or(INVALID_FILE_ID) .unwrap_or(INVALID_FILE_ID)
} }
pub fn file_id_for_path_or_error(path: &wstr) -> std::io::Result<FileId> {
let path = wcs2zstring(path);
let path = OsStr::from_bytes(path.to_bytes());
fs::metadata(path).map(|md| FileId::from_md(&md))
}