| 194 | } |
| 195 | |
| 196 | FileItem *FileSystem::createLocalFileItem(const QString &name, const QString &directory, bool virt) |
| 197 | { |
| 198 | const QDir dir = QDir(directory); |
| 199 | const QString path = dir.filePath(name); |
| 200 | const QByteArray pathByteArray = path.toLocal8Bit(); |
| 201 | const QString fileItemName = virt ? path : name; |
| 202 | const QUrl fileItemUrl = QUrl::fromLocalFile(path); |
| 203 | |
| 204 | // read file status; in case of error create a "broken" file item |
| 205 | QT_STATBUF stat_p; |
| 206 | memset(&stat_p, 0, sizeof(stat_p)); |
| 207 | if (QT_LSTAT(pathByteArray.data(), &stat_p) < 0) |
| 208 | return FileItem::createBroken(fileItemName, fileItemUrl); |
| 209 | |
| 210 | const KIO::filesize_t size = stat_p.st_size; |
| 211 | bool isDir = S_ISDIR(stat_p.st_mode); |
| 212 | const bool isLink = S_ISLNK(stat_p.st_mode); |
| 213 | |
| 214 | // for links, read link destination and determine whether it's broken or not |
| 215 | QString linkDestination; |
| 216 | bool brokenLink = false; |
| 217 | if (isLink) { |
| 218 | linkDestination = readLinkSafely(pathByteArray.data()); |
| 219 | |
| 220 | if (linkDestination.isNull()) { |
| 221 | brokenLink = true; |
| 222 | } else { |
| 223 | const QFileInfo linkFile(dir, linkDestination); |
| 224 | if (!linkFile.exists()) |
| 225 | brokenLink = true; |
| 226 | else if (linkFile.isDir()) |
| 227 | isDir = true; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // TODO use statx available in glibc >= 2.28 supporting creation time (btime) and more |
| 232 | |
| 233 | // create normal file item |
| 234 | return new FileItem(fileItemName, |
| 235 | fileItemUrl, |
| 236 | isDir, |
| 237 | size, |
| 238 | stat_p.st_mode, |
| 239 | stat_p.st_mtime, |
| 240 | stat_p.st_ctime, |
| 241 | stat_p.st_atime, |
| 242 | -1, |
| 243 | stat_p.st_uid, |
| 244 | stat_p.st_gid, |
| 245 | QString(), |
| 246 | QString(), |
| 247 | isLink, |
| 248 | linkDestination, |
| 249 | brokenLink); |
| 250 | } |
| 251 | |
| 252 | QString FileSystem::readLinkSafely(const char *path) |
| 253 | { |