Merge pull request #26577 from ChenQi1989/revert-revert-journal

Revert "Revert "journal: Make sd_journal_previous/next() return 0 at …
This commit is contained in:
Daan De Meyer
2023-05-22 07:51:01 +02:00
committed by GitHub
2 changed files with 150 additions and 41 deletions
+143 -35
View File
@@ -78,9 +78,9 @@ static void test_check_number(sd_journal *j, int n) {
assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
assert_se(k = strndup(d, l));
printf("%s\n", k);
printf("%s (expected=%i)\n", k, n);
assert_se(safe_atoi(k + 7, &x) >= 0);
assert_se(safe_atoi(k + STRLEN("NUMBER="), &x) >= 0);
assert_se(n == x);
}
@@ -113,27 +113,43 @@ static void test_check_numbers_up(sd_journal *j, int count) {
}
static void setup_sequential(void) {
ManagedJournalFile *one, *two;
one = test_open("one.journal");
two = test_open("two.journal");
append_number(one, 1, NULL);
append_number(one, 2, NULL);
append_number(two, 3, NULL);
append_number(two, 4, NULL);
test_close(one);
test_close(two);
ManagedJournalFile *f1, *f2, *f3;
f1 = test_open("one.journal");
f2 = test_open("two.journal");
f3 = test_open("three.journal");
append_number(f1, 1, NULL);
append_number(f1, 2, NULL);
append_number(f1, 3, NULL);
append_number(f2, 4, NULL);
append_number(f2, 5, NULL);
append_number(f2, 6, NULL);
append_number(f3, 7, NULL);
append_number(f3, 8, NULL);
append_number(f3, 9, NULL);
test_close(f1);
test_close(f2);
test_close(f3);
}
static void setup_interleaved(void) {
ManagedJournalFile *one, *two;
one = test_open("one.journal");
two = test_open("two.journal");
append_number(one, 1, NULL);
append_number(two, 2, NULL);
append_number(one, 3, NULL);
append_number(two, 4, NULL);
test_close(one);
test_close(two);
ManagedJournalFile *f1, *f2, *f3;
f1 = test_open("one.journal");
f2 = test_open("two.journal");
f3 = test_open("three.journal");
append_number(f1, 1, NULL);
append_number(f2, 2, NULL);
append_number(f3, 3, NULL);
append_number(f1, 4, NULL);
append_number(f2, 5, NULL);
append_number(f3, 6, NULL);
append_number(f1, 7, NULL);
append_number(f2, 8, NULL);
append_number(f3, 9, NULL);
test_close(f1);
test_close(f2);
test_close(f3);
}
static void mkdtemp_chdir_chattr(char *path) {
@@ -154,38 +170,130 @@ static void test_skip_one(void (*setup)(void)) {
setup();
/* Seek to head, iterate down.
*/
/* Seek to head, iterate down. */
assert_ret(sd_journal_open_directory(&j, t, 0));
assert_ret(sd_journal_seek_head(j));
assert_ret(sd_journal_next(j));
test_check_numbers_down(j, 4);
assert_ret(sd_journal_next(j)); /* pointing the first entry */
test_check_numbers_down(j, 9);
sd_journal_close(j);
/* Seek to tail, iterate up.
*/
/* Seek to head, iterate down. */
assert_ret(sd_journal_open_directory(&j, t, 0));
assert_ret(sd_journal_seek_head(j));
assert_ret(sd_journal_next(j)); /* pointing the first entry */
assert_ret(sd_journal_previous(j)); /* no-op */
test_check_numbers_down(j, 9);
sd_journal_close(j);
/* Seek to head, move to previous, then iterate down. */
assert_ret(sd_journal_open_directory(&j, t, 0));
assert_ret(sd_journal_seek_head(j));
assert_ret(sd_journal_previous(j)); /* no-op */
assert_ret(sd_journal_next(j)); /* pointing the first entry */
test_check_numbers_down(j, 9);
sd_journal_close(j);
/* Seek to head, walk several steps, then iterate down. */
assert_ret(sd_journal_open_directory(&j, t, 0));
assert_ret(sd_journal_seek_head(j));
assert_ret(sd_journal_previous(j)); /* no-op */
assert_ret(sd_journal_previous(j)); /* no-op */
assert_ret(sd_journal_previous(j)); /* no-op */
assert_ret(sd_journal_next(j)); /* pointing the first entry */
assert_ret(sd_journal_previous(j)); /* no-op */
assert_ret(sd_journal_previous(j)); /* no-op */
test_check_numbers_down(j, 9);
sd_journal_close(j);
/* Seek to tail, iterate up. */
assert_ret(sd_journal_open_directory(&j, t, 0));
assert_ret(sd_journal_seek_tail(j));
assert_ret(sd_journal_previous(j));
test_check_numbers_up(j, 4);
test_check_numbers_up(j, 9);
sd_journal_close(j);
/* Seek to tail, skip to head, iterate down.
*/
/* Seek to tail, move to next, then iterate up. */
assert_ret(sd_journal_open_directory(&j, t, 0));
assert_ret(sd_journal_seek_tail(j));
assert_ret(r = sd_journal_previous_skip(j, 4));
assert_se(r == 4);
test_check_numbers_down(j, 4);
assert_ret(sd_journal_next(j)); /* no-op */
assert_ret(sd_journal_previous(j)); /* pointing the first entry */
test_check_numbers_up(j, 9);
sd_journal_close(j);
/* Seek to head, skip to tail, iterate up.
*/
/* Seek to tail, walk several steps, then iterate up. */
assert_ret(sd_journal_open_directory(&j, t, 0));
assert_ret(sd_journal_seek_tail(j));
assert_ret(sd_journal_next(j)); /* no-op */
assert_ret(sd_journal_next(j)); /* no-op */
assert_ret(sd_journal_next(j)); /* no-op */
assert_ret(sd_journal_previous(j)); /* pointing the last entry. */
assert_ret(sd_journal_next(j)); /* no-op */
assert_ret(sd_journal_next(j)); /* no-op */
test_check_numbers_up(j, 9);
sd_journal_close(j);
/* Seek to tail, skip to head, iterate down. */
assert_ret(sd_journal_open_directory(&j, t, 0));
assert_ret(sd_journal_seek_tail(j));
assert_ret(r = sd_journal_previous_skip(j, 9));
assert_se(r == 9);
test_check_numbers_down(j, 9);
sd_journal_close(j);
/* Seek to tail, skip to head in a more complext way, then iterate down. */
assert_ret(sd_journal_open_directory(&j, t, 0));
assert_ret(sd_journal_seek_tail(j));
assert_ret(sd_journal_next(j)); /* no-op */
assert_ret(r = sd_journal_previous_skip(j, 4));
assert_se(r == 4);
assert_ret(r = sd_journal_previous_skip(j, 5));
assert_se(r == 5);
assert_ret(sd_journal_previous(j)); /* no-op */
assert_ret(r = sd_journal_previous_skip(j, 5));
assert_se(r == 0);
assert_ret(sd_journal_next(j));
assert_ret(r = sd_journal_previous_skip(j, 5));
assert_se(r == 1);
assert_ret(sd_journal_next(j));
assert_ret(sd_journal_next(j));
assert_ret(sd_journal_previous(j));
assert_ret(sd_journal_next(j));
assert_ret(sd_journal_next(j));
assert_ret(r = sd_journal_previous_skip(j, 5));
assert_se(r == 3);
test_check_numbers_down(j, 9);
sd_journal_close(j);
/* Seek to head, skip to tail, iterate up. */
assert_ret(sd_journal_open_directory(&j, t, 0));
assert_ret(sd_journal_seek_head(j));
assert_ret(r = sd_journal_next_skip(j, 9));
assert_se(r == 9);
test_check_numbers_up(j, 9);
sd_journal_close(j);
/* Seek to head, skip to tail in a more complex way, then iterate up. */
assert_ret(sd_journal_open_directory(&j, t, 0));
assert_ret(sd_journal_seek_head(j));
assert_ret(sd_journal_previous(j)); /* no-op */
assert_ret(r = sd_journal_next_skip(j, 4));
assert_se(r == 4);
test_check_numbers_up(j, 4);
assert_ret(r = sd_journal_next_skip(j, 5));
assert_se(r == 5);
assert_ret(sd_journal_next(j)); /* no-op */
assert_ret(r = sd_journal_next_skip(j, 5));
assert_se(r == 0);
assert_ret(sd_journal_previous(j));
assert_ret(r = sd_journal_next_skip(j, 5));
assert_se(r == 1);
assert_ret(sd_journal_previous(j));
assert_ret(sd_journal_previous(j));
assert_ret(sd_journal_next(j));
assert_ret(sd_journal_previous(j));
assert_ret(sd_journal_previous(j));
assert_ret(r = sd_journal_next_skip(j, 5));
assert_se(r == 3);
test_check_numbers_up(j, 9);
sd_journal_close(j);
log_info("Done...");
+7 -6
View File
@@ -660,9 +660,9 @@ static int find_location_for_match(
/* FIXME: missing: find by monotonic */
if (j->current_location.type == LOCATION_HEAD)
return journal_file_next_entry_for_data(f, d, DIRECTION_DOWN, ret, offset);
return direction == DIRECTION_DOWN ? journal_file_next_entry_for_data(f, d, DIRECTION_DOWN, ret, offset) : 0;
if (j->current_location.type == LOCATION_TAIL)
return journal_file_next_entry_for_data(f, d, DIRECTION_UP, ret, offset);
return direction == DIRECTION_UP ? journal_file_next_entry_for_data(f, d, DIRECTION_UP, ret, offset) : 0;
if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
return journal_file_move_to_entry_by_seqnum_for_data(f, d, j->current_location.seqnum, direction, ret, offset);
if (j->current_location.monotonic_set) {
@@ -755,9 +755,9 @@ static int find_location_with_matches(
/* No matches is simple */
if (j->current_location.type == LOCATION_HEAD)
return journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset);
return direction == DIRECTION_DOWN ? journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset) : 0;
if (j->current_location.type == LOCATION_TAIL)
return journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset);
return direction == DIRECTION_UP ? journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset) : 0;
if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, offset);
if (j->current_location.monotonic_set) {
@@ -812,7 +812,8 @@ static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direc
/* If we hit EOF before, we don't need to look into this file again
* unless direction changed or new entries appeared. */
if (f->last_direction == direction && f->location_type == LOCATION_TAIL &&
if (f->last_direction == direction &&
f->location_type == (direction == DIRECTION_DOWN ? LOCATION_TAIL : LOCATION_HEAD) &&
n_entries == f->last_n_entries)
return 0;
@@ -940,7 +941,7 @@ static int real_journal_next(sd_journal *j, direction_t direction) {
remove_file_real(j, f);
continue;
} else if (r == 0) {
f->location_type = LOCATION_TAIL;
f->location_type = direction == DIRECTION_DOWN ? LOCATION_TAIL : LOCATION_HEAD;
continue;
}