Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion ldk-server/src/util/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,14 @@ fn open_log_file(log_file_path: &Path) -> Result<File, io::Error> {
OpenOptions::new().create(true).append(true).mode(0o600).open(log_file_path)
}

/// Returns the directory containing the log file. A bare file name such as `ldk-server.log` has an
/// empty parent, which refers to the current working directory.
fn log_file_dir(log_file_path: &Path) -> Option<&Path> {
log_file_path.parent().map(|p| if p.as_os_str().is_empty() { Path::new(".") } else { p })
}

fn cleanup_old_logs(log_file_path: &Path, max_files: usize) -> io::Result<()> {
let parent = log_file_path.parent().ok_or_else(|| {
let parent = log_file_dir(log_file_path).ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "Log file path has no parent directory")
})?;

Expand Down Expand Up @@ -344,4 +350,12 @@ mod tests {
assert_eq!(mode & 0o077, 0);
fs::remove_dir_all(dir).unwrap();
}

#[test]
fn log_file_dir_handles_bare_file_name() {
assert_eq!(log_file_dir(Path::new("ldk-server.log")), Some(Path::new(".")));
assert_eq!(log_file_dir(Path::new("logs/ldk-server.log")), Some(Path::new("logs")));
assert_eq!(log_file_dir(Path::new("/var/log/ldk-server.log")), Some(Path::new("/var/log")));
assert_eq!(log_file_dir(Path::new("/")), None);
}
}
Loading