1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2008 Tobias Koenig <tokoe@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "discspaceutil.h"
9#include "kiotrashdebug.h"
10
11#include <QDirIterator>
12#include <QFileInfo>
13#include <QStorageInfo>
14
15#include <qplatformdefs.h> // QT_LSTAT, QT_STAT, QT_STATBUF
16
17DiscSpaceUtil::DiscSpaceUtil(const QString &directory)
18 : mFullSize(0)
19{
20 QStorageInfo storageInfo(directory);
21 if (storageInfo.isValid() && storageInfo.isReady()) {
22 mFullSize = storageInfo.bytesTotal();
23 mMountPoint = storageInfo.rootPath();
24 }
25}
26
27qint64 DiscSpaceUtil::sizeOfPath(const QString &path)
28{
29 QFileInfo info(path);
30 if (!info.exists()) {
31 return 0;
32 }
33
34 if (info.isSymLink()) {
35 // QFileInfo::size does not return the actual size of a symlink. #253776
36 QT_STATBUF buff;
37 return QT_LSTAT(file: QFile::encodeName(fileName: path).constData(), buf: &buff) == 0 ? buff.st_size : 0;
38 }
39
40 if (info.isFile()) {
41 return info.size();
42 }
43
44 if (info.isDir()) {
45 QDirIterator it(path, QDirIterator::NoIteratorFlags);
46
47 qint64 sum = 0;
48 while (it.hasNext()) {
49 it.next();
50 const QFileInfo info = it.fileInfo();
51 const QString name = info.fileName();
52
53 if (name != QLatin1Char('.') && name != QLatin1String("..")) {
54 sum += sizeOfPath(path: info.absoluteFilePath());
55 }
56 }
57
58 return sum;
59 }
60
61 return 0;
62}
63
64double DiscSpaceUtil::usage(qint64 size) const
65{
66 if (mFullSize == 0) {
67 return 0;
68 }
69
70 return (static_cast<double>(size) * 100) / static_cast<double>(mFullSize);
71}
72
73qint64 DiscSpaceUtil::size() const
74{
75 return mFullSize;
76}
77
78QString DiscSpaceUtil::mountPoint() const
79{
80 return mMountPoint;
81}
82

source code of kio/src/kioworkers/trash/discspaceutil.cpp