QDirIterator have any hang issue, while iterate directories that have more sub directories.



  • Please see the below code for search *.pdf file from the directory, that have more sub directories (more than 2000 sub directory).

    QDirIterator it(dir, QStringList() << "*.pdf", QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext())
    {
    qDebug() <<"PDF file : " << it.next();
    }

    This code have any hang issue like recursion hang issue ?.

    Please share your thoughts.

    Thanks for your support in advance.


  • Lifetime Qt Champion

    Hi,

    What do you mean by hang issue ?

    From your description you are at least exploring folders 2000 level deep whith who knows how many files in them so it might indeed take some times before the complete iteration is done.



  • Hi....
    Here a sample:
    from https://github.com/pehohlva/AnalizeDir/blob/master/robotsystem.cpp

    void Robotsystem::followOneDir( const QString &namepathfull ) {
      QTextStream out(stdout);
      QDirIterator dirIt(namepathfull,QDirIterator::Subdirectories);
      while (dirIt.hasNext()) {
                dirIt.next();
                if (!dirIt.filePath().isNull()) {
                    Dirscount++;
                    QString spaceholder;
                    spaceholder.fill(QChar('.'),120);
                    QString fdirx = dirIt.filePath();
                    QStringList piecesdir = fdirx.split( "/" );
                    QString DirIndex= QString("Read %1:").arg(Dirscount);
                    if ( piecesdir.size() > 4 ) {
                      QString linewritteln = DirIndex + QString(" /") + piecesdir.at(3) + spaceholder;
                      linewritteln.resize(40);
                        out <<  linewritteln << "\r";
                    } else if ( piecesdir.size() == 1 ) {
                        QString linewritteln = DirIndex + QString(" /") + piecesdir.at(0)  + spaceholder;
                        linewritteln.resize(40);
                        out << linewritteln  << "\r";
                    } else if ( piecesdir.size() == 2 ) {
                        QString linewritteln = DirIndex + QString(" /") + piecesdir.at(1)  + spaceholder;
                        linewritteln.resize(40);
                        out << linewritteln  << "\r";
                    }
                       out.flush();
                  }
                    if (QFileInfo(dirIt.filePath()).isFile()) {
                        if (QFileInfo(dirIt.filePath()).suffix() == EXTENSION ) {
                             Filefinded++;
                             const qint64 sizefullfromfile = dirIt.filePath().size();
                             FoundResults.append(dirIt.filePath());
                             out << Filefinded << " > " <<  dirIt.filePath()  << ".\n";
                             out.flush();
                             /// qDebug()<<dirIt.filePath();
                          }
                    }
        }
        out.flush();
    }
    


  • Hi,

    Thank you for your replay.

    The hang issue means crash by the stack overflow due to recursion.



  • @patrik08

    Thank you for your response.

    I think the function "void Robotsystem::followOneDir( const QString &namepathfull )" is give the file list that match.
    The same thing should i get from the following code,

    QDirIterator it(dir, QStringList() << "*.pdf", QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext())
    {
    qDebug() <<"PDF file : " << it.next();
    }

    My application run on a touch device. The device have 1GB RAM . Application run on LINUX OS.

    Any way QDirIterator have some recursion happens internally due the recursion is there have a possibility to crash the application by stack overflow due to recursion ?.



  • Yes reursive it self as function or class..

    Its the same as:

    
    /* remove dir recursive */
    void DownDir_RM(const QString d) {
       QDir dir(d);
        QDir enddir(d);
       if (dir.exists())
       {
          const QFileInfoList list = dir.entryInfoList();
          QFileInfo fi;
          for (int l = 0; l < list.size(); l++)
          {
             fi = list.at(l);
             if (fi.isDir() && fi.fileName() != "." && fi.fileName() != "..")
                DownDir_RM(fi.absoluteFilePath());
             else if (fi.isFile())
             {
                unlink(fi.absoluteFilePath());
             }
    
          }
             dir.rmdir(d);
       }
    
       //// return enddir.exists();
    }
    
    /* remove a file */
    bool unlink(const QString fullFileName)
    {
        if (fullFileName.size() > 0) {
        QFile f( fullFileName );
            if ( f.exists(fullFileName) ) {
           if (f.remove()) {
            return true;
           }
            }
        }
    return false;
    }
    


  • @Vellayil although from QDirIterator documentation

    It is similar to QDir::entryList() and QDir::entryInfoList(), but because it lists entries one at a time instead of all at once, it scales better and is more suitable for large directories.

    it looks like your walking on an edge case condition. QDirIterator is based on QStack, and as everything in like, it's finite...

    Given your conditions (thousands of files/folders) and restrictions (limited HW) it's advisable you use some defensive programming approach, not relaying entirely in just one QDirIterator for the whole bunch of folders/files you have, but rather having QDirIterator by levels, so if the need arises of going down in the folder hierarchy, you'll be creating QDirIterator for only the levels traversed


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.