]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: merge three if blocks for seeking to the initial position
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 19 Mar 2024 09:54:24 +0000 (18:54 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 26 Mar 2024 13:21:13 +0000 (22:21 +0900)
No functional change, just refactoring.

src/journal/journalctl.c

index 898e08774a26ab8922110a4c61345a838e894ddd..19b4f076c1cf9c2595971a8f08ce9e2ef9f2c4cc 100644 (file)
@@ -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. */
-
-                r = sd_journal_seek_realtime_usec(j, arg_until);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to seek to date: %m");
+        } 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. */
+
+                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.