| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2024 Méven Car <meven@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | #ifndef STAT_UNIX_H |
| 7 | #define STAT_UNIX_H |
| 8 | |
| 9 | #include "kioglobal_p.h" |
| 10 | #include "qplatformdefs.h" |
| 11 | |
| 12 | #include <config-stat-unix.h> |
| 13 | #include <kio/statjob.h> |
| 14 | |
| 15 | #if HAVE_STATX |
| 16 | #include <sys/stat.h> |
| 17 | #include <sys/sysmacros.h> // for makedev() |
| 18 | #endif |
| 19 | |
| 20 | #ifdef Q_OS_WIN |
| 21 | // QT_LSTAT on Windows |
| 22 | #include "kioglobal_p.h" |
| 23 | #endif |
| 24 | |
| 25 | #if HAVE_STATX |
| 26 | // statx syscall is available |
| 27 | inline int LSTAT(const char *path, struct statx *buff, KIO::StatDetails details) |
| 28 | { |
| 29 | uint32_t mask = 0; |
| 30 | if (details & KIO::StatBasic) { |
| 31 | // filename, access, type, size, linkdest |
| 32 | mask |= STATX_SIZE | STATX_TYPE; |
| 33 | } |
| 34 | if (details & KIO::StatUser) { |
| 35 | // uid, gid |
| 36 | mask |= STATX_UID | STATX_GID; |
| 37 | } |
| 38 | if (details & KIO::StatTime) { |
| 39 | // atime, mtime, btime |
| 40 | mask |= STATX_ATIME | STATX_MTIME | STATX_BTIME; |
| 41 | } |
| 42 | if (details & KIO::StatInode) { |
| 43 | // dev, inode |
| 44 | mask |= STATX_INO; |
| 45 | } |
| 46 | return statx(AT_FDCWD, path: path, AT_SYMLINK_NOFOLLOW, mask: mask, buf: buff); |
| 47 | } |
| 48 | inline int STAT(const char *path, struct statx *buff, const KIO::StatDetails &details) |
| 49 | { |
| 50 | uint32_t mask = 0; |
| 51 | // KIO::StatAcl needs type |
| 52 | if (details & (KIO::StatBasic | KIO::StatAcl | KIO::StatResolveSymlink)) { |
| 53 | // filename, access, type |
| 54 | mask |= STATX_TYPE; |
| 55 | } |
| 56 | if (details & (KIO::StatBasic | KIO::StatResolveSymlink)) { |
| 57 | // size, linkdest |
| 58 | mask |= STATX_SIZE; |
| 59 | } |
| 60 | if (details & KIO::StatUser) { |
| 61 | // uid, gid |
| 62 | mask |= STATX_UID | STATX_GID; |
| 63 | } |
| 64 | if (details & KIO::StatTime) { |
| 65 | // atime, mtime, btime |
| 66 | mask |= STATX_ATIME | STATX_MTIME | STATX_BTIME; |
| 67 | } |
| 68 | // KIO::Inode is ignored as when STAT is called, the entry inode field has already been filled |
| 69 | return statx(AT_FDCWD, path: path, AT_STATX_SYNC_AS_STAT, mask: mask, buf: buff); |
| 70 | } |
| 71 | inline static uint16_t stat_mode(const struct statx &buf) |
| 72 | { |
| 73 | return buf.stx_mode; |
| 74 | } |
| 75 | inline static dev_t stat_dev(const struct statx &buf) |
| 76 | { |
| 77 | return makedev(buf.stx_dev_major, buf.stx_dev_minor); |
| 78 | } |
| 79 | inline static uint64_t stat_ino(const struct statx &buf) |
| 80 | { |
| 81 | return buf.stx_ino; |
| 82 | } |
| 83 | inline static size_t stat_size(const struct statx &buf) |
| 84 | { |
| 85 | return buf.stx_size; |
| 86 | } |
| 87 | inline static uint32_t stat_uid(const struct statx &buf) |
| 88 | { |
| 89 | return buf.stx_uid; |
| 90 | } |
| 91 | inline static uint32_t stat_gid(const struct statx &buf) |
| 92 | { |
| 93 | return buf.stx_gid; |
| 94 | } |
| 95 | inline static int64_t stat_atime(const struct statx &buf) |
| 96 | { |
| 97 | return buf.stx_atime.tv_sec; |
| 98 | } |
| 99 | inline static int64_t stat_mtime(const struct statx &buf) |
| 100 | { |
| 101 | return buf.stx_mtime.tv_sec; |
| 102 | } |
| 103 | #else |
| 104 | // regular stat struct |
| 105 | inline int LSTAT(const char *path, QT_STATBUF *buff, KIO::StatDetails details) |
| 106 | { |
| 107 | Q_UNUSED(details) |
| 108 | return QT_LSTAT(path, buff); |
| 109 | } |
| 110 | inline int STAT(const char *path, QT_STATBUF *buff, KIO::StatDetails details) |
| 111 | { |
| 112 | Q_UNUSED(details) |
| 113 | return QT_STAT(path, buff); |
| 114 | } |
| 115 | inline static mode_t stat_mode(const QT_STATBUF &buf) |
| 116 | { |
| 117 | return buf.st_mode; |
| 118 | } |
| 119 | inline static dev_t stat_dev(const QT_STATBUF &buf) |
| 120 | { |
| 121 | return buf.st_dev; |
| 122 | } |
| 123 | inline static ino_t stat_ino(const QT_STATBUF &buf) |
| 124 | { |
| 125 | return buf.st_ino; |
| 126 | } |
| 127 | inline static size_t stat_size(const QT_STATBUF &buf) |
| 128 | { |
| 129 | return buf.st_size; |
| 130 | } |
| 131 | inline static uint32_t stat_uid(const QT_STATBUF &buf) |
| 132 | { |
| 133 | return buf.st_uid; |
| 134 | } |
| 135 | inline static uint32_t stat_gid(const QT_STATBUF &buf) |
| 136 | { |
| 137 | return buf.st_gid; |
| 138 | } |
| 139 | inline static time_t stat_atime(const QT_STATBUF &buf) |
| 140 | { |
| 141 | return buf.st_atime; |
| 142 | } |
| 143 | inline static time_t stat_mtime(const QT_STATBUF &buf) |
| 144 | { |
| 145 | return buf.st_mtime; |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | #endif // STAT_UNIX_H |
| 150 | |