[logfile] a captured file cannot be overwritten

Fixes #741
This commit is contained in:
Timothy Stack 2020-12-07 15:21:33 -08:00
parent 560c2d4ec3
commit e3f28eaa43
3 changed files with 15 additions and 3 deletions

View File

@ -91,6 +91,7 @@ logfile::logfile(const string &filename, logfile_open_options &loo)
} }
else { else {
log_perror(fstat(this->lf_options.loo_fd, &this->lf_stat)); log_perror(fstat(this->lf_options.loo_fd, &this->lf_stat));
this->lf_named_file = false;
this->lf_valid_filename = false; this->lf_valid_filename = false;
} }
@ -298,9 +299,10 @@ logfile::rebuild_result_t logfile::rebuild_index()
} }
// Check the previous stat against the last to see if things are wonky. // Check the previous stat against the last to see if things are wonky.
if (st.st_size < this->lf_stat.st_size || if (this->lf_named_file &&
(st.st_size < this->lf_stat.st_size ||
(this->lf_stat.st_size == st.st_size && (this->lf_stat.st_size == st.st_size &&
this->lf_stat.st_mtime != st.st_mtime)) { this->lf_stat.st_mtime != st.st_mtime))) {
log_info("overwritten file detected, closing -- %s new: %" PRId64 log_info("overwritten file detected, closing -- %s new: %" PRId64
"/%" PRId64 " old: %" PRId64 "/%" PRId64, "/%" PRId64 " old: %" PRId64 "/%" PRId64,
this->lf_filename.c_str(), this->lf_filename.c_str(),

View File

@ -377,6 +377,7 @@ protected:
logfile_open_options lf_options; logfile_open_options lf_options;
logfile_activity lf_activity; logfile_activity lf_activity;
bool lf_named_file{true};
bool lf_valid_filename; bool lf_valid_filename;
std::string lf_filename; std::string lf_filename;
std::string lf_basename; std::string lf_basename;

View File

@ -1,6 +1,15 @@
#! /bin/bash #! /bin/bash
echo "Hi" | run_test ${lnav_test} -d /tmp/lnav.err -nt -w logfile_stdin.0.log \
-c ':shexec sleep 1 && touch -t 200711030923 logfile_stdin.0.log'
check_output "piping to stdin is not working?" <<EOF
2013-06-06T12:13:20.123 Hi
2013-06-06T12:13:20.123 ---- END-OF-STDIN ----
EOF
cp ${srcdir}/logfile_syslog.0 truncfile.0 cp ${srcdir}/logfile_syslog.0 truncfile.0
chmod u+w truncfile.0 chmod u+w truncfile.0