mirror of
https://github.com/tstack/lnav.git
synced 2024-09-11 13:05:51 +03:00
[logfile] truncation detection was not taking into account compressed files
This commit is contained in:
parent
619076d89c
commit
e8f400abfa
@ -113,7 +113,7 @@ private:
|
||||
line_buffer::line_buffer()
|
||||
: lb_gz_file(NULL),
|
||||
lb_bz_file(false),
|
||||
lb_gz_offset(0),
|
||||
lb_compressed_offset(0),
|
||||
lb_file_size((size_t)-1),
|
||||
lb_file_offset(0),
|
||||
lb_file_time(0),
|
||||
@ -190,7 +190,7 @@ throw (error)
|
||||
if (this->lb_file_time < 0) {
|
||||
this->lb_file_time = 0;
|
||||
}
|
||||
this->lb_gz_offset = lseek(this->lb_fd, 0, SEEK_CUR);
|
||||
this->lb_compressed_offset = lseek(this->lb_fd, 0, SEEK_CUR);
|
||||
}
|
||||
#ifdef HAVE_BZLIB_H
|
||||
else if (gz_id[0] == 'B' && gz_id[1] == 'Z') {
|
||||
@ -204,6 +204,8 @@ throw (error)
|
||||
* to keep as much in memory as possible.
|
||||
*/
|
||||
this->resize_buffer(MAX_COMPRESSED_BUFFER_SIZE);
|
||||
|
||||
this->lb_compressed_offset = 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@ -334,14 +336,14 @@ throw (error)
|
||||
else {
|
||||
lock_hack::guard guard;
|
||||
|
||||
lseek(this->lb_fd, this->lb_gz_offset, SEEK_SET);
|
||||
lseek(this->lb_fd, this->lb_compressed_offset, SEEK_SET);
|
||||
gzseek(this->lb_gz_file,
|
||||
this->lb_file_offset + this->lb_buffer_size,
|
||||
SEEK_SET);
|
||||
rc = gzread(this->lb_gz_file,
|
||||
&this->lb_buffer[this->lb_buffer_size],
|
||||
this->lb_buffer_max - this->lb_buffer_size);
|
||||
this->lb_gz_offset = lseek(this->lb_fd, 0, SEEK_CUR);
|
||||
this->lb_compressed_offset = lseek(this->lb_fd, 0, SEEK_CUR);
|
||||
}
|
||||
}
|
||||
#ifdef HAVE_BZLIB_H
|
||||
@ -391,6 +393,7 @@ throw (error)
|
||||
rc = BZ2_bzread(bz_file,
|
||||
&this->lb_buffer[this->lb_buffer_size],
|
||||
this->lb_buffer_max - this->lb_buffer_size);
|
||||
this->lb_compressed_offset = lseek(bzfd, 0, SEEK_SET);
|
||||
BZ2_bzclose(bz_file);
|
||||
|
||||
if (rc != -1 && (
|
||||
|
@ -108,14 +108,18 @@ public:
|
||||
|
||||
off_t get_read_offset(off_t off) const
|
||||
{
|
||||
if (this->lb_gz_file) {
|
||||
return this->lb_gz_offset;
|
||||
if (this->is_compressed()) {
|
||||
return this->lb_compressed_offset;
|
||||
}
|
||||
else{
|
||||
return off;
|
||||
}
|
||||
};
|
||||
|
||||
bool is_data_available(off_t off) {
|
||||
return (this->lb_file_size == -1 || off < this->lb_file_size);
|
||||
};
|
||||
|
||||
/**
|
||||
* Read up to the end of file or a given delimiter.
|
||||
*
|
||||
@ -233,7 +237,7 @@ private:
|
||||
auto_fd lb_fd; /*< The file to read data from. */
|
||||
gzFile lb_gz_file; /*< File handle for gzipped files. */
|
||||
bool lb_bz_file; /*< Flag set for bzip2 compressed files. */
|
||||
off_t lb_gz_offset; /*< The offset into the compressed file. */
|
||||
off_t lb_compressed_offset; /*< The offset into the compressed file. */
|
||||
|
||||
auto_mem<char> lb_buffer; /*< The internal buffer where data is cached */
|
||||
|
||||
|
@ -272,14 +272,16 @@ throw (line_buffer::error, logfile::error)
|
||||
throw error(this->lf_filename, errno);
|
||||
}
|
||||
|
||||
/* Check for new data based on the file size. */
|
||||
if (this->lf_index_size > st.st_size) {
|
||||
// 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",
|
||||
this->lf_filename.c_str());
|
||||
this->close();
|
||||
return false;
|
||||
}
|
||||
else if (this->lf_index_size < st.st_size) {
|
||||
else if (this->lf_line_buffer.is_data_available(this->lf_index_size)) {
|
||||
// We haven't reached the end of the file. Note that we use the
|
||||
// line buffer's notion of the file size since it may be compressed.
|
||||
bool has_format = this->lf_format.get() != NULL;
|
||||
shared_buffer_ref sbr;
|
||||
off_t last_off, off;
|
||||
@ -353,6 +355,7 @@ throw (line_buffer::error, logfile::error)
|
||||
* size.
|
||||
*/
|
||||
this->lf_index_size = off;
|
||||
this->lf_stat = st;
|
||||
|
||||
retval = true;
|
||||
}
|
||||
|
@ -110,7 +110,11 @@ int main(int argc, char *argv[])
|
||||
assert(strcmp(argv[0], lf.get_filename().c_str()) == 0);
|
||||
|
||||
lf.rebuild_index();
|
||||
assert(!lf.is_closed());
|
||||
lf.rebuild_index();
|
||||
assert(!lf.is_closed());
|
||||
lf.rebuild_index();
|
||||
assert(!lf.is_closed());
|
||||
if (expected_format == "") {
|
||||
assert(lf.get_format() == NULL);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user