Skip to content

Commit

Permalink
Force a reopening of the file when the user presses 'reload'.
Browse files Browse the repository at this point in the history
Allow to get out of some situation where the file has changed but glogg
didn't register it.
  • Loading branch information
nickbnf committed Aug 13, 2018
1 parent c6a7187 commit 84a8a91
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/data/logdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ void LogData::reload()
{
workerThread_.interrupt();

// Re-open the file, useful in case the file has been moved
reOpenFile();

enqueueOperation( std::make_shared<FullIndexOperation>() );
}

Expand Down Expand Up @@ -216,10 +219,7 @@ void LogData::fileChangedOnDisk()
if ( ( info.size() != attached_file_->size() )
|| ( attached_file_->openMode() == QIODevice::NotOpen ) ) {
LOG(logINFO) << "Inconsistent size, the file might have changed, re-opening";
auto reopened = std::make_unique<QFile>( name );
reopened->open( QIODevice::ReadOnly );
QMutexLocker locker( &fileMutex_ );
attached_file_ = std::move( reopened ); // This will close the old one and open the new
reOpenFile();

// We don't force a (slow) full reindex as this routinely happens if
// the file is appended quickly.
Expand Down Expand Up @@ -525,3 +525,14 @@ qint64 LogData::beginningOfNextLine( qint64 end_pos ) const
{
return end_pos + 1 + before_cr_offset_ + after_cr_offset_;
}

// Close and reopen the file.
// Used if we suspect the file has been moved (we follow the old
// inode but really want the one now associated with the name)
void LogData::reOpenFile()
{
auto reopened = std::make_unique<QFile>( attached_file_->fileName() );
reopened->open( QIODevice::ReadOnly );
QMutexLocker locker( &fileMutex_ );
attached_file_ = std::move( reopened ); // This will close the old one and open the new
}
1 change: 1 addition & 0 deletions src/data/logdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class LogData : public AbstractLogData {

void enqueueOperation( std::shared_ptr<const LogDataOperation> newOperation );
void startOperation();
void reOpenFile();

qint64 endOfLinePosition( qint64 line ) const;
qint64 beginningOfNextLine( qint64 end_pos ) const;
Expand Down

0 comments on commit 84a8a91

Please sign in to comment.