diff --git a/src/solid/devices/backends/iokit/dadictionary.cpp b/src/solid/devices/backends/iokit/dadictionary.cpp index af9d3de..2c53b4e 100644 --- a/src/solid/devices/backends/iokit/dadictionary.cpp +++ b/src/solid/devices/backends/iokit/dadictionary.cpp @@ -1,98 +1,102 @@ /* Copyright 2018 René J.V. Bertin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "dadictionary_p.h" using namespace Solid::Backends::IOKit; DADictionary::DADictionary(const IOKitDevice *device) : device(device) , daDict(nullptr) { daSession = DASessionCreate(kCFAllocatorDefault); if (daSession) { const QString devName = device->property(QStringLiteral("BSD Name")).toString(); daRef = DADiskCreateFromBSDName(kCFAllocatorDefault, daSession, devName.toStdString().c_str()); } else { daRef = nullptr; } } DADictionary::~DADictionary() { releaseDict(); if (daRef) { CFRelease(daRef); daRef = nullptr; } if (daSession) { CFRelease(daSession); daSession = nullptr; } } bool DADictionary::getDict() { + // daDict may cache the latest disk description dict; + // we will refresh it now. + releaseDict(); if (daRef) { daDict = DADiskCopyDescription(daRef); } - return daRef != nullptr; + return daDict != nullptr; } void DADictionary::releaseDict() { if (daDict) { CFRelease(daDict); daDict = nullptr; } } const QString DADictionary::stringForKey(const CFStringRef key) { QString ret; if (getDict()) { ret = QString::fromCFString((const CFStringRef) CFDictionaryGetValue(daDict, key)); + releaseDict(); } - releaseDict(); return ret; } CFURLRef DADictionary::cfUrLRefForKey(const CFStringRef key) { CFURLRef ret = nullptr; if (getDict()) { ret = (const CFURLRef) CFDictionaryGetValue(daDict, key); } // we cannot release the dictionary here, or else we'd need to // copy the CFURLRef and oblige our caller to release the return value. return ret; } bool DADictionary::boolForKey(const CFStringRef key, bool &value) { if (getDict()) { const CFBooleanRef boolRef = (const CFBooleanRef) CFDictionaryGetValue(daDict, key); if (boolRef) { value = CFBooleanGetValue(boolRef); } + releaseDict(); return boolRef != nullptr; } return false; } diff --git a/src/solid/devices/backends/iokit/dadictionary_p.h b/src/solid/devices/backends/iokit/dadictionary_p.h index 28cfc7f..b8d4133 100644 --- a/src/solid/devices/backends/iokit/dadictionary_p.h +++ b/src/solid/devices/backends/iokit/dadictionary_p.h @@ -1,58 +1,81 @@ /* Copyright 2018 René J.V. Bertin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #ifndef SOLID_BACKENDS_IOKIT_DADICTIONARY_H #define SOLID_BACKENDS_IOKIT_DADICTIONARY_H #include #include "iokitdevice.h" #include #include namespace Solid { namespace Backends { namespace IOKit { class DADictionary { public: DADictionary(const IOKitDevice *device); virtual ~DADictionary(); + /** + * get a fresh copy of the DA disk description dict; + * the result is stored in daRef (after releasing any + * dict it may currently point to). + */ bool getDict(); + /** + * release the DA disk description dict and reset daRef. + */ void releaseDict(); + + /** + * fetch the value of @p key as a string, from the current + * disk description (calls getDict() and releaseDict()). + */ const QString stringForKey(const CFStringRef key); + /** + * fetch the value of @p key as a CFURLRef, from the current + * disk description. Calls getDict() but not releaseDict(). + *The contents of the CFURLRef must be retrieved before + * calling releaseDict() (and thus getDict()). + */ CFURLRef cfUrLRefForKey(const CFStringRef key); + /** + * fetch the value of @p key as a boolean, from the current + * disk description (calls getDict() and releaseDict()). + */ bool boolForKey(const CFStringRef key, bool &value); const IOKitDevice *device; DASessionRef daSession; DADiskRef daRef; CFDictionaryRef daDict; }; } } } #endif // SOLID_BACKENDS_IOKIT_DADICTIONARY_H