[logfile] the index rebuild didn't check the mtime if the file size was the same

Fixes #668
This commit is contained in:
Timothy Stack 2019-07-25 07:31:46 -07:00
parent d381197195
commit 34db64aa32
5 changed files with 38 additions and 4 deletions

3
.gitignore vendored
View File

@ -10,6 +10,8 @@
*.pyc
*.tmp
*.trs
*~
.idea/
Makefile
Makefile.in
TESTS_ENVIRONMENT
@ -34,6 +36,7 @@ release/osx-pkg/
release/outbox/
release/vagrant-static-linux/.vagrant
release/vagrant-static-linux/lnav
cmake-build-debug/
src/bin2c
src/config.h
src/config.h.in

View File

@ -280,8 +280,10 @@ logfile::rebuild_result_t logfile::rebuild_index()
}
// Check the previous stat against the last to see if things are wonky.
if (this->lf_stat.st_size > st.st_size) {
log_info("truncated file detected, closing -- %s",
if (st.st_size < this->lf_stat.st_size ||
(this->lf_stat.st_size == st.st_size &&
this->lf_stat.st_mtime != st.st_mtime)) {
log_info("overwritten file detected, closing -- %s",
this->lf_filename.c_str());
this->close();
return RR_NO_NEW_LINES;

View File

@ -64,7 +64,7 @@ class logline_observer;
*/
class logfile_observer {
public:
virtual ~logfile_observer() { };
virtual ~logfile_observer() = default;
/**
* @param lf The logfile object that is doing the indexing.
@ -421,7 +421,7 @@ protected:
class logline_observer {
public:
virtual ~logline_observer() { };
virtual ~logline_observer() = default;
virtual void logline_restart(const logfile &lf, size_t rollback_size) = 0;

View File

@ -345,6 +345,7 @@ DISTCLEANFILES = \
*.bz2 \
hw.txt \
hw2.txt \
reload_test.0 \
truncfile.0 \
logfile_append.0 \
logfile_changed.0 \

View File

@ -29,6 +29,8 @@
#include "config.h"
#include <fstream>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.hh"
@ -36,9 +38,35 @@
#include "view_curses.hh"
#include "relative_time.hh"
#include "unique_path.hh"
#include "logfile.hh"
using namespace std;
std::vector<log_format *>& log_format::get_root_formats()
{
static std::vector<log_format *> retval;
return retval;
}
TEST_CASE("overwritten-logfile") {
string fname = "reload_test.0";
ofstream(fname) << "test 1\n";
logfile_open_options loo;
logfile lf(fname, loo);
auto build_result = lf.rebuild_index();
CHECK(build_result == logfile::RR_NEW_LINES);
CHECK(lf.size() == 1);
sleep(1);
ofstream(fname) << "test 2\n";
auto rebuild_result = lf.rebuild_index();
CHECK(rebuild_result == logfile::RR_NO_NEW_LINES);
CHECK(lf.is_closed());
}
TEST_CASE("duration2str") {
string val;