From 99ce0a7bbbf7b53ea06f0823bfb7c564248a2784 Mon Sep 17 00:00:00 2001 From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com> Date: Tue, 16 Jul 2024 16:00:17 +0800 Subject: [PATCH] fix: SplitFile thread-safe guard (#1688) * fix: SplitFile thread-safe guard * add friendly log message --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/dict/dsl.cc | 2 +- src/dict/gls.cc | 2 +- src/dict/stardict.cc | 2 +- src/dict/xdxf.cc | 2 +- src/indexedzip.cc | 13 ++++++++++++- src/indexedzip.hh | 2 ++ src/splitfile.cc | 5 +++++ src/splitfile.hh | 1 + 8 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/dict/dsl.cc b/src/dict/dsl.cc index 83ae04de..91d81f7a 100644 --- a/src/dict/dsl.cc +++ b/src/dict/dsl.cc @@ -1565,7 +1565,7 @@ void DslResourceRequest::run() string n = dict.getContainingFolder().toStdString() + Utils::Fs::separator() + resourceName; - GD_DPRINTF( "n is %s\n", n.c_str() ); + GD_DPRINTF( "dsl resource name is %s\n", n.c_str() ); try { try { diff --git a/src/dict/gls.cc b/src/dict/gls.cc index 8f20a915..2b96ae50 100644 --- a/src/dict/gls.cc +++ b/src/dict/gls.cc @@ -1069,7 +1069,7 @@ void GlsResourceRequest::run() try { string n = dict.getContainingFolder().toStdString() + Utils::Fs::separator() + resourceName; - GD_DPRINTF( "n is %s\n", n.c_str() ); + GD_DPRINTF( "gls resource name is %s\n", n.c_str() ); try { QMutexLocker _( &dataMutex ); diff --git a/src/dict/stardict.cc b/src/dict/stardict.cc index d2bfcbaf..46862641 100644 --- a/src/dict/stardict.cc +++ b/src/dict/stardict.cc @@ -1517,7 +1517,7 @@ void StardictResourceRequest::run() string n = dict.getContainingFolder().toStdString() + Utils::Fs::separator() + "res" + Utils::Fs::separator() + resourceName; - GD_DPRINTF( "n is %s\n", n.c_str() ); + GD_DPRINTF( "startdict resource name is %s\n", n.c_str() ); try { QMutexLocker _( &dataMutex ); diff --git a/src/dict/xdxf.cc b/src/dict/xdxf.cc index 3c491a15..79e01e21 100644 --- a/src/dict/xdxf.cc +++ b/src/dict/xdxf.cc @@ -942,7 +942,7 @@ void XdxfResourceRequest::run() string n = dict.getContainingFolder().toStdString() + Utils::Fs::separator() + resourceName; - GD_DPRINTF( "n is %s\n", n.c_str() ); + GD_DPRINTF( "xdxf resource name is %s\n", n.c_str() ); try { try { diff --git a/src/indexedzip.cc b/src/indexedzip.cc index 67e26c4d..4990d708 100644 --- a/src/indexedzip.cc +++ b/src/indexedzip.cc @@ -13,6 +13,7 @@ #else #include #endif +#include using namespace BtreeIndexing; using std::vector; @@ -54,15 +55,23 @@ bool IndexedZip::loadFile( uint32_t offset, vector< char > & data ) if ( !zipIsOpen ) return false; + QMutexLocker _( &mutex ); // Now seek into the zip file and read its header - if ( !zip.seek( offset ) ) return false; ZipFile::LocalFileHeader header; if ( !ZipFile::readLocalHeader( zip, header ) ) { + vector< string > zipFileNames; + zip.getFilenames( zipFileNames ); GD_DPRINTF( "Failed to load header" ); + string filename; + if ( zip.getCurrentFile() < zipFileNames.size() ) { + filename = zipFileNames.at( zip.getCurrentFile() ); + } + + qDebug() << "Current failed zip file:" << QString::fromStdString( filename ); return false; } @@ -123,6 +132,8 @@ bool IndexedZip::indexFile( BtreeIndexing::IndexedWords & zipFileNames, quint32 { if ( !zipIsOpen ) return false; + + QMutexLocker _( &mutex ); if ( !ZipFile::positionAtCentralDir( zip ) ) return false; diff --git a/src/indexedzip.hh b/src/indexedzip.hh index d904a170..20466720 100644 --- a/src/indexedzip.hh +++ b/src/indexedzip.hh @@ -7,6 +7,7 @@ #include "btreeidx.hh" #include #include "zipfile.hh" +#include /// Allows using a btree index to read zip files. Basically built on top of /// the base dictionary infrastructure adapted for zips. @@ -14,6 +15,7 @@ class IndexedZip: public BtreeIndexing::BtreeIndex { ZipFile::SplitZipFile zip; bool zipIsOpen; + QMutex mutex; public: diff --git a/src/splitfile.cc b/src/splitfile.cc index 55ca3a69..e1ed0c0d 100644 --- a/src/splitfile.cc +++ b/src/splitfile.cc @@ -44,6 +44,11 @@ void SplitFile::getFilenames( vector< string > & names ) const names.push_back( ( *i )->fileName().toStdString() ); } +int SplitFile::getCurrentFile() const +{ + return currentFile; +} + bool SplitFile::open( QFile::OpenMode mode ) { for ( QVector< QFile * >::iterator i = files.begin(); i != files.end(); ++i ) diff --git a/src/splitfile.hh b/src/splitfile.hh index 2b308094..3801e5f2 100644 --- a/src/splitfile.hh +++ b/src/splitfile.hh @@ -32,6 +32,7 @@ public: virtual void setFileName( const QString & name ) = 0; void getFilenames( vector< string > & names ) const; + int getCurrentFile() const; bool open( QFile::OpenMode mode ); void close(); bool seek( quint64 pos );