Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qstorageinfo.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2014 Ivan Komissarov <ABBAPOH@gmail.com> | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "qstorageinfo.h" | - | ||||||
41 | #include "qstorageinfo_p.h" | - | ||||||
42 | - | |||||||
43 | QT_BEGIN_NAMESPACE | - | ||||||
44 | - | |||||||
45 | /*! | - | ||||||
46 | \class QStorageInfo | - | ||||||
47 | \inmodule QtCore | - | ||||||
48 | \since 5.4 | - | ||||||
49 | \brief Provides information about currently mounted storage and drives. | - | ||||||
50 | - | |||||||
51 | \ingroup io | - | ||||||
52 | \ingroup shared | - | ||||||
53 | - | |||||||
54 | Allows retrieving information about the volume's space, its mount point, | - | ||||||
55 | label, and filesystem name. | - | ||||||
56 | - | |||||||
57 | You can create an instance of QStorageInfo by passing the path to the | - | ||||||
58 | volume's mount point as a constructor parameter, or you can set it using | - | ||||||
59 | the setPath() method. The static mountedVolumes() method can be used to get the | - | ||||||
60 | list of all mounted filesystems. | - | ||||||
61 | - | |||||||
62 | QStorageInfo always caches the retrieved information, but you can call | - | ||||||
63 | refresh() to invalidate the cache. | - | ||||||
64 | - | |||||||
65 | The following example retrieves the most common information about the root | - | ||||||
66 | volume of the system, and prints information about it. | - | ||||||
67 | - | |||||||
68 | \snippet code/src_corelib_io_qstorageinfo.cpp 2 | - | ||||||
69 | */ | - | ||||||
70 | - | |||||||
71 | /*! | - | ||||||
72 | Constructs an empty QStorageInfo object. | - | ||||||
73 | - | |||||||
74 | Objects created with the default constructor will be invalid and therefore | - | ||||||
75 | not ready for use. | - | ||||||
76 | - | |||||||
77 | \sa setPath(), isReady(), isValid() | - | ||||||
78 | */ | - | ||||||
79 | QStorageInfo::QStorageInfo() | - | ||||||
80 | : d(new QStorageInfoPrivate) | - | ||||||
81 | { | - | ||||||
82 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||
83 | - | |||||||
84 | /*! | - | ||||||
85 | Constructs a new QStorageInfo object that gives information about the volume | - | ||||||
86 | mounted at \a path. | - | ||||||
87 | - | |||||||
88 | If you pass a directory or file, the QStorageInfo object will refer to the | - | ||||||
89 | volume where this directory or file is located. | - | ||||||
90 | You can check if the created object is correct using the isValid() method. | - | ||||||
91 | - | |||||||
92 | The following example shows how to get the volume on which the application is | - | ||||||
93 | located. It is recommended to always check that the volume is ready and valid. | - | ||||||
94 | - | |||||||
95 | \snippet code/src_corelib_io_qstorageinfo.cpp 0 | - | ||||||
96 | - | |||||||
97 | \sa setPath() | - | ||||||
98 | */ | - | ||||||
99 | QStorageInfo::QStorageInfo(const QString &path) | - | ||||||
100 | : d(new QStorageInfoPrivate) | - | ||||||
101 | { | - | ||||||
102 | setPath(path); | - | ||||||
103 | } executed 24 times by 1 test: end of block Executed by:
| 24 | ||||||
104 | - | |||||||
105 | /*! | - | ||||||
106 | Constructs a new QStorageInfo object that gives information about the volume | - | ||||||
107 | containing the \a dir folder. | - | ||||||
108 | */ | - | ||||||
109 | QStorageInfo::QStorageInfo(const QDir &dir) | - | ||||||
110 | : d(new QStorageInfoPrivate) | - | ||||||
111 | { | - | ||||||
112 | setPath(dir.absolutePath()); | - | ||||||
113 | } never executed: end of block | 0 | ||||||
114 | - | |||||||
115 | /*! | - | ||||||
116 | Constructs a new QStorageInfo object that is a copy of the \a other QStorageInfo object. | - | ||||||
117 | */ | - | ||||||
118 | QStorageInfo::QStorageInfo(const QStorageInfo &other) | - | ||||||
119 | : d(other.d) | - | ||||||
120 | { | - | ||||||
121 | } executed 21 times by 1 test: end of block Executed by:
| 21 | ||||||
122 | - | |||||||
123 | /*! | - | ||||||
124 | Destroys the QStorageInfo object and frees its resources. | - | ||||||
125 | */ | - | ||||||
126 | QStorageInfo::~QStorageInfo() | - | ||||||
127 | { | - | ||||||
128 | } | - | ||||||
129 | - | |||||||
130 | /*! | - | ||||||
131 | Makes a copy of the QStorageInfo object \a other and assigns it to this QStorageInfo object. | - | ||||||
132 | */ | - | ||||||
133 | QStorageInfo &QStorageInfo::operator=(const QStorageInfo &other) | - | ||||||
134 | { | - | ||||||
135 | d = other.d; | - | ||||||
136 | return *this; never executed: return *this; | 0 | ||||||
137 | } | - | ||||||
138 | - | |||||||
139 | /*! | - | ||||||
140 | \fn QStorageInfo &QStorageInfo::operator=(QStorageInfo &&other) | - | ||||||
141 | - | |||||||
142 | Assigns \a other to this QStorageInfo instance. | - | ||||||
143 | */ | - | ||||||
144 | - | |||||||
145 | /*! | - | ||||||
146 | \fn void QStorageInfo::swap(QStorageInfo &other) | - | ||||||
147 | - | |||||||
148 | Swaps this volume info with \a other. This function is very fast and | - | ||||||
149 | never fails. | - | ||||||
150 | */ | - | ||||||
151 | - | |||||||
152 | /*! | - | ||||||
153 | Sets this QStorageInfo object to the filesystem mounted where \a path is located. | - | ||||||
154 | - | |||||||
155 | \a path can either be a root path of the filesystem, a directory, or a file | - | ||||||
156 | within that filesystem. | - | ||||||
157 | - | |||||||
158 | \sa rootPath() | - | ||||||
159 | */ | - | ||||||
160 | void QStorageInfo::setPath(const QString &path) | - | ||||||
161 | { | - | ||||||
162 | if (d->rootPath == path)
| 0-24 | ||||||
163 | return; never executed: return; | 0 | ||||||
164 | d.detach(); | - | ||||||
165 | d->rootPath = path; | - | ||||||
166 | d->doStat(); | - | ||||||
167 | } executed 24 times by 1 test: end of block Executed by:
| 24 | ||||||
168 | - | |||||||
169 | /*! | - | ||||||
170 | Returns the mount point of the filesystem this QStorageInfo object | - | ||||||
171 | represents. | - | ||||||
172 | - | |||||||
173 | On Windows, it returns the volume letter in case the volume is not mounted to | - | ||||||
174 | a directory. | - | ||||||
175 | - | |||||||
176 | Note that the value returned by rootPath() is the real mount point of a | - | ||||||
177 | volume, and may not be equal to the value passed to the constructor or setPath() | - | ||||||
178 | method. For example, if you have only the root volume in the system, and | - | ||||||
179 | pass '/directory' to setPath(), then this method will return '/'. | - | ||||||
180 | - | |||||||
181 | \sa setPath(), device() | - | ||||||
182 | */ | - | ||||||
183 | QString QStorageInfo::rootPath() const | - | ||||||
184 | { | - | ||||||
185 | return d->rootPath; executed 8 times by 1 test: return d->rootPath; Executed by:
| 8 | ||||||
186 | } | - | ||||||
187 | - | |||||||
188 | /*! | - | ||||||
189 | Returns the size (in bytes) available for the current user. It returns | - | ||||||
190 | the total size available if the user is the root user or a system administrator. | - | ||||||
191 | - | |||||||
192 | This size can be less than or equal to the free size returned by | - | ||||||
193 | bytesFree() function. | - | ||||||
194 | - | |||||||
195 | Returns -1 if QStorageInfo object is not valid. | - | ||||||
196 | - | |||||||
197 | \sa bytesTotal(), bytesFree() | - | ||||||
198 | */ | - | ||||||
199 | qint64 QStorageInfo::bytesAvailable() const | - | ||||||
200 | { | - | ||||||
201 | return d->bytesAvailable; executed 3 times by 1 test: return d->bytesAvailable; Executed by:
| 3 | ||||||
202 | } | - | ||||||
203 | - | |||||||
204 | /*! | - | ||||||
205 | Returns the number of free bytes in a volume. Note that if there are | - | ||||||
206 | quotas on the filesystem, this value can be larger than the value | - | ||||||
207 | returned by bytesAvailable(). | - | ||||||
208 | - | |||||||
209 | Returns -1 if QStorageInfo object is not valid. | - | ||||||
210 | - | |||||||
211 | \sa bytesTotal(), bytesAvailable() | - | ||||||
212 | */ | - | ||||||
213 | qint64 QStorageInfo::bytesFree() const | - | ||||||
214 | { | - | ||||||
215 | return d->bytesFree; executed 15 times by 1 test: return d->bytesFree; Executed by:
| 15 | ||||||
216 | } | - | ||||||
217 | - | |||||||
218 | /*! | - | ||||||
219 | Returns the total volume size in bytes. | - | ||||||
220 | - | |||||||
221 | Returns -1 if QStorageInfo object is not valid. | - | ||||||
222 | - | |||||||
223 | \sa bytesFree(), bytesAvailable() | - | ||||||
224 | */ | - | ||||||
225 | qint64 QStorageInfo::bytesTotal() const | - | ||||||
226 | { | - | ||||||
227 | return d->bytesTotal; executed 24 times by 1 test: return d->bytesTotal; Executed by:
| 24 | ||||||
228 | } | - | ||||||
229 | - | |||||||
230 | /*! | - | ||||||
231 | \since 5.6 | - | ||||||
232 | Returns the optimal transfer block size for this filesystem. | - | ||||||
233 | - | |||||||
234 | Returns -1 if QStorageInfo could not determine the size or if the QStorageInfo | - | ||||||
235 | object is not valid. | - | ||||||
236 | */ | - | ||||||
237 | int QStorageInfo::blockSize() const | - | ||||||
238 | { | - | ||||||
239 | return d->blockSize; executed 5 times by 1 test: return d->blockSize; Executed by:
| 5 | ||||||
240 | } | - | ||||||
241 | - | |||||||
242 | /*! | - | ||||||
243 | Returns the type name of the filesystem. | - | ||||||
244 | - | |||||||
245 | This is a platform-dependent function, and filesystem names can vary | - | ||||||
246 | between different operating systems. For example, on Windows filesystems | - | ||||||
247 | they can be named \c NTFS, and on Linux they can be named \c ntfs-3g or \c fuseblk. | - | ||||||
248 | - | |||||||
249 | \sa name() | - | ||||||
250 | */ | - | ||||||
251 | QByteArray QStorageInfo::fileSystemType() const | - | ||||||
252 | { | - | ||||||
253 | return d->fileSystemType; executed 15 times by 1 test: return d->fileSystemType; Executed by:
| 15 | ||||||
254 | } | - | ||||||
255 | - | |||||||
256 | /*! | - | ||||||
257 | Returns the device for this volume. | - | ||||||
258 | - | |||||||
259 | For example, on Unix filesystems (including \macos), this returns the | - | ||||||
260 | devpath like \c /dev/sda0 for local storages. On Windows, it returns the UNC | - | ||||||
261 | path starting with \c \\\\?\\ for local storages (in other words, the volume GUID). | - | ||||||
262 | - | |||||||
263 | \sa rootPath() | - | ||||||
264 | */ | - | ||||||
265 | QByteArray QStorageInfo::device() const | - | ||||||
266 | { | - | ||||||
267 | return d->device; executed 48 times by 1 test: return d->device; Executed by:
| 48 | ||||||
268 | } | - | ||||||
269 | - | |||||||
270 | /*! | - | ||||||
271 | Returns the human-readable name of a filesystem, usually called \c label. | - | ||||||
272 | - | |||||||
273 | Not all filesystems support this feature. In this case, the value returned by | - | ||||||
274 | this method could be empty. An empty string is returned if the file system | - | ||||||
275 | does not support labels, or if no label is set. | - | ||||||
276 | - | |||||||
277 | On Linux, retrieving the volume's label requires \c udev to be present in the | - | ||||||
278 | system. | - | ||||||
279 | - | |||||||
280 | \sa fileSystemType() | - | ||||||
281 | */ | - | ||||||
282 | QString QStorageInfo::name() const | - | ||||||
283 | { | - | ||||||
284 | return d->name; executed 5 times by 1 test: return d->name; Executed by:
| 5 | ||||||
285 | } | - | ||||||
286 | - | |||||||
287 | /*! | - | ||||||
288 | Returns the volume's name, if available, or the root path if not. | - | ||||||
289 | */ | - | ||||||
290 | QString QStorageInfo::displayName() const | - | ||||||
291 | { | - | ||||||
292 | if (!d->name.isEmpty())
| 0 | ||||||
293 | return d->name; never executed: return d->name; | 0 | ||||||
294 | return d->rootPath; never executed: return d->rootPath; | 0 | ||||||
295 | } | - | ||||||
296 | - | |||||||
297 | /*! | - | ||||||
298 | \fn bool QStorageInfo::isRoot() const | - | ||||||
299 | - | |||||||
300 | Returns true if this QStorageInfo represents the system root volume; false | - | ||||||
301 | otherwise. | - | ||||||
302 | - | |||||||
303 | On Unix filesystems, the root volume is a volume mounted on \c /. On Windows, | - | ||||||
304 | the root volume is the volume where the OS is installed. | - | ||||||
305 | - | |||||||
306 | \sa root() | - | ||||||
307 | */ | - | ||||||
308 | - | |||||||
309 | /*! | - | ||||||
310 | Returns true if the current filesystem is protected from writing; false | - | ||||||
311 | otherwise. | - | ||||||
312 | */ | - | ||||||
313 | bool QStorageInfo::isReadOnly() const | - | ||||||
314 | { | - | ||||||
315 | return d->readOnly; executed 5 times by 1 test: return d->readOnly; Executed by:
| 5 | ||||||
316 | } | - | ||||||
317 | - | |||||||
318 | /*! | - | ||||||
319 | Returns true if the current filesystem is ready to work; false otherwise. For | - | ||||||
320 | example, false is returned if the CD volume is not inserted. | - | ||||||
321 | - | |||||||
322 | Note that fileSystemType(), name(), bytesTotal(), bytesFree(), and | - | ||||||
323 | bytesAvailable() will return invalid data until the volume is ready. | - | ||||||
324 | - | |||||||
325 | \sa isValid() | - | ||||||
326 | */ | - | ||||||
327 | bool QStorageInfo::isReady() const | - | ||||||
328 | { | - | ||||||
329 | return d->ready; executed 7 times by 1 test: return d->ready; Executed by:
| 7 | ||||||
330 | } | - | ||||||
331 | - | |||||||
332 | /*! | - | ||||||
333 | Returns true if the QStorageInfo specified by rootPath exists and is mounted | - | ||||||
334 | correctly. | - | ||||||
335 | - | |||||||
336 | \sa isReady() | - | ||||||
337 | */ | - | ||||||
338 | bool QStorageInfo::isValid() const | - | ||||||
339 | { | - | ||||||
340 | return d->valid; executed 7 times by 1 test: return d->valid; Executed by:
| 7 | ||||||
341 | } | - | ||||||
342 | - | |||||||
343 | /*! | - | ||||||
344 | Resets QStorageInfo's internal cache. | - | ||||||
345 | - | |||||||
346 | QStorageInfo caches information about storage to speed up performance. | - | ||||||
347 | QStorageInfo retrieves information during object construction and/or when calling | - | ||||||
348 | the setPath() method. You have to manually reset the cache by calling this | - | ||||||
349 | function to update storage information. | - | ||||||
350 | */ | - | ||||||
351 | void QStorageInfo::refresh() | - | ||||||
352 | { | - | ||||||
353 | d.detach(); | - | ||||||
354 | d->doStat(); | - | ||||||
355 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||
356 | - | |||||||
357 | /*! | - | ||||||
358 | Returns the list of QStorageInfo objects that corresponds to the list of currently | - | ||||||
359 | mounted filesystems. | - | ||||||
360 | - | |||||||
361 | On Windows, this returns the drives visible in the \gui{My Computer} folder. On Unix | - | ||||||
362 | operating systems, it returns the list of all mounted filesystems (except for | - | ||||||
363 | pseudo filesystems). | - | ||||||
364 | - | |||||||
365 | Returns all currently mounted filesystems by default. | - | ||||||
366 | - | |||||||
367 | The example shows how to retrieve all available filesystems, skipping read-only ones. | - | ||||||
368 | - | |||||||
369 | \snippet code/src_corelib_io_qstorageinfo.cpp 1 | - | ||||||
370 | - | |||||||
371 | \sa root() | - | ||||||
372 | */ | - | ||||||
373 | QList<QStorageInfo> QStorageInfo::mountedVolumes() | - | ||||||
374 | { | - | ||||||
375 | return QStorageInfoPrivate::mountedVolumes(); executed 2 times by 1 test: return QStorageInfoPrivate::mountedVolumes(); Executed by:
| 2 | ||||||
376 | } | - | ||||||
377 | - | |||||||
378 | Q_GLOBAL_STATIC_WITH_ARGS(QStorageInfo, getRoot, (QStorageInfoPrivate::root())) executed 1 time by 1 test: end of block Executed by:
executed 1 time by 1 test: guard.store(QtGlobalStatic::Destroyed); Executed by:
executed 10 times by 1 test: return &holder.value; Executed by:
| 0-10 | ||||||
379 | - | |||||||
380 | /*! | - | ||||||
381 | Returns a QStorageInfo object that represents the system root volume. | - | ||||||
382 | - | |||||||
383 | On Unix systems this call returns the root ('/') volume; in Windows the volume where | - | ||||||
384 | the operating system is installed. | - | ||||||
385 | - | |||||||
386 | \sa isRoot() | - | ||||||
387 | */ | - | ||||||
388 | QStorageInfo QStorageInfo::root() | - | ||||||
389 | { | - | ||||||
390 | return *getRoot(); executed 10 times by 1 test: return *getRoot(); Executed by:
| 10 | ||||||
391 | } | - | ||||||
392 | - | |||||||
393 | /*! | - | ||||||
394 | \fn inline bool operator==(const QStorageInfo &first, const QStorageInfo &second) | - | ||||||
395 | - | |||||||
396 | \relates QStorageInfo | - | ||||||
397 | - | |||||||
398 | Returns true if the \a first QStorageInfo object refers to the same drive or volume | - | ||||||
399 | as the \a second; otherwise it returns false. | - | ||||||
400 | - | |||||||
401 | Note that the result of comparing two invalid QStorageInfo objects is always | - | ||||||
402 | positive. | - | ||||||
403 | */ | - | ||||||
404 | - | |||||||
405 | /*! | - | ||||||
406 | \fn inline bool operator!=(const QStorageInfo &first, const QStorageInfo &second) | - | ||||||
407 | - | |||||||
408 | \relates QStorageInfo | - | ||||||
409 | - | |||||||
410 | Returns true if the \a first QStorageInfo object refers to a different drive or | - | ||||||
411 | volume than the \a second; otherwise returns false. | - | ||||||
412 | */ | - | ||||||
413 | - | |||||||
414 | QT_END_NAMESPACE | - | ||||||
Source code | Switch to Preprocessed file |