Skip to content

Commit d00a84e

Browse files
benthecarmanclaude
andcommitted
Fix log cleanup for bare relative log paths
When the log file is configured as a bare file name such as `ldk-server.log`, its parent is an empty path. Passing that to `read_dir` fails, so rotated archives were never removed and `log_max_files` had no effect. Treat an empty parent as the current working directory, which is where the log file itself is opened. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 08316de commit d00a84e

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

‎ldk-server/src/util/logger.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,14 @@ fn open_log_file(log_file_path: &Path) -> Result<File, io::Error> {
265265
OpenOptions::new().create(true).append(true).mode(0o600).open(log_file_path)
266266
}
267267

268+
/// Returns the directory containing the log file. A bare file name such as `ldk-server.log` has an
269+
/// empty parent, which refers to the current working directory.
270+
fn log_file_dir(log_file_path: &Path) -> Option<&Path> {
271+
log_file_path.parent().map(|p| if p.as_os_str().is_empty() { Path::new(".") } else { p })
272+
}
273+
268274
fn cleanup_old_logs(log_file_path: &Path, max_files: usize) -> io::Result<()> {
269-
let parent = log_file_path.parent().ok_or_else(|| {
275+
let parent = log_file_dir(log_file_path).ok_or_else(|| {
270276
io::Error::new(io::ErrorKind::InvalidInput, "Log file path has no parent directory")
271277
})?;
272278

@@ -344,4 +350,12 @@ mod tests {
344350
assert_eq!(mode & 0o077, 0);
345351
fs::remove_dir_all(dir).unwrap();
346352
}
353+
354+
#[test]
355+
fn log_file_dir_handles_bare_file_name() {
356+
assert_eq!(log_file_dir(Path::new("ldk-server.log")), Some(Path::new(".")));
357+
assert_eq!(log_file_dir(Path::new("logs/ldk-server.log")), Some(Path::new("logs")));
358+
assert_eq!(log_file_dir(Path::new("/var/log/ldk-server.log")), Some(Path::new("/var/log")));
359+
assert_eq!(log_file_dir(Path::new("/")), None);
360+
}
347361
}

0 commit comments

Comments
 (0)