Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qstorageinfo_unix.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||||||||
2 | - | |||||||||||||||||||
3 | - | |||||||||||||||||||
4 | - | |||||||||||||||||||
5 | - | |||||||||||||||||||
6 | class QStorageIterator | - | ||||||||||||||||||
7 | { | - | ||||||||||||||||||
8 | public: | - | ||||||||||||||||||
9 | QStorageIterator(); | - | ||||||||||||||||||
10 | ~QStorageIterator(); | - | ||||||||||||||||||
11 | - | |||||||||||||||||||
12 | inline bool isValid() const; | - | ||||||||||||||||||
13 | inline bool next(); | - | ||||||||||||||||||
14 | inline QString rootPath() const; | - | ||||||||||||||||||
15 | inline QByteArray fileSystemType() const; | - | ||||||||||||||||||
16 | inline QByteArray device() const; | - | ||||||||||||||||||
17 | private: | - | ||||||||||||||||||
18 | FILE *fp; | - | ||||||||||||||||||
19 | mntent mnt; | - | ||||||||||||||||||
20 | QByteArray buffer; | - | ||||||||||||||||||
21 | - | |||||||||||||||||||
22 | - | |||||||||||||||||||
23 | - | |||||||||||||||||||
24 | - | |||||||||||||||||||
25 | - | |||||||||||||||||||
26 | - | |||||||||||||||||||
27 | - | |||||||||||||||||||
28 | }; | - | ||||||||||||||||||
29 | - | |||||||||||||||||||
30 | template <typename String> | - | ||||||||||||||||||
31 | static bool isParentOf(const String &parent, const QString &dirName) | - | ||||||||||||||||||
32 | { | - | ||||||||||||||||||
33 | return executed 762 times by 1 test: dirName.startsWith(parent)return dirName.startsWith(parent) && (dirName.size() == parent.size() || dirName.at(parent.size()) == QLatin1Char('/') || parent.size() == 1); Executed by:
executed 762 times by 1 test: return dirName.startsWith(parent) && (dirName.size() == parent.size() || dirName.at(parent.size()) == QLatin1Char('/') || parent.size() == 1); Executed by:
| 92-762 | ||||||||||||||||||
34 | (dirName.size() == parent.size()
executed 762 times by 1 test: return dirName.startsWith(parent) && (dirName.size() == parent.size() || dirName.at(parent.size()) == QLatin1Char('/') || parent.size() == 1); Executed by:
| 24-762 | ||||||||||||||||||
35 | parent.size() == 1
executed 762 times by 1 test: return dirName.startsWith(parent) && (dirName.size() == parent.size() || dirName.at(parent.size()) == QLatin1Char('/') || parent.size() == 1); Executed by:
| 0-762 | ||||||||||||||||||
36 | } | - | ||||||||||||||||||
37 | - | |||||||||||||||||||
38 | static bool isPseudoFs(const QStorageIterator &it) | - | ||||||||||||||||||
39 | { | - | ||||||||||||||||||
40 | QString mountDir = it.rootPath(); | - | ||||||||||||||||||
41 | if (isParentOf(QLatin1String("/dev"), mountDir)
| 10-50 | ||||||||||||||||||
42 | || isParentOf(QLatin1String("/proc"), mountDir)
| 4-46 | ||||||||||||||||||
43 | || isParentOf(QLatin1String("/sys"), mountDir)
| 18-28 | ||||||||||||||||||
44 | || isParentOf(QLatin1String("/var/run"), mountDir)
| 0-18 | ||||||||||||||||||
45 | || isParentOf(QLatin1String("/var/lock"), mountDir)
| 0-18 | ||||||||||||||||||
46 | return executed 42 times by 1 test: true;return true; Executed by:
executed 42 times by 1 test: return true; Executed by:
| 42 | ||||||||||||||||||
47 | } | - | ||||||||||||||||||
48 | - | |||||||||||||||||||
49 | QByteArray type = it.fileSystemType(); | - | ||||||||||||||||||
50 | if (type == "tmpfs"
| 6-12 | ||||||||||||||||||
51 | return executed 6 times by 1 test: false;return false; Executed by:
executed 6 times by 1 test: return false; Executed by:
| 6 | ||||||||||||||||||
52 | - | |||||||||||||||||||
53 | if (type == "rootfs"
| 2-10 | ||||||||||||||||||
54 | return executed 4 times by 1 test: true;return true; Executed by:
executed 4 times by 1 test: return true; Executed by:
| 4 | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | - | |||||||||||||||||||
57 | if (!it.device().startsWith('/')
| 4 | ||||||||||||||||||
58 | return executed 4 times by 1 test: true;return true; Executed by:
executed 4 times by 1 test: return true; Executed by:
| 4 | ||||||||||||||||||
59 | - | |||||||||||||||||||
60 | return executed 4 times by 1 test: false;return false; Executed by:
executed 4 times by 1 test: return false; Executed by:
| 4 | ||||||||||||||||||
61 | } | - | ||||||||||||||||||
62 | static const char pathMounted[] = "/etc/mtab"; | - | ||||||||||||||||||
63 | static const int bufferSize = 1024; | - | ||||||||||||||||||
64 | - | |||||||||||||||||||
65 | - | |||||||||||||||||||
66 | inline QStorageIterator::QStorageIterator() : | - | ||||||||||||||||||
67 | buffer(QByteArray(bufferSize, 0)) | - | ||||||||||||||||||
68 | { | - | ||||||||||||||||||
69 | fp = ::setmntent(pathMounted, "r"); | - | ||||||||||||||||||
70 | } executed 21 times by 1 test: end of block Executed by:
| 21 | ||||||||||||||||||
71 | - | |||||||||||||||||||
72 | inline QStorageIterator::~QStorageIterator() | - | ||||||||||||||||||
73 | { | - | ||||||||||||||||||
74 | if (fp
| 0-21 | ||||||||||||||||||
75 | :: executed 21 times by 1 test: endmntent(fp);::endmntent(fp); Executed by:
executed 21 times by 1 test: ::endmntent(fp); Executed by:
| 21 | ||||||||||||||||||
76 | } executed 21 times by 1 test: end of block Executed by:
| 21 | ||||||||||||||||||
77 | - | |||||||||||||||||||
78 | inline bool QStorageIterator::isValid() const | - | ||||||||||||||||||
79 | { | - | ||||||||||||||||||
80 | return executed 21 times by 1 test: fp != nullptr;return fp != nullptr; Executed by:
executed 21 times by 1 test: return fp != nullptr; Executed by:
| 21 | ||||||||||||||||||
81 | } | - | ||||||||||||||||||
82 | - | |||||||||||||||||||
83 | inline bool QStorageIterator::next() | - | ||||||||||||||||||
84 | { | - | ||||||||||||||||||
85 | return executed 651 times by 1 test: ::getmntent_r(fp, &mnt, buffer.data(), buffer.size()) != nullptr;return ::getmntent_r(fp, &mnt, buffer.data(), buffer.size()) != nullptr; Executed by:
executed 651 times by 1 test: return ::getmntent_r(fp, &mnt, buffer.data(), buffer.size()) != nullptr; Executed by:
| 651 | ||||||||||||||||||
86 | } | - | ||||||||||||||||||
87 | - | |||||||||||||||||||
88 | inline QString QStorageIterator::rootPath() const | - | ||||||||||||||||||
89 | { | - | ||||||||||||||||||
90 | return executed 640 times by 1 test: QFile::decodeName(mnt.mnt_dir);return QFile::decodeName(mnt.mnt_dir); Executed by:
executed 640 times by 1 test: return QFile::decodeName(mnt.mnt_dir); Executed by:
| 640 | ||||||||||||||||||
91 | } | - | ||||||||||||||||||
92 | - | |||||||||||||||||||
93 | inline QByteArray QStorageIterator::fileSystemType() const | - | ||||||||||||||||||
94 | { | - | ||||||||||||||||||
95 | return executed 588 times by 1 test: QByteArray(mnt.mnt_type);return QByteArray(mnt.mnt_type); Executed by:
executed 588 times by 1 test: return QByteArray(mnt.mnt_type); Executed by:
| 588 | ||||||||||||||||||
96 | } | - | ||||||||||||||||||
97 | - | |||||||||||||||||||
98 | inline QByteArray QStorageIterator::device() const | - | ||||||||||||||||||
99 | { | - | ||||||||||||||||||
100 | return executed 39 times by 1 test: QByteArray(mnt.mnt_fsname);return QByteArray(mnt.mnt_fsname); Executed by:
executed 39 times by 1 test: return QByteArray(mnt.mnt_fsname); Executed by:
| 39 | ||||||||||||||||||
101 | } | - | ||||||||||||||||||
102 | void QStorageInfoPrivate::initRootPath() | - | ||||||||||||||||||
103 | { | - | ||||||||||||||||||
104 | rootPath = QFileInfo(rootPath).canonicalFilePath(); | - | ||||||||||||||||||
105 | - | |||||||||||||||||||
106 | if (rootPath.isEmpty()
| 0-19 | ||||||||||||||||||
107 | return; never executed: return; | 0 | ||||||||||||||||||
108 | - | |||||||||||||||||||
109 | QStorageIterator it; | - | ||||||||||||||||||
110 | if (!it.isValid()
| 0-19 | ||||||||||||||||||
111 | rootPath = ([]() -> QString { enum { Size = sizeof(u"" "/")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "/" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return never executed: qstring_literal_temp;return qstring_literal_temp; never executed: }());return qstring_literal_temp; | 0 | ||||||||||||||||||
112 | return; never executed: return; | 0 | ||||||||||||||||||
113 | } | - | ||||||||||||||||||
114 | - | |||||||||||||||||||
115 | int maxLength = 0; | - | ||||||||||||||||||
116 | const QString oldRootPath = rootPath; | - | ||||||||||||||||||
117 | rootPath.clear(); | - | ||||||||||||||||||
118 | - | |||||||||||||||||||
119 | while (it.next()
| 19-570 | ||||||||||||||||||
120 | const QString mountDir = it.rootPath(); | - | ||||||||||||||||||
121 | const QByteArray fsName = it.fileSystemType(); | - | ||||||||||||||||||
122 | - | |||||||||||||||||||
123 | if (isParentOf(mountDir, oldRootPath)
| 19-520 | ||||||||||||||||||
124 | maxLength = mountDir.length(); | - | ||||||||||||||||||
125 | rootPath = mountDir; | - | ||||||||||||||||||
126 | device = it.device(); | - | ||||||||||||||||||
127 | fileSystemType = fsName; | - | ||||||||||||||||||
128 | } executed 31 times by 1 test: end of block Executed by:
| 31 | ||||||||||||||||||
129 | } executed 570 times by 1 test: end of block Executed by:
| 570 | ||||||||||||||||||
130 | } executed 19 times by 1 test: end of block Executed by:
| 19 | ||||||||||||||||||
131 | - | |||||||||||||||||||
132 | static inline QString retrieveLabel(const QByteArray &device) | - | ||||||||||||||||||
133 | { | - | ||||||||||||||||||
134 | - | |||||||||||||||||||
135 | static const char pathDiskByLabel[] = "/dev/disk/by-label"; | - | ||||||||||||||||||
136 | - | |||||||||||||||||||
137 | QFileInfo devinfo(QFile::decodeName(device)); | - | ||||||||||||||||||
138 | QString devicePath = devinfo.canonicalFilePath(); | - | ||||||||||||||||||
139 | - | |||||||||||||||||||
140 | QDirIterator it(QLatin1String(pathDiskByLabel), QDir::NoDotAndDotDot); | - | ||||||||||||||||||
141 | while (it.hasNext()
| 0-19 | ||||||||||||||||||
142 | it.next(); | - | ||||||||||||||||||
143 | QFileInfo fileInfo(it.fileInfo()); | - | ||||||||||||||||||
144 | if (fileInfo.isSymLink()
| 0 | ||||||||||||||||||
145 | return never executed: fileInfo.fileName();return fileInfo.fileName(); never executed: return fileInfo.fileName(); | 0 | ||||||||||||||||||
146 | } never executed: end of block | 0 | ||||||||||||||||||
147 | return executed 19 times by 1 test: QString();return QString(); Executed by:
executed 19 times by 1 test: return QString(); Executed by:
| 19 | ||||||||||||||||||
148 | } | - | ||||||||||||||||||
149 | - | |||||||||||||||||||
150 | void QStorageInfoPrivate::doStat() | - | ||||||||||||||||||
151 | { | - | ||||||||||||||||||
152 | initRootPath(); | - | ||||||||||||||||||
153 | if (rootPath.isEmpty()
| 0-19 | ||||||||||||||||||
154 | return; never executed: return; | 0 | ||||||||||||||||||
155 | - | |||||||||||||||||||
156 | retrieveVolumeInfo(); | - | ||||||||||||||||||
157 | name = retrieveLabel(device); | - | ||||||||||||||||||
158 | } executed 19 times by 1 test: end of block Executed by:
| 19 | ||||||||||||||||||
159 | - | |||||||||||||||||||
160 | void QStorageInfoPrivate::retrieveVolumeInfo() | - | ||||||||||||||||||
161 | { | - | ||||||||||||||||||
162 | struct statvfs64 statfs_buf; | - | ||||||||||||||||||
163 | int result; | - | ||||||||||||||||||
164 | do { result = ::statvfs64(QFile::encodeName(rootPath).constData(), &statfs_buf); } executed 19 times by 1 test: while (result == -1end of block Executed by:
| 0-19 | ||||||||||||||||||
165 | if (result == 0
| 0-19 | ||||||||||||||||||
166 | valid = true; | - | ||||||||||||||||||
167 | ready = true; | - | ||||||||||||||||||
168 | - | |||||||||||||||||||
169 | - | |||||||||||||||||||
170 | - | |||||||||||||||||||
171 | - | |||||||||||||||||||
172 | - | |||||||||||||||||||
173 | - | |||||||||||||||||||
174 | bytesTotal = statfs_buf.f_blocks * statfs_buf.f_frsize; | - | ||||||||||||||||||
175 | bytesFree = statfs_buf.f_bfree * statfs_buf.f_frsize; | - | ||||||||||||||||||
176 | bytesAvailable = statfs_buf.f_bavail * statfs_buf.f_frsize; | - | ||||||||||||||||||
177 | - | |||||||||||||||||||
178 | blockSize = statfs_buf.f_bsize; | - | ||||||||||||||||||
179 | - | |||||||||||||||||||
180 | - | |||||||||||||||||||
181 | - | |||||||||||||||||||
182 | - | |||||||||||||||||||
183 | - | |||||||||||||||||||
184 | readOnly = (statfs_buf.f_flag & ST_RDONLY) != 0; | - | ||||||||||||||||||
185 | - | |||||||||||||||||||
186 | } executed 19 times by 1 test: end of block Executed by:
| 19 | ||||||||||||||||||
187 | } executed 19 times by 1 test: end of block Executed by:
| 19 | ||||||||||||||||||
188 | - | |||||||||||||||||||
189 | QList<QStorageInfo> QStorageInfoPrivate::mountedVolumes() | - | ||||||||||||||||||
190 | { | - | ||||||||||||||||||
191 | QStorageIterator it; | - | ||||||||||||||||||
192 | if (!it.isValid()
| 0-2 | ||||||||||||||||||
193 | return never executed: QList<QStorageInfo>() << root();return QList<QStorageInfo>() << root(); never executed: return QList<QStorageInfo>() << root(); | 0 | ||||||||||||||||||
194 | - | |||||||||||||||||||
195 | QList<QStorageInfo> volumes; | - | ||||||||||||||||||
196 | - | |||||||||||||||||||
197 | while (it.next()
| 2-60 | ||||||||||||||||||
198 | if (isPseudoFs(it)
| 10-50 | ||||||||||||||||||
199 | continue; executed 50 times by 1 test: continue; Executed by:
| 50 | ||||||||||||||||||
200 | - | |||||||||||||||||||
201 | const QString mountDir = it.rootPath(); | - | ||||||||||||||||||
202 | volumes.append(QStorageInfo(mountDir)); | - | ||||||||||||||||||
203 | } executed 10 times by 1 test: end of block Executed by:
| 10 | ||||||||||||||||||
204 | - | |||||||||||||||||||
205 | return executed 2 times by 1 test: volumes;return volumes; Executed by:
executed 2 times by 1 test: return volumes; Executed by:
| 2 | ||||||||||||||||||
206 | } | - | ||||||||||||||||||
207 | - | |||||||||||||||||||
208 | QStorageInfo QStorageInfoPrivate::root() | - | ||||||||||||||||||
209 | { | - | ||||||||||||||||||
210 | return executed 1 time by 1 test: QStorageInfo(([]() -> QString { enum { Size = sizeof(u"" "/")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "/" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); returnreturn QStorageInfo(([]() -> QString { enum { Size = sizeof(u"" "/")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "/" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }())); Executed by:
executed 1 time by 1 test: qstring_literal_temp;return qstring_literal_temp; Executed by:
executed 1 time by 1 test: }()));return qstring_literal_temp; Executed by:
executed 1 time by 1 test: return QStorageInfo(([]() -> QString { enum { Size = sizeof(u"" "/")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "/" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }())); Executed by:
| 1 | ||||||||||||||||||
211 | } | - | ||||||||||||||||||
212 | - | |||||||||||||||||||
213 | - | |||||||||||||||||||
Switch to Source code | Preprocessed file |