Don't cache the file size 55/243155/3
authorMateusz Majewski <m.majewski2@samsung.com>
Thu, 3 Sep 2020 09:50:16 +0000 (11:50 +0200)
committerMichal Bloch <m.bloch@partner.samsung.com>
Tue, 8 Sep 2020 07:17:27 +0000 (07:17 +0000)
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
src/logger/reader_pipe.c
src/shared/log_file.c
src/tests/log_file.c

index 2fc0382..2fee957 100644 (file)
@@ -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;
index 330bd82..57b5c1f 100644 (file)
@@ -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);
index 46069b3..043fce5 100644 (file)
@@ -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);
 
index 95fb460..da72019 100644 (file)
@@ -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;