File indexing completed on 2024-04-28 03:51:50

0001 /*
0002     SPDX-FileCopyrightText: 2005 David Faure <faure@kde.org>
0003     SPDX-FileCopyrightText: 2007 Thiago Macieira <thiago@kde.org>
0004     SPDX-FileCopyrightText: 2020 Stefan BrĂ¼ns <bruns@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef BALOO_KIO_COMMON_UDSTOOLS_H_
0010 #define BALOO_KIO_COMMON_UDSTOOLS_H_
0011 
0012 #include "usergroupcache.h"
0013 #include "idutils.h"
0014 #include <QUrl>
0015 
0016 namespace Baloo {
0017 
0018 class UdsFactory
0019 {
0020 public:
0021     KIO::UDSEntry createUdsEntry(const QString& filePath) const;
0022 
0023 private:
0024     UserGroupCache m_userGroupCache;
0025 };
0026 
0027 inline KIO::UDSEntry UdsFactory::createUdsEntry(const QString& filePath) const
0028 {
0029     KIO::UDSEntry uds;
0030 
0031     QT_STATBUF statBuf;
0032     const QByteArray ba = QFile::encodeName(filePath);
0033     if (filePathToStat(ba, statBuf) != 0) {
0034     return uds;
0035     }
0036 
0037     uds.reserve(12);
0038     uds.fastInsert(KIO::UDSEntry::UDS_DEVICE_ID, statBuf.st_dev);
0039     uds.fastInsert(KIO::UDSEntry::UDS_INODE, statBuf.st_ino);
0040 
0041     mode_t type = statBuf.st_mode & S_IFMT;
0042     mode_t access = statBuf.st_mode & 07777;
0043 
0044     uds.fastInsert(KIO::UDSEntry::UDS_SIZE, statBuf.st_size);
0045     uds.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, type);
0046     uds.fastInsert(KIO::UDSEntry::UDS_ACCESS, access);
0047     uds.fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME, statBuf.st_mtime);
0048     uds.fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME, statBuf.st_atime);
0049 #ifndef Q_OS_WIN
0050     uds.fastInsert(KIO::UDSEntry::UDS_USER, m_userGroupCache.getUserName(KUserId(statBuf.st_uid)));
0051     uds.fastInsert(KIO::UDSEntry::UDS_GROUP, m_userGroupCache.getGroupName(KGroupId(statBuf.st_gid)));
0052 #else
0053 #pragma message("TODO: st_uid and st_gid are always zero, use GetSecurityInfo to find the owner")
0054 #endif
0055 
0056     QUrl url = QUrl::fromLocalFile(filePath);
0057     uds.fastInsert(KIO::UDSEntry::UDS_NAME, url.fileName());
0058     uds.fastInsert(KIO::UDSEntry::UDS_URL, url.url());
0059     uds.fastInsert(KIO::UDSEntry::UDS_LOCAL_PATH, filePath);
0060 
0061     return uds;
0062 }
0063 
0064 }
0065 #endif