-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathimpl-apple-lion.cc
More file actions
41 lines (30 loc) · 1.22 KB
/
impl-apple-lion.cc
File metadata and controls
41 lines (30 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#define NAPI_VERSION 3
#include <napi.h>
#include <CoreFoundation/CFURL.h>
#include <CoreFoundation/CFString.h>
Napi::String MYCFStringGetV8String(Napi::Env env, CFStringRef aString) {
if (aString == NULL) {
return Napi::String::New(env, "");
}
CFIndex length = CFStringGetLength(aString);
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
char *buffer = (char *) malloc(maxSize);
if (!CFStringGetCString(aString, buffer, maxSize, kCFStringEncodingUTF8)) {
return Napi::String::New(env, "");
}
Napi::String result = Napi::String::New(env, buffer);
free(buffer);
return result;
}
Napi::Value MethodGetVolumeName(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string aPath = info[0].As<Napi::String>();
CFStringRef out;
CFErrorRef error;
CFStringRef volumePath = CFStringCreateWithCString(NULL, aPath.c_str(), kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, volumePath, kCFURLPOSIXPathStyle, true);
if(!CFURLCopyResourcePropertyForKey(url, kCFURLVolumeNameKey, &out, &error)) {
throw Napi::Error::New(env, MYCFStringGetV8String(env, CFErrorCopyDescription(error)));
}
return MYCFStringGetV8String(env, out);
}