Skip to content

Commit

Permalink
fix: [statistics]Inaccurate display of size in folder property box
Browse files Browse the repository at this point in the history
Because there are linked files in the counted files, the source file size of the linked files was counted. Resulting in multiple counts of the same file. Modify the size of the statistics folder

Log: Inaccurate display of size in folder property box
Bug: https://pms.uniontech.com/bug-view-205655.html
  • Loading branch information
liyigang1 authored and deepin-bot[bot] committed Jul 17, 2023
1 parent c07a18e commit e4326c7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
67 changes: 56 additions & 11 deletions src/dfm-base/utils/filestatisticsjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ FileStatisticsJobPrivate::~FileStatisticsJobPrivate()
notifyDataTimer->stop();
notifyDataTimer->deleteLater();
}
inodelist.clear();
}

void FileStatisticsJobPrivate::setState(FileStatisticsJob::State s)
Expand Down Expand Up @@ -112,7 +113,8 @@ void FileStatisticsJobPrivate::processFile(const QUrl &url, const bool followLin
return;
}

qint64 size = 0;
if (!checkInode(info))
return;

if (info->isAttributes(OptInfoType::kIsFile)) {
do {
Expand Down Expand Up @@ -141,7 +143,7 @@ void FileStatisticsJobPrivate::processFile(const QUrl &url, const bool followLin
if (!checkFileType(type))
break;

size = info->size();
auto size = info->size();
if (size > 0) {
totalSize += size;
emitSizeChanged();
Expand Down Expand Up @@ -233,13 +235,12 @@ void FileStatisticsJobPrivate::processFileByFts(const QUrl &url, const bool foll
if (!stateCheck())
break;

unsigned short flag = ent->fts_info;
if (!checkInode(ent, fts))
continue;

QUrl currentUrl = QUrl::fromLocalFile(ent->fts_path);

// 逆序的dir跳过统计了2次
if (flag == FTS_DP || sizeInfo->allFiles.contains(currentUrl))
continue;
if (fileHints.testFlag(FileStatisticsJob::kExcludeSourceFile) && QString(ent->fts_path) == url.path())
if (fileHints.testFlag(FileStatisticsJob::kExcludeSourceFile) && currentUrl.path() == url.path())
continue;

sizeInfo->allFiles.append(currentUrl);
Expand Down Expand Up @@ -369,6 +370,15 @@ void FileStatisticsJobPrivate::statisticFile(FTSENT *ent)

void FileStatisticsJobPrivate::statisticSysLink(const QUrl &currentUrl, FTS *fts, FTSENT *ent, const bool singleDepth, const bool followLink)
{
if (!singleDepth) {
if (S_ISDIR(ent->fts_statp->st_mode)) {
directoryCount++;
} else {
filesCount++;
}
totalSize += ent->fts_statp->st_size;
return;
}
auto info = InfoFactory::create<FileInfo>(currentUrl, Global::CreateFileInfoType::kCreateFileInfoSync);
if (!info) {
filesCount++;
Expand Down Expand Up @@ -404,6 +414,37 @@ void FileStatisticsJobPrivate::statisticSysLink(const QUrl &currentUrl, FTS *fts
fts_set(fts, ent, FTS_SYMFOLLOW);
}

bool FileStatisticsJobPrivate::checkInode(const FileInfoPointer info)
{
auto fileInode = info->extendAttributes(ExtInfoType::kInode).toULongLong();
if (fileInode > 0) {
if (inodelist.contains(fileInode)) {
if (info->isAttributes(OptInfoType::kIsFile)) {
filesCount++;
} else {
directoryCount++;
}
return false;
}
inodelist.append(fileInode);
}
return true;
}

bool FileStatisticsJobPrivate::checkInode(FTSENT *ent, FTS *fts)
{
auto fileInode = ent->fts_statp->st_ino;
if (fileInode > 0) {
if (inodelist.contains(fileInode)) {
if (S_ISDIR(ent->fts_statp->st_mode))
fts_set(fts, ent, FTS_SKIP);
return false;
}
inodelist.append(fileInode);
}
return true;
}

FileStatisticsJob::FileStatisticsJob(QObject *parent)
: QThread(parent), d(new FileStatisticsJobPrivate(this))
{
Expand Down Expand Up @@ -433,7 +474,7 @@ FileStatisticsJob::FileHints FileStatisticsJob::fileHints() const

qint64 FileStatisticsJob::totalSize() const
{
return d->totalSize.load();
return d->totalSize;
}
// fix bug 30548 ,以为有些文件大小为0,文件夹为空,size也为零,重新计算显示大小
qint64 FileStatisticsJob::totalProgressSize() const
Expand All @@ -443,15 +484,15 @@ qint64 FileStatisticsJob::totalProgressSize() const

int FileStatisticsJob::filesCount() const
{
return d->filesCount.load();
return d->filesCount;
}

int FileStatisticsJob::directorysCount(bool includeSelf) const
{
if (includeSelf) {
return d->directoryCount.load();
return d->directoryCount;
} else {
return qMax(d->directoryCount.load() - 1, 0);
return qMax(d->directoryCount - 1, 0);
}
}

Expand Down Expand Up @@ -516,6 +557,7 @@ void FileStatisticsJob::run()
d->totalSize = 0;
d->filesCount = 0;
d->directoryCount = 0;
d->inodelist.clear();
d->sizeInfo.reset(new FileUtils::FilesSizeInfo());
if (d->sourceUrlList.isEmpty())
return;
Expand Down Expand Up @@ -560,6 +602,9 @@ void FileStatisticsJob::statistcsOtherFileSystem()
continue;
}

if (!d->checkInode(info))
continue;

if (info->isAttributes(OptInfoType::kIsDir) && d->fileHints.testFlag(kSingleDepth)) {
fileCount += d->countFileCount(info->pathOf(PathInfoType::kAbsoluteFilePath).toStdString().c_str());
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/dfm-base/utils/private/filestatissticsjob_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class FileStatisticsJobPrivate : public QObject
void statisticDir(const QUrl &url, FTS *fts, const bool singleDepth, FTSENT *ent);
void statisticFile(FTSENT *ent);
void statisticSysLink(const QUrl &currentUrl, FTS *fts, FTSENT *ent, const bool singleDepth, const bool followLink);
bool checkInode(const FileInfoPointer info);
bool checkInode(FTSENT *ent, FTS *fts);

FileStatisticsJob *q;
QTimer *notifyDataTimer;
Expand All @@ -51,6 +53,7 @@ class FileStatisticsJobPrivate : public QObject
SizeInfoPointer sizeInfo { nullptr };
QList<QUrl> fileStatistics;
QList<QString> skipPath;
QList<quint64> inodelist;
AbstractDirIteratorPointer iterator { nullptr };
std::atomic_bool iteratorCanStop { false };
};
Expand Down

0 comments on commit e4326c7

Please sign in to comment.