From 3ca87bd7c0fe1c6dab4307f0a58cbee1d249e11c Mon Sep 17 00:00:00 2001 From: Mateusz Majewski Date: Thu, 3 Sep 2020 11:50:16 +0200 Subject: [PATCH] Don't cache the file size Little point in caching the file size if you are going to update it before using anyway. Yes, in some edge cases, the accurate size might be unavailable, but I don't think that there are many cases in which we can't check the file size, but we can write to it. More importantly, the cached value was wrong anyway, since log_print_log_line has been returning hogwash instead of an accurate written byte count for some time. Change-Id: I73d8b6d757ac0cd6574b66349ac2e8054bb15aaa --- include/log_file.h | 1 - src/logger/reader_pipe.c | 1 - src/shared/log_file.c | 12 +++++------- src/tests/log_file.c | 10 ---------- 4 files changed, 5 insertions(+), 19 deletions(-) diff --git a/include/log_file.h b/include/log_file.h index 2fc0382..2fee957 100644 --- a/include/log_file.h +++ b/include/log_file.h @@ -34,7 +34,6 @@ struct log_file { char *path; int fd; int should_close; - int size; size_t rotate_size_kbytes; size_t max_rotated; struct log_format format; diff --git a/src/logger/reader_pipe.c b/src/logger/reader_pipe.c index 330bd82..57b5c1f 100644 --- a/src/logger/reader_pipe.c +++ b/src/logger/reader_pipe.c @@ -139,7 +139,6 @@ int reader_print_out_single_log(struct reader_pipe *reader, const dlogutil_entry r = 0; } - reader->file.size += r; if (r < dlogutil_entry->len) { reader->partial_log_size = dlogutil_entry->len - r; memcpy(reader->partial_log, ((char *)dlogutil_entry) + r, reader->partial_log_size); diff --git a/src/shared/log_file.c b/src/shared/log_file.c index 46069b3..043fce5 100644 --- a/src/shared/log_file.c +++ b/src/shared/log_file.c @@ -134,7 +134,7 @@ void logfile_free(struct log_file *l_file) logfile_close_if_needed(l_file); } -static int logfile_update_fsize(struct log_file *l_file) +static int logfile_get_fsize(struct log_file *l_file) { assert(l_file); assert(l_file->fd >= 0); @@ -144,8 +144,7 @@ static int logfile_update_fsize(struct log_file *l_file) if (fstat(l_file->fd, &st) < 0) return -errno; - l_file->size = st.st_size; - return 0; + return st.st_size; } static int logfile_open_internal(struct log_file *l_file) @@ -160,7 +159,6 @@ static int logfile_open_internal(struct log_file *l_file) return -errno; logfile_set_fd(l_file, fd, 1); - logfile_update_fsize(l_file); return 0; } @@ -208,9 +206,10 @@ int logfile_rotate_needed(struct log_file *l_file) if (l_file->path == NULL || l_file->rotate_size_kbytes <= 0 || l_file->max_rotated <= 0) return 0; - logfile_update_fsize(l_file); + int size = logfile_get_fsize(l_file); + if (size < 0) + return size; - size_t size = l_file->size; if (l_file->buffer.data) size += l_file->buffer.position; @@ -313,7 +312,6 @@ int logfile_write_with_rotation(const dlogutil_entry_s *e, struct log_file *file written_bytes += log_print_log_line(file->format, file->fd, e, &file->buffer); if (written_bytes <= 0) return 1; - file->size += written_bytes; logfile_add_timestamp(file, ts); diff --git a/src/tests/log_file.c b/src/tests/log_file.c index 95fb460..da72019 100644 --- a/src/tests/log_file.c +++ b/src/tests/log_file.c @@ -167,22 +167,12 @@ int main() fake_open = true; open_errno = 456; assert(-456 == logfile_open(&lf)); - open_errno = 0; - fail_fstat = true; - lf.size = 9999; - open_calls = 0; - assert(!logfile_open(&lf)); - assert(lf.fd == 0xFD); - assert(lf.size == 9999); - assert(open_calls == 1); - fail_fstat = false; open_calls = 0; lf.rotate_size_kbytes = 0; assert(!logfile_open(&lf)); assert(lf.fd == 0xFD); - assert(lf.size == 7654321); assert(open_calls == 1); lf.rotate_size_kbytes = 10; -- 2.7.4