From dc586975b27e333d1c8fa9c0e883e8578001d655 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 19 Mar 2024 18:54:24 +0900 Subject: [PATCH] journalctl: merge three if blocks for seeking to the initial position No functional change, just refactoring. --- src/journal/journalctl.c | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 898e08774a2..19b4f076c1c 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -2635,36 +2635,28 @@ static int run(int argc, char *argv[]) { else arg_lines = 0; } - } else if (arg_until_set && (arg_reverse || arg_lines_needs_seek_end())) { - /* If both --until and any of --reverse and --lines=N is specified, things get - * a little tricky. We seek to the place of --until first. If only --reverse or - * --reverse and --lines is specified, we search backwards and let the output - * counter handle --lines for us. If only --lines is used, we just jump backwards - * arg_lines and search afterwards from there. */ + } else if (arg_reverse || arg_lines_needs_seek_end()) { + /* If --reverse and/or --lines=N are specified, things get a little tricky. First we seek to + * the place of --until if specified, otherwise seek to tail. Then, if --reverse is + * specified, we search backwards and let the output counter in show() handle --lines for us. + * If --reverse is unspecified, we just jump backwards arg_lines and search afterwards from + * there. */ - r = sd_journal_seek_realtime_usec(j, arg_until); - if (r < 0) - return log_error_errno(r, "Failed to seek to date: %m"); + if (arg_until_set) { + r = sd_journal_seek_realtime_usec(j, arg_until); + if (r < 0) + return log_error_errno(r, "Failed to seek to date: %m"); + } else { + r = sd_journal_seek_tail(j); + if (r < 0) + return log_error_errno(r, "Failed to seek to tail: %m"); + } if (arg_reverse) r = sd_journal_previous(j); else /* arg_lines_needs_seek_end */ r = sd_journal_previous_skip(j, arg_lines); - } else if (arg_reverse) { - r = sd_journal_seek_tail(j); - if (r < 0) - return log_error_errno(r, "Failed to seek to tail: %m"); - - r = sd_journal_previous(j); - - } else if (arg_lines_needs_seek_end()) { - r = sd_journal_seek_tail(j); - if (r < 0) - return log_error_errno(r, "Failed to seek to tail: %m"); - - r = sd_journal_previous_skip(j, arg_lines); - } else if (arg_since_set) { /* This is placed after arg_reverse and arg_lines. If --since is used without * both, we seek to the place of --since and search afterwards from there.