Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SplitFile thread-safe guard #1688

Merged
merged 11 commits into from
Jul 16, 2024
2 changes: 1 addition & 1 deletion src/dict/dsl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/dict/gls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/dict/stardict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/dict/xdxf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion src/indexedzip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#else
#include <QTextCodec>
#endif
#include <QMutexLocker>

using namespace BtreeIndexing;
using std::vector;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -123,6 +132,8 @@ bool IndexedZip::indexFile( BtreeIndexing::IndexedWords & zipFileNames, quint32
{
if ( !zipIsOpen )
return false;

QMutexLocker _( &mutex );
if ( !ZipFile::positionAtCentralDir( zip ) )
return false;

Expand Down
2 changes: 2 additions & 0 deletions src/indexedzip.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#include "btreeidx.hh"
#include <QFile>
#include "zipfile.hh"
#include <QMutex>

/// Allows using a btree index to read zip files. Basically built on top of
/// the base dictionary infrastructure adapted for zips.
class IndexedZip: public BtreeIndexing::BtreeIndex
{
ZipFile::SplitZipFile zip;
bool zipIsOpen;
QMutex mutex;

public:

Expand Down
5 changes: 5 additions & 0 deletions src/splitfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
1 change: 1 addition & 0 deletions src/splitfile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Loading