Skip to content

Instantly share code, notes, and snippets.

@ilovezfs
Last active December 30, 2015 14:39
Show Gist options
  • Save ilovezfs/7843069 to your computer and use it in GitHub Desktop.
Save ilovezfs/7843069 to your computer and use it in GitHub Desktop.
pathmatch
#include <DiskArbitration/DiskArbitration.h>
//clang -o pathmatch pathmatch.c -framework DiskArbitration -framework CoreServices
static const CFStringRef CoreStorageGPTGUID = CFSTR("53746F72-6167-11AA-AA11-00306543ECAC");
static const CFStringRef CoreStorage = CFSTR("CoreStorage");
typedef struct {
DASessionRef session;
DADiskRef disk;
} DADiskSession;
Boolean
CFDictionaryValueIfPresentMatchesSubstring(CFDictionaryRef dict, CFStringRef key, CFStringRef substr)
{
Boolean ret = false;
CFStringRef existing;
if (dict && CFDictionaryGetValueIfPresent(dict, key, (const void **)&existing)) {
CFRange range = CFStringFind(existing, substr, kCFCompareCaseInsensitive);
if (range.location != kCFNotFound) ret = true;
}
return ret;
}
int
setupDADiskSession(DADiskSession *ds, const char *bsdName)
{
int err = 0;
ds->session = DASessionCreate(NULL);
if (ds->session == NULL) {
err = EINVAL;
}
if (err == 0) {
ds->disk = DADiskCreateFromBSDName(NULL, ds->session, bsdName);
if (ds->disk == NULL) {
err = EINVAL;
}
}
return err;
}
void
teardownDADiskSession(DADiskSession *ds)
{
if (ds->session != NULL) CFRelease(ds->session);
if (ds->disk != NULL) CFRelease(ds->disk);
}
int isPathMatchForKeyAndSubstr(char *path, CFStringRef key, CFStringRef substr, Boolean *isMatch)
{
if (!isMatch) return -1;
int error = 0;
DADiskSession ds = { 0 };
if ((error = setupDADiskSession(&ds, path)) == 0) {
CFDictionaryRef descDict = NULL;
if((descDict = DADiskCopyDescription(ds.disk)) != NULL) {
*isMatch = CFDictionaryValueIfPresentMatchesSubstring(descDict, key, substr);
} else {
error = -1;
fprintf(stderr, "no DADiskCopyDescription for path %s\n", path);
*isMatch = false;
}
}
teardownDADiskSession(&ds);
return error;
}
int isPathCoreStoragePhysicalVolume(char *path, Boolean *isCoreStoragePhysRet)
{
return isPathMatchForKeyAndSubstr(path, kDADiskDescriptionMediaContentKey, CoreStorageGPTGUID, isCoreStoragePhysRet);
}
int isPathCoreStorageLogicalVolume(char *path, Boolean *isCoreStorageLogRet)
{
return isPathMatchForKeyAndSubstr(path, kDADiskDescriptionMediaPathKey, CoreStorage, isCoreStorageLogRet);
}
int
main(int argc, char ** argv)
{
if (argc < 2) return -1;
const char *bsdName = (char *)argv[1];
if (!bsdName) exit(1);
int err = 0;
Boolean isCSLV = false;
err = isPathCoreStorageLogicalVolume(argv[1], &isCSLV);
fprintf(stderr, "isCSLV %d : err %d\n", isCSLV, err);
Boolean isCSPV = false;
err = isPathCoreStoragePhysicalVolume(argv[1], &isCSPV);
fprintf(stderr, "isCSPV %d : err %d\n", isCSPV, err);
Boolean misc = false;
err = isPathMatchForKeyAndSubstr(argv[1], kDADiskDescriptionMediaContentKey, CFSTR("48465300-0000-11AA-AA11-00306543ECAC"), &misc);
fprintf(stderr, "misc %d : err %d\n", misc, err);
if (argc == 4) {
char *bytes;
char *bytes2;
CFStringRef str;
CFStringRef str2;
bytes = CFAllocatorAllocate(CFAllocatorGetDefault(), 200, 0);
strcpy(bytes, argv[2]);
bytes2 = CFAllocatorAllocate(CFAllocatorGetDefault(), 200, 0);
strcpy(bytes2, argv[3]);
str = CFStringCreateWithCStringNoCopy(NULL, bytes, kCFStringEncodingMacRoman, NULL);
str2 = CFStringCreateWithCStringNoCopy(NULL, bytes2, kCFStringEncodingMacRoman, NULL);
Boolean answer = false;
err = isPathMatchForKeyAndSubstr(argv[1], str, str2, &answer);
fprintf(stderr, "answer %d : err %d\n", answer, err);
CFRelease(str);
CFRelease(str2);
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment