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;
}
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.
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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With