summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qstorageinfo_mac.cpp
blob: c6c0f501dabfaf45d101af70f02dcdb0ddadb657 (plain)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
// Copyright (C) 2014 Ivan Komissarov <ABBAPOH@gmail.com>// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only#include"qstorageinfo_p.h"#include <QtCore/qfileinfo.h>#include <QtCore/private/qcore_mac_p.h>#include <CoreFoundation/CoreFoundation.h>#include <CoreFoundation/CFURLEnumerator.h>#include <sys/mount.h>#define QT_STATFSBUF struct statfs#define QT_STATFS ::statfs QT_BEGIN_NAMESPACE voidQStorageInfoPrivate::initRootPath(){ rootPath =QFileInfo(rootPath).canonicalFilePath();if(rootPath.isEmpty())return;retrieveUrlProperties(true);}voidQStorageInfoPrivate::doStat(){initRootPath();if(rootPath.isEmpty())return;retrieveLabel();retrievePosixInfo();retrieveUrlProperties();}voidQStorageInfoPrivate::retrievePosixInfo(){ QT_STATFSBUF statfs_buf;int result =QT_STATFS(QFile::encodeName(rootPath).constData(), &statfs_buf);if(result ==0) { device.assign(statfs_buf.f_mntfromname); readOnly = (statfs_buf.f_flags & MNT_RDONLY) !=0; fileSystemType.assign(statfs_buf.f_fstypename); blockSize = statfs_buf.f_bsize;}}staticinline qint64 CFDictionaryGetInt64(CFDictionaryRef dictionary,const void*key){ CFNumberRef cfNumber = (CFNumberRef)CFDictionaryGetValue(dictionary, key);if(!cfNumber)return-1; qint64 result;bool ok =CFNumberGetValue(cfNumber, kCFNumberSInt64Type, &result);if(!ok)return-1;return result;}voidQStorageInfoPrivate::retrieveUrlProperties(bool initRootPath){static const void*rootPathKeys[] = { kCFURLVolumeURLKey };static const void*propertyKeys[] = {// kCFURLVolumeNameKey, // 10.7// kCFURLVolumeLocalizedNameKey, // 10.7 kCFURLVolumeTotalCapacityKey, kCFURLVolumeAvailableCapacityKey,// kCFURLVolumeIsReadOnlyKey // 10.7};size_t size = (initRootPath ?sizeof(rootPathKeys) :sizeof(propertyKeys)) /sizeof(void*); QCFType<CFArrayRef> keys =CFArrayCreate(kCFAllocatorDefault, initRootPath ? rootPathKeys : propertyKeys, size,nullptr);if(!keys)return;const QCFString cfPath = rootPath;if(initRootPath) rootPath.clear(); QCFType<CFURLRef> url =CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfPath, kCFURLPOSIXPathStyle,true);if(!url)return; CFErrorRef error; QCFType<CFDictionaryRef> map =CFURLCopyResourcePropertiesForKeys(url, keys, &error);if(!map)return;if(initRootPath) {const CFURLRef rootUrl = (CFURLRef)CFDictionaryGetValue(map, kCFURLVolumeURLKey);if(!rootUrl)return; rootPath =QCFString(CFURLCopyFileSystemPath(rootUrl, kCFURLPOSIXPathStyle)); valid =true; ready =true;return;} bytesTotal =CFDictionaryGetInt64(map, kCFURLVolumeTotalCapacityKey); bytesAvailable =CFDictionaryGetInt64(map, kCFURLVolumeAvailableCapacityKey); bytesFree = bytesAvailable;}voidQStorageInfoPrivate::retrieveLabel(){ QCFString path =CFStringCreateWithFileSystemRepresentation(0,QFile::encodeName(rootPath).constData());if(!path)return; QCFType<CFURLRef> url =CFURLCreateWithFileSystemPath(0, path, kCFURLPOSIXPathStyle,true);if(!url)return; QCFType<CFURLRef> volumeUrl;if(!CFURLCopyResourcePropertyForKey(url, kCFURLVolumeURLKey, &volumeUrl, NULL))return; QCFString volumeName;if(!CFURLCopyResourcePropertyForKey(url, kCFURLNameKey, &volumeName, NULL))return; name = volumeName;} QList<QStorageInfo>QStorageInfoPrivate::mountedVolumes(){ QList<QStorageInfo> volumes; QCFType<CFURLEnumeratorRef> enumerator; enumerator =CFURLEnumeratorCreateForMountedVolumes(nullptr, kCFURLEnumeratorSkipInvisibles,nullptr); CFURLEnumeratorResult result = kCFURLEnumeratorSuccess;do{ CFURLRef url; CFErrorRef error; result =CFURLEnumeratorGetNextURL(enumerator, &url, &error);if(result == kCFURLEnumeratorSuccess) {const QCFString urlString =CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle); volumes.append(QStorageInfo(urlString));}}while(result != kCFURLEnumeratorEnd);return volumes;} QT_END_NAMESPACE 
close