File indexing completed on 2024-06-16 07:42:25

0001 /*
0002     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "k3bdirsizejob.h"
0007 
0008 #include "k3bthread.h"
0009 #include "k3bthreadjob.h"
0010 #include "k3bsimplejobhandler.h"
0011 #include "k3bglobals.h"
0012 
0013 #include <QDebug>
0014 #include <QDir>
0015 #include <QFileInfo>
0016 
0017 
0018 class K3b::DirSizeJob::Private
0019 {
0020 public:
0021     Private()
0022         : followSymlinks(false),
0023           totalSize(0),
0024           totalFiles(0),
0025           totalDirs(0),
0026           totalSymlinks(0) {
0027     }
0028 
0029     QList<QUrl> urls;
0030     bool followSymlinks;
0031 
0032     KIO::filesize_t totalSize;
0033     KIO::filesize_t totalFiles;
0034     KIO::filesize_t totalDirs;
0035     KIO::filesize_t totalSymlinks;
0036 };
0037 
0038 
0039 
0040 K3b::DirSizeJob::DirSizeJob( QObject* parent )
0041     : K3b::ThreadJob( new K3b::SimpleJobHandler(), parent ),
0042       d( new Private() )
0043 {
0044 }
0045 
0046 
0047 K3b::DirSizeJob::~DirSizeJob()
0048 {
0049     delete d;
0050     delete jobHandler();
0051 }
0052 
0053 
0054 KIO::filesize_t K3b::DirSizeJob::totalSize() const
0055 {
0056     return d->totalSize;
0057 }
0058 
0059 
0060 KIO::filesize_t K3b::DirSizeJob::totalFiles() const
0061 {
0062     return d->totalFiles;
0063 }
0064 
0065 
0066 KIO::filesize_t K3b::DirSizeJob::totalDirs() const
0067 {
0068     return d->totalDirs;
0069 }
0070 
0071 
0072 KIO::filesize_t K3b::DirSizeJob::totalSymlinks() const
0073 {
0074     return d->totalSymlinks;
0075 }
0076 
0077 
0078 void K3b::DirSizeJob::setUrls( const QList<QUrl>& urls )
0079 {
0080     d->urls = urls;
0081 }
0082 
0083 
0084 void K3b::DirSizeJob::setFollowSymlinks( bool b )
0085 {
0086     d->followSymlinks = b;
0087 }
0088 
0089 bool K3b::DirSizeJob::run()
0090 {
0091     d->totalSize = 0;
0092     d->totalFiles = 0;
0093     d->totalDirs = 0;
0094     d->totalSymlinks = 0;
0095 
0096     QStringList l;
0097     for( QList<QUrl>::const_iterator it = d->urls.constBegin();
0098          it != d->urls.constEnd(); ++it ) {
0099         const QUrl& url = *it;
0100 
0101         if( !url.isLocalFile() ) {
0102             qDebug() << "(K3b::DirSizeJob) no remote support.";
0103             return false;
0104         }
0105 
0106         l.append( url.toLocalFile() );
0107     }
0108 
0109     return countFiles( l, QString() );
0110 }
0111 
0112 
0113 bool K3b::DirSizeJob::countDir( const QString& dir )
0114 {
0115     QStringList l = QDir(dir).entryList( QDir::AllEntries|QDir::Hidden|QDir::System|QDir::NoDotAndDotDot );
0116     return countFiles( l, dir );
0117 }
0118 
0119 
0120 bool K3b::DirSizeJob::countFiles( const QStringList& l, const QString& dir )
0121 {
0122     for( QStringList::const_iterator it = l.begin();
0123          it != l.end(); ++it ) {
0124 
0125         if( canceled() )
0126             return false;
0127 
0128         k3b_struct_stat s;
0129         if( k3b_lstat( QFile::encodeName( dir + *it ), &s ) )
0130             return false;
0131 
0132         if( S_ISLNK( s.st_mode ) ) {
0133             ++d->totalSymlinks;
0134             if( d->followSymlinks ) {
0135                 if( k3b_stat( QFile::encodeName( dir + *it ), &s ) )
0136                     return false;
0137             }
0138         }
0139 
0140         if( S_ISDIR( s.st_mode ) ) {
0141             ++d->totalDirs;
0142             if( !countDir( dir + *it + '/' ) )
0143                 return false;
0144         }
0145         else if( !S_ISLNK( s.st_mode ) ) {
0146             ++d->totalFiles;
0147             d->totalSize += (KIO::filesize_t)s.st_size;
0148         }
0149     }
0150 
0151     return true;
0152 }
0153 
0154 #include "moc_k3bdirsizejob.cpp"