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