Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// fs_capabilities.m
//
// Created by Charles Francoise on 13/05/14.
// Copyright (c) 2014 Charles Francoise. All rights reserved.
//
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/attr.h>
#import <Foundation/Foundation.h>
int main(int argc, char** argv)
{
@autoreleasepool
{
// Using pathconf
long res = pathconf(argv[1], _PC_CASE_SENSITIVE);
if (res != -1)
{
if (res == 1)
{
printf("Filesystem is case sensitive (_PC_CASE_SENSITIVE).\n");
}
else if (res == 0)
{
printf("Filesystem is NOT case sensitive (_PC_CASE_SENSITIVE).\n");
}
}
else
{
fprintf(stderr, "error: pathconf(%s, _PC_CASE_SENSITIVE): %s\n", argv[1], strerror(errno));
}
res = pathconf(argv[1], _PC_CASE_PRESERVING);
if (res != -1)
{
if (res == 1)
{
printf("Filesystem is case preserving (_PC_CASE_PRESERVING).\n");
}
else if (res == 0)
{
printf("Filesystem is NOT case preserving (_PC_CASE_PRESERVING).\n");
}
}
else
{
fprintf(stderr, "error: pathconf(%s, _PC_CASE_PRESERVING): %s\n", argv[1], strerror(errno));
}
printf("\n");
// Using getattrlist
struct attrlist attrs = {ATTR_BIT_MAP_COUNT, 0, 0, ATTR_VOL_CAPABILITIES, 0, 0, 0};
struct {
u_int32_t size;
vol_capabilities_attr_t cap;
} attr_buf;
res = getattrlist(argv[1], &attrs, &attr_buf, sizeof(attr_buf), 0);
if (res == 0)
{
if (attr_buf.cap.valid[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_CASE_SENSITIVE)
{
if (attr_buf.cap.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_CASE_SENSITIVE)
{
printf("Filesystem is case sensitive (VOL_CAP_FMT_CASE_SENSITIVE).\n");
}
else
{
printf("Filesystem is NOT case sensitive (VOL_CAP_FMT_CASE_SENSITIVE).\n");
}
}
if (attr_buf.cap.valid[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_CASE_PRESERVING)
{
if (attr_buf.cap.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_CASE_PRESERVING)
{
printf("Filesystem is case preserving (VOL_CAP_FMT_CASE_PRESERVING).\n");
}
else
{
printf("Filesystem is NOT case preserving (VOL_CAP_FMT_CASE_PRESERVING).\n");
}
}
}
else
{
fprintf(stderr, "error: getattrlist(%s): %s\n", argv[1], strerror(errno));
}
printf("\n");
// Using NSURL
NSURL* url = [NSURL fileURLWithPath:@(argv[1])];
NSError* error = nil;
NSNumber* caseSensitive = nil;
res = [url getResourceValue:&caseSensitive forKey:NSURLVolumeSupportsCaseSensitiveNamesKey error:&error];
if (res)
{
if (caseSensitive.boolValue)
{
printf("Filesystem is case sensitive (NSURLVolumeSupportsCaseSensitiveNamesKey).\n");
}
else
{
printf("Filesystem is NOT case sensitive (NSURLVolumeSupportsCaseSensitiveNamesKey).\n");
}
}
else
{
fprintf(stderr, "error: -[NSURL getResourceValue:forKey:error:] (%s, %s): %s\n", [[url description] UTF8String], [NSURLVolumeSupportsCaseSensitiveNamesKey UTF8String], [[error localizedDescription] UTF8String]);
}
res = [url getResourceValue:&caseSensitive forKey:NSURLVolumeSupportsCasePreservedNamesKey error:&error];
if (res)
{
if (caseSensitive.boolValue)
{
printf("Filesystem is case preserving (NSURLVolumeSupportsCasePreservedNamesKey).\n");
}
else
{
printf("Filesystem is NOT case preserving (NSURLVolumeSupportsCasePreservedNamesKey).\n");
}
}
else
{
fprintf(stderr, "error: -[NSURL getResourceValue:forKey:error:] (%s, %s): %s\n", [[url description] UTF8String], [NSURLVolumeSupportsCasePreservedNamesKey UTF8String], [[error localizedDescription] UTF8String]);
}
printf("\n");
// Using CFURL
CFStringRef path = CFStringCreateWithCString(kCFAllocatorDefault, argv[1], kCFStringEncodingUTF8);
CFURLRef url2 = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path, kCFURLPOSIXPathStyle, YES);
CFErrorRef error2;
CFBooleanRef caseSensitive2;
res = CFURLCopyResourcePropertyForKey(url2, kCFURLVolumeSupportsCaseSensitiveNamesKey, &caseSensitive2, &error2);
if (res)
{
if (CFBooleanGetValue(caseSensitive2))
{
printf("Filesystem is case sensitive (kCFURLVolumeSupportsCaseSensitiveNamesKey).\n");
}
else
{
printf("Filesystem is NOT case sensitive (kCFURLVolumeSupportsCaseSensitiveNamesKey).\n");
}
}
else
{
fprintf(stderr, "error: CFURLCopyResourcePropertyForKey(%s, kCFURLVolumeSupportsCaseSensitiveNamesKey): %s\n",
CFStringGetCStringPtr(CFURLGetString(url2), kCFStringEncodingUTF8),
CFStringGetCStringPtr(CFErrorCopyDescription(error2), kCFStringEncodingUTF8));
}
res = CFURLCopyResourcePropertyForKey(url2, kCFURLVolumeSupportsCasePreservedNamesKey, &caseSensitive2, &error2);
if (res)
{
if (CFBooleanGetValue(caseSensitive2))
{
printf("Filesystem is case preserving (kCFURLVolumeSupportsCasePreservedNamesKey).\n");
}
else
{
printf("Filesystem is NOT case preserving (kCFURLVolumeSupportsCasePreservedNamesKey).\n");
}
}
else
{
fprintf(stderr, "error: CFURLCopyResourcePropertyForKey(%s, kCFURLVolumeSupportsCasePreservedNamesKey): %s\n",
CFStringGetCStringPtr(CFURLGetString(url2), kCFStringEncodingUTF8),
CFStringGetCStringPtr(CFErrorCopyDescription(error2), kCFStringEncodingUTF8));
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment