1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "kdirectorycontentscounterworker.h"
23 // Required includes for subItemsCount():
28 #include <qplatformdefs.h>
31 #include "dolphin_detailsmodesettings.h"
33 KDirectoryContentsCounterWorker::KDirectoryContentsCounterWorker(QObject
* parent
) :
36 qRegisterMetaType
<KDirectoryContentsCounterWorker::Options
>();
39 KDirectoryContentsCounterWorker::CountResult
walkDir(const QString
&dirPath
,
40 const bool countHiddenFiles
,
41 const bool countDirectoriesOnly
,
43 const uint allowedRecursiveLevel
)
47 auto dir
= QT_OPENDIR(QFile::encodeName(dirPath
));
52 while ((dirEntry
= QT_READDIR(dir
))) {
53 if (dirEntry
->d_name
[0] == '.') {
54 if (dirEntry
->d_name
[1] == '\0' || !countHiddenFiles
) {
55 // Skip "." or hidden files
58 if (dirEntry
->d_name
[1] == '.' && dirEntry
->d_name
[2] == '\0') {
64 // If only directories are counted, consider an unknown file type and links also
65 // as directory instead of trying to do an expensive stat()
66 // (see bugs 292642 and 299997).
67 const bool countEntry
= !countDirectoriesOnly
||
68 dirEntry
->d_type
== DT_DIR
||
69 dirEntry
->d_type
== DT_LNK
||
70 dirEntry
->d_type
== DT_UNKNOWN
;
75 if (allowedRecursiveLevel
> 0) {
77 bool linkFound
= false;
78 QString nameBuf
= QStringLiteral("%1/%2").arg(dirPath
, dirEntry
->d_name
);
80 if (dirEntry
->d_type
== DT_REG
|| dirEntry
->d_type
== DT_LNK
) {
81 if (QT_STAT(nameBuf
.toLocal8Bit(), &buf
) == 0) {
82 if (S_ISDIR(buf
.st_mode
)) {
83 // was a dir link, recurse
89 if (dirEntry
->d_type
== DT_DIR
|| linkFound
) {
90 // recursion for dirs and dir links
91 size
+= walkDir(nameBuf
, countHiddenFiles
, countDirectoriesOnly
, dirEntry
, allowedRecursiveLevel
- 1).size
;
97 return KDirectoryContentsCounterWorker::CountResult
{count
, size
};
100 KDirectoryContentsCounterWorker::CountResult
KDirectoryContentsCounterWorker::subItemsCount(const QString
& path
, Options options
)
102 const bool countHiddenFiles
= options
& CountHiddenFiles
;
103 const bool countDirectoriesOnly
= options
& CountDirectoriesOnly
;
107 QDir::Filters filters
= QDir::NoDotAndDotDot
| QDir::System
;
108 if (countHiddenFiles
) {
109 filters
|= QDir::Hidden
;
111 if (countDirectoriesOnly
) {
112 filters
|= QDir::Dirs
;
114 filters
|= QDir::AllEntries
;
116 return {dir
.entryList(filters
).count(), 0};
119 const uint maxRecursiveLevel
= DetailsModeSettings::directorySizeCount() ? 1 : DetailsModeSettings::recursiveDirectorySizeLimit();
121 QT_DIRENT
*dirEntry
= nullptr;
123 auto res
= walkDir(QFile::encodeName(path
), countHiddenFiles
, countDirectoriesOnly
, dirEntry
, maxRecursiveLevel
);
129 void KDirectoryContentsCounterWorker::countDirectoryContents(const QString
& path
, Options options
)
131 auto res
= subItemsCount(path
, options
);
132 emit
result(path
, res
.count
, res
.size
);