| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2005 David Faure <faure@kde.org> |
| 3 | SPDX-FileCopyrightText: 2007 Thiago Macieira <thiago@kde.org> |
| 4 | SPDX-FileCopyrightText: 2020 Stefan BrĂ¼ns <bruns@kde.org> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #ifndef BALOO_KIO_COMMON_UDSTOOLS_H_ |
| 10 | #define BALOO_KIO_COMMON_UDSTOOLS_H_ |
| 11 | |
| 12 | #include "usergroupcache.h" |
| 13 | #include "idutils.h" |
| 14 | #include <QUrl> |
| 15 | |
| 16 | namespace Baloo { |
| 17 | |
| 18 | class UdsFactory |
| 19 | { |
| 20 | public: |
| 21 | KIO::UDSEntry createUdsEntry(const QString& filePath) const; |
| 22 | |
| 23 | private: |
| 24 | UserGroupCache m_userGroupCache; |
| 25 | }; |
| 26 | |
| 27 | inline KIO::UDSEntry UdsFactory::createUdsEntry(const QString& filePath) const |
| 28 | { |
| 29 | KIO::UDSEntry uds; |
| 30 | |
| 31 | QT_STATBUF statBuf; |
| 32 | const QByteArray ba = QFile::encodeName(fileName: filePath); |
| 33 | if (filePathToStat(filePath: ba, statBuf) != 0) { |
| 34 | return uds; |
| 35 | } |
| 36 | |
| 37 | uds.reserve(size: 12); |
| 38 | uds.fastInsert(field: KIO::UDSEntry::UDS_DEVICE_ID, l: statBuf.st_dev); |
| 39 | uds.fastInsert(field: KIO::UDSEntry::UDS_INODE, l: statBuf.st_ino); |
| 40 | |
| 41 | mode_t type = statBuf.st_mode & S_IFMT; |
| 42 | mode_t access = statBuf.st_mode & 07777; |
| 43 | |
| 44 | uds.fastInsert(field: KIO::UDSEntry::UDS_SIZE, l: statBuf.st_size); |
| 45 | uds.fastInsert(field: KIO::UDSEntry::UDS_FILE_TYPE, l: type); |
| 46 | uds.fastInsert(field: KIO::UDSEntry::UDS_ACCESS, l: access); |
| 47 | uds.fastInsert(field: KIO::UDSEntry::UDS_MODIFICATION_TIME, l: statBuf.st_mtime); |
| 48 | uds.fastInsert(field: KIO::UDSEntry::UDS_ACCESS_TIME, l: statBuf.st_atime); |
| 49 | #ifndef Q_OS_WIN |
| 50 | uds.fastInsert(field: KIO::UDSEntry::UDS_USER, value: m_userGroupCache.getUserName(uid: KUserId(statBuf.st_uid))); |
| 51 | uds.fastInsert(field: KIO::UDSEntry::UDS_GROUP, value: m_userGroupCache.getGroupName(gid: KGroupId(statBuf.st_gid))); |
| 52 | #else |
| 53 | #pragma message("TODO: st_uid and st_gid are always zero, use GetSecurityInfo to find the owner") |
| 54 | #endif |
| 55 | |
| 56 | QUrl url = QUrl::fromLocalFile(localfile: filePath); |
| 57 | uds.fastInsert(field: KIO::UDSEntry::UDS_NAME, value: url.fileName()); |
| 58 | uds.fastInsert(field: KIO::UDSEntry::UDS_URL, value: url.url()); |
| 59 | uds.fastInsert(field: KIO::UDSEntry::UDS_LOCAL_PATH, value: filePath); |
| 60 | |
| 61 | return uds; |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | #endif |
| 66 | |