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>
This commit is contained in:
xiaoyifang 2024-07-16 16:00:17 +08:00 committed by GitHub
parent fac8dfb28b
commit 99ce0a7bbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 24 additions and 5 deletions

View file

@ -1565,7 +1565,7 @@ void DslResourceRequest::run()
string n = dict.getContainingFolder().toStdString() + Utils::Fs::separator() + resourceName; 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 {
try { try {

View file

@ -1069,7 +1069,7 @@ void GlsResourceRequest::run()
try { try {
string n = dict.getContainingFolder().toStdString() + Utils::Fs::separator() + resourceName; 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 { try {
QMutexLocker _( &dataMutex ); QMutexLocker _( &dataMutex );

View file

@ -1517,7 +1517,7 @@ void StardictResourceRequest::run()
string n = string n =
dict.getContainingFolder().toStdString() + Utils::Fs::separator() + "res" + Utils::Fs::separator() + resourceName; 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 { try {
QMutexLocker _( &dataMutex ); QMutexLocker _( &dataMutex );

View file

@ -942,7 +942,7 @@ void XdxfResourceRequest::run()
string n = dict.getContainingFolder().toStdString() + Utils::Fs::separator() + resourceName; 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 {
try { try {

View file

@ -13,6 +13,7 @@
#else #else
#include <QTextCodec> #include <QTextCodec>
#endif #endif
#include <QMutexLocker>
using namespace BtreeIndexing; using namespace BtreeIndexing;
using std::vector; using std::vector;
@ -54,15 +55,23 @@ bool IndexedZip::loadFile( uint32_t offset, vector< char > & data )
if ( !zipIsOpen ) if ( !zipIsOpen )
return false; return false;
QMutexLocker _( &mutex );
// Now seek into the zip file and read its header // Now seek into the zip file and read its header
if ( !zip.seek( offset ) ) if ( !zip.seek( offset ) )
return false; return false;
ZipFile::LocalFileHeader header; ZipFile::LocalFileHeader header;
if ( !ZipFile::readLocalHeader( zip, header ) ) { if ( !ZipFile::readLocalHeader( zip, header ) ) {
vector< string > zipFileNames;
zip.getFilenames( zipFileNames );
GD_DPRINTF( "Failed to load header" ); 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; return false;
} }
@ -123,6 +132,8 @@ bool IndexedZip::indexFile( BtreeIndexing::IndexedWords & zipFileNames, quint32
{ {
if ( !zipIsOpen ) if ( !zipIsOpen )
return false; return false;
QMutexLocker _( &mutex );
if ( !ZipFile::positionAtCentralDir( zip ) ) if ( !ZipFile::positionAtCentralDir( zip ) )
return false; return false;

View file

@ -7,6 +7,7 @@
#include "btreeidx.hh" #include "btreeidx.hh"
#include <QFile> #include <QFile>
#include "zipfile.hh" #include "zipfile.hh"
#include <QMutex>
/// Allows using a btree index to read zip files. Basically built on top of /// Allows using a btree index to read zip files. Basically built on top of
/// the base dictionary infrastructure adapted for zips. /// the base dictionary infrastructure adapted for zips.
@ -14,6 +15,7 @@ class IndexedZip: public BtreeIndexing::BtreeIndex
{ {
ZipFile::SplitZipFile zip; ZipFile::SplitZipFile zip;
bool zipIsOpen; bool zipIsOpen;
QMutex mutex;
public: public:

View file

@ -44,6 +44,11 @@ void SplitFile::getFilenames( vector< string > & names ) const
names.push_back( ( *i )->fileName().toStdString() ); names.push_back( ( *i )->fileName().toStdString() );
} }
int SplitFile::getCurrentFile() const
{
return currentFile;
}
bool SplitFile::open( QFile::OpenMode mode ) bool SplitFile::open( QFile::OpenMode mode )
{ {
for ( QVector< QFile * >::iterator i = files.begin(); i != files.end(); ++i ) for ( QVector< QFile * >::iterator i = files.begin(); i != files.end(); ++i )

View file

@ -32,6 +32,7 @@ public:
virtual void setFileName( const QString & name ) = 0; virtual void setFileName( const QString & name ) = 0;
void getFilenames( vector< string > & names ) const; void getFilenames( vector< string > & names ) const;
int getCurrentFile() const;
bool open( QFile::OpenMode mode ); bool open( QFile::OpenMode mode );
void close(); void close();
bool seek( quint64 pos ); bool seek( quint64 pos );