1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Ivan Komissarov <ABBAPOH@gmail.com> |
4 | ** Copyright (C) 2016 Intel Corporation. |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the QtCore module of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU Lesser General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
22 | ** packaging of this file. Please review the following information to |
23 | ** ensure the GNU Lesser General Public License version 3 requirements |
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
25 | ** |
26 | ** GNU General Public License Usage |
27 | ** Alternatively, this file may be used under the terms of the GNU |
28 | ** General Public License version 2.0 or (at your option) the GNU General |
29 | ** Public license version 3 or any later version approved by the KDE Free |
30 | ** Qt Foundation. The licenses are as published by the Free Software |
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
32 | ** included in the packaging of this file. Please review the following |
33 | ** information to ensure the GNU General Public License requirements will |
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
36 | ** |
37 | ** $QT_END_LICENSE$ |
38 | ** |
39 | ****************************************************************************/ |
40 | |
41 | #include "qstorageinfo_p.h" |
42 | |
43 | #include <QtCore/qdiriterator.h> |
44 | #include <QtCore/qfileinfo.h> |
45 | #include <QtCore/qtextstream.h> |
46 | |
47 | #include <QtCore/private/qcore_unix_p.h> |
48 | #include <QtCore/private/qlocale_tools_p.h> |
49 | |
50 | #include <errno.h> |
51 | #include <sys/stat.h> |
52 | |
53 | #if defined(Q_OS_BSD4) |
54 | # include <sys/mount.h> |
55 | # include <sys/statvfs.h> |
56 | #elif defined(Q_OS_ANDROID) |
57 | # include <sys/mount.h> |
58 | # include <sys/vfs.h> |
59 | # include <mntent.h> |
60 | #elif defined(Q_OS_LINUX) || defined(Q_OS_HURD) |
61 | # include <mntent.h> |
62 | # include <sys/statvfs.h> |
63 | # include <sys/sysmacros.h> |
64 | #elif defined(Q_OS_SOLARIS) |
65 | # include <sys/mnttab.h> |
66 | # include <sys/statvfs.h> |
67 | #elif defined(Q_OS_HAIKU) |
68 | # include <Directory.h> |
69 | # include <Path.h> |
70 | # include <Volume.h> |
71 | # include <VolumeRoster.h> |
72 | # include <fs_info.h> |
73 | # include <sys/statvfs.h> |
74 | #else |
75 | # include <sys/statvfs.h> |
76 | #endif |
77 | |
78 | #if defined(Q_OS_BSD4) |
79 | # if defined(Q_OS_NETBSD) |
80 | # define QT_STATFSBUF struct statvfs |
81 | # define QT_STATFS ::statvfs |
82 | # else |
83 | # define QT_STATFSBUF struct statfs |
84 | # define QT_STATFS ::statfs |
85 | # endif |
86 | |
87 | # if !defined(ST_RDONLY) |
88 | # define ST_RDONLY MNT_RDONLY |
89 | # endif |
90 | # if !defined(_STATFS_F_FLAGS) && !defined(Q_OS_NETBSD) |
91 | # define _STATFS_F_FLAGS 1 |
92 | # endif |
93 | #elif defined(Q_OS_ANDROID) |
94 | # define QT_STATFS ::statfs |
95 | # define QT_STATFSBUF struct statfs |
96 | # if !defined(ST_RDONLY) |
97 | # define ST_RDONLY 1 // hack for missing define on Android |
98 | # endif |
99 | #elif defined(Q_OS_HAIKU) |
100 | # define QT_STATFSBUF struct statvfs |
101 | # define QT_STATFS ::statvfs |
102 | #else |
103 | # if defined(QT_LARGEFILE_SUPPORT) |
104 | # define QT_STATFSBUF struct statvfs64 |
105 | # define QT_STATFS ::statvfs64 |
106 | # else |
107 | # define QT_STATFSBUF struct statvfs |
108 | # define QT_STATFS ::statvfs |
109 | # endif // QT_LARGEFILE_SUPPORT |
110 | #endif // Q_OS_BSD4 |
111 | |
112 | #if __has_include(<paths.h>) |
113 | # include <paths.h> |
114 | #endif |
115 | #ifndef _PATH_MOUNTED |
116 | # define _PATH_MOUNTED "/etc/mnttab" |
117 | #endif |
118 | |
119 | QT_BEGIN_NAMESPACE |
120 | |
121 | class QStorageIterator |
122 | { |
123 | public: |
124 | QStorageIterator(); |
125 | ~QStorageIterator(); |
126 | |
127 | inline bool isValid() const; |
128 | inline bool next(); |
129 | inline QString rootPath() const; |
130 | inline QByteArray fileSystemType() const; |
131 | inline QByteArray device() const; |
132 | inline QByteArray options() const; |
133 | inline QByteArray subvolume() const; |
134 | private: |
135 | #if defined(Q_OS_BSD4) |
136 | QT_STATFSBUF *stat_buf; |
137 | int entryCount; |
138 | int currentIndex; |
139 | #elif defined(Q_OS_SOLARIS) |
140 | FILE *fp; |
141 | mnttab mnt; |
142 | #elif defined(Q_OS_ANDROID) |
143 | QFile file; |
144 | QByteArray m_rootPath; |
145 | QByteArray m_fileSystemType; |
146 | QByteArray m_device; |
147 | QByteArray m_options; |
148 | #elif defined(Q_OS_LINUX) || defined(Q_OS_HURD) |
149 | struct mountinfoent : public mntent { |
150 | // Details from proc(5) section from /proc/<pid>/mountinfo: |
151 | //(1) mount ID: a unique ID for the mount (may be reused after umount(2)). |
152 | int mount_id; |
153 | //(2) parent ID: the ID of the parent mount (or of self for the top of the mount tree). |
154 | // int parent_id; |
155 | //(3) major:minor: the value of st_dev for files on this filesystem (see stat(2)). |
156 | dev_t rdev; |
157 | //(4) root: the pathname of the directory in the filesystem which forms the root of this mount. |
158 | char *subvolume; |
159 | //(5) mount point: the pathname of the mount point relative to the process's root directory. |
160 | // char *mnt_dir; // in mntent |
161 | //(6) mount options: per-mount options. |
162 | // char *mnt_opts; // in mntent |
163 | //(7) optional fields: zero or more fields of the form "tag[:value]"; see below. |
164 | // int flags; |
165 | //(8) separator: the end of the optional fields is marked by a single hyphen. |
166 | |
167 | //(9) filesystem type: the filesystem type in the form "type[.subtype]". |
168 | // char *mnt_type; // in mntent |
169 | //(10) mount source: filesystem-specific information or "none". |
170 | // char *mnt_fsname; // in mntent |
171 | //(11) super options: per-superblock options. |
172 | char *superopts; |
173 | }; |
174 | |
175 | FILE *fp; |
176 | QByteArray buffer; |
177 | mountinfoent mnt; |
178 | bool usingMountinfo; |
179 | #elif defined(Q_OS_HAIKU) |
180 | BVolumeRoster m_volumeRoster; |
181 | |
182 | QByteArray m_rootPath; |
183 | QByteArray m_fileSystemType; |
184 | QByteArray m_device; |
185 | #endif |
186 | }; |
187 | |
188 | template <typename String> |
189 | static bool isParentOf(const String &parent, const QString &dirName) |
190 | { |
191 | return dirName.startsWith(parent) && |
192 | (dirName.size() == parent.size() || dirName.at(i: parent.size()) == QLatin1Char('/') || |
193 | parent.size() == 1); |
194 | } |
195 | |
196 | static bool shouldIncludeFs(const QStorageIterator &it) |
197 | { |
198 | /* |
199 | * This function implements a heuristic algorithm to determine whether a |
200 | * given mount should be reported to the user. Our objective is to list |
201 | * only entries that the end-user would find useful. |
202 | * |
203 | * We therefore ignore: |
204 | * - mounted in /dev, /proc, /sys: special mounts |
205 | * (this will catch /sys/fs/cgroup, /proc/sys/fs/binfmt_misc, /dev/pts, |
206 | * some of which are tmpfs on Linux) |
207 | * - mounted in /var/run or /var/lock: most likely pseudofs |
208 | * (on earlier systemd versions, /var/run was a bind-mount of /run, so |
209 | * everything would be unnecessarily duplicated) |
210 | * - filesystem type is "rootfs": artifact of the root-pivot on some Linux |
211 | * initrd |
212 | * - if the filesystem total size is zero, it's a pseudo-fs (not checked here). |
213 | */ |
214 | |
215 | QString mountDir = it.rootPath(); |
216 | if (isParentOf(parent: QLatin1String("/dev" ), dirName: mountDir) |
217 | || isParentOf(parent: QLatin1String("/proc" ), dirName: mountDir) |
218 | || isParentOf(parent: QLatin1String("/sys" ), dirName: mountDir) |
219 | || isParentOf(parent: QLatin1String("/var/run" ), dirName: mountDir) |
220 | || isParentOf(parent: QLatin1String("/var/lock" ), dirName: mountDir)) { |
221 | return false; |
222 | } |
223 | |
224 | #if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) |
225 | if (it.fileSystemType() == "rootfs" ) |
226 | return false; |
227 | #endif |
228 | |
229 | // size checking in mountedVolumes() |
230 | return true; |
231 | } |
232 | |
233 | #if defined(Q_OS_BSD4) |
234 | |
235 | #ifndef MNT_NOWAIT |
236 | # define MNT_NOWAIT 0 |
237 | #endif |
238 | |
239 | inline QStorageIterator::QStorageIterator() |
240 | : entryCount(::getmntinfo(&stat_buf, MNT_NOWAIT)), |
241 | currentIndex(-1) |
242 | { |
243 | } |
244 | |
245 | inline QStorageIterator::~QStorageIterator() |
246 | { |
247 | } |
248 | |
249 | inline bool QStorageIterator::isValid() const |
250 | { |
251 | return entryCount != -1; |
252 | } |
253 | |
254 | inline bool QStorageIterator::next() |
255 | { |
256 | return ++currentIndex < entryCount; |
257 | } |
258 | |
259 | inline QString QStorageIterator::rootPath() const |
260 | { |
261 | return QFile::decodeName(stat_buf[currentIndex].f_mntonname); |
262 | } |
263 | |
264 | inline QByteArray QStorageIterator::fileSystemType() const |
265 | { |
266 | return QByteArray(stat_buf[currentIndex].f_fstypename); |
267 | } |
268 | |
269 | inline QByteArray QStorageIterator::device() const |
270 | { |
271 | return QByteArray(stat_buf[currentIndex].f_mntfromname); |
272 | } |
273 | |
274 | inline QByteArray QStorageIterator::options() const |
275 | { |
276 | return QByteArray(); |
277 | } |
278 | |
279 | inline QByteArray QStorageIterator::subvolume() const |
280 | { |
281 | return QByteArray(); |
282 | } |
283 | #elif defined(Q_OS_SOLARIS) |
284 | |
285 | inline QStorageIterator::QStorageIterator() |
286 | { |
287 | const int fd = qt_safe_open(_PATH_MOUNTED, O_RDONLY); |
288 | fp = ::fdopen(fd, "r" ); |
289 | } |
290 | |
291 | inline QStorageIterator::~QStorageIterator() |
292 | { |
293 | if (fp) |
294 | ::fclose(fp); |
295 | } |
296 | |
297 | inline bool QStorageIterator::isValid() const |
298 | { |
299 | return fp != nullptr; |
300 | } |
301 | |
302 | inline bool QStorageIterator::next() |
303 | { |
304 | return ::getmntent(fp, &mnt) == 0; |
305 | } |
306 | |
307 | inline QString QStorageIterator::rootPath() const |
308 | { |
309 | return QFile::decodeName(mnt.mnt_mountp); |
310 | } |
311 | |
312 | inline QByteArray QStorageIterator::fileSystemType() const |
313 | { |
314 | return QByteArray(mnt.mnt_fstype); |
315 | } |
316 | |
317 | inline QByteArray QStorageIterator::device() const |
318 | { |
319 | return QByteArray(mnt.mnt_mntopts); |
320 | } |
321 | |
322 | inline QByteArray QStorageIterator::subvolume() const |
323 | { |
324 | return QByteArray(); |
325 | } |
326 | #elif defined(Q_OS_ANDROID) |
327 | |
328 | inline QStorageIterator::QStorageIterator() |
329 | { |
330 | file.setFileName(_PATH_MOUNTED); |
331 | file.open(QIODevice::ReadOnly | QIODevice::Text); |
332 | } |
333 | |
334 | inline QStorageIterator::~QStorageIterator() |
335 | { |
336 | } |
337 | |
338 | inline bool QStorageIterator::isValid() const |
339 | { |
340 | return file.isOpen(); |
341 | } |
342 | |
343 | inline bool QStorageIterator::next() |
344 | { |
345 | QList<QByteArray> data; |
346 | // If file is virtual, file.readLine() may succeed even when file.atEnd(). |
347 | do { |
348 | const QByteArray line = file.readLine(); |
349 | if (line.isEmpty() && file.atEnd()) |
350 | return false; |
351 | data = line.split(' '); |
352 | } while (data.count() < 4); |
353 | |
354 | m_device = data.at(0); |
355 | m_rootPath = data.at(1); |
356 | m_fileSystemType = data.at(2); |
357 | m_options = data.at(3); |
358 | |
359 | return true; |
360 | } |
361 | |
362 | inline QString QStorageIterator::rootPath() const |
363 | { |
364 | return QFile::decodeName(m_rootPath); |
365 | } |
366 | |
367 | inline QByteArray QStorageIterator::fileSystemType() const |
368 | { |
369 | return m_fileSystemType; |
370 | } |
371 | |
372 | inline QByteArray QStorageIterator::device() const |
373 | { |
374 | return m_device; |
375 | } |
376 | |
377 | inline QByteArray QStorageIterator::options() const |
378 | { |
379 | return m_options; |
380 | } |
381 | |
382 | inline QByteArray QStorageIterator::subvolume() const |
383 | { |
384 | return QByteArray(); |
385 | } |
386 | #elif defined(Q_OS_LINUX) || defined(Q_OS_HURD) |
387 | |
388 | static const int bufferSize = 1024; // 2 paths (mount point+device) and metainfo; |
389 | // should be enough |
390 | |
391 | inline QStorageIterator::QStorageIterator() : |
392 | buffer(QByteArray(bufferSize, 0)) |
393 | { |
394 | fp = nullptr; |
395 | |
396 | #ifdef Q_OS_LINUX |
397 | // first, try to open /proc/self/mountinfo, which has more details |
398 | fp = ::fopen(filename: "/proc/self/mountinfo" , modes: "re" ); |
399 | #endif |
400 | if (fp) { |
401 | usingMountinfo = true; |
402 | } else { |
403 | usingMountinfo = false; |
404 | fp = ::setmntent(_PATH_MOUNTED, mode: "r" ); |
405 | } |
406 | } |
407 | |
408 | inline QStorageIterator::~QStorageIterator() |
409 | { |
410 | if (fp) { |
411 | if (usingMountinfo) |
412 | ::fclose(stream: fp); |
413 | else |
414 | ::endmntent(stream: fp); |
415 | } |
416 | } |
417 | |
418 | inline bool QStorageIterator::isValid() const |
419 | { |
420 | return fp != nullptr; |
421 | } |
422 | |
423 | inline bool QStorageIterator::next() |
424 | { |
425 | mnt.subvolume = nullptr; |
426 | mnt.superopts = nullptr; |
427 | if (!usingMountinfo) |
428 | return ::getmntent_r(stream: fp, result: &mnt, buffer: buffer.data(), bufsize: buffer.size()) != nullptr; |
429 | |
430 | // Helper function to parse paths that the kernel inserts escape sequences |
431 | // for. The unescaped string is left at \a src and is properly |
432 | // NUL-terminated. Returns a pointer to the delimiter that terminated the |
433 | // path, or nullptr if it failed. |
434 | auto parseMangledPath = [](char *src) { |
435 | // The kernel escapes with octal the following characters: |
436 | // space ' ', tab '\t', backslask '\\', and newline '\n' |
437 | char *dst = src; |
438 | while (*src) { |
439 | switch (*src) { |
440 | case ' ': |
441 | // Unescaped space: end of the field. |
442 | *dst = '\0'; |
443 | return src; |
444 | |
445 | default: |
446 | *dst++ = *src++; |
447 | break; |
448 | |
449 | case '\\': |
450 | // It always uses exactly three octal characters. |
451 | ++src; |
452 | char c = (*src++ - '0') << 6; |
453 | c |= (*src++ - '0') << 3; |
454 | c |= (*src++ - '0'); |
455 | *dst++ = c; |
456 | break; |
457 | } |
458 | } |
459 | |
460 | // Found a NUL before the end of the field. |
461 | src = nullptr; |
462 | return src; |
463 | }; |
464 | |
465 | char *ptr = buffer.data(); |
466 | if (fgets(s: ptr, n: buffer.size(), stream: fp) == nullptr) |
467 | return false; |
468 | |
469 | size_t len = strlen(s: buffer.data()); |
470 | if (len == 0) |
471 | return false; |
472 | while (Q_UNLIKELY(ptr[len - 1] != '\n' && !feof(fp))) { |
473 | // buffer wasn't large enough. Enlarge and try again. |
474 | // (we're readidng from the kernel, so OOM is unlikely) |
475 | buffer.resize(size: (buffer.size() + 4096) & ~4095); |
476 | ptr = buffer.data(); |
477 | if (fgets(s: ptr + len, n: buffer.size() - len, stream: fp) == nullptr) |
478 | return false; |
479 | |
480 | len += strlen(s: ptr + len); |
481 | Q_ASSERT(len < size_t(buffer.size())); |
482 | } |
483 | ptr[len - 1] = '\0'; |
484 | |
485 | // parse the line |
486 | bool ok; |
487 | mnt.mnt_freq = 0; |
488 | mnt.mnt_passno = 0; |
489 | |
490 | mnt.mount_id = qstrtoll(nptr: ptr, endptr: const_cast<const char **>(&ptr), base: 10, ok: &ok); |
491 | if (!ptr || !ok) |
492 | return false; |
493 | |
494 | int parent_id = qstrtoll(nptr: ptr, endptr: const_cast<const char **>(&ptr), base: 10, ok: &ok); |
495 | Q_UNUSED(parent_id); |
496 | if (!ptr || !ok) |
497 | return false; |
498 | |
499 | int rdevmajor = qstrtoll(nptr: ptr, endptr: const_cast<const char **>(&ptr), base: 10, ok: &ok); |
500 | if (!ptr || !ok) |
501 | return false; |
502 | if (*ptr != ':') |
503 | return false; |
504 | int rdevminor = qstrtoll(nptr: ptr + 1, endptr: const_cast<const char **>(&ptr), base: 10, ok: &ok); |
505 | if (!ptr || !ok) |
506 | return false; |
507 | mnt.rdev = makedev(rdevmajor, rdevminor); |
508 | |
509 | if (*ptr != ' ') |
510 | return false; |
511 | |
512 | mnt.subvolume = ++ptr; |
513 | ptr = parseMangledPath(ptr); |
514 | if (!ptr) |
515 | return false; |
516 | |
517 | // unset a subvolume of "/" -- it's not a *sub* volume |
518 | if (mnt.subvolume + 1 == ptr) |
519 | *mnt.subvolume = '\0'; |
520 | |
521 | mnt.mnt_dir = ++ptr; |
522 | ptr = parseMangledPath(ptr); |
523 | if (!ptr) |
524 | return false; |
525 | |
526 | mnt.mnt_opts = ++ptr; |
527 | ptr = strchr(s: ptr, c: ' '); |
528 | if (!ptr) |
529 | return false; |
530 | |
531 | // we don't parse the flags, so just find the separator |
532 | if (char *const dashed = strstr(haystack: ptr, needle: " - " )) { |
533 | *ptr = '\0'; |
534 | ptr = dashed + strlen(s: " - " ) - 1; |
535 | } else { |
536 | return false; |
537 | } |
538 | |
539 | mnt.mnt_type = ++ptr; |
540 | ptr = strchr(s: ptr, c: ' '); |
541 | if (!ptr) |
542 | return false; |
543 | *ptr = '\0'; |
544 | |
545 | mnt.mnt_fsname = ++ptr; |
546 | ptr = parseMangledPath(ptr); |
547 | if (!ptr) |
548 | return false; |
549 | |
550 | mnt.superopts = ++ptr; |
551 | ptr += strcspn(s: ptr, reject: " \n" ); |
552 | *ptr = '\0'; |
553 | |
554 | return true; |
555 | } |
556 | |
557 | inline QString QStorageIterator::rootPath() const |
558 | { |
559 | return QFile::decodeName(localFileName: mnt.mnt_dir); |
560 | } |
561 | |
562 | inline QByteArray QStorageIterator::fileSystemType() const |
563 | { |
564 | return QByteArray(mnt.mnt_type); |
565 | } |
566 | |
567 | inline QByteArray QStorageIterator::device() const |
568 | { |
569 | // check that the device exists |
570 | if (mnt.mnt_fsname[0] == '/' && access(name: mnt.mnt_fsname, F_OK) != 0) { |
571 | // It doesn't, so let's try to resolve the dev_t from /dev/block. |
572 | // Note how strlen("4294967295") == digits10 + 1, so we need to add 1 |
573 | // for each number, plus the ':'. |
574 | char buf[sizeof("/dev/block/" ) + 2 * std::numeric_limits<unsigned>::digits10 + 3]; |
575 | QByteArray dev(PATH_MAX, Qt::Uninitialized); |
576 | char *devdata = dev.data(); |
577 | |
578 | snprintf(s: buf, maxlen: sizeof(buf), format: "/dev/block/%u:%u" , major(mnt.rdev), minor(mnt.rdev)); |
579 | if (realpath(name: buf, resolved: devdata)) { |
580 | dev.truncate(pos: strlen(s: devdata)); |
581 | return dev; |
582 | } |
583 | } |
584 | return QByteArray(mnt.mnt_fsname); |
585 | } |
586 | |
587 | inline QByteArray QStorageIterator::options() const |
588 | { |
589 | // Merge the two options, starting with the superblock options and letting |
590 | // the per-mount options override. |
591 | const char *superopts = mnt.superopts; |
592 | |
593 | // Both mnt_opts and superopts start with "ro" or "rw", so we can skip the |
594 | // superblock's field (see show_mountinfo() in fs/proc_namespace.c). |
595 | if (superopts && superopts[0] == 'r') { |
596 | if (superopts[2] == '\0') // no other superopts besides "ro" / "rw"? |
597 | superopts = nullptr; |
598 | else if (superopts[2] == ',') |
599 | superopts += 3; |
600 | } |
601 | |
602 | if (superopts) |
603 | return QByteArray(superopts) + ',' + mnt.mnt_opts; |
604 | return QByteArray(mnt.mnt_opts); |
605 | } |
606 | |
607 | inline QByteArray QStorageIterator::subvolume() const |
608 | { |
609 | return QByteArray(mnt.subvolume); |
610 | } |
611 | #elif defined(Q_OS_HAIKU) |
612 | inline QStorageIterator::QStorageIterator() |
613 | { |
614 | } |
615 | |
616 | inline QStorageIterator::~QStorageIterator() |
617 | { |
618 | } |
619 | |
620 | inline bool QStorageIterator::isValid() const |
621 | { |
622 | return true; |
623 | } |
624 | |
625 | inline bool QStorageIterator::next() |
626 | { |
627 | BVolume volume; |
628 | |
629 | if (m_volumeRoster.GetNextVolume(&volume) != B_OK) |
630 | return false; |
631 | |
632 | BDirectory directory; |
633 | if (volume.GetRootDirectory(&directory) != B_OK) |
634 | return false; |
635 | |
636 | const BPath path(&directory); |
637 | |
638 | fs_info fsInfo; |
639 | memset(&fsInfo, 0, sizeof(fsInfo)); |
640 | |
641 | if (fs_stat_dev(volume.Device(), &fsInfo) != 0) |
642 | return false; |
643 | |
644 | m_rootPath = path.Path(); |
645 | m_fileSystemType = QByteArray(fsInfo.fsh_name); |
646 | |
647 | const QByteArray deviceName(fsInfo.device_name); |
648 | m_device = (deviceName.isEmpty() ? QByteArray::number(qint32(volume.Device())) : deviceName); |
649 | |
650 | return true; |
651 | } |
652 | |
653 | inline QString QStorageIterator::rootPath() const |
654 | { |
655 | return QFile::decodeName(m_rootPath); |
656 | } |
657 | |
658 | inline QByteArray QStorageIterator::fileSystemType() const |
659 | { |
660 | return m_fileSystemType; |
661 | } |
662 | |
663 | inline QByteArray QStorageIterator::device() const |
664 | { |
665 | return m_device; |
666 | } |
667 | |
668 | inline QByteArray QStorageIterator::options() const |
669 | { |
670 | return QByteArray(); |
671 | } |
672 | |
673 | inline QByteArray QStorageIterator::subvolume() const |
674 | { |
675 | return QByteArray(); |
676 | } |
677 | #else |
678 | |
679 | inline QStorageIterator::QStorageIterator() |
680 | { |
681 | } |
682 | |
683 | inline QStorageIterator::~QStorageIterator() |
684 | { |
685 | } |
686 | |
687 | inline bool QStorageIterator::isValid() const |
688 | { |
689 | return false; |
690 | } |
691 | |
692 | inline bool QStorageIterator::next() |
693 | { |
694 | return false; |
695 | } |
696 | |
697 | inline QString QStorageIterator::rootPath() const |
698 | { |
699 | return QString(); |
700 | } |
701 | |
702 | inline QByteArray QStorageIterator::fileSystemType() const |
703 | { |
704 | return QByteArray(); |
705 | } |
706 | |
707 | inline QByteArray QStorageIterator::device() const |
708 | { |
709 | return QByteArray(); |
710 | } |
711 | |
712 | inline QByteArray QStorageIterator::options() const |
713 | { |
714 | return QByteArray(); |
715 | } |
716 | |
717 | inline QByteArray QStorageIterator::subvolume() const |
718 | { |
719 | return QByteArray(); |
720 | } |
721 | #endif |
722 | |
723 | void QStorageInfoPrivate::initRootPath() |
724 | { |
725 | rootPath = QFileInfo(rootPath).canonicalFilePath(); |
726 | |
727 | if (rootPath.isEmpty()) |
728 | return; |
729 | |
730 | QStorageIterator it; |
731 | if (!it.isValid()) { |
732 | rootPath = QStringLiteral("/" ); |
733 | return; |
734 | } |
735 | |
736 | int maxLength = 0; |
737 | const QString oldRootPath = rootPath; |
738 | rootPath.clear(); |
739 | |
740 | while (it.next()) { |
741 | const QString mountDir = it.rootPath(); |
742 | const QByteArray fsName = it.fileSystemType(); |
743 | // we try to find most suitable entry |
744 | if (isParentOf(parent: mountDir, dirName: oldRootPath) && maxLength < mountDir.length()) { |
745 | maxLength = mountDir.length(); |
746 | rootPath = mountDir; |
747 | device = it.device(); |
748 | fileSystemType = fsName; |
749 | subvolume = it.subvolume(); |
750 | } |
751 | } |
752 | } |
753 | |
754 | #ifdef Q_OS_LINUX |
755 | // udev encodes the labels with ID_LABEL_FS_ENC which is done with |
756 | // blkid_encode_string(). Within this function some 1-byte utf-8 |
757 | // characters not considered safe (e.g. '\' or ' ') are encoded as hex |
758 | static QString decodeFsEncString(const QString &str) |
759 | { |
760 | QString decoded; |
761 | decoded.reserve(asize: str.size()); |
762 | |
763 | int i = 0; |
764 | while (i < str.size()) { |
765 | if (i <= str.size() - 4) { // we need at least four characters \xAB |
766 | if (str.at(i) == QLatin1Char('\\') && |
767 | str.at(i: i+1) == QLatin1Char('x')) { |
768 | bool bOk; |
769 | const int code = str.midRef(position: i+2, n: 2).toInt(ok: &bOk, base: 16); |
770 | // only decode characters between 0x20 and 0x7f but not |
771 | // the backslash to prevent collisions |
772 | if (bOk && code >= 0x20 && code < 0x80 && code != '\\') { |
773 | decoded += QChar(code); |
774 | i += 4; |
775 | continue; |
776 | } |
777 | } |
778 | } |
779 | decoded += str.at(i); |
780 | ++i; |
781 | } |
782 | return decoded; |
783 | } |
784 | #endif |
785 | |
786 | static inline QString retrieveLabel(const QByteArray &device) |
787 | { |
788 | #ifdef Q_OS_LINUX |
789 | static const char pathDiskByLabel[] = "/dev/disk/by-label" ; |
790 | |
791 | QFileInfo devinfo(QFile::decodeName(localFileName: device)); |
792 | QString devicePath = devinfo.canonicalFilePath(); |
793 | |
794 | QDirIterator it(QLatin1String(pathDiskByLabel), QDir::NoDotAndDotDot); |
795 | while (it.hasNext()) { |
796 | it.next(); |
797 | QFileInfo fileInfo(it.fileInfo()); |
798 | if (fileInfo.isSymLink() && fileInfo.symLinkTarget() == devicePath) |
799 | return decodeFsEncString(str: fileInfo.fileName()); |
800 | } |
801 | #elif defined Q_OS_HAIKU |
802 | fs_info fsInfo; |
803 | memset(&fsInfo, 0, sizeof(fsInfo)); |
804 | |
805 | int32 pos = 0; |
806 | dev_t dev; |
807 | while ((dev = next_dev(&pos)) >= 0) { |
808 | if (fs_stat_dev(dev, &fsInfo) != 0) |
809 | continue; |
810 | |
811 | if (qstrcmp(fsInfo.device_name, device.constData()) == 0) |
812 | return QString::fromLocal8Bit(fsInfo.volume_name); |
813 | } |
814 | #else |
815 | Q_UNUSED(device); |
816 | #endif |
817 | |
818 | return QString(); |
819 | } |
820 | |
821 | void QStorageInfoPrivate::doStat() |
822 | { |
823 | initRootPath(); |
824 | if (rootPath.isEmpty()) |
825 | return; |
826 | |
827 | retrieveVolumeInfo(); |
828 | name = retrieveLabel(device); |
829 | } |
830 | |
831 | void QStorageInfoPrivate::retrieveVolumeInfo() |
832 | { |
833 | QT_STATFSBUF statfs_buf; |
834 | int result; |
835 | EINTR_LOOP(result, QT_STATFS(QFile::encodeName(rootPath).constData(), &statfs_buf)); |
836 | if (result == 0) { |
837 | valid = true; |
838 | ready = true; |
839 | |
840 | #if defined(Q_OS_INTEGRITY) || (defined(Q_OS_BSD4) && !defined(Q_OS_NETBSD)) || defined(Q_OS_RTEMS) |
841 | bytesTotal = statfs_buf.f_blocks * statfs_buf.f_bsize; |
842 | bytesFree = statfs_buf.f_bfree * statfs_buf.f_bsize; |
843 | bytesAvailable = statfs_buf.f_bavail * statfs_buf.f_bsize; |
844 | #else |
845 | bytesTotal = statfs_buf.f_blocks * statfs_buf.f_frsize; |
846 | bytesFree = statfs_buf.f_bfree * statfs_buf.f_frsize; |
847 | bytesAvailable = statfs_buf.f_bavail * statfs_buf.f_frsize; |
848 | #endif |
849 | blockSize = statfs_buf.f_bsize; |
850 | #if defined(Q_OS_ANDROID) || defined(Q_OS_BSD4) || defined(Q_OS_INTEGRITY) || defined(Q_OS_RTEMS) |
851 | #if defined(_STATFS_F_FLAGS) |
852 | readOnly = (statfs_buf.f_flags & ST_RDONLY) != 0; |
853 | #endif |
854 | #else |
855 | readOnly = (statfs_buf.f_flag & ST_RDONLY) != 0; |
856 | #endif |
857 | } |
858 | } |
859 | |
860 | QList<QStorageInfo> QStorageInfoPrivate::mountedVolumes() |
861 | { |
862 | QStorageIterator it; |
863 | if (!it.isValid()) |
864 | return QList<QStorageInfo>() << root(); |
865 | |
866 | QList<QStorageInfo> volumes; |
867 | |
868 | while (it.next()) { |
869 | if (!shouldIncludeFs(it)) |
870 | continue; |
871 | |
872 | const QString mountDir = it.rootPath(); |
873 | QStorageInfo info(mountDir); |
874 | info.d->device = it.device(); |
875 | info.d->fileSystemType = it.fileSystemType(); |
876 | info.d->subvolume = it.subvolume(); |
877 | if (info.bytesTotal() == 0 && info != root()) |
878 | continue; |
879 | volumes.append(t: info); |
880 | } |
881 | |
882 | return volumes; |
883 | } |
884 | |
885 | QStorageInfo QStorageInfoPrivate::root() |
886 | { |
887 | return QStorageInfo(QStringLiteral("/" )); |
888 | } |
889 | |
890 | QT_END_NAMESPACE |
891 | |