Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the file size in directory traversal?

Tags:

c++

qt

How can I get the size of a file when I am using recursion to look at each file? I'm getting the next error:

project.exe exited with code -1073741819

int dir_size(const QString _wantedDirPath)
{
    long int sizex = 0;
    QFileInfo str_info(_wantedDirPath);
    if (str_info.isDir())
    {
        QDir dir(_wantedDirPath);
        QStringList ext_list;
        dir.setFilter(QDir::Files | QDir::Dirs |  QDir::Hidden | QDir::NoSymLinks);
        QFileInfoList list = dir.entryInfoList();

        for(int i = 0; i < list.size(); ++i)
        {
            QFileInfo fileInfo = list.at(i);
            if ((fileInfo.fileName() != ".") && (fileInfo.fileName() != ".."))
            {
                sizex += (fileInfo.isDir()) ? this->dir_size(fileInfo.path()) : fileInfo.size():
                QApplication::processEvents();
            }
        } 
    } 

    return sizex;
}
like image 781
Dmitry Avatar asked Aug 17 '11 16:08

Dmitry


People also ask

What is directory traversal and how does it work?

What is directory traversal? Directory traversal (also known as file path traversal) is a web security vulnerability that allows an attacker to read arbitrary files on the server that is running an application. This might include application code and data, credentials for back-end systems, and sensitive operating system files.

What does an attacker need to perform a directory traversal attack?

All an attacker needs to perform a directory traversal attack is a web browser and some knowledge on where to find any default files and directories on the system. How does a Directory Traversal attack work?

How to get file size and directory size from command line?

Get File size and directory size from command line. In Windows, we can use dir command to get the file size. But there is no option/switch to print only the file size. Dir command accepts wild cards. We can use ‘*” to get the file sizes for all the files in a directory.

How to get the file size of a file using Dirdir?

Dir command accepts wild cards. We can use ‘*” to get the file sizes for all the files in a directory. We can also get size for files of certain type. For example, to get file size for mp3 files, we can run the command ‘ dir *.mp3 ‘. The above command prints file modified time also.


1 Answers

Start by cleaning your code a bit.

quint64 dir_size(const QString & str)
{
    quint64 sizex = 0;
    QFileInfo str_info(str);
    if (str_info.isDir())
    {
        QDir dir(str);
        QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs |  QDir::Hidden | QDir::NoSymLinks | QDir::NoDotAndDotDot);
        for (int i = 0; i < list.size(); ++i)
        {
            QFileInfo fileInfo = list.at(i);
            if(fileInfo.isDir())
            {
                    sizex += dir_size(fileInfo.absoluteFilePath());
            }
            else 
                sizex += fileInfo.size();

        }
    }
    return sizex;
} 

If you want to keep the ui reactive, do the calcul in a separate thread, call processEvent() at each file is a burden. You should also use quint64 ( unsigned long long ) to handle big files ( > 2Go ) But, it's not clear where the crash is.

like image 102
cor3ntin Avatar answered Sep 21 '22 17:09

cor3ntin